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,231 @@
1
+ require 'eq_json'
2
+ require 'json'
3
+ require 'spec_helper'
4
+
5
+ describe 'test objects not same type' do
6
+ it 'expected JSON array actual JSON object' do
7
+ actual = {
8
+ name: 'Harry Potter and the Sorcerer\'s Stone',
9
+ author: 'J.K. Rowling'
10
+ }
11
+
12
+ expected = [
13
+ {name: 'Harry Potter and the Sorcerer\'s Stone'},
14
+ {author: 'J.K. Rowling'}
15
+ ]
16
+
17
+ expectedJson=expected.to_json
18
+
19
+ actualJson=actual.to_json
20
+
21
+ customMatcher=EqualWithOutOrderJson.new(expected)
22
+
23
+ expect(customMatcher.matches?(actual)).to eq(false)
24
+
25
+ String expectedErrorMessage= "Expected: #{expectedJson}\n" +
26
+ makeGreen(" Actual: #{actualJson}") + "\n" +
27
+ "Diff:\n" +
28
+ "JSON path $. expected array type but actual is object\n"
29
+
30
+ expect(customMatcher.failure_message).to eq(expectedErrorMessage)
31
+
32
+ expect(actual).not_to eq_json(expected)
33
+ end
34
+
35
+ end
36
+
37
+ describe 'test single level json objects' do
38
+
39
+ it 'test that objects equal in order' do
40
+ actual = {
41
+ name: 'Harry Potter and the Sorcerer\'s Stone',
42
+ author: 'J.K. Rowling'
43
+ }
44
+
45
+ expected = {
46
+ name: 'Harry Potter and the Sorcerer\'s Stone',
47
+ author: 'J.K. Rowling'
48
+ }
49
+
50
+ expect(actual).to eq_json(expected)
51
+ end
52
+
53
+ it 'test that objects equal out of order' do
54
+ actual = {
55
+ name: 'Harry Potter and the Sorcerer\'s Stone',
56
+ author: 'J.K. Rowling'
57
+ }
58
+
59
+ expected = {
60
+ author: 'J.K. Rowling',
61
+ name: 'Harry Potter and the Sorcerer\'s Stone'
62
+ }
63
+
64
+ expect(actual).to eq_json(expected)
65
+ end
66
+
67
+ it 'actual missing object' do
68
+
69
+ actual = {
70
+ name: 'Harry Potter and the Sorcerer\'s Stone'
71
+ }
72
+
73
+ expected = {
74
+ name: 'Harry Potter and the Sorcerer\'s Stone',
75
+ author: 'J.K. Rowling'
76
+ }
77
+
78
+ customMatcher=EqualWithOutOrderJson.new(expected)
79
+
80
+ expect(customMatcher.matches?(actual)).to eq(false)
81
+
82
+ expectedJson=expected.to_json;
83
+ actualJson=actual.to_json;
84
+
85
+ String expectedErrorMessage= "Expected: #{expectedJson}\n" +
86
+ makeGreen(" Actual: #{actualJson}") + "\n" +
87
+ "\nDiff:\n" +
88
+ "JSON path $.\n" +
89
+ makeGreen("actual does not contain {\"author\":\"J.K. Rowling\"}\n") +
90
+ wrapWithResetColor("\n") + makeBlue("@@ -1,2 +1,3 @@\n") +
91
+ makeGreen("+:author => \"J.K. Rowling\",\n") +
92
+ wrapWithResetColor(" :name => \"Harry Potter and the Sorcerer's Stone\",\n")
93
+
94
+ expect(customMatcher.failure_message).to eq(expectedErrorMessage)
95
+
96
+ expect(actual).not_to eq_json(expected)
97
+ end
98
+
99
+ it 'expected missing object' do
100
+
101
+ actual = {
102
+ name: 'Harry Potter and the Sorcerer\'s Stone',
103
+ author: 'J.K. Rowling'
104
+ }
105
+
106
+ expected = {
107
+ name: 'Harry Potter and the Sorcerer\'s Stone',
108
+ }
109
+
110
+ customMatcher=EqualWithOutOrderJson.new(expected)
111
+
112
+ expect(customMatcher.matches?(actual)).to eq(false)
113
+
114
+ expectedJson=expected.to_json;
115
+ actualJson=actual.to_json;
116
+
117
+ String expectedErrorMessage= "Expected: #{expectedJson}\n" +
118
+ makeGreen(" Actual: #{actualJson}") + "\n" +
119
+ "\nDiff:\n" +
120
+ "JSON path $.\n" +
121
+ "expected does not contain {\"author\":\"J.K. Rowling\"}\n" +
122
+ wrapWithResetColor("\n") + makeBlue("@@ -1,3 +1,2 @@\n") +
123
+ makeRed("-:author => \"J.K. Rowling\",\n") +
124
+ wrapWithResetColor(" :name => \"Harry Potter and the Sorcerer's Stone\",\n")
125
+
126
+ expect(customMatcher.failure_message).to eq(expectedErrorMessage)
127
+
128
+ expect(actual).not_to eq_json(expected)
129
+ end
130
+
131
+ it 'expected missing mutiple objects' do
132
+
133
+ actual = {
134
+ name: 'Harry Potter and the Sorcerer\'s Stone',
135
+ author: 'J.K. Rowling',
136
+ isbn: 439708184
137
+ }
138
+
139
+ expected = {
140
+ name: 'Harry Potter and the Sorcerer\'s Stone',
141
+ }
142
+
143
+ customMatcher=EqualWithOutOrderJson.new(expected)
144
+
145
+ expect(customMatcher.matches?(actual)).to eq(false)
146
+
147
+ expectedJson=expected.to_json;
148
+ actualJson=actual.to_json;
149
+
150
+ String expectedErrorMessage= "Expected: #{expectedJson}\n" +
151
+ makeGreen(" Actual: #{actualJson}") + "\n" +
152
+ "\nDiff:\n" +
153
+ "JSON path $.\n" +
154
+ "expected does not contain {\"author\":\"J.K. Rowling\",\"isbn\":439708184}\n" +
155
+ wrapWithResetColor("\n") + makeBlue("@@ -1,4 +1,2 @@\n") +
156
+ makeRed("-:author => \"J.K. Rowling\",\n") +
157
+ makeRed("-:isbn => 439708184,\n") +
158
+ wrapWithResetColor(" :name => \"Harry Potter and the Sorcerer's Stone\",\n")
159
+
160
+ expect(customMatcher.failure_message).to eq(expectedErrorMessage)
161
+
162
+ expect(actual).not_to eq_json(expected)
163
+ end
164
+
165
+ it 'expected and actual both have missing objects' do
166
+
167
+ actual = {
168
+ name: 'Harry Potter and the Sorcerer\'s Stone',
169
+ author: 'J.K. Rowling'
170
+ }
171
+
172
+ expected = {
173
+ name: 'Harry Potter and the Sorcerer\'s Stone',
174
+ publisher: 'ACME Publisher Inc.'
175
+ }
176
+
177
+
178
+ customMatcher=EqualWithOutOrderJson.new(expected)
179
+
180
+ expect(customMatcher.matches?(actual)).to eq(false)
181
+
182
+ expectedJson=expected.to_json;
183
+ actualJson=actual.to_json;
184
+
185
+ String expectedErrorMessage= "Expected: #{expectedJson}\n" +
186
+ makeGreen(" Actual: #{actualJson}") + "\n" +
187
+ "\nDiff:\n" +
188
+ "JSON path $.\n" +
189
+ "expected does not contain {\"author\":\"J.K. Rowling\"}\n" +
190
+ makeGreen("actual does not contain {\"publisher\":\"ACME Publisher Inc.\"}\n") + wrapWithResetColor("\n") +
191
+ wrapWithResetColor("\n") + makeBlue("@@ -1,3 +1,3 @@\n") +
192
+ makeRed("-:author => \"J.K. Rowling\",\n") +
193
+ wrapWithResetColor(" :name => \"Harry Potter and the Sorcerer's Stone\",\n") +
194
+ makeGreen("+:publisher => \"ACME Publisher Inc.\",\n")
195
+
196
+ expect(customMatcher.failure_message).to eq(expectedErrorMessage)
197
+
198
+ expect(actual).not_to eq_json(expected)
199
+ end
200
+
201
+ it 'expected and actual have different values for key' do
202
+
203
+ actual = {
204
+ name: 'Harry Potter and the Chamber of Secrets',
205
+ author: 'J.K. Rowling'
206
+ }
207
+
208
+ expected = {
209
+ name: 'Harry Potter and the Sorcerer\'s Stone',
210
+ author: 'J.K. Rowling'
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
+ "Diff:\n" +
223
+ "JSON path $.name\n" +
224
+ "\texpected: \"Harry Potter and the Sorcerer\'s Stone\"\n" +
225
+ makeGreen("\t got: \"Harry Potter and the Chamber of Secrets\"")
226
+
227
+ expect(customMatcher.failure_message).to eq(expectedErrorMessage)
228
+
229
+ expect(actual).not_to eq_json(expected)
230
+ end
231
+ end