forked from clarabstract/treecompare
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
401 lines (356 loc) · 11.6 KB
/
test.py
File metadata and controls
401 lines (356 loc) · 11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
import unittest
from treecompare import diff
from treecompare.difference import Difference
import pprint
ANY_DIFFERENCE = object()
class TestTreeCompare(unittest.TestCase):
def assertNotDifferent(self, expected, actual, options={}):
diffs = diff(expected, actual, options)
if diffs:
message = """Expected object
%s
and actual object
%s
should be considered equal, but the the following differences were reported:
%s\n""" % ( pprint.pformat(expected),
pprint.pformat(actual),
'\n'.join([" - %s" % d for d in diffs]))
self.fail(message)
def assertDifferent(self, expected_diffs, expected, actual, options={}):
diffs = diff(expected, actual, options)
if expected_diffs is ANY_DIFFERENCE:
if not diffs:
diff_diffs = [Difference("", "expected ANY differences, none found!")]
else:
diff_diffs = []
else:
#Let's get meta!
diff_diffs = diff([Difference(path, message) for path, message in expected_diffs.iteritems()], diffs, options={r'^\[\d+\]$':'ignore_key'})
if diff_diffs:
message = """Expected object
%s
and actual object
%s
failed to produce the expected differences:
%s
actual differences produced:
%s""" % ( pprint.pformat(expected),
pprint.pformat(actual),
'\n'.join([" - %s" % d for d in diff_diffs]),
'\n'.join([" - %s" % d for d in diffs]))
self.fail(message)
def test_trivial_equal(self):
self.assertNotDifferent(42,42)
def test_equal_trees(self):
self.assertNotDifferent({
'foo': ['bar', 3L, 1.2],
'bipples': None
},{
'foo': ['bar', 3L, 1.2],
'bipples': None
})
def test_unequal_trees(self):
self.assertDifferent({
"['b'][0]['y']": "expected 11, got nothing",
"['b'][0]['z']": "unexpected value: 11",
},
dict(a=1, b=[dict(x=10, y=11)]),
dict(a=1, b=[dict(x=10, z=11)])
)
def test_unequal_trees_longer_expected(self):
self.assertDifferent({
"['b'][0]['z']": "expected 12, got nothing"
},
dict(a=1, b=[dict(x=10, y=11, z=12)]),
dict(a=1, b=[dict(x=10, y=11)])
)
def test_unequal_trees_longer_actual(self):
self.assertDifferent({
"['b'][0]['z']": "unexpected value: 12"
},
dict(a=1, b=[dict(x=10, y=11)]),
dict(a=1, b=[dict(x=10, y=11, z=12)])
)
def test_equal_lists(self):
self.assertNotDifferent(
[(1,2,'3'), dict(foo=3)],
[(1,2,'3'), dict(foo=3)]
)
def test_unequal_lists(self):
self.assertDifferent({
'[1]': "expected 'g', got 'b'"
},
["a", "g", "c"],
["a", "b", "c"]
)
def test_lists_not_equal_strings(self):
self.assertDifferent({
'': "expected 'xyz', got ('x', 'y', 'z')"
},
options= 'ignore_case',
expected= "xyz",
actual= ('x', 'y', 'z')
)
self.assertDifferent({
'': "expected ('x', 'y', 'z'), got 'xyz'"
},
options= 'ignore_case',
expected= ('x', 'y', 'z'),
actual= "xyz"
)
def test_unequal_lists_longer_expected(self):
self.assertDifferent({
'[3]': "expected 'd', got nothing"
},
["a", "b", "c", "d"],
["a", "b", "c"]
)
def test_unequal_lists_longer_actual(self):
self.assertDifferent({
'[3]': "unexpected value: 'd'"
},
["a", "b", "c"],
["a", "b", "c", "d"]
)
def test_equal_with_different_type(self):
self.assertNotDifferent(
(1, "fafa"),
(1L, u"fafa")
)
def test_ignore_key_option(self):
self.assertNotDifferent(
options = {
r'^\[\d+\]$' : 'ignore_key'
},
expected=['apples', 'oranges', 'starfruit', [1,2,3]],
actual= ['oranges', 'starfruit', [1,2,3], 'apples'],
)
def test_ignore_key_option_with_dict(self):
self.assertNotDifferent(
options = 'ignore_key',
expected=dict(a=1, b=2,c=3),
actual= dict(c=1, b=2,a=3)
)
def test_ignore_key_option_with_dict_difference(self):
self.assertDifferent({
"['a']": "expected 1, got 6",
"['b']": "expected 2, got 5",
"['c']": "expected 3, got 4",
},
options = 'ignore_key',
expected=dict(a=1, b=2,c=3),
actual= dict(c=4, b=5,a=6)
)
def test_ignore_key_option_with_difference(self):
self.assertDifferent(
{
'[2][1]': "expected 2, got 3",
'[2][2]': "expected 3, got 2"
},
options = {
r'^\[\d+\]$' : 'ignore_key'
},
expected=['apples', 'oranges', 'starfruit', [1,2,3]],
actual= ['oranges', 'starfruit', [1,3,2], 'apples'],
)
def test_ignore_key_option_with_deep_difference(self):
self.assertDifferent(
{
"[2][2]['a']": "expected 1, got 2",
"[3]": "expected 'farkle', got ('a', 'b', 'c')",
},
options = {
r'^\[\d+\]$' : 'ignore_key'
},
expected=['apples', 'oranges', 'starfruit', [1,2,dict(a=1)], "farkle"],
actual= ['oranges', 'starfruit', [1,2,dict(a=2)], ('a','b','c'), 'apples'],
)
def test_partial_ignore_key_rule(self):
self.assertDifferent(
{
"[0]": "expected 'apples', got 'pineapple'",
"[3]": "expected 'pineapple', got 'apples'"
},
options = {
r'^\[[1-2]\]$' : 'ignore_key'
},
expected=['apples', 'oranges', 'starfruit', 'pineapple'],
actual= ['pineapple', 'starfruit', 'oranges', 'apples'],
)
def test_ignore_key_option_with_unexpected_values(self):
self.assertDifferent(
{
'[2]': "unexpected value: 'maples'",
},
options ='ignore_key',
expected=['apples', 'oranges', 'starfruit'],
actual= ['oranges', 'starfruit', 'maples', 'apples'],
)
def test_ignore_option_with_missing_value(self):
self.assertDifferent(
{
'[2]': "expected 'poppler', got nothing",
},
options = {
r'^\[\d+\]$' : 'ignore_key'
},
expected=['apples', 'oranges', 'poppler', 'starfruit'],
actual= ['oranges', 'starfruit', 'apples'],
)
def test_assert_includes_option(self):
self.assertNotDifferent(
options = {
r'^\[1\]': "assert_includes"
},
expected = [
'ninjas',
('apple', 'orange', 'watermelon'),
'orchards'
],
actual = [
'ninjas',
'orange',
'orchards'
]
)
def test_assert_includes_option_failure(self):
self.assertDifferent(
{
'[1]': "'pear' not included in ('apple', 'orange', 'watermelon')"
},
options = {
r'^\[1\]$': 'assert_includes'
},
expected = [
'ninjas',
('apple', 'orange', 'watermelon'),
'orchards'
],
actual = [
'ninjas',
'pear',
'orchards'
]
)
def test_assert_includes_option_with_ignore_key(self):
self.assertNotDifferent(
options = ('assert_includes', 'ignore_key'),
expected = [
'ninjas',
'orchards',
('apple', 'orange', 'watermelon'),
],
actual = [
'ninjas',
'orange',
'orchards'
]
)
def test_assert_includes_option_with_ignore_key_in_dict(self):
self.assertNotDifferent(
options = {
r'^\[1\]': ('assert_includes', 'ignore_key')
},
expected = [
'ninjas',
dict(whatever=('apple', 'orange', 'watermelon')),
'orchards'
],
actual = [
'ninjas',
dict(whaja='orange'),
'orchards'
]
)
def test_assert_includes_option_with_ignore_case(self):
self.assertNotDifferent(
options = {
r'^\[1\]\[\'whatever\'\]': ('assert_includes', 'ignore_case')
},
expected = [
'ninjas',
dict(whatever=('apple', 'ORANGE', 'watermelon')),
'orchards'
],
actual = [
'ninjas',
dict(whatever='orAnGe'),
'orchards'
]
)
def test_ignore_option(self):
self.assertNotDifferent(
options = {
r'\[\'papaya\'\]': 'ignore'
},
expected = {
'banana': 'republic',
},
actual = {
'banana': 'republic',
'papaya': 'republic'
}
)
def test_ignore_option_with_expected(self):
self.assertNotDifferent(
options = {
r'\[\'(pineapple|papaya)\'\]': 'ignore'
},
expected = {
'banana': 'republic',
'pineapple': 'commonwealth',
},
actual = {
'banana': 'republic',
'papaya': 'republic'
}
)
def test_ignore_option_failure(self):
self.assertDifferent(
{
"['pineapple']": "expected 'commonwealth', got nothing"
},
options = {
r'\[\'papaya\'\]': 'ignore'
},
expected = {
'banana': 'republic',
'pineapple': 'commonwealth',
},
actual = {
'banana': 'republic',
'papaya': 'republic'
}
)
def test_ignore_spacing_option(self):
self.assertNotDifferent(
options = 'ignore_spacing',
expected = "Welcome, you are logged in as <bobo>.",
actual = """Welcome,
you are logged \tin as< bobo>
.
"""
)
def test_ignore_line_spacing_option(self):
self.assertNotDifferent(
options = 'ignore_spacing',
expected = """Welcome,
you are logged in as <bobo>.
""",
actual = """Welcome, \r
\t\tyou are logged in as <bobo>.
"""
)
def test_ignore_line_whitespace_option_failure(self):
self.assertDifferent(
ANY_DIFFERENCE,
options = 'ignore_line_whitespace',
expected = """Welcome,
you are logged in as <bobo>.
""",
actual = """Welcome, \r
\t\tyou are logged in as <bobo>.
"""
)
if __name__ == '__main__':
unittest.main()