eq_json 1.0.2 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,519 @@
1
+ require 'json'
2
+ require 'eq_json_array_with_key'
3
+ require 'spec_helper.rb'
4
+
5
+ describe 'test item miss match' do
6
+
7
+ it 'actual does not contain an expected item' do
8
+
9
+ actual = {
10
+ bookSeller: "amazon",
11
+ bookWholeSellers: {
12
+ publisherInfo: {
13
+ name: "ACME Publisher Inc.",
14
+ publishDate: {
15
+ month: 3,
16
+ day: 23,
17
+ year: 2015
18
+ },
19
+ products: {
20
+ books: [
21
+ {
22
+ bookId: "1",
23
+ name: "Harry Potter and the Sorcerer's Stone",
24
+ author: "J.K. Rowling"
25
+ },
26
+ {
27
+ bookId: "2",
28
+ name: "Eragon",
29
+ author: "Christopher Paolini",
30
+ },
31
+ {
32
+ bookdId: "4",
33
+ name: "Effective Java",
34
+ authoer: "Cannot Remember"
35
+ }
36
+ ]
37
+ }
38
+ }
39
+ },
40
+ url: "www.amazon.com"
41
+
42
+ }
43
+ actualArray = actual[:bookWholeSellers][:publisherInfo][:products][:books]
44
+
45
+ expected = {
46
+ bookSeller: "amazon",
47
+ bookWholeSellers: {
48
+ publisherInfo: {
49
+ name: "ACME Publisher Inc.",
50
+ publishDate: {
51
+ month: 3,
52
+ day: 23,
53
+ year: 2015
54
+ },
55
+ products: {
56
+ books: [
57
+ {
58
+ bookId: "3",
59
+ name: "The Fellowship of the Ring",
60
+ author: "J.R.R. Tolkien"
61
+
62
+ },
63
+ {
64
+ bookId: "1",
65
+ name: "Harry Potter and the Sorcerer's Stone",
66
+ author: "J.K. Rowling"
67
+ },
68
+ {
69
+ bookId: "2",
70
+ name: "Eragon",
71
+ author: "Christopher Paolini"
72
+ }
73
+ ]
74
+ }
75
+ }
76
+ },
77
+ url: "www.amazon.com"
78
+ }
79
+
80
+ expectedArray = expected[:bookWholeSellers][:publisherInfo][:products][:books]
81
+
82
+
83
+ customMatcher=EqualJsonArrayWithKey.new(expectedArray, :bookId)
84
+
85
+ expect(customMatcher.matches?(actualArray)).to eq(false)
86
+
87
+ expectedJson=expected.to_json;
88
+ actualJson=actual.to_json;
89
+
90
+ String expectedErrorMessage= "bookId 3 not found in actual\n" +
91
+ "Expected: {\"bookId\":\"3\",\"name\":\"The Fellowship of the Ring\",\"author\":\"J.R.R. Tolkien\"}\n";
92
+
93
+ expect(customMatcher.failure_message).to eq(expectedErrorMessage)
94
+
95
+ expect(actualArray).not_to eq_json_array_with_key(expectedArray, :bookId)
96
+
97
+ end
98
+
99
+ it 'actual and expected equal' do
100
+
101
+ actualArray = [
102
+ {
103
+ bookId: "1",
104
+ name: "Harry Potter and the Sorcerer's Stone",
105
+ author: "J.K. Rowling"
106
+ },
107
+ {
108
+ bookId: "2",
109
+ name: "Eragon",
110
+ author: "Christopher Paolini",
111
+ },
112
+ {
113
+ bookId: "3",
114
+ name: "The Fellowship of the Ring",
115
+ author: "J.R.R. Tolkien"
116
+
117
+ }
118
+ ]
119
+
120
+ expectedArray = [
121
+ {
122
+ bookId: "3",
123
+ name: "The Fellowship of the Ring",
124
+ author: "J.R.R. Tolkien"
125
+
126
+ },
127
+ {
128
+ bookId: "1",
129
+ name: "Harry Potter and the Sorcerer's Stone",
130
+ author: "J.K. Rowling"
131
+ },
132
+ {
133
+ bookId: "2",
134
+ name: "Eragon",
135
+ author: "Christopher Paolini"
136
+ }
137
+ ]
138
+
139
+ expect(actualArray).to eq_json_array_with_key(expectedArray, :bookId)
140
+
141
+ end
142
+
143
+ it 'test excepted dpes not have key' do
144
+ actual = {
145
+ bookSeller: "amazon",
146
+ bookWholeSellers: {
147
+ publisherInfo: {
148
+ name: "ACME Publisher Inc.",
149
+ publishDate: {
150
+ month: 3,
151
+ day: 23,
152
+ year: 2015
153
+ },
154
+ products: {
155
+ books: [
156
+ {
157
+ bookId: "1",
158
+ name: "Harry Potter and the Sorcerer's Stone",
159
+ author: "J.K. Rowling"
160
+ },
161
+ {
162
+ bookId: "2",
163
+ name: "Eragon",
164
+ author: "Christopher Paolini",
165
+ isbn: 1234
166
+ },
167
+ {
168
+ bookId: "3",
169
+ name: "The Fellowship of the Ring",
170
+ author: "J.R.R. Tolkien"
171
+ }
172
+ ]
173
+ }
174
+ }
175
+ },
176
+ url: "www.amazon.com"
177
+
178
+ }
179
+ actualArray = actual[:bookWholeSellers][:publisherInfo][:products][:books]
180
+
181
+ book1 = {
182
+ bookIdentity: "1",
183
+ name: "Harry Potter and the Sorcerer's Stone",
184
+ author: "J.K. Rowling"
185
+ }
186
+
187
+ expected = {
188
+ bookSeller: "amazon",
189
+ bookWholeSellers: {
190
+ publisherInfo: {
191
+ name: "ACME Publisher Inc.",
192
+ publishDate: {
193
+ month: 3,
194
+ day: 23,
195
+ year: 2015
196
+ },
197
+ products: {
198
+ books: [
199
+ book1,
200
+ {
201
+ bookIdentity: "2",
202
+ name: "Eragon",
203
+ author: "Christopher Paolini"
204
+ },
205
+ {
206
+ bookIdentity: "3",
207
+ name: "The Fellowship of the Ring",
208
+ author: "J.R.R. Tolkien"
209
+
210
+ }
211
+ ]
212
+ }
213
+ }
214
+ },
215
+ url: "www.amazon.com"
216
+ }
217
+
218
+ expectedArray = expected[:bookWholeSellers][:publisherInfo][:products][:books]
219
+
220
+ customMatcher=EqualJsonArrayWithKey.new(expectedArray, :bookId)
221
+
222
+ expect(customMatcher.matches?(actualArray)).to eq(false)
223
+
224
+ expectedJson=expected.to_json;
225
+ actualJson=actual.to_json;
226
+
227
+ String expectedErrorMessage= "Tester error expected item does not have key bookId.\n" +
228
+ "Expected item: #{book1.to_json}\n"
229
+
230
+ expect(customMatcher.failure_message).to eq(expectedErrorMessage)
231
+
232
+ expect(actualArray).not_to eq_json_array_with_key(expectedArray, :bookId)
233
+
234
+ end
235
+
236
+ it 'test exception raised when key not a symbol' do
237
+ actual = {
238
+ bookSeller: "amazon",
239
+ bookWholeSellers: {
240
+ publisherInfo: {
241
+ name: "ACME Publisher Inc.",
242
+ publishDate: {
243
+ month: 3,
244
+ day: 23,
245
+ year: 2015
246
+ },
247
+ products: {
248
+ books: [
249
+ {
250
+ bookId: "1",
251
+ name: "Harry Potter and the Sorcerer's Stone",
252
+ author: "J.K. Rowling"
253
+ },
254
+ {
255
+ bookId: "2",
256
+ name: "Eragon",
257
+ author: "Christopher Paolini",
258
+ isbn: 1234
259
+ },
260
+ {
261
+ bookId: "3",
262
+ name: "The Fellowship of the Ring",
263
+ author: "J.R.R. Tolkien"
264
+ }
265
+ ]
266
+ }
267
+ }
268
+ },
269
+ url: "www.amazon.com"
270
+
271
+ }
272
+ actualArray = actual[:bookWholeSellers][:publisherInfo][:products][:books]
273
+
274
+ expected = {
275
+ bookSeller: "amazon",
276
+ bookWholeSellers: {
277
+ publisherInfo: {
278
+ name: "ACME Publisher Inc.",
279
+ publishDate: {
280
+ month: 3,
281
+ day: 23,
282
+ year: 2015
283
+ },
284
+ products: {
285
+ books: [
286
+ {
287
+ bookId: "1",
288
+ name: "Harry Potter and the Sorcerer's Stone",
289
+ author: "J.K. Rowling"
290
+ },
291
+ {
292
+ bookId: "2",
293
+ name: "Eragon",
294
+ author: "Christopher Paolini"
295
+ },
296
+ {
297
+ bookId: "3",
298
+ name: "The Fellowship of the Ring",
299
+ author: "J.R.R. Tolkien"
300
+
301
+ }
302
+ ]
303
+ }
304
+ }
305
+ },
306
+ url: "www.amazon.com"
307
+ }
308
+
309
+ expectedArray = expected[:bookWholeSellers][:publisherInfo][:products][:books]
310
+
311
+ expect {EqualJsonArrayWithKey.new(expectedArray, "bookId")}.to raise_error(RuntimeError, 'Key should be a symbol');
312
+
313
+ end
314
+
315
+ it 'actual contains more items than expected' do
316
+
317
+ actual = {
318
+ bookSeller: "amazon",
319
+ bookWholeSellers: {
320
+ publisherInfo: {
321
+ name: "ACME Publisher Inc.",
322
+ publishDate: {
323
+ month: 3,
324
+ day: 23,
325
+ year: 2015
326
+ },
327
+ products: {
328
+ books: [
329
+ {
330
+ bookId: "1",
331
+ name: "Harry Potter and the Sorcerer's Stone",
332
+ author: "J.K. Rowling"
333
+ },
334
+ {
335
+ bookId: "2",
336
+ name: "Eragon",
337
+ author: "Christopher Paolini",
338
+ },
339
+ {
340
+ bookId: "4",
341
+ name: "Effective Java",
342
+ authoer: "Cannot Remember"
343
+ },
344
+ {
345
+ bookId: "5",
346
+ name: "The HP Way",
347
+ authoer: "Bill and Dave"
348
+ },
349
+ {
350
+ bookId: "3",
351
+ name: "The Fellowship of the Ring",
352
+ author: "J.R.R. Tolkien"
353
+
354
+ }
355
+ ]
356
+ }
357
+ }
358
+ },
359
+ url: "www.amazon.com"
360
+
361
+ }
362
+ actualArray = actual[:bookWholeSellers][:publisherInfo][:products][:books]
363
+
364
+ expected = {
365
+ bookSeller: "amazon",
366
+ bookWholeSellers: {
367
+ publisherInfo: {
368
+ name: "ACME Publisher Inc.",
369
+ publishDate: {
370
+ month: 3,
371
+ day: 23,
372
+ year: 2015
373
+ },
374
+ products: {
375
+ books: [
376
+ {
377
+ bookId: "3",
378
+ name: "The Fellowship of the Ring",
379
+ author: "J.R.R. Tolkien"
380
+
381
+ },
382
+ {
383
+ bookId: "1",
384
+ name: "Harry Potter and the Sorcerer's Stone",
385
+ author: "J.K. Rowling"
386
+ },
387
+ {
388
+ bookId: "2",
389
+ name: "Eragon",
390
+ author: "Christopher Paolini"
391
+ }
392
+ ]
393
+ }
394
+ }
395
+ },
396
+ url: "www.amazon.com"
397
+ }
398
+
399
+ expectedArray = expected[:bookWholeSellers][:publisherInfo][:products][:books]
400
+
401
+
402
+ customMatcher=EqualJsonArrayWithKey.new(expectedArray, :bookId)
403
+
404
+ expect(customMatcher.matches?(actualArray)).to eq(false)
405
+
406
+ expectedJson=expected.to_json;
407
+ actualJson=actual.to_json;
408
+
409
+ String expectedErrorMessage= "Array size does not match. Expected 3 actual 5\n" +
410
+ "expected does not contain bookIds [\"4\", \"5\"]\n"
411
+
412
+ expect(customMatcher.failure_message).to eq(expectedErrorMessage)
413
+
414
+ expect(actualArray).not_to eq_json_array_with_key(expectedArray, :bookId)
415
+
416
+ end
417
+
418
+ it 'actual missing item in expected' do
419
+
420
+ actual = {
421
+ bookSeller: "amazon",
422
+ bookWholeSellers: {
423
+ publisherInfo: {
424
+ name: "ACME Publisher Inc.",
425
+ publishDate: {
426
+ month: 3,
427
+ day: 23,
428
+ year: 2015
429
+ },
430
+ products: {
431
+ books: [
432
+ {
433
+ bookId: "1",
434
+ name: "Harry Potter and the Sorcerer's Stone",
435
+ author: "J.K. Rowling"
436
+ },
437
+ {
438
+ bookId: "2",
439
+ name: "Eragon",
440
+ author: "Christopher Paolini",
441
+ isbn: 1234
442
+ },
443
+ {
444
+ bookId: "3",
445
+ name: "The Fellowship of the Ring",
446
+ author: "J.R.R. Tolkien"
447
+ }
448
+ ]
449
+ }
450
+ }
451
+ },
452
+ url: "www.amazon.com"
453
+
454
+ }
455
+ actualArray = actual[:bookWholeSellers][:publisherInfo][:products][:books]
456
+
457
+ expected = {
458
+ bookSeller: "amazon",
459
+ bookWholeSellers: {
460
+ publisherInfo: {
461
+ name: "ACME Publisher Inc.",
462
+ publishDate: {
463
+ month: 3,
464
+ day: 23,
465
+ year: 2015
466
+ },
467
+ products: {
468
+ books: [
469
+ {
470
+ bookId: "1",
471
+ name: "Harry Potter and the Sorcerer's Stone",
472
+ author: "J.K. Rowling"
473
+ },
474
+ {
475
+ bookId: "2",
476
+ name: "Eragon",
477
+ author: "Christopher Paolini"
478
+ },
479
+ {
480
+ bookId: "3",
481
+ name: "The Fellowship of the Ring",
482
+ author: "J.R.R. Tolkien"
483
+
484
+ }
485
+ ]
486
+ }
487
+ }
488
+ },
489
+ url: "www.amazon.com"
490
+ }
491
+
492
+ expectedArray = expected[:bookWholeSellers][:publisherInfo][:products][:books]
493
+
494
+
495
+ customMatcher=EqualJsonArrayWithKey.new(expectedArray, :bookId)
496
+
497
+ expect(customMatcher.matches?(actualArray)).to eq(false)
498
+
499
+ expectedJson=expected.to_json;
500
+ actualJson=actual.to_json;
501
+
502
+ String expectedErrorMessage= "bookId 2\n" +
503
+ "Expected: {\"bookId\":\"2\",\"name\":\"Eragon\",\"author\":\"Christopher Paolini\"}\n" +
504
+ makeGreen(" Actual: {\"bookId\":\"2\",\"name\":\"Eragon\",\"author\":\"Christopher Paolini\",\"isbn\":1234}") + "\n" +
505
+ "\nDiff:\n" +
506
+ "JSON path $.\n" +
507
+ "expected does not contain {\"isbn\":1234}\n" +
508
+ wrapWithResetColor("\n") + makeBlue("@@ -1,5 +1,4 @@\n") +
509
+ wrapWithResetColor(" :author => \"Christopher Paolini\",\n") +
510
+ wrapWithResetColor(" :bookId => \"2\",\n") +
511
+ makeRed("-:isbn => 1234,\n") +
512
+ wrapWithResetColor(" :name => \"Eragon\",\n")
513
+
514
+ expect(customMatcher.failure_message).to eq(expectedErrorMessage)
515
+
516
+ expect(actualArray).not_to eq_json_array_with_key(expectedArray, :bookId)
517
+
518
+ end
519
+ end
@@ -69,7 +69,7 @@ describe 'test nested array not same' do
69
69
  url: "www.amazon.com"
