| 1 |
"""Test the black.ranges module.""" |
| 2 |
|
| 3 |
import pytest |
| 4 |
|
| 5 |
from black.ranges import adjusted_lines, sanitized_lines |
| 6 |
|
| 7 |
|
| 8 |
@pytest.mark.parametrize( |
| 9 |
"lines", |
| 10 |
[[(1, 1)], [(1, 3)], [(1, 1), (3, 4)]], |
| 11 |
) |
| 12 |
def test_no_diff(lines: list[tuple[int, int]]) -> None: |
| 13 |
source = """\ |
| 14 |
import re |
| 15 |
|
| 16 |
def func(): |
| 17 |
pass |
| 18 |
""" |
| 19 |
assert lines == adjusted_lines(lines, source, source) |
| 20 |
|
| 21 |
|
| 22 |
@pytest.mark.parametrize( |
| 23 |
"lines", |
| 24 |
[ |
| 25 |
[(1, 0)], |
| 26 |
[(-8, 0)], |
| 27 |
[(-8, 8)], |
| 28 |
[(1, 100)], |
| 29 |
[(2, 1)], |
| 30 |
[(0, 8), (3, 1)], |
| 31 |
], |
| 32 |
) |
| 33 |
def test_invalid_lines(lines: list[tuple[int, int]]) -> None: |
| 34 |
original_source = """\ |
| 35 |
import re |
| 36 |
def foo(arg): |
| 37 |
'''This is the foo function. |
| 38 |
|
| 39 |
This is foo function's |
| 40 |
docstring with more descriptive texts. |
| 41 |
''' |
| 42 |
|
| 43 |
def func(arg1, |
| 44 |
arg2, arg3): |
| 45 |
pass |
| 46 |
""" |
| 47 |
modified_source = """\ |
| 48 |
import re |
| 49 |
def foo(arg): |
| 50 |
'''This is the foo function. |
| 51 |
|
| 52 |
This is foo function's |
| 53 |
docstring with more descriptive texts. |
| 54 |
''' |
| 55 |
|
| 56 |
def func(arg1, arg2, arg3): |
| 57 |
pass |
| 58 |
""" |
| 59 |
assert not adjusted_lines(lines, original_source, modified_source) |
| 60 |
|
| 61 |
|
| 62 |
@pytest.mark.parametrize( |
| 63 |
"lines,adjusted", |
| 64 |
[ |
| 65 |
( |
| 66 |
[(1, 1)], |
| 67 |
[(1, 1)], |
| 68 |
), |
| 69 |
( |
| 70 |
[(1, 2)], |
| 71 |
[(1, 1)], |
| 72 |
), |
| 73 |
( |
| 74 |
[(1, 6)], |
| 75 |
[(1, 2)], |
| 76 |
), |
| 77 |
( |
| 78 |
[(6, 6)], |
| 79 |
[], |
| 80 |
), |
| 81 |
], |
| 82 |
) |
| 83 |
def test_removals( |
| 84 |
lines: list[tuple[int, int]], adjusted: list[tuple[int, int]] |
| 85 |
) -> None: |
| 86 |
original_source = """\ |
| 87 |
1. first line |
| 88 |
2. second line |
| 89 |
3. third line |
| 90 |
4. fourth line |
| 91 |
5. fifth line |
| 92 |
6. sixth line |
| 93 |
""" |
| 94 |
modified_source = """\ |
| 95 |
2. second line |
| 96 |
5. fifth line |
| 97 |
""" |
| 98 |
assert adjusted == adjusted_lines(lines, original_source, modified_source) |
| 99 |
|
| 100 |
|
| 101 |
@pytest.mark.parametrize( |
| 102 |
"lines,adjusted", |
| 103 |
[ |
| 104 |
( |
| 105 |
[(1, 1)], |
| 106 |
[(2, 2)], |
| 107 |
), |
| 108 |
( |
| 109 |
[(1, 2)], |
| 110 |
[(2, 5)], |
| 111 |
), |
| 112 |
( |
| 113 |
[(2, 2)], |
| 114 |
[(5, 5)], |
| 115 |
), |
| 116 |
], |
| 117 |
) |
| 118 |
def test_additions( |
| 119 |
lines: list[tuple[int, int]], adjusted: list[tuple[int, int]] |
| 120 |
) -> None: |
| 121 |
original_source = """\ |
| 122 |
1. first line |
| 123 |
2. second line |
| 124 |
""" |
| 125 |
modified_source = """\ |
| 126 |
this is added |
| 127 |
1. first line |
| 128 |
this is added |
| 129 |
this is added |
| 130 |
2. second line |
| 131 |
this is added |
| 132 |
""" |
| 133 |
assert adjusted == adjusted_lines(lines, original_source, modified_source) |
| 134 |
|
| 135 |
|
| 136 |
@pytest.mark.parametrize( |
| 137 |
"lines,adjusted", |
| 138 |
[ |
| 139 |
( |
| 140 |
[(1, 11)], |
| 141 |
[(1, 10)], |
| 142 |
), |
| 143 |
( |
| 144 |
[(1, 12)], |
| 145 |
[(1, 11)], |
| 146 |
), |
| 147 |
( |
| 148 |
[(10, 10)], |
| 149 |
[(9, 9)], |
| 150 |
), |
| 151 |
([(1, 1), (9, 10)], [(1, 1), (9, 9)]), |
| 152 |
([(9, 10), (1, 1)], [(1, 1), (9, 9)]), |
| 153 |
], |
| 154 |
) |
| 155 |
def test_diffs(lines: list[tuple[int, int]], adjusted: list[tuple[int, int]]) -> None: |
| 156 |
original_source = """\ |
| 157 |
1. import re |
| 158 |
2. def foo(arg): |
| 159 |
3. '''This is the foo function. |
| 160 |
4. |
| 161 |
5. This is foo function's |
| 162 |
6. docstring with more descriptive texts. |
| 163 |
7. ''' |
| 164 |
8. |
| 165 |
9. def func(arg1, |
| 166 |
10. arg2, arg3): |
| 167 |
11. pass |
| 168 |
12. # last line |
| 169 |
""" |
| 170 |
modified_source = """\ |
| 171 |
1. import re # changed |
| 172 |
2. def foo(arg): |
| 173 |
3. '''This is the foo function. |
| 174 |
4. |
| 175 |
5. This is foo function's |
| 176 |
6. docstring with more descriptive texts. |
| 177 |
7. ''' |
| 178 |
8. |
| 179 |
9. def func(arg1, arg2, arg3): |
| 180 |
11. pass |
| 181 |
12. # last line changed |
| 182 |
""" |
| 183 |
assert adjusted == adjusted_lines(lines, original_source, modified_source) |
| 184 |
|
| 185 |
|
| 186 |
@pytest.mark.parametrize( |
| 187 |
"lines,sanitized", |
| 188 |
[ |
| 189 |
( |
| 190 |
[(1, 4)], |
| 191 |
[(1, 4)], |
| 192 |
), |
| 193 |
( |
| 194 |
[(2, 3)], |
| 195 |
[(2, 3)], |
| 196 |
), |
| 197 |
( |
| 198 |
[(2, 10)], |
| 199 |
[(2, 4)], |
| 200 |
), |
| 201 |
( |
| 202 |
[(0, 3)], |
| 203 |
[(1, 3)], |
| 204 |
), |
| 205 |
( |
| 206 |
[(0, 10)], |
| 207 |
[(1, 4)], |
| 208 |
), |
| 209 |
( |
| 210 |
[(-2, 3)], |
| 211 |
[(1, 3)], |
| 212 |
), |
| 213 |
( |
| 214 |
[(0, 0)], |
| 215 |
[], |
| 216 |
), |
| 217 |
( |
| 218 |
[(-2, -1)], |
| 219 |
[], |
| 220 |
), |
| 221 |
( |
| 222 |
[(-1, 0)], |
| 223 |
[], |
| 224 |
), |
| 225 |
( |
| 226 |
[(3, 1), (1, 3), (5, 6)], |
| 227 |
[(1, 3)], |
| 228 |
), |
| 229 |
], |
| 230 |
) |
| 231 |
def test_sanitize( |
| 232 |
lines: list[tuple[int, int]], sanitized: list[tuple[int, int]] |
| 233 |
) -> None: |
| 234 |
source = """\ |
| 235 |
1. import re |
| 236 |
2. def func(arg1, |
| 237 |
3. arg2, arg3): |
| 238 |
4. pass |
| 239 |
""" |
| 240 |
assert sanitized == sanitized_lines(lines, source) |
| 241 |
|
| 242 |
source_no_trailing_nl = """\ |
| 243 |
1. import re |
| 244 |
2. def func(arg1, |
| 245 |
3. arg2, arg3): |
| 246 |
4. pass""" |
| 247 |
assert sanitized == sanitized_lines(lines, source_no_trailing_nl) |