-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtest.js
235 lines (207 loc) · 5.28 KB
/
test.js
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
var tap = require('tap');
var diff = require('./');
var test = tap.test;
test('null no diff', function(t) {
t.same(diff(null, null, {color: false}), {
changed: false,
text: ''
});
t.end();
});
test('null and object', function(t) {
t.same(diff(null, { a: 1 }, {color: false}), {
changed: true,
text: 'null => {"a":1}'
});
t.end();
});
test('null and string', function(t) {
t.same(diff(null, 'b', {color: false}), {
changed: true,
text: 'null => "b"'
});
t.end();
});
test('undefined no diff', function(t) {
t.same(diff(undefined, undefined, {color: false}), {
changed: false,
text: ''
});
t.end();
});
test('undefined and number', function(t) {
t.same(diff(undefined, 21, {color: false}), {
changed: true,
text: 'undefined => 21'
});
t.end();
});
test('string no diff', function(t) {
t.same(diff('hello', 'hello', {color: false}), {
changed: false,
text: ''
});
t.end();
});
test('string changed', function(t) {
t.same(diff('hello', 'hellos', {color: false}), {
changed: true,
text: '"hello" => "hellos"'
});
t.end();
});
test('string to number', function(t) {
t.same(diff('fun', 9, {color: false}), {
changed: true,
text: '"fun" => 9'
});
t.end();
});
test('string to function', function(t) {
t.same(diff('fun', function hello() { console.log('test') }, {color: false}), {
changed: true,
text: '"fun" => function hello() {}'
});
t.end();
});
test('string to regex', function(t) {
t.same(diff('fun', /search/, {color: false}), {
changed: true,
text: '"fun" => /search/'
});
t.end();
});
test('number not changed', function(t) {
t.same(diff(987, 987, {color: false}), {
changed: false,
text: ''
});
t.end();
});
test('number and string', function(t) {
t.same(diff(1, 'hellos', {color: false}), {
changed: true,
text: '1 => "hellos"'
});
t.end();
});
test('number and object', function(t) {
t.same(diff(9, { test: 1 }, {color: false}), {
changed: true,
text: '9 => {"test":1}'
});
t.end();
});
test('function not changed', function(t) {
function myFunc() {}
t.same(diff(myFunc, myFunc, {color: false}), {
changed: false,
text: ''
});
t.end();
});
test('function same code different function', function(t) {
t.same(diff(function test() { console.log('1'); } , function test() { console.log('1'); }, {color: false}), {
changed: true,
text: 'function test() {} => function test() {}'
});
t.end();
});
test('function and number', function(t) {
t.same(diff(function () { console.log('1'); } , 2, {color: false}), {
changed: true,
text: 'function () {} => 2'
});
t.end();
});
test('array switch index', function(t) {
t.same(diff([9,8,1], [8,9,1], {color: false}), {
changed: true,
text: '[\n 0: 9 => 8\n 1: 8 => 9\n]'
});
t.end();
});
test('array add 2 items', function(t) {
t.same(diff([8,9,1], [8,9,1,4,3], {color: false}), {
changed: true,
text: '[\n+ 3: 4\n+ 4: 3\n]'
});
t.end();
});
test('array remove 2 items', function(t) {
t.same(diff([9,8,7], [9], {color: false}), {
changed: true,
text: '[\n- 1: 8\n- 2: 7\n]'
});
t.end();
});
test('array empty', function(t) {
t.same(diff([], [], {color: false}), {
changed: false,
text: ''
});
t.end();
});
test('array and object', function(t) {
t.same(diff([1,2], { key: 1 }, {color: false}), {
changed: true,
text: '[1,2] => {"key":1}'
});
t.end();
});
test('array and null', function(t) {
t.same(diff([2], null, {color: false}), {
changed: true,
text: '[2] => null'
});
t.end();
});
test('object same structure', function(t) {
t.same(diff({ test: 1, and: 'fun'}, { test: 1, and: 'fun'}, {color: false}), {
changed: false,
text: ''
});
t.end();
});
test('object nested object', function(t) {
t.same(diff({ test: { a: { b: 3, c: 5}}, and: 'fun'}, { test: { a: { b: 3, c: 5}}, and: 'fun'}, {color: false}), {
changed: false,
text: ''
});
t.end();
});
test('object nested object different', function(t) {
t.same(diff({ test: { a: { b: 3, c: 5}}, and: 'fun'}, { test: { a: { b: 3, c: 'test'}}, and: 'fun'}, {color: false}), {
changed: true,
text: '{\n test: {\n a: {\n c: 5 => "test"\n }\n }\n}'
});
t.end();
});
test('object keys removed', function(t) {
t.same(diff({ a: 1, b: 2, c: 3}, { a: 1 }, {color: false}), {
changed: true,
text: '{\n- b: 2\n- c: 3\n}'
});
t.end();
});
test('object keys added', function(t) {
t.same(diff({ a: 1 }, { a: 1, b: 9, c: 2 }, {color: false}), {
changed: true,
text: '{\n+ b: 9\n+ c: 2\n}'
});
t.end();
});
test('object with nested array', function(t) {
t.same(diff({ test: { a: [5,6,7]}, and: 'fun'}, { test: { a: [9,8,2,4]}, and: 'fun'}, {color: false}), {
changed: true,
text: '{\n test: {\n a: [\n 0: 5 => 9\n 1: 6 => 8\n 2: 7 => 2\n + 3: 4\n ]\n }\n}'
});
t.end();
});
test('complex nested object difference', function(t) {
t.same(diff({ test: { a: [5,6,7]}, b: { c: 1 }, and: 'fun'}, { test: { a: [9,8,2,4]}, b: {d: 2 }, and: [1,2]}, {color: false}), {
changed: true,
text: '{\n and: "fun" => [1,2]\n b: {\n - c: 1\n + d: 2\n }\n test: {\n a: [\n 0: 5 => 9\n 1: 6 => 8\n 2: 7 => 2\n + 3: 4\n ]\n }\n}'
});
t.end();
});