70
70
  }
71
71
 
72
- customMatcher=EqualWithOutOrderJson.new(expected)
72
+ customMatcher=EqualJson.new(expected)
73
73
 
74
74
  expect(customMatcher.matches?(actual)).to eq(false)
75
75
 
@@ -155,7 +155,7 @@ describe 'test nested array not same' do
155
155
  url: "www.amazon.com"
156
156
  }
157
157
 
158
- customMatcher=EqualWithOutOrderJson.new(expected)
158
+ customMatcher=EqualJson.new(expected)
159
159
 
160
160
  expect(customMatcher.matches?(actual)).to eq(false)
161
161
 
@@ -398,7 +398,7 @@ describe 'test nested array' do
398
398
  url: "www.amazon.com"
399
399
  }
400
400
 
401
- customMatcher=EqualWithOutOrderJson.new(expected)
401
+ customMatcher=EqualJson.new(expected)
402
402
 
403
403
  expect(customMatcher.matches?(actual)).to eq(false)
404
404
 
@@ -492,7 +492,7 @@ describe 'test nested array' do
492
492
  url: "www.amazon.com"
493
493
  }
494
494
 
495
- customMatcher=EqualWithOutOrderJson.new(expected)
495
+ customMatcher=EqualJson.new(expected)
496
496
 
