1
1
import icdiff
2
- from unittest import mock
3
- import pytest
4
2
import re
5
3
from pprintpp import pformat
6
4
7
- YELLOW_ON = '\x1b [1;33m'
8
- COLOR_OFF = '\x1b [m'
9
- GREEN_ON = '\x1b [1;32m'
10
- ANSI_ESCAPE_RE = re .compile (r'(\x9B|\x1B\[)[0-?]*[ -/]*[@-~]' )
11
-
5
+ YELLOW_ON = "\x1b [1;33m"
6
+ COLOR_OFF = "\x1b [m"
7
+ GREEN_ON = "\x1b [1;32m"
8
+ ANSI_ESCAPE_RE = re .compile (r"(\x9B|\x1B\[)[0-?]*[ -/]*[@-~]" )
12
9
13
10
14
11
def test_short_dict (testdir ):
@@ -19,15 +16,15 @@ def test_short_dict(testdir):
19
16
two = {
20
17
1 : "the number one" ,
21
18
2 : "the number three" ,
22
- 6 : [1 , 2 , 3 ]
19
+ 6 : [1 , 2 , 3 ],
23
20
}
24
21
testdir .makepyfile (
25
22
f"""
26
23
def test_one():
27
24
assert { one !r} == { two !r}
28
25
"""
29
26
)
30
- output = testdir .runpytest (' -vv' ).stdout .str ()
27
+ output = testdir .runpytest (" -vv" ).stdout .str ()
31
28
print (repr (output ))
32
29
two_left = "'the number two'"
33
30
two_right = "'the number three'"
@@ -45,7 +42,7 @@ def test_short_dict_with_colorization(testdir):
45
42
two = {
46
43
1 : "the number one" ,
47
44
2 : "the number three" ,
48
- 6 : [1 , 2 , 3 ]
45
+ 6 : [1 , 2 , 3 ],
49
46
}
50
47
testdir .makepyfile (
51
48
f"""
@@ -54,8 +51,8 @@ def test_one():
54
51
"""
55
52
)
56
53
# Force colorization in py TerminalWriter
57
- testdir .monkeypatch .setenv (' PY_COLORS' , '1' )
58
- output = testdir .runpytest (' -vv' ).stdout .str ()
54
+ testdir .monkeypatch .setenv (" PY_COLORS" , "1" )
55
+ output = testdir .runpytest (" -vv" ).stdout .str ()
59
56
print (repr (output ))
60
57
two_left = f"'the number t{ YELLOW_ON } wo{ COLOR_OFF } '"
61
58
two_right = f"'the number t{ YELLOW_ON } hree{ COLOR_OFF } '"
@@ -67,55 +64,55 @@ def test_one():
67
64
68
65
def test_long_dict (testdir ):
69
66
one = {
70
- ' currency' : ' USD' ,
71
- ' default_UK_warehouse' : ' xforce' ,
72
- ' default_incoterm' : ' EXW' ,
73
- ' name' : ' John Doe' ,
74
- ' payment_term' : ' 30% deposit, 70% balance' ,
75
- ' reference' : ' 42551456-a1b3-49bd-beed-b168d9a5ac83' ,
76
- ' website' : ' http://megasofas.example.com' ,
77
- ' main_contact' : {
78
- ' city' : ' Madeira' ,
79
- ' country' : 'PT' ,
80
-
81
- ' fax' : ' 012356 789039' ,
82
- ' mobile' : ' 012356 789039' ,
83
- ' name' : ' Almeida & Filhos - Example, S.A.' ,
84
- ' phone' : ' 253444802010' ,
85
- ' postcode' : ' 4815-123' ,
86
- ' street' : "Senhora Test D'Ajuda, 432" ,
87
- ' street2' : ' Moreira de Conegos'
67
+ " currency" : " USD" ,
68
+ " default_UK_warehouse" : " xforce" ,
69
+ " default_incoterm" : " EXW" ,
70
+ " name" : " John Doe" ,
71
+ " payment_term" : " 30% deposit, 70% balance" ,
72
+ " reference" : " 42551456-a1b3-49bd-beed-b168d9a5ac83" ,
73
+ " website" : " http://megasofas.example.com" ,
74
+ " main_contact" : {
75
+ " city" : " Madeira" ,
76
+ " country" : "PT" ,
77
+
78
+ " fax" : " 012356 789039" ,
79
+ " mobile" : " 012356 789039" ,
80
+ " name" : " Almeida & Filhos - Example, S.A." ,
81
+ " phone" : " 253444802010" ,
82
+ " postcode" : " 4815-123" ,
83
+ " street" : "Senhora Test D'Ajuda, 432" ,
84
+ " street2" : " Moreira de Conegos" ,
88
85
},
89
86
}
90
87
two = {
91
- ' currency' : ' USD' ,
92
- ' default_UK_warehouse' : ' iforce' ,
93
- ' default_incoterm' : ' EXW' ,
94
- ' freight_forwarder' : ' flexport' ,
95
- ' name' : ' John Doe' ,
96
- ' payment_term' : ' 30% deposit, 70% balance' ,
97
- ' reference' : ' 42551456-a1b3-49bd-beed-b168d9a5ac83' ,
98
- ' website' : ' http://megasofas.example.com' ,
99
- ' main_contact' : {
100
- ' name' : ' Almeida & Filhos - Example, S.A.' ,
101
-
102
- ' street' : "Senhora Test D'Ajuda, 432" ,
103
- ' street2' : ' Moreira de Conegos' ,
104
- ' postcode' : ' 4815-123' ,
105
- ' city' : ' Madeira' ,
106
- ' country' : 'PT' ,
107
- ' phone' : ' 253444802010' ,
108
- ' fax' : ' 012356 789039' ,
109
- ' mobile' : ' 012356 789039'
110
- }
88
+ " currency" : " USD" ,
89
+ " default_UK_warehouse" : " iforce" ,
90
+ " default_incoterm" : " EXW" ,
91
+ " freight_forwarder" : " flexport" ,
92
+ " name" : " John Doe" ,
93
+ " payment_term" : " 30% deposit, 70% balance" ,
94
+ " reference" : " 42551456-a1b3-49bd-beed-b168d9a5ac83" ,
95
+ " website" : " http://megasofas.example.com" ,
96
+ " main_contact" : {
97
+ " name" : " Almeida & Filhos - Example, S.A." ,
98
+
99
+ " street" : "Senhora Test D'Ajuda, 432" ,
100
+ " street2" : " Moreira de Conegos" ,
101
+ " postcode" : " 4815-123" ,
102
+ " city" : " Madeira" ,
103
+ " country" : "PT" ,
104
+ " phone" : " 253444802010" ,
105
+ " fax" : " 012356 789039" ,
106
+ " mobile" : " 012356 789039" ,
107
+ },
111
108
}
112
109
testdir .makepyfile (
113
110
f"""
114
111
def test_two():
115
112
assert { one !r} == { two !r}
116
113
"""
117
114
)
118
- output = testdir .runpytest (' -vv' , ' --color=yes' ).stdout .str ()
115
+ output = testdir .runpytest (" -vv" , " --color=yes" ).stdout .str ()
119
116
expected_l = f"'default_UK_warehouse': '{ YELLOW_ON } x{ COLOR_OFF } force'"
120
117
expected_r = f"'default_UK_warehouse': '{ YELLOW_ON } i{ COLOR_OFF } force'"
121
118
expected_missing = f"{ GREEN_ON } 'freight_forwarder': 'flexport',{ COLOR_OFF } "
@@ -126,7 +123,7 @@ def test_two():
126
123
127
124
def test_only_works_for_equals (testdir ):
128
125
testdir .makepyfile (
129
- f """
126
+ """
130
127
def test_in():
131
128
assert 1 in [2, 3, 4]
132
129
@@ -145,29 +142,31 @@ def _assert_line_in_ignoring_whitespace(expected, block):
145
142
for line in block .splitlines ():
146
143
if all (part in line for part in parts ):
147
144
return True
148
- assert False , f' could not find { expected } in:\n { block } '
145
+ assert False , f" could not find { expected } in:\n { block } "
149
146
150
147
151
148
def test_prepends_icdiff_output_lines_with_color_off (testdir ):
152
- one = [' hello' , ' hello' ]
153
- two = [' bello' , ' hella' ]
149
+ one = [" hello" , " hello" ]
150
+ two = [" bello" , " hella" ]
154
151
testdir .makepyfile (
155
152
f"""
156
153
def test_thing():
157
154
assert { one !r} == { two !r}
158
155
"""
159
156
)
160
- output = testdir .runpytest ('--color=yes' ).stdout .str ()
161
- expected = list (icdiff .ConsoleDiff ().make_table (
162
- pformat (one , width = 1 ).splitlines (),
163
- pformat (two , width = 1 ).splitlines (),
164
- ))
165
- print ('\n ' .join (repr (l ) for l in output .splitlines ()))
157
+ output = testdir .runpytest ("--color=yes" ).stdout .str ()
158
+ expected = list (
159
+ icdiff .ConsoleDiff ().make_table (
160
+ pformat (one , width = 1 ).splitlines (),
161
+ pformat (two , width = 1 ).splitlines (),
162
+ )
163
+ )
164
+ print ("\n " .join (repr (l ) for l in output .splitlines ()))
166
165
_assert_line_in_ignoring_whitespace (expected [0 ], output )
167
166
168
167
169
168
def strip_color_codes (s ):
170
- return re .sub (r' \x1b\[[0-9;]*m' , '' , s )
169
+ return re .sub (r" \x1b\[[0-9;]*m" , "" , s )
171
170
172
171
173
172
def test_avoids_single_line_diffs (testdir ):
@@ -179,7 +178,7 @@ def test_one():
179
178
assert { one !r} == { two !r}
180
179
"""
181
180
)
182
- output = testdir .runpytest (' -vv' ).stdout .str ()
181
+ output = testdir .runpytest (" -vv" ).stdout .str ()
183
182
print (repr (output ))
184
183
assert "1: '1', 1: '1'," in strip_color_codes (output )
185
184
@@ -192,22 +191,24 @@ def test_a():
192
191
"""
193
192
)
194
193
output = testdir .runpytest ().stdout .str ()
195
- drilldown_expression = ' where 3 = len([1, 2, 3])'
194
+ drilldown_expression = " where 3 = len([1, 2, 3])"
196
195
assert drilldown_expression in output
197
196
198
197
199
198
def test_long_lines_in_comparators_are_wrapped_sensibly_multiline (testdir ):
200
- left = {1 : "hello " * 20 , 2 : ' two' }
201
- right = {1 : "hella " * 20 , 2 : ' two' }
199
+ left = {1 : "hello " * 20 , 2 : " two" }
200
+ right = {1 : "hella " * 20 , 2 : " two" }
202
201
testdir .makepyfile (
203
202
f"""
204
203
def test_one():
205
204
assert { left !r} == { right !r}
206
205
"""
207
206
)
208
- output = testdir .runpytest ('-vv' , '--color=yes' ).stdout .str ()
209
- comparison_line = next (l for l in output .splitlines () if '1:' in l and "assert" not in l )
210
- assert comparison_line .count ('hell' ) < 13
207
+ output = testdir .runpytest ("-vv" , "--color=yes" ).stdout .str ()
208
+ comparison_line = next (
209
+ l for l in output .splitlines () if "1:" in l and "assert" not in l
210
+ )
211
+ assert comparison_line .count ("hell" ) < 13
211
212
212
213
213
214
def test_long_lines_in_comparators_are_wrapped_sensibly_singleline (testdir ):
@@ -219,12 +220,11 @@ def test_one():
219
220
assert { left !r} == { right !r}
220
221
"""
221
222
)
222
- output = testdir .runpytest (' -vv' , ' --color=yes' ).stdout .str ()
223
+ output = testdir .runpytest (" -vv" , " --color=yes" ).stdout .str ()
223
224
comparison_line = next (
224
- l for l in output .splitlines ()
225
- if "hell" in l and "assert" not in l
225
+ l for l in output .splitlines () if "hell" in l and "assert" not in l
226
226
)
227
- assert comparison_line .count (' hell' ) < 15
227
+ assert comparison_line .count (" hell" ) < 15
228
228
229
229
230
230
def test_columns_are_calculated_outside_hook (testdir ):
@@ -245,50 +245,50 @@ def test_one():
245
245
assert { left !r} == { right !r}
246
246
"""
247
247
)
248
- testdir .monkeypatch .setenv (' COLUMNS' , '50' )
248
+ testdir .monkeypatch .setenv (" COLUMNS" , "50" )
249
249
# testdir._method = 'subprocess'
250
250
output = testdir .runpytest (
251
- '-vv' , '--color=yes' ,
251
+ "-vv" ,
252
+ "--color=yes" ,
252
253
).stdout .str ()
253
254
comparison_line = next (
254
- l for l in output .splitlines ()
255
- if 'hell' in l and "assert" not in l
255
+ l for l in output .splitlines () if "hell" in l and "assert" not in l
256
256
)
257
- assert comparison_line .count (' hell' ) > 5
257
+ assert comparison_line .count (" hell" ) > 5
258
258
259
259
260
260
def test_small_numbers_are_specialcased (testdir ):
261
261
testdir .makepyfile (
262
- f """
262
+ """
263
263
def test_one():
264
264
assert 404 == 400
265
265
"""
266
266
)
267
- output = testdir .runpytest (' -vv' , ' --color=yes' ).stdout .str ()
267
+ output = testdir .runpytest (" -vv" , " --color=yes" ).stdout .str ()
268
268
assert "assert 404 == 400" in output
269
269
assert "E assert 404 == 400" in output
270
270
271
271
272
272
def test_larger_numbers_are_sane (testdir ):
273
273
testdir .makepyfile (
274
- f """
274
+ """
275
275
def test_one():
276
276
assert 123456 == 1234567
277
277
"""
278
278
)
279
- output = testdir .runpytest (' -vv' , ' --color=yes' ).stdout .str ()
279
+ output = testdir .runpytest (" -vv" , " --color=yes" ).stdout .str ()
280
280
assert f"123456 123456{ GREEN_ON } 7" in output
281
281
282
282
283
283
def test_really_long_diffs_use_context_mode (testdir ):
284
284
testdir .makepyfile (
285
- f """
285
+ """
286
286
def test_one():
287
287
one = list(range(100))
288
288
two = list(range(20)) + ["X"] + list(range(20, 50)) + ["Y"] + list(range(53, 100))
289
289
assert one == two
290
290
"""
291
291
)
292
- output = testdir .runpytest (' -vv' , ' --color=yes' , ' -r=no' ).stdout .str ()
292
+ output = testdir .runpytest (" -vv" , " --color=yes" , " -r=no" ).stdout .str ()
293
293
assert len (output .splitlines ()) < 50
294
294
assert "---" in output # context split marker
0 commit comments