eq_json 1.0.2

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,604 @@
1
+ require 'eq_json'
2
+ require 'json'
3
+ require 'spec_helper'
4
+
5
+ describe 'test nested array not same' do
6
+
7
+ it 'test not the same size' 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
+ bookId: "3",
33
+ name: "The Fellowship of the Ring",
34
+ author: "J.R.R. Tolkien"
35
+ }
36
+ ]
37
+ }
38
+ }
39
+ },
40
+ url: "www.amazon.com"
41
+ }
42
+
43
+ expected = {
44
+ bookSeller: "amazon",
45
+ bookWholeSellers: {
46
+ publisherInfo: {
47
+ name: "ACME Publisher Inc.",
48
+ publishDate: {
49
+ month: 3,
50
+ day: 23,
51
+ year: 2015
52
+ },
53
+ products: {
54
+ books: [
55
+ {
56
+ bookId: "1",
57
+ name: "Harry Potter and the Sorcerer's Stone",
58
+ author: "J.K. Rowling"
59
+ },
60
+ {
61
+ bookId: "2",
62
+ name: "Eragon",
63
+ author: "Christopher Paolini"
64
+ }
65
+ ]
66
+ }
67
+ }
68
+ },
69
+ url: "www.amazon.com"
70
+ }
71
+
72
+ customMatcher=EqualWithOutOrderJson.new(expected)
73
+
74
+ expect(customMatcher.matches?(actual)).to eq(false)
75
+
76
+ expectedJson=expected.to_json;
77
+ actualJson=actual.to_json;
78
+
79
+ String expectedErrorMessage= "Expected: #{expectedJson}\n" +
80
+ makeGreen(" Actual: #{actualJson}") + "\n" +
81
+ "\nDiff:\n" +
82
+ "JSON path $.bookWholeSellers.publisherInfo.products.books[] expected length 2 actual length 3\n"
83
+
84
+ expect(customMatcher.failure_message).to eq(expectedErrorMessage)
85
+
86
+ expect(expected).not_to eq_json(actual)
87
+ end
88
+
89
+ it 'test not the same object type' do
90
+
91
+ actualBooks = [
92
+ {
93
+ bookId: "1",
94
+ name: "Harry Potter and the Sorcerer's Stone",
95
+ author: "J.K. Rowling"
96
+ },
97
+ {
98
+ bookId: "2",
99
+ name: "Eragon",
100
+ author: "Christopher Paolini"
101
+ },
102
+ {
103
+ bookId: "3",
104
+ name: "The Fellowship of the Ring",
105
+ author: "J.R.R. Tolkien"
106
+ }
107
+ ]
108
+
109
+ actual = {
110
+ bookSeller: "amazon",
111
+ bookWholeSellers: {
112
+ publisherInfo: {
113
+ name: "ACME Publisher Inc.",
114
+ publishDate: {
115
+ month: 3,
116
+ day: 23,
117
+ year: 2015
118
+ },
119
+ products: {
120
+ books: actualBooks
121
+ }
122
+ }
123
+ },
124
+ url: "www.amazon.com"
125
+ }
126
+
127
+ expectedBooks = {
128
+ book1: {
129
+ bookId: "1",
130
+ name: "Harry Potter and the Sorcerer's Stone",
131
+ author: "J.K. Rowling"
132
+ },
133
+ book2: {
134
+ bookId: "2",
135
+ name: "Eragon",
136
+ author: "Christopher Paolini"
137
+ }
138
+ }
139
+
140
+ expected = {
141
+ bookSeller: "amazon",
142
+ bookWholeSellers: {
143
+ publisherInfo: {
144
+ name: "ACME Publisher Inc.",
145
+ publishDate: {
146
+ month: 3,
147
+ day: 23,
148
+ year: 2015
149
+ },
150
+ products: {
151
+ books: expectedBooks
152
+ }
153
+ }
154
+ },
155
+ url: "www.amazon.com"
156
+ }
157
+
158
+ customMatcher=EqualWithOutOrderJson.new(expected)
159
+
160
+ expect(customMatcher.matches?(actual)).to eq(false)
161
+
162
+ expectedJson=expected.to_json;
163
+ actualJson=actual.to_json;
164
+
165
+ String expectedErrorMessage= "Expected: #{expectedJson}\n" +
166
+ makeGreen(" Actual: #{actualJson}") + "\n" +
167
+ "Diff:\n" +
168
+ "JSON path $.bookWholeSellers.publisherInfo.products.books expected object type but actual is array\n" +
169
+ "\tExpected: #{expectedBooks.to_json}\n" +
170
+ makeGreen("\t Actual: #{actualBooks.to_json}") + "\n"
171
+
172
+ expect(customMatcher.failure_message).to eq(expectedErrorMessage)
173
+
174
+ expect(expected).not_to eq_json(actual)
175
+ end
176
+
177
+ end
178
+
179
+ describe 'test nested array' do
180
+
181
+ it 'test actual and expected equal' do
182
+
183
+ actual = {
184
+ bookSeller: "amazon",
185
+ bookWholeSellers: {
186
+ publisherInfo: {
187
+ name: "ACME Publisher Inc.",
188
+ publishDate: {
189
+ month: 3,
190
+ day: 23,
191
+ year: 2015
192
+ },
193
+ products: {
194
+ books: [
195
+ {
196
+ bookId: "1",
197
+ name: "Harry Potter and the Sorcerer's Stone",
198
+ author: "J.K. Rowling"
199
+ },
200
+ {
201
+ bookId: "2",
202
+ name: "Eragon",
203
+ author: "Christopher Paolini"
204
+ },
205
+ {
206
+ bookId: "3",
207
+ name: "The Fellowship of the Ring",
208
+ author: "J.R.R. Tolkien"
209
+ }
210
+ ]
211
+ }
212
+ }
213
+ },
214
+ url: "www.amazon.com"
215
+ }
216
+
217
+ expected = {
218
+ bookSeller: "amazon",
219
+ bookWholeSellers: {
220
+ publisherInfo: {
221
+ name: "ACME Publisher Inc.",
222
+ publishDate: {
223
+ month: 3,
224
+ day: 23,
225
+ year: 2015
226
+ },
227
+ products: {
228
+ books: [
229
+ {
230
+ bookId: "1",
231
+ name: "Harry Potter and the Sorcerer's Stone",
232
+ author: "J.K. Rowling"
233
+ },
234
+ {
235
+ bookId: "2",
236
+ name: "Eragon",
237
+ author: "Christopher Paolini"
238
+ },
239
+ {
240
+ bookId: "3",
241
+ name: "The Fellowship of the Ring",
242
+ author: "J.R.R. Tolkien"
243
+ }
244
+ ]
245
+ }
246
+ }
247
+ },
248
+ url: "www.amazon.com"
249
+ }
250
+
251
+ expect(expected).to eq_json(actual)
252
+ end
253
+
254
+ it 'test actual and expected equal but out of order' do
255
+
256
+ actual = {
257
+ bookSeller: "amazon",
258
+ bookWholeSellers: {
259
+ publisherInfo: {
260
+ name: "ACME Publisher Inc.",
261
+ publishDate: {
262
+ month: 3,
263
+ day: 23,
264
+ year: 2015
265
+ },
266
+ products: {
267
+ books: [
268
+ {
269
+ bookId: "1",
270
+ name: "Harry Potter and the Sorcerer's Stone",
271
+ author: "J.K. Rowling"
272
+ },
273
+
274
+ {
275
+ bookId: "3",
276
+ name: "The Fellowship of the Ring",
277
+ author: "J.R.R. Tolkien"
278
+ },
279
+ {
280
+ bookId: "2",
281
+ name: "Eragon",
282
+ author: "Christopher Paolini"
283
+ }
284
+ ]
285
+ }
286
+ }
287
+ },
288
+ url: "www.amazon.com"
289
+ }
290
+
291
+ expected = {
292
+ bookSeller: "amazon",
293
+ bookWholeSellers: {
294
+ publisherInfo: {
295
+ name: "ACME Publisher Inc.",
296
+ publishDate: {
297
+ month: 3,
298
+ day: 23,
299
+ year: 2015
300
+ },
301
+ products: {
302
+ books: [
303
+ {
304
+ bookId: "1",
305
+ name: "Harry Potter and the Sorcerer's Stone",
306
+ author: "J.K. Rowling"
307
+ },
308
+ {
309
+ bookId: "2",
310
+ name: "Eragon",
311
+ author: "Christopher Paolini"
312
+ },
313
+ {
314
+ bookId: "3",
315
+ name: "The Fellowship of the Ring",
316
+ author: "J.R.R. Tolkien"
317
+ }
318
+ ]
319
+ }
320
+ }
321
+ },
322
+ url: "www.amazon.com"
323
+ }
324
+
325
+ expect(expected).to eq_json(actual)
326
+ end
327
+
328
+ it 'test actual does not contain an element in expected' do
329
+
330
+ actual = {
331
+ bookSeller: "amazon",
332
+ bookWholeSellers: {
333
+ publisherInfo: {
334
+ name: "ACME Publisher Inc.",
335
+ publishDate: {
336
+ month: 3,
337
+ day: 23,
338
+ year: 2015
339
+ },
340
+ products: {
341
+ books: [
342
+ {
343
+ bookId: "1",
344
+ name: "Harry Potter and the Sorcerer's Stone",
345
+ author: "J.K. Rowling"
346
+ },
347
+ {
348
+ bookId: "2",
349
+ name: "Eragon",
350
+ author: "Christopher Paolini"
351
+ },
352
+ {
353
+ bookId: "4",
354
+ name: "Effective Java",
355
+ author: "Cannot Remember"
356
+ }
357
+ ]
358
+ }
359
+ }
360
+ },
361
+ url: "www.amazon.com"
362
+ }
363
+
364
+ book3Item =
365
+ {
366
+ bookId: "3",
367
+ name: "The Fellowship of the Ring",
368
+ author: "J.R.R. Tolkien"
369
+ }
370
+
371
+ expected = {
372
+ bookSeller: "amazon",
373
+ bookWholeSellers: {
374
+ publisherInfo: {
375
+ name: "ACME Publisher Inc.",
376
+ publishDate: {
377
+ month: 3,
378
+ day: 23,
379
+ year: 2015
380
+ },
381
+ products: {
382
+ books: [
383
+ {
384
+ bookId: "1",
385
+ name: "Harry Potter and the Sorcerer's Stone",
386
+ author: "J.K. Rowling"
387
+ },
388
+ {
389
+ bookId: "2",
390
+ name: "Eragon",
391
+ author: "Christopher Paolini"
392
+ },
393
+ book3Item
394
+ ]
395
+ }
396
+ }
397
+ },
398
+ url: "www.amazon.com"
399
+ }
400
+
401
+ customMatcher=EqualWithOutOrderJson.new(expected)
402
+
403
+ expect(customMatcher.matches?(actual)).to eq(false)
404
+
405
+ expectedJson=expected.to_json;
406
+ actualJson=actual.to_json;
407
+
408
+ String expectedErrorMessage= "Expected: #{expectedJson}\n" +
409
+ makeGreen(" Actual: #{actualJson}") + "\n" +
410
+ "\nDiff:\n" +
411
+ "JSON path $.bookWholeSellers.publisherInfo.products.books[] could not find:\n" +
412
+ "#{book3Item.to_json}\n" +
413
+ "in actual\n"
414
+
415
+ expect(customMatcher.failure_message).to eq(expectedErrorMessage)
416
+
417
+ expect(expected).not_to eq_json(actual)
418
+ end
419
+
420
+ it 'test expected has two of same elements and actual has one' do
421
+
422
+ book3Item =
423
+ {
424
+ bookId: "3",
425
+ name: "The Fellowship of the Ring",
426
+ author: "J.R.R. Tolkien"
427
+ }
428
+
429
+ actual = {
430
+ bookSeller: "amazon",
431
+ bookWholeSellers: {
432
+ publisherInfo: {
433
+ name: "ACME Publisher Inc.",
434
+ publishDate: {
435
+ month: 3,
436
+ day: 23,
437
+ year: 2015
438
+ },
439
+ products: {
440
+ books: [
441
+ {
442
+ bookId: "1",
443
+ name: "Harry Potter and the Sorcerer's Stone",
444
+ author: "J.K. Rowling"
445
+ },
446
+ {
447
+ bookId: "2",
448
+ name: "Eragon",
449
+ author: "Christopher Paolini"
450
+ },
451
+ {
452
+ bookId: "4",
453
+ name: "Effective Java",
454
+ author: "Cannot Remember"
455
+ },
456
+ book3Item
457
+ ]
458
+ }
459
+ }
460
+ },
461
+ url: "www.amazon.com"
462
+ }
463
+
464
+ expected = {
465
+ bookSeller: "amazon",
466
+ bookWholeSellers: {
467
+ publisherInfo: {
468
+ name: "ACME Publisher Inc.",
469
+ publishDate: {
470
+ month: 3,
471
+ day: 23,
472
+ year: 2015
473
+ },
474
+ products: {
475
+ books: [
476
+ {
477
+ bookId: "1",
478
+ name: "Harry Potter and the Sorcerer's Stone",
479
+ author: "J.K. Rowling"
480
+ },
481
+ book3Item,
482
+ book3Item,
483
+ {
484
+ bookId: "2",
485
+ name: "Eragon",
486
+ author: "Christopher Paolini"
487
+ }
488
+ ]
489
+ }
490
+ }
491
+ },
492
+ url: "www.amazon.com"
493
+ }
494
+
495
+ customMatcher=EqualWithOutOrderJson.new(expected)
496
+
497
+ expect(customMatcher.matches?(actual)).to eq(false)
498
+
499
+ expectedJson=expected.to_json;
500
+ actualJson=actual.to_json;
501
+
502
+ String expectedErrorMessage= "Expected: #{expectedJson}\n" +
503
+ makeGreen(" Actual: #{actualJson}") + "\n" +
504
+ "\nDiff:\n" +
505
+ "JSON path $.bookWholeSellers.publisherInfo.products.books[] wrong number of:\n" +
506
+ "#{book3Item.to_json}\n" +
507
+ "in actual\n" +
508
+ "expected: 2\n" +
509
+ makeGreen(" got: 1") + "\n"
510
+
511
+ expect(customMatcher.failure_message).to eq(expectedErrorMessage)
512
+
513
+ expect(expected).not_to eq_json(actual)
514
+ end
515
+
516
+ it 'test expected has two of same elements and actual has three' do
517
+
518
+ book3Item =
519
+ {
520
+ bookId: "3",
521
+ name: "The Fellowship of the Ring",
522
+ author: "J.R.R. Tolkien"
523
+ }
524
+
525
+ actual = {
526
+ bookSeller: "amazon",
527
+ bookWholeSellers: {
528
+ publisherInfo: {
529
+ name: "ACME Publisher Inc.",
530
+ publishDate: {
531
+ month: 3,
532
+ day: 23,
533
+ year: 2015
534
+ },
535
+ products: {
536
+ books: [
537
+ {
538
+ bookId: "4",
539
+ name: "Effective Java",
540
+ author: "Cannot Remember"
541
+ },
542
+ book3Item,
543
+ book3Item,
544
+ book3Item
545
+ ]
546
+ }
547
+ }
548
+ },
549
+ url: "www.amazon.com"
550
+ }
551
+
552
+ expected = {
553
+ bookSeller: "amazon",
554
+ bookWholeSellers: {
555
+ publisherInfo: {
556
+ name: "ACME Publisher Inc.",
557
+ publishDate: {
558
+ month: 3,
559
+ day: 23,
560
+ year: 2015
561
+ },
562
+ products: {
563
+ books: [
564
+ book3Item,
565
+ {
566
+ bookId: "1",
567
+ name: "Harry Potter and the Sorcerer's Stone",
568
+ author: "J.K. Rowling"
569
+ },
570
+ book3Item,
571
+ {
572
+ bookId: "2",
573
+ name: "Eragon",
574
+ author: "Christopher Paolini"
575
+ }
576
+ ]
577
+ }
578
+ }
579
+ },
580
+ url: "www.amazon.com"
581
+ }
582
+
583
+ customMatcher=EqualWithOutOrderJson.new(expected)
584
+
585
+ expect(customMatcher.matches?(actual)).to eq(false)
586
+
587
+ expectedJson=expected.to_json;
588
+ actualJson=actual.to_json;
589
+
590
+ String expectedErrorMessage= "Expected: #{expectedJson}\n" +
591
+ makeGreen(" Actual: #{actualJson}") + "\n" +
592
+ "\nDiff:\n" +
593
+ "JSON path $.bookWholeSellers.publisherInfo.products.books[] wrong number of:\n" +
594
+ "#{book3Item.to_json}\n" +
595
+ "in actual\n" +
596
+ "expected: 2\n" +
597
+ makeGreen(" got: 3") + "\n"
598
+
599
+ expect(customMatcher.failure_message).to eq(expectedErrorMessage)
600
+
601
+ expect(expected).not_to eq_json(actual)
602
+ end
603
+
604
+ end