497
497
  expect(customMatcher.matches?(actual)).to eq(false)
498
498
 
@@ -580,7 +580,7 @@ describe 'test nested array' do
580
580
  url: "www.amazon.com"
581
581
  }
582
582
 
583
- customMatcher=EqualWithOutOrderJson.new(expected)
583
+ customMatcher=EqualJson.new(expected)
584
584
 
585
585
  expect(customMatcher.matches?(actual)).to eq(false)
586
586
 
@@ -41,7 +41,7 @@ describe 'test nested objects not same type' do
41
41
 
42
42
  actualJson=actual.to_json
43
43
 
44
- customMatcher=EqualWithOutOrderJson.new(expected)
44
+ customMatcher=EqualJson.new(expected)
45
45
 
46
46
  expect(customMatcher.matches?(actual)).to eq(false)
47
47
 
@@ -151,7 +151,7 @@ describe 'test nested level json objects' do
151
151
  }
152
152
  }
153
153
 
154
- customMatcher=EqualWithOutOrderJson.new(expected)
154
+ customMatcher=EqualJson.new(expected)
155
155
 
156
156
  expect(customMatcher.matches?(actual)).to eq(false)
157
157
 
@@ -200,7 +200,7 @@ describe 'test nested level json objects' do
200
200
  }
