1
1
package diff
2
2
3
3
import (
4
+ "fmt"
4
5
"strings"
5
6
"testing"
6
7
)
@@ -28,14 +29,15 @@ func TestDiff(t *testing.T) {
28
29
{LHS : []int {1 , 2 , 3 }, RHS : []int {4 , 5 }, Want : ContentDiffer },
29
30
{LHS : []int {1 , 2 , 3 }, RHS : []float64 {4 , 5 }, Want : TypesDiffer },
30
31
{LHS : []int {1 , 2 , 3 }, RHS : []float64 {4 , 5 }, Want : TypesDiffer },
31
- {LHS : []func (){func () {}}, RHS : []func (){func () {}}, Want : TypesDiffer , Error : true },
32
+ {LHS : []func (){func () {}}, RHS : []func (){func () {}}, Want : ContentDiffer , Error : true },
32
33
{LHS : map [int ]int {2 : 4 , 6 : 12 }, RHS : map [int ]int {2 : 4 , 6 : 12 }, Want : Identical },
33
34
{LHS : map [int ]int {2 : 4 , 6 : 12 , 8 : 16 }, RHS : map [int ]int {2 : 4 , 6 : 12 }, Want : ContentDiffer },
34
35
{LHS : map [int ]int {2 : 4 , 6 : 12 }, RHS : map [int ]int {2 : 4 , 6 : 12 , 1 : 2 }, Want : ContentDiffer },
35
36
{LHS : map [int ]int {2 : 4 , 6 : 12 }, RHS : map [float64 ]int {2 : 4 , 6 : 12 }, Want : TypesDiffer },
36
37
{
37
38
LHS : map [int ]func (){0 : func () {}},
38
39
RHS : map [int ]func (){0 : func () {}},
40
+ Want : ContentDiffer ,
39
41
Error : true ,
40
42
},
41
43
{LHS : map [int ]int {2 : 4 , 6 : 12 }, RHS : map [int ]int {1 : 2 , 3 : 6 }, Want : ContentDiffer },
@@ -53,7 +55,7 @@ func TestDiff(t *testing.T) {
53
55
{LHS : []interface {}{1 , 2 , 3 }, RHS : []interface {}{1 , 2 , 3 }, Want : Identical },
54
56
{LHS : []interface {}{1 , 2 , 3 }, RHS : []interface {}{1 , 2 , 3.3 }, Want : ContentDiffer },
55
57
{LHS : []interface {}(nil ), RHS : []interface {}{1 , 2 , 3.3 }, Want : ContentDiffer },
56
- {LHS : []int (nil ), RHS : []int {}, Want : ContentDiffer },
58
+ {LHS : []int (nil ), RHS : []int {}, Want : Identical },
57
59
{LHS : func () {}, RHS : func () {}, Want : TypesDiffer , Error : true },
58
60
} {
59
61
diff , err := Diff (test .LHS , test .RHS )
@@ -68,7 +70,7 @@ func TestDiff(t *testing.T) {
68
70
if diff .Diff () != test .Want {
69
71
t .Logf ("LHS: %+#v\n " , test .LHS )
70
72
t .Logf ("LHS: %+#v\n " , test .RHS )
71
- t .Errorf ("Diff(%v, %v) = %q, expected %q" , test .LHS , test .RHS , diff .Diff (), test .Want )
73
+ t .Errorf ("Diff(%# v, %# v) = %q, expected %q" , test .LHS , test .RHS , diff .Diff (), test .Want )
72
74
}
73
75
}
74
76
}
@@ -124,7 +126,7 @@ func TestTypes(t *testing.T) {
124
126
125
127
ss := typ .Strings ()
126
128
indented := typ .StringIndent (testKey , testPrefix , testOutput )
127
- testStrings (t , test , ss , indented )
129
+ testStrings ("TestTypes" , t , test , ss , indented )
128
130
}
129
131
}
130
132
@@ -165,7 +167,7 @@ func TestScalar(t *testing.T) {
165
167
166
168
ss := typ .Strings ()
167
169
indented := typ .StringIndent (testKey , testPrefix , testOutput )
168
- testStrings (t , test , ss , indented )
170
+ testStrings ("TestScalar" , t , test , ss , indented )
169
171
}
170
172
}
171
173
@@ -233,11 +235,16 @@ func TestSlice(t *testing.T) {
233
235
234
236
ss := typ .Strings ()
235
237
indented := typ .StringIndent (testKey , testPrefix , testOutput )
236
- testStrings (t , test , ss , indented )
238
+ testStrings ("TestSlice" , t , test , ss , indented )
237
239
}
238
240
239
- invalid := & Slice {
240
- Type : Type (- 1 ),
241
+ invalid , err := NewSlice (nil , nil )
242
+ if invalidErr , ok := err .(InvalidType ); ok {
243
+ if ! strings .Contains (invalidErr .Error (), "nil" ) {
244
+ t .Errorf ("NewSlice(nil, nil): unexpected format for InvalidType error: got %s" , err )
245
+ }
246
+ } else {
247
+ t .Errorf ("NewSlice(nil, nil): expected InvalidType error, got %s" , err )
241
248
}
242
249
ss := invalid .Strings ()
243
250
if len (ss ) != 0 {
@@ -248,10 +255,28 @@ func TestSlice(t *testing.T) {
248
255
if indented != "" {
249
256
t .Errorf ("invalidSlice.StringIndent(%q, %q, %+v) = %q, expected %q" , testKey , testPrefix , testOutput , indented , "" )
250
257
}
258
+
259
+ invalid , err = NewSlice ([]int {}, nil )
260
+ if invalidErr , ok := err .(InvalidType ); ok {
261
+ if ! strings .Contains (invalidErr .Error (), "nil" ) {
262
+ t .Errorf ("NewSlice([]int{}, nil): unexpected format for InvalidType error: got %s" , err )
263
+ }
264
+ } else {
265
+ t .Errorf ("NewSlice([]int{}, nil): expected InvalidType error, got %s" , err )
266
+ }
267
+ ss = invalid .Strings ()
268
+ if len (ss ) != 0 {
269
+ t .Errorf ("len(invalidSlice.Strings()) = %d, expected 0" , len (ss ))
270
+ }
271
+
272
+ indented = invalid .StringIndent (testKey , testPrefix , testOutput )
273
+ if indented != "" {
274
+ t .Errorf ("invalidSlice.StringIndent(%q, %q, %+v) = %q, expected %q" , testKey , testPrefix , testOutput , indented , "" )
275
+ }
251
276
}
252
277
253
278
func TestMap (t * testing.T ) {
254
- for _ , test := range []stringTest {
279
+ for i , test := range []stringTest {
255
280
{
256
281
LHS : map [int ]int {1 : 2 , 3 : 4 },
257
282
RHS : map [int ]int {1 : 2 , 3 : 4 },
@@ -280,6 +305,18 @@ func TestMap(t *testing.T) {
280
305
},
281
306
Type : ContentDiffer ,
282
307
},
308
+ {
309
+ LHS : map [int ]int {1 : 2 , 2 : 3 },
310
+ RHS : map [int ]int {1 : 3 , 2 : 3 },
311
+ Want : [][]string {
312
+ []string {},
313
+ []string {"-" , "int" , "1" , "2" },
314
+ []string {"+" , "int" , "1" , "3" },
315
+ []string {"int" , "2" , "3" },
316
+ []string {},
317
+ },
318
+ Type : ContentDiffer ,
319
+ },
283
320
{
284
321
LHS : map [int ]int {1 : 2 },
285
322
RHS : map [int ]int {},
@@ -301,23 +338,28 @@ func TestMap(t *testing.T) {
301
338
Type : ContentDiffer ,
302
339
},
303
340
} {
304
- typ , err := NewMap (test .LHS , test .RHS )
341
+ m , err := NewMap (test .LHS , test .RHS )
305
342
306
343
if err != nil {
307
344
t .Errorf ("NewMap(%+v, %+v): unexpected error: %q" , test .LHS , test .RHS , err )
308
345
continue
309
346
}
310
- if typ .Diff () != test .Type {
311
- t .Errorf ("Types.Diff() = %q, expected %q" , typ .Diff (), test .Type )
347
+ if m .Diff () != test .Type {
348
+ t .Errorf ("Types.Diff() = %q, expected %q" , m .Diff (), test .Type )
312
349
}
313
350
314
- ss := typ .Strings ()
315
- indented := typ .StringIndent (testKey , testPrefix , testOutput )
316
- testStrings (t , test , ss , indented )
351
+ ss := m .Strings ()
352
+ indented := m .StringIndent (testKey , testPrefix , testOutput )
353
+ testStrings (fmt . Sprintf ( "TestMap[%d]" , i ), t , test , ss , indented )
317
354
}
318
355
319
- invalid := & Map {
320
- Type : Type (- 1 ),
356
+ invalid , err := NewMap (nil , nil )
357
+ if invalidErr , ok := err .(InvalidType ); ok {
358
+ if ! strings .Contains (invalidErr .Error (), "nil" ) {
359
+ t .Errorf ("NewMap(nil, nil): unexpected format for InvalidType error: got %s" , err )
360
+ }
361
+ } else {
362
+ t .Errorf ("NewMap(nil, nil): expected InvalidType error, got %s" , err )
321
363
}
322
364
ss := invalid .Strings ()
323
365
if len (ss ) != 0 {
@@ -328,20 +370,52 @@ func TestMap(t *testing.T) {
328
370
if indented != "" {
329
371
t .Errorf ("invalidMap.StringIndent(%q, %q, %+v) = %q, expected %q" , testKey , testPrefix , testOutput , indented , "" )
330
372
}
373
+
374
+ invalid , err = NewMap (map [int ]int {}, nil )
375
+ if invalidErr , ok := err .(InvalidType ); ok {
376
+ if ! strings .Contains (invalidErr .Error (), "nil" ) {
377
+ t .Errorf ("NewMap(map[int]int{}, nil): unexpected format for InvalidType error: got %s" , err )
378
+ }
379
+ } else {
380
+ t .Errorf ("NewMap(map[int]int{}, nil): expected InvalidType error, got %s" , err )
381
+ }
382
+ ss = invalid .Strings ()
383
+ if len (ss ) != 0 {
384
+ t .Errorf ("len(invalidMap.Strings()) = %d, expected 0" , len (ss ))
385
+ }
386
+
387
+ indented = invalid .StringIndent (testKey , testPrefix , testOutput )
388
+ if indented != "" {
389
+ t .Errorf ("invalidMap.StringIndent(%q, %q, %+v) = %q, expected %q" , testKey , testPrefix , testOutput , indented , "" )
390
+ }
391
+ }
392
+
393
+ func TestIgnore (t * testing.T ) {
394
+ ignore := Ignore {}
395
+
396
+ if ignore .Diff () != Identical {
397
+ t .Errorf ("Ignore{}.Diff() = %q, expected %q" , ignore .Diff (), Identical )
398
+ }
399
+ if len (ignore .Strings ()) != 0 {
400
+ t .Errorf ("len(Ignore{}.Strings()) = %d, expected 0" , len (ignore .Strings ()))
401
+ }
402
+ if indented := ignore .StringIndent (testKey , testPrefix , testOutput ); indented != "" {
403
+ t .Errorf ("Ignore{}.StringIndent(...) = %q, expected %q" , indented , "" )
404
+ }
331
405
}
332
406
333
- func testStrings (t * testing.T , test stringTest , ss []string , indented string ) {
407
+ func testStrings (context string , t * testing.T , test stringTest , ss []string , indented string ) {
334
408
for i , want := range test .Want {
335
409
s := ss [i ]
336
410
337
- for _ , needle := range want {
411
+ for i , needle := range want {
338
412
if ! strings .Contains (s , needle ) {
339
- t .Errorf ("typ.Strings() = %#v , expected it to contain %q" , ss , needle )
413
+ t .Errorf ("%s: typ.Strings()[%d] = %q , expected it to contain %q" , context , i , ss [ i ] , needle )
340
414
}
341
415
if ! strings .Contains (indented , needle ) {
342
416
t .Errorf (
343
- "typ.StringIndent(%q, %q, %+v) = %q, expected it to contain %q" ,
344
- testKey , testPrefix , testOutput , indented , needle ,
417
+ "%s: typ.StringIndent(%q, %q, %+v) = %q, expected it to contain %q" ,
418
+ context , testKey , testPrefix , testOutput , indented , needle ,
345
419
)
346
420
}
347
421
}
0 commit comments