| 1 |
import pytest |
| 2 |
|
| 3 |
import isort |
| 4 |
import isort.literal |
| 5 |
from isort import exceptions |
| 6 |
from isort.settings import Config |
| 7 |
|
| 8 |
|
| 9 |
def test_value_mismatch() -> None: |
| 10 |
with pytest.raises(exceptions.LiteralSortTypeMismatch): |
| 11 |
isort.literal.assignment("x = [1, 2, 3]", "set", "py") |
| 12 |
|
| 13 |
|
| 14 |
def test_invalid_syntax() -> None: |
| 15 |
with pytest.raises(exceptions.LiteralParsingFailure): |
| 16 |
isort.literal.assignment("x = [1, 2, 3", "list", "py") |
| 17 |
|
| 18 |
|
| 19 |
def test_invalid_sort_type() -> None: |
| 20 |
with pytest.raises(ValueError, match=r"Trying to sort using an undefined sort_type. Defined"): |
| 21 |
isort.literal.assignment("x = [1, 2, 3", "tuple-list-not-exist", "py") |
| 22 |
|
| 23 |
|
| 24 |
def test_value_assignment_assignments() -> None: |
| 25 |
assert isort.literal.assignment("b = 1\na = 2\n", "assignments", "py") == "a = 2\nb = 1\n" |
| 26 |
|
| 27 |
|
| 28 |
def test_assignments_invalid_section() -> None: |
| 29 |
with pytest.raises(exceptions.AssignmentsFormatMismatch): |
| 30 |
isort.literal.assignment("\n\nx = 1\nx++", "assignments", "py") |
| 31 |
|
| 32 |
|
| 33 |
def test_list_uses_double_quotes() -> None: |
| 34 |
assert isort.literal.assignment("x = ['b', 'a']", "list", "py") == 'x = ["a", "b"]' |
| 35 |
|
| 36 |
|
| 37 |
def test_list_preserves_bracket_type_tuple() -> None: |
| 38 |
assert isort.literal.assignment("x = ('b', 'a')", "tuple", "py") == 'x = ("a", "b")' |
| 39 |
|
| 40 |
|
| 41 |
def test_single_element_tuple_keeps_trailing_comma() -> None: |
| 42 |
assert isort.literal.assignment("x = ('a',)", "tuple", "py") == 'x = ("a",)' |
| 43 |
|
| 44 |
|
| 45 |
def test_set_bracket_and_quotes() -> None: |
| 46 |
assert isort.literal.assignment("x = {'b', 'a'}", "set", "py") == 'x = {"a", "b"}' |
| 47 |
|
| 48 |
|
| 49 |
def test_long_list_wraps_vertical_hanging_indent() -> None: |
| 50 |
code = "__all__ = ['" + "', '".join(f"name_{i:02d}" for i in range(12)) + "']" |
| 51 |
result = isort.literal.assignment(code, "list", "py", config=Config(profile="black")) |
| 52 |
expected = "__all__ = [\n" + "".join(f' "name_{i:02d}",\n' for i in range(12)) + "]" |
| 53 |
assert result == expected |
| 54 |
|
| 55 |
|
| 56 |
def test_wrap_without_trailing_comma() -> None: |
| 57 |
code = "__all__ = ['" + "', '".join(f"name_{i:02d}" for i in range(12)) + "']" |
| 58 |
result = isort.literal.assignment( |
| 59 |
code, "list", "py", config=Config(line_length=20, include_trailing_comma=False) |
| 60 |
) |
| 61 |
assert result.endswith('"name_11"\n]') # no trailing comma before the closing bracket |
| 62 |
|
| 63 |
|
| 64 |
def test_assignment_applies_formatting_function() -> None: |
| 65 |
def formatting_function(code: str, extension: str, config: Config) -> str: |
| 66 |
assert extension == "py" |
| 67 |
assert config.formatting_function is formatting_function |
| 68 |
return code.replace('"a"', '"A"') |
| 69 |
|
| 70 |
result = isort.literal.assignment( |
| 71 |
"x = ['b', 'a']", "list", "py", config=Config(formatting_function=formatting_function) |
| 72 |
) |
| 73 |
assert result == 'x = ["A", "b"]' |
| 74 |
|
| 75 |
|
| 76 |
def test_trailing_comma_detection_requires_matching_brackets() -> None: |
| 77 |
assert not isort.literal._has_trailing_comma("(\n 'a',\n]") |
| 78 |
|
| 79 |
|
| 80 |
def test_quote_fallback_for_embedded_quote() -> None: |
| 81 |
# value containing a double quote but no single quote -> single quotes (black rule) |
| 82 |
assert isort.literal.assignment("x = ['a\"b']", "list", "py") == "x = ['a\"b']" |
| 83 |
|
| 84 |
|
| 85 |
def test_black_quote_prefers_double_quotes() -> None: |
| 86 |
assert isort.literal._black_quote("foo") == '"foo"' |
| 87 |
|
| 88 |
|
| 89 |
def test_black_quote_uses_single_when_value_has_double_quote_only() -> None: |
| 90 |
assert isort.literal._black_quote('a"b') == "'a\"b'" |
| 91 |
|
| 92 |
|
| 93 |
def test_black_quote_escapes_double_when_value_has_both_quotes() -> None: |
| 94 |
assert isort.literal._black_quote("a'b\"c") == '"a\'b\\"c"' |
| 95 |
|
| 96 |
|
| 97 |
def test_black_quote_falls_back_to_repr_for_control_chars() -> None: |
| 98 |
assert isort.literal._black_quote("a\tb") == repr("a\tb") |
| 99 |
|
| 100 |
|
| 101 |
def test_list_of_non_strings_uses_repr() -> None: |
| 102 |
assert isort.literal.assignment("x = [3, 1, 2]", "list", "py") == "x = [1, 2, 3]" |
| 103 |
|
| 104 |
|
| 105 |
def test_unique_tuple_dedupes_and_sorts() -> None: |
| 106 |
assert isort.literal.assignment("x = ('b', 'a', 'a')", "unique-tuple", "py") == 'x = ("a", "b")' |
| 107 |
|
| 108 |
|
| 109 |
def test_single_element_tuple_wraps_and_keeps_comma() -> None: |
| 110 |
long_name = "z" * 100 |
| 111 |
result = isort.literal.assignment(f"x = ('{long_name}',)", "tuple", "py") |
| 112 |
assert result == f'x = (\n "{long_name}",\n)' |
| 113 |
|
| 114 |
|
| 115 |
def test_dict_sorts_by_value_with_double_quotes() -> None: |
| 116 |
# dict is now formatted config-aware too (no more pprint); sorted by value |
| 117 |
assert ( |
| 118 |
isort.literal.assignment("x = {'a': 'z', 'b': 'y'}", "dict", "py") |
| 119 |
== 'x = {"b": "y", "a": "z"}' |
| 120 |
) |
| 121 |
|
| 122 |
|
| 123 |
def test_dict_non_string_values_use_repr() -> None: |
| 124 |
assert isort.literal.assignment("x = {'b': 2, 'a': 1}", "dict", "py") == 'x = {"a": 1, "b": 2}' |
| 125 |
|
| 126 |
|
| 127 |
def test_long_dict_wraps_vertical_hanging_indent() -> None: |
| 128 |
pairs = {f"key_{i:02d}": f"value_{i:02d}" for i in range(10)} |
| 129 |
code = "d = {" + ", ".join(f"'{k}': '{v}'" for k, v in pairs.items()) + "}" |
| 130 |
result = isort.literal.assignment(code, "dict", "py", config=Config(profile="black")) |
| 131 |
expected = ( |
| 132 |
"d = {\n" + "".join(f' "key_{i:02d}": "value_{i:02d}",\n' for i in range(10)) + "}" |
| 133 |
) |
| 134 |
assert result == expected |
| 135 |
|
| 136 |
|
| 137 |
def test_value_assignment_dict() -> None: |
| 138 |
assert ( |
| 139 |
isort.literal.assignment("x = {3: 'c', 1: 'a', 2: 'b'}", "dict", "py") |
| 140 |
== 'x = {1: "a", 2: "b", 3: "c"}' |
| 141 |
) |
| 142 |
|
| 143 |
|
| 144 |
def test_value_assignment_unique_tuple() -> None: |
| 145 |
assert ( |
| 146 |
isort.literal.assignment("x = ('a', 'b', '1', '1')", "unique-tuple", "py") |
| 147 |
== 'x = ("1", "a", "b")' |
| 148 |
) |
| 149 |
|
| 150 |
|
| 151 |
def test_trailing_comment_is_preserved() -> None: |
| 152 |
assert ( |
| 153 |
isort.literal.assignment("x = ['b', 'a'] # exports", "list", "py") |
| 154 |
== 'x = ["a", "b"] # exports' |
| 155 |
) |
| 156 |
|
| 157 |
|
| 158 |
def test_trailing_comment_with_equals_is_preserved() -> None: |
| 159 |
assert ( |
| 160 |
isort.literal.assignment("x = ['b', 'a'] # note = value", "list", "py") |
| 161 |
== 'x = ["a", "b"] # note = value' |
| 162 |
) |
| 163 |
|
| 164 |
|
| 165 |
def test_literal_with_equals_in_string_sorts() -> None: |
| 166 |
assert isort.literal.assignment("x = ['c', 'a=b']", "list", "py") == 'x = ["a=b", "c"]' |
| 167 |
|
| 168 |
|
| 169 |
def test_trailing_comment_on_multiline_literal_is_preserved() -> None: |
| 170 |
assert ( |
| 171 |
isort.literal.assignment("x = (\n 'b',\n 'a',\n) # exports", "tuple", "py") |
| 172 |
== 'x = ("a", "b") # exports' |
| 173 |
) |
| 174 |
|
| 175 |
|
| 176 |
def test_wrapped_literal_honors_config_line_ending() -> None: |
| 177 |
code = "__all__ = ['" + "', '".join(f"name_{i:02d}" for i in range(12)) + "']" |
| 178 |
result = isort.literal.assignment( |
| 179 |
code, "list", "py", config=Config(profile="black", line_ending="\r\n") |
| 180 |
) |
| 181 |
expected = "__all__ = [\r\n" + "".join(f' "name_{i:02d}",\r\n' for i in range(12)) + "]" |
| 182 |
assert result == expected |
| 183 |
|
| 184 |
|
| 185 |
def test_wrapped_literal_infers_crlf_from_source() -> None: |
| 186 |
code = '__all__ = (\r\n "name_01",\r\n "name_00",\r\n)' |
| 187 |
result = isort.literal.assignment(code, "tuple", "py", config=Config(profile="black")) |
| 188 |
expected = '__all__ = (\r\n "name_00",\r\n "name_01",\r\n)' |
| 189 |
assert result == expected |