201
201
  }
202
202
 
203
- customMatcher=EqualWithOutOrderJson.new(expected)
203
+ customMatcher=EqualJson.new(expected)
204
204
 
205
205
  expect(customMatcher.matches?(actual)).to eq(false)
206
206
 
@@ -248,7 +248,7 @@ describe 'test nested level json objects' do
248
248
  name: 'Harry Potter and the Sorcerer\'s Stone'
249
249
  }
250
250
 
251
- customMatcher=EqualWithOutOrderJson.new(expected)
251
+ customMatcher=EqualJson.new(expected)
252
252
 
253
253
  expect(customMatcher.matches?(actual)).to eq(false)
254
254
 
@@ -298,7 +298,7 @@ describe 'test nested level json objects' do
298
298
  }
299
299
  }
300
300
 
301
- customMatcher=EqualWithOutOrderJson.new(expected)
301
+ customMatcher=EqualJson.new(expected)
302
302
 
303
303
  expect(customMatcher.matches?(actual)).to eq(false)
304
304
 
@@ -349,7 +349,7 @@ describe 'test nested level json objects' do
349
349
  }
350
350
  }
351
351
 
352
- customMatcher=EqualWithOutOrderJson.new(expected)
352
+ customMatcher=EqualJson.new(expected)
353
353
 
354
354
  expect(customMatcher.matches?(actual)).to eq(false)
355
355
 
@@ -36,7 +36,7 @@ describe 'test top level array not same size' do
36
36
  }
37
37
  ]
38
38
 
39
- customMatcher=EqualWithOutOrderJson.new(expected)
39
+ customMatcher=EqualJson.new(expected)
40
40
 
41
41
  expect(customMatcher.matches?(actual)).to eq(false)
42
42
 
@@ -86,7 +86,7 @@ describe 'test top level array not same size' do
86
86
  }
87
87
  }
88
88
 
89
- customMatcher=EqualWithOutOrderJson.new(expected)
89
+ customMatcher=EqualJson.new(expected)
90
90
 
91
91
  expect(customMatcher.matches?(actual)).to eq(false)
92
92
 
@@ -210,7 +210,7 @@ describe 'test top level array' do
210
210
  book3Item
211
211
  ]
212
212
 
213
- customMatcher=EqualWithOutOrderJson.new(expected)
213
+ customMatcher=EqualJson.new(expected)
214
214
 
215
215
  expect(customMatcher.matches?(actual)).to eq(false)
216
216
 
@@ -273,7 +273,7 @@ describe 'test top level array' do
273
273
  book3Item
274
274
  ]
275
275
 
276
- customMatcher=EqualWithOutOrderJson.new(expected)
276
+ customMatcher=EqualJson.new(expected)
277
277
 
278
278
  expect(customMatcher.matches?(actual)).to eq(false)
279
279
 
@@ -329,7 +329,7 @@ describe 'test top level array' do
329
329
  book3Item
330
330
  ]
331
331
 
332
- customMatcher=EqualWithOutOrderJson.new(expected)
332
+ customMatcher=EqualJson.new(expected)
333
333
 
334
334
  expect(customMatcher.matches?(actual)).to eq(false)
335
335