|
| 1 | +import pytest |
| 2 | + |
| 3 | +from rewrite.python import IntelliJ, SpacesVisitor |
| 4 | +from rewrite.test import rewrite_run, python, RecipeSpec, from_visitor |
| 5 | + |
| 6 | + |
| 7 | +@pytest.mark.parametrize("within_brackets", [False, True]) |
| 8 | +def test_spaces_with_list_comprehension(within_brackets): |
| 9 | + style = IntelliJ.spaces() |
| 10 | + style = style.with_within( |
| 11 | + style.within.with_brackets(within_brackets) |
| 12 | + ) |
| 13 | + _s = " " if within_brackets else "" |
| 14 | + rewrite_run( |
| 15 | + # language=python |
| 16 | + python( |
| 17 | + """\ |
| 18 | + a = [ i*2 for i in range(0, 10)] |
| 19 | + a = [ i*2 for i in [1, 2, 3 ]] |
| 20 | + """, |
| 21 | + f"""\ |
| 22 | + a = [i * 2 for i in range(0, 10)] |
| 23 | + a = [i * 2 for i in [1, 2, 3]] |
| 24 | + """.replace("[", "[" + _s).replace("]", _s + "]") |
| 25 | + ), |
| 26 | + spec=RecipeSpec() |
| 27 | + .with_recipe(from_visitor(SpacesVisitor(style))) |
| 28 | + ) |
| 29 | + |
| 30 | + |
| 31 | +def test_spaces_with_generator_comprehension(): |
| 32 | + style = IntelliJ.spaces() |
| 33 | + |
| 34 | + rewrite_run( |
| 35 | + # language=python |
| 36 | + python( |
| 37 | + """\ |
| 38 | + a = ( i*2 for i in range(0, 10)) |
| 39 | + """, |
| 40 | + f"""\ |
| 41 | + a = (i * 2 for i in range(0, 10)) |
| 42 | + """ |
| 43 | + ), |
| 44 | + spec=RecipeSpec() |
| 45 | + .with_recipe(from_visitor(SpacesVisitor(style))) |
| 46 | + ) |
| 47 | + |
| 48 | + |
| 49 | +@pytest.mark.parametrize("within_brackets", [False, True]) |
| 50 | +def test_spaces_with_list_comprehension_with_condition(within_brackets): |
| 51 | + style = IntelliJ.spaces() |
| 52 | + style = style.with_within( |
| 53 | + style.within.with_brackets(within_brackets) |
| 54 | + ) |
| 55 | + _s = " " if within_brackets else "" |
| 56 | + rewrite_run( |
| 57 | + # language=python |
| 58 | + python( |
| 59 | + """\ |
| 60 | + a = [ i* 2 for i in range(0, 10) if i % 2 == 0 ] |
| 61 | + """, |
| 62 | + """\ |
| 63 | + a = [i * 2 for i in range(0, 10) if i % 2 == 0] |
| 64 | + """.replace("[", "[" + _s).replace("]", _s + "]") |
| 65 | + ), |
| 66 | + spec=RecipeSpec() |
| 67 | + .with_recipe(from_visitor(SpacesVisitor(style))) |
| 68 | + ) |
| 69 | + |
| 70 | + |
| 71 | +@pytest.mark.parametrize("within_braces", [False, True]) |
| 72 | +def test_spaces_with_set_comprehension(within_braces): |
| 73 | + style = IntelliJ.spaces() |
| 74 | + style = style.with_within( |
| 75 | + style.within.with_braces(within_braces) |
| 76 | + ) |
| 77 | + _s = " " if within_braces else "" |
| 78 | + rewrite_run( |
| 79 | + # language=python |
| 80 | + python( |
| 81 | + """\ |
| 82 | + a = {i*2 for i in range(0, 10)} |
| 83 | + a = {i for i in {1, 2, 3 }} |
| 84 | + """, |
| 85 | + """\ |
| 86 | + a = {i * 2 for i in range(0, 10)} |
| 87 | + a = {i for i in {1, 2, 3}} |
| 88 | + """.replace("{", "{" + _s).replace("}", _s + "}") |
| 89 | + ), |
| 90 | + spec=RecipeSpec() |
| 91 | + .with_recipe(from_visitor(SpacesVisitor(style))) |
| 92 | + ) |
| 93 | + |
| 94 | + |
| 95 | +@pytest.mark.parametrize("within_braces", [False, True]) |
| 96 | +def test_spaces_with_dict_comprehension(within_braces): |
| 97 | + style = IntelliJ.spaces() |
| 98 | + style = style.with_within( |
| 99 | + style.within.with_braces(within_braces) |
| 100 | + ) |
| 101 | + _s = " " if within_braces else "" |
| 102 | + rewrite_run( |
| 103 | + # language=python |
| 104 | + python( |
| 105 | + """\ |
| 106 | + a = {i: i*2 for i in range(0, 10)} |
| 107 | + a = {i: i for i in [1, 2, 3]} |
| 108 | + a = {k: v*2 for k,v in { "a": 2, "b": 4}.items( ) } |
| 109 | + """, |
| 110 | + """\ |
| 111 | + a = {i: i * 2 for i in range(0, 10)} |
| 112 | + a = {i: i for i in [1, 2, 3]} |
| 113 | + a = {k: v * 2 for k, v in {"a": 2, "b": 4}.items()} |
| 114 | + """.replace("{", "{" + _s).replace("}", _s + "}") |
| 115 | + ), |
| 116 | + spec=RecipeSpec() |
| 117 | + .with_recipe(from_visitor(SpacesVisitor(style))) |
| 118 | + ) |
0 commit comments