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,371 @@
1
+ require 'eq_json'
2
+ require 'json'
3
+
4
+ describe 'test nested objects not same type' do
5
+ it 'expected JSON array actual JSON object' do
6
+
7
+ actual = {
8
+ name: 'Harry Potter and the Sorcerer\'s Stone',
9
+ author: 'J.K. Rowling',
10
+ publisherInfo: {
11
+ name: "ACME Publisher Inc.",
12
+ publishDate: {
13
+ month: 3,
14
+ day: 23,
15
+ year: 2015
16
+ }
17
+ }
18
+ }
19
+
20
+ expected = {
21
+ name: 'Harry Potter and the Sorcerer\'s Stone',
22
+ author: 'J.K. Rowling',
23
+ publisherInfo: {
24
+ name: "ACME Publisher Inc.",
25
+ publishDate: [
26
+ {
27
+ month: 2,
28
+ day: 24,
29
+ year: 2011
30
+ },
31
+ {
32
+ month: 1,
33
+ day: 2,
34
+ year: 1999
35
+ }
36
+ ]
37
+ }
38
+ }
39
+
40
+ expectedJson=expected.to_json
41
+
42
+ actualJson=actual.to_json
43
+
44
+ customMatcher=EqualWithOutOrderJson.new(expected)
45
+
46
+ expect(customMatcher.matches?(actual)).to eq(false)
47
+
48
+ String expectedErrorMessage= "Expected: #{expectedJson}\n" +
49
+ makeGreen(" Actual: #{actualJson}") + "\n" +
50
+ "Diff:\n" +
51
+ "JSON path $.publisherInfo.publishDate expected array type but actual is object\n" +
52
+ "\tExpected: [" +
53
+ "{\"month\":2,\"day\":24,\"year\":2011}," +
54
+ "{\"month\":1,\"day\":2,\"year\":1999}" +
55
+ "]\n" +
56
+ makeGreen("\t Actual: {\"month\":3,\"day\":23,\"year\":2015}") + "\n"
57
+
58
+ expect(customMatcher.failure_message).to eq(expectedErrorMessage)
59
+
60
+ expect(expected).not_to eq_json(actual)
61
+ end
62
+ end
63
+
64
+ describe 'test nested level json objects' do
65
+
66
+ it 'test that objects equal in order' do
67
+ actual = {
68
+ name: 'Harry Potter and the Sorcerer\'s Stone',
69
+ author: 'J.K. Rowling',
70
+ publisherInfo: {
71
+ name: "ACME Publisher Inc.",
72
+ publishDate: {
73
+ month: 3,
74
+ day: 23,
75
+ year: 2015
76
+ }
77
+ }
78
+ }
79
+
80
+ expected = {
81
+ name: 'Harry Potter and the Sorcerer\'s Stone',
82
+ author: 'J.K. Rowling',
83
+ publisherInfo: {
84
+ name: "ACME Publisher Inc.",
85
+ publishDate: {
86
+ month: 3,
87
+ day: 23,
88
+ year: 2015
89
+ }
90
+ }
91
+ }
92
+
93
+ expect(expected).to eq_json(actual)
94
+ end
95
+
96
+ it 'test that objects equal out of order' do
97
+
98
+ actual = {
99
+ name: 'Harry Potter and the Sorcerer\'s Stone',
100
+ publisherInfo: {
101
+ publishDate: {
102
+ year: 2015,
103
+ month: 3,
104
+ day: 23
105
+ },
106
+ name: "ACME Publisher Inc."
107
+ },
108
+ author: 'J.K. Rowling'
109
+ }
110
+
111
+ expected = {
112
+ name: 'Harry Potter and the Sorcerer\'s Stone',
113
+ author: 'J.K. Rowling',
114
+ publisherInfo: {
115
+ name: "ACME Publisher Inc.",
116
+ publishDate: {
117
+ month: 3,
118
+ day: 23,
119
+ year: 2015
120
+ }
121
+ }
122
+ }
123
+
124
+ expect(expected).to eq_json(actual)
125
+ end
126
+
127
+ it 'actual missing object' do
128
+
129
+ actual = {
130
+ name: 'Harry Potter and the Sorcerer\'s Stone',
131
+ author: 'J.K. Rowling',
132
+ publisherInfo: {
133
+ name: "ACME Publisher Inc.",
134
+ publishDate: {
135
+ month: 3,
136
+ year: 2015
137
+ }
138
+ }
139
+ }
140
+
141
+ expected = {
142
+ name: 'Harry Potter and the Sorcerer\'s Stone',
143
+ author: 'J.K. Rowling',
144
+ publisherInfo: {
145
+ name: "ACME Publisher Inc.",
146
+ publishDate: {
147
+ month: 3,
148
+ day: 23,
149
+ year: 2015
150
+ }
151
+ }
152
+ }
153
+
154
+ customMatcher=EqualWithOutOrderJson.new(expected)
155
+
156
+ expect(customMatcher.matches?(actual)).to eq(false)
157
+
158
+ expectedJson=expected.to_json;
159
+ actualJson=actual.to_json;
160
+
161
+ String expectedErrorMessage= "Expected: #{expectedJson}\n" +
162
+ makeGreen(" Actual: #{actualJson}") + "\n" +
163
+ "\nDiff:\n" +
164
+ "JSON path $.publisherInfo.publishDate\n" +
165
+ makeGreen("actual does not contain {\"day\":23}\n") +
166
+ wrapWithResetColor("\n") + makeBlue("@@ -1,3 +1,4 @@\n") +
167
+ makeGreen("+:day => 23,\n") +
168
+ wrapWithResetColor(" :month => 3,\n") +
169
+ wrapWithResetColor(" :year => 2015,\n")
170
+
171
+ expect(customMatcher.failure_message).to eq(expectedErrorMessage)
172
+
173
+ expect(expected).not_to eq_json(actual)
174
+ end
175
+
176
+ it 'expected missing object' do
177
+
178
+ actual = {
179
+ name: 'Harry Potter and the Sorcerer\'s Stone',
180
+ author: 'J.K. Rowling',
181
+ publisherInfo: {
182
+ name: "ACME Publisher Inc.",
183
+ publishDate: {
184
+ month: 3,
185
+ day: 23,
186
+ year: 2015
187
+ }
188
+ }
189
+ }
190
+
191
+ expected = {
192
+ name: 'Harry Potter and the Sorcerer\'s Stone',
193
+ author: 'J.K. Rowling',
194
+ publisherInfo: {
195
+ name: "ACME Publisher Inc.",
196
+ publishDate: {
197
+ month: 3,
198
+ day: 23,
199
+ }
200
+ }
201
+ }
202
+
203
+ customMatcher=EqualWithOutOrderJson.new(expected)
204
+
205
+ expect(customMatcher.matches?(actual)).to eq(false)
206
+
207
+ expectedJson=expected.to_json;
208
+ actualJson=actual.to_json;
209
+
210
+ String expectedErrorMessage= "Expected: #{expectedJson}\n" +
211
+ makeGreen(" Actual: #{actualJson}") + "\n" +
212
+ "\nDiff:\n" +
213
+ "JSON path $.publisherInfo.publishDate\n" +
214
+ "expected does not contain {\"year\":2015}\n" +
215
+ wrapWithResetColor("\n") + makeBlue("@@ -1,4 +1,3 @@\n") +
216
+ wrapWithResetColor(" :day => 23,\n") +
217
+ wrapWithResetColor(" :month => 3,\n") +
218
+ makeRed("-:year => 2015,\n")
219
+
220
+ expect(customMatcher.failure_message).to eq(expectedErrorMessage)
221
+
222
+ expect(expected).not_to eq_json(actual)
223
+ end
224
+
225
+ it 'expected missing mutiple objects' do
226
+
227
+ actual = {
228
+ name: 'Harry Potter and the Sorcerer\'s Stone',
229
+ author: 'J.K. Rowling',
230
+ publisherInfo: {
231
+ name: "ACME Publisher Inc.",
232
+ publishDate: {
233
+ month: 3,
234
+ day: 23,
235
+ year: 2015
236
+ }
237
+ }
238
+ }
239
+
240
+ expected = {
241
+ publisherInfo: {
242
+ name: "ACME Publisher Inc.",
243
+ publishDate: {
244
+ month: 3
245
+ }
246
+ },
247
+ author: 'J.K. Rowling',
248
+ name: 'Harry Potter and the Sorcerer\'s Stone'
249
+ }
250
+
251
+ customMatcher=EqualWithOutOrderJson.new(expected)
252
+
253
+ expect(customMatcher.matches?(actual)).to eq(false)
254
+
255
+ expectedJson=expected.to_json;
256
+ actualJson=actual.to_json;
257
+
258
+ String expectedErrorMessage= "Expected: #{expectedJson}\n" +
259
+ makeGreen(" Actual: #{actualJson}") + "\n" +
260
+ "\nDiff:\n" +
261
+ "JSON path $.publisherInfo.publishDate\n" +
262
+ "expected does not contain {\"day\":23,\"year\":2015}\n" +
263
+ wrapWithResetColor("\n") + wrapWithResetColor("\n") +
264
+ makeBlue("@@ -1,4 +1,2 @@\n") +
265
+ makeRed("-:day => 23,\n") +
266
+ wrapWithResetColor(" :month => 3,\n") +
267
+ makeRed("-:year => 2015,\n")
268
+
269
+ expect(customMatcher.failure_message).to eq(expectedErrorMessage)
270
+
271
+ expect(expected).not_to eq_json(actual)
272
+ end
273
+
274
+
275
+ it 'expected and actual both have missing objects' do
276
+
277
+ actual = {
278
+ name: 'Harry Potter and the Sorcerer\'s Stone',
279
+ author: 'J.K. Rowling',
280
+ publisherInfo: {
281
+ name: "ACME Publisher Inc.",
282
+ publishDate: {
283
+ day: 23,
284
+ year: 2015
285
+ }
286
+ }
287
+ }
288
+
289
+ expected = {
290
+ name: 'Harry Potter and the Sorcerer\'s Stone',
291
+ author: 'J.K. Rowling',
292
+ publisherInfo: {
293
+ name: "ACME Publisher Inc.",
294
+ publishDate: {
295
+ month: 3,
296
+ day: 23
297
+ }
298
+ }
299
+ }
300
+
301
+ customMatcher=EqualWithOutOrderJson.new(expected)
302
+
303
+ expect(customMatcher.matches?(actual)).to eq(false)
304
+
305
+ expectedJson=expected.to_json;
306
+ actualJson=actual.to_json;
307
+
308
+ String expectedErrorMessage= "Expected: #{expectedJson}\n" +
309
+ makeGreen(" Actual: #{actualJson}") + "\n" +
310
+ "\nDiff:\n" +
311
+ "JSON path $.publisherInfo.publishDate\n" +
312
+ "expected does not contain {\"year\":2015}\n" +
313
+ makeGreen("actual does not contain {\"month\":3}\n") +
314
+ wrapWithResetColor("\n") + makeBlue("@@ -1,3 +1,3 @@\n") +
315
+ wrapWithResetColor(" :day => 23,\n") +
316
+ makeRed("-:year => 2015,\n") +
317
+ makeGreen("+:month => 3,\n")
318
+
319
+ expect(customMatcher.failure_message).to eq(expectedErrorMessage)
320
+
321
+ expect(expected).not_to eq_json(actual)
322
+ end
323
+
324
+ it 'expected and actual have different values for key' do
325
+
326
+ actual = {
327
+ name: 'Harry Potter and the Sorcerer\'s Stone',
328
+ author: 'J.K. Rowling',
329
+ publisherInfo: {
330
+ name: "ACME Publisher Inc.",
331
+ publishDate: {
332
+ month: 3,
333
+ day: 23,
334
+ year: 2017
335
+ }
336
+ }
337
+ }
338
+
339
+ expected = {
340
+ name: 'Harry Potter and the Sorcerer\'s Stone',
341
+ author: 'J.K. Rowling',
342
+ publisherInfo: {
343
+ name: "ACME Publisher Inc.",
344
+ publishDate: {
345
+ month: 3,
346
+ day: 23,
347
+ year: 2015
348
+ }
349
+ }
350
+ }
351
+
352
+ customMatcher=EqualWithOutOrderJson.new(expected)
353
+
354
+ expect(customMatcher.matches?(actual)).to eq(false)
355
+
356
+ expectedJson=expected.to_json;
357
+ actualJson=actual.to_json;
358
+
359
+ String expectedErrorMessage= "Expected: #{expectedJson}\n" +
360
+ makeGreen(" Actual: #{actualJson}") + "\n" +
361
+ "Diff:\n" +
362
+ "JSON path $.publisherInfo.publishDate.year\n" +
363
+ "\texpected: \"2015\"\n" +
364
+ makeGreen("\t got: \"2017\"")
365
+
366
+ expect(customMatcher.failure_message).to eq(expectedErrorMessage)
367
+
368
+ expect(expected).not_to eq_json(actual)
369
+ end
370
+
371
+ end
@@ -0,0 +1,353 @@
1
+ require 'eq_json'
2
+ require 'json'
3
+ require 'spec_helper'
4
+
5
+ describe 'test top level array not same size' do
6
+ it 'test not the same size' do
7
+
8
+ actual = [
9
+ {
10
+ bookId: "1",
11
+ name: "Harry Potter and the Sorcerer's Stone",
12
+ author: "J.K. Rowling"
13
+ },
14
+ {
15
+ bookId: "2",
16
+ name: "Eragon",
17
+ author: "Christopher Paolini"
18
+ },
19
+ {
20
+ bookId: "3",
21
+ name: "The Fellowship of the Ring",
22
+ author: "J.R.R. Tolkien"
23
+ }
24
+ ]
25
+
26
+ expected = [
27
+ {
28
+ bookId: "1",
29
+ name: "Harry Potter and the Sorcerer's Stone",
30
+ author: "J.K. Rowling"
31
+ },
32
+ {
33
+ bookId: "2",
34
+ name: "Eragon",
35
+ author: "Christopher Paolini"
36
+ }
37
+ ]
38
+
39
+ customMatcher=EqualWithOutOrderJson.new(expected)
40
+
41
+ expect(customMatcher.matches?(actual)).to eq(false)
42
+
43
+ expectedJson=expected.to_json;
44
+ actualJson=actual.to_json;
45
+
46
+ String expectedErrorMessage= "Expected: #{expectedJson}\n" +
47
+ makeGreen(" Actual: #{actualJson}") + "\n" +
48
+ "\nDiff:\n" +
49
+ "JSON path $.[] expected length 2 actual length 3\n"
50
+
51
+ expect(customMatcher.failure_message).to eq(expectedErrorMessage)
52
+
53
+ expect(expected).not_to eq_json(actual)
54
+ end
55
+
56
+ it 'test not the same object type' do
57
+
58
+ actual = [
59
+ {
60
+ bookId: "1",
61
+ name: "Harry Potter and the Sorcerer's Stone",
62
+ author: "J.K. Rowling"
63
+ },
64
+ {
65
+ bookId: "2",
66
+ name: "Eragon",
67
+ author: "Christopher Paolini"
68
+ },
69
+ {
70
+ bookId: "3",
71
+ name: "The Fellowship of the Ring",
72
+ author: "J.R.R. Tolkien"
73
+ }
74
+ ]
75
+
76
+ expected = {
77
+ book1: {
78
+ bookId: "1",
79
+ name: "Harry Potter and the Sorcerer's Stone",
80
+ author: "J.K. Rowling"
81
+ },
82
+ book2: {
83
+ bookId: "2",
84
+ name: "Eragon",
85
+ author: "Christopher Paolini"
86
+ }
87
+ }
88
+
89
+ customMatcher=EqualWithOutOrderJson.new(expected)
90
+
91
+ expect(customMatcher.matches?(actual)).to eq(false)
92
+
93
+ expectedJson=expected.to_json;
94
+ actualJson=actual.to_json;
95
+
96
+ String expectedErrorMessage= "Expected: #{expectedJson}\n" +
97
+ makeGreen(" Actual: #{actualJson}") + "\n" +
98
+ "Diff:\n" +
99
+ "JSON path $. expected object type but actual is array\n"
100
+
101
+ expect(customMatcher.failure_message).to eq(expectedErrorMessage)
102
+
103
+ expect(expected).not_to eq_json(actual)
104
+ end
105
+
106
+ end
107
+
108
+ describe 'test top level array' do
109
+
110
+ it 'test actual and expected equal' do
111
+
112
+ actual = [
113
+ {
114
+ bookId: "1",
115
+ name: "Harry Potter and the Sorcerer's Stone",
116
+ author: "J.K. Rowling"
117
+ },
118
+ {
119
+ bookId: "2",
120
+ name: "Eragon",
121
+ author: "Christopher Paolini"
122
+ }
123
+ ]
124
+
125
+ expected = [
126
+ {
127
+ bookId: "1",
128
+ name: "Harry Potter and the Sorcerer's Stone",
129
+ author: "J.K. Rowling"
130
+ },
131
+ {
132
+ bookId: "2",
133
+ name: "Eragon",
134
+ author: "Christopher Paolini"
135
+ }
136
+ ]
137
+
138
+ expect(expected).to eq_json(actual)
139
+ end
140
+
141
+ it 'test actual and expected equal but out of order' do
142
+
143
+ actual = [
144
+ {
145
+ bookId: "1",
146
+ name: "Harry Potter and the Sorcerer's Stone",
147
+ author: "J.K. Rowling"
148
+ },
149
+ {
150
+ bookId: "2",
151
+ name: "Eragon",
152
+ author: "Christopher Paolini"
153
+ }
154
+ ]
155
+
156
+ expected = [
157
+ {
158
+ bookId: "2",
159
+ name: "Eragon",
160
+ author: "Christopher Paolini"
161
+ },
162
+ {
163
+ bookId: "1",
164
+ name: "Harry Potter and the Sorcerer's Stone",
165
+ author: "J.K. Rowling"
166
+ }
167
+ ]
168
+
169
+ expect(expected).to eq_json(actual)
170
+ end
171
+
172
+ it 'test actual does not contain an element in expected' do
173
+
174
+ actual = [
175
+ {
176
+ bookId: "1",
177
+ name: "Harry Potter and the Sorcerer's Stone",
178
+ author: "J.K. Rowling"
179
+ },
180
+ {
181
+ bookId: "2",
182
+ name: "Eragon",
183
+ author: "Christopher Paolini"
184
+ },
185
+ {
186
+ bookId: "4",
187
+ name: "Effective Java",
188
+ author: "Cannot Remember"
189
+ }
190
+ ]
191
+
192
+ book3Item =
193
+ {
194
+ bookId: "3",
195
+ name: "The Fellowship of the Ring",
196
+ author: "J.R.R. Tolkien"
197
+ }
198
+
199
+ expected = [
200
+ {
201
+ bookId: "1",
202
+ name: "Harry Potter and the Sorcerer's Stone",
203
+ author: "J.K. Rowling"
204
+ },
205
+ {
206
+ bookId: "2",
207
+ name: "Eragon",
208
+ author: "Christopher Paolini"
209
+ },
210
+ book3Item
211
+ ]
212
+
213
+ customMatcher=EqualWithOutOrderJson.new(expected)
214
+
215
+ expect(customMatcher.matches?(actual)).to eq(false)
216
+
217
+ expectedJson=expected.to_json;
218
+ actualJson=actual.to_json;
219
+
220
+ String expectedErrorMessage= "Expected: #{expectedJson}\n" +
221
+ makeGreen(" Actual: #{actualJson}") + "\n" +
222
+ "\nDiff:\n" +
223
+ "JSON path $.[] could not find:\n" +
224
+ "#{book3Item.to_json}\n" +
225
+ "in actual\n"
226
+
227
+ expect(customMatcher.failure_message).to eq(expectedErrorMessage)
228
+
229
+ expect(expected).not_to eq_json(actual)
230
+ end
231
+
232
+ it 'test expected has two of same elements and actual has one' do
233
+
234
+ book3Item =
235
+ {
236
+ bookId: "3",
237
+ name: "The Fellowship of the Ring",
238
+ author: "J.R.R. Tolkien"
239
+ }
240
+
241
+ actual = [
242
+ {
243
+ bookId: "1",
244
+ name: "Harry Potter and the Sorcerer's Stone",
245
+ author: "J.K. Rowling"
246
+ },
247
+ {
248
+ bookId: "2",
249
+ name: "Eragon",
250
+ author: "Christopher Paolini"
251
+ },
252
+ {
253
+ bookId: "4",
254
+ name: "Effective Java",
255
+ author: "Cannot Remember"
256
+ },
257
+ book3Item
258
+ ]
259
+
260
+
261
+ expected = [
262
+ book3Item,
263
+ {
264
+ bookId: "1",
265
+ name: "Harry Potter and the Sorcerer's Stone",
266
+ author: "J.K. Rowling"
267
+ },
268
+ {
269
+ bookId: "2",
270
+ name: "Eragon",
271
+ author: "Christopher Paolini"
272
+ },
273
+ book3Item
274
+ ]
275
+
276
+ customMatcher=EqualWithOutOrderJson.new(expected)
277
+
278
+ expect(customMatcher.matches?(actual)).to eq(false)
279
+
280
+ expectedJson=expected.to_json;
281
+ actualJson=actual.to_json;
282
+
283
+ String expectedErrorMessage= "Expected: #{expectedJson}\n" +
284
+ makeGreen(" Actual: #{actualJson}") + "\n" +
285
+ "\nDiff:\n" +
286
+ "JSON path $.[] wrong number of:\n" +
287
+ "#{book3Item.to_json}\n" +
288
+ "in actual\n" +
289
+ "expected: 2\n" +
290
+ makeGreen(" got: 1") + "\n"
291
+
292
+ expect(customMatcher.failure_message).to eq(expectedErrorMessage)
293
+
294
+ expect(expected).not_to eq_json(actual)
295
+ end
296
+
297
+ it 'test expected has two of same elements and actual has three' do
298
+
299
+ book3Item =
300
+ {
301
+ bookId: "3",
302
+ name: "The Fellowship of the Ring",
303
+ author: "J.R.R. Tolkien"
304
+ }
305
+
306
+ actual = [
307
+ book3Item,
308
+ {
309
+ bookId: "4",
310
+ name: "Effective Java",
311
+ author: "Cannot Remember"
312
+ },
313
+ book3Item,
314
+ book3Item
315
+ ]
316
+
317
+ expected = [
318
+ book3Item,
319
+ {
320
+ bookId: "1",
321
+ name: "Harry Potter and the Sorcerer's Stone",
322
+ author: "J.K. Rowling"
323
+ },
324
+ {
325
+ bookId: "2",
326
+ name: "Eragon",
327
+ author: "Christopher Paolini"
328
+ },
329
+ book3Item
330
+ ]
331
+
332
+ customMatcher=EqualWithOutOrderJson.new(expected)
333
+
334
+ expect(customMatcher.matches?(actual)).to eq(false)
335
+
336
+ expectedJson=expected.to_json;
337
+ actualJson=actual.to_json;
338
+
339
+ String expectedErrorMessage= "Expected: #{expectedJson}\n" +
340
+ makeGreen(" Actual: #{actualJson}") + "\n" +
341
+ "\nDiff:\n" +
342
+ "JSON path $.[] wrong number of:\n" +
343
+ "#{book3Item.to_json}\n" +
344
+ "in actual\n" +
345
+ "expected: 2\n" +
346
+ makeGreen(" got: 3") + "\n"
347
+
348
+ expect(customMatcher.failure_message).to eq(expectedErrorMessage)
349
+
350
+ expect(expected).not_to eq_json(actual)
351
+ end
352
+
353
+ end