@@ -69,12 +69,57 @@ func TestDiff(t *testing.T) {
69
69
70
70
if diff .Diff () != test .Want {
71
71
t .Logf ("LHS: %+#v\n " , test .LHS )
72
- t .Logf ("LHS : %+#v\n " , test .RHS )
72
+ t .Logf ("RHS : %+#v\n " , test .RHS )
73
73
t .Errorf ("Diff(%#v, %#v) = %q, expected %q" , test .LHS , test .RHS , diff .Diff (), test .Want )
74
74
}
75
75
}
76
76
}
77
77
78
+ func TestDiffMyers (t * testing.T ) {
79
+ for _ , test := range []struct {
80
+ LHS interface {}
81
+ RHS interface {}
82
+ Want Type
83
+ Error bool
84
+ }{
85
+ {LHS : []int {1 , 2 , 3 }, RHS : []int {1 , 2 , 3 }, Want : Identical },
86
+ {LHS : []int {1 , 2 , 3 , 4 }, RHS : []int {1 , 2 , 3 }, Want : ContentDiffer },
87
+ {LHS : []int {1 , 2 , 3 }, RHS : []int {1 , 2 , 3 , 4 }, Want : ContentDiffer },
88
+ {LHS : []int {1 , 2 , 3 }, RHS : []int {4 , 5 }, Want : ContentDiffer },
89
+ {LHS : []int {1 , 2 , 3 }, RHS : []float64 {4 , 5 }, Want : TypesDiffer },
90
+ {LHS : []int {1 , 2 , 3 }, RHS : []float64 {4 , 5 }, Want : TypesDiffer },
91
+ {LHS : []func (){func () {}}, RHS : []func (){func () {}}, Want : ContentDiffer , Error : true },
92
+ {
93
+ LHS : map [int ][]int {1 : {2 , 3 }, 2 : {3 , 4 }},
94
+ RHS : map [int ][]int {1 : {2 , 3 }, 2 : {3 , 4 }},
95
+ Want : Identical ,
96
+ },
97
+ {
98
+ LHS : map [int ][]int {1 : {2 , 3 }, 2 : {3 , 4 }},
99
+ RHS : map [int ][]int {1 : {2 , 3 }, 2 : {3 , 5 }},
100
+ Want : ContentDiffer ,
101
+ },
102
+ {LHS : []interface {}{1 , 2 , 3 }, RHS : []interface {}{1 , 2 , 3 }, Want : Identical },
103
+ {LHS : []interface {}{1 , 2 , 3 }, RHS : []interface {}{1 , 2 , 3.3 }, Want : ContentDiffer },
104
+ {LHS : []interface {}(nil ), RHS : []interface {}{1 , 2 , 3.3 }, Want : ContentDiffer },
105
+ {LHS : []int (nil ), RHS : []int {}, Want : Identical },
106
+ } {
107
+ diff , err := Diff (test .LHS , test .RHS , UseSliceMyers ())
108
+
109
+ if err == nil && test .Error {
110
+ t .Errorf ("Diff(%#v, %#v) expected an error, got nil instead" , test .LHS , test .RHS )
111
+ }
112
+ if err != nil && ! test .Error {
113
+ t .Errorf ("Diff(%#v, %#v): unexpected error: %q" , test .LHS , test .RHS , err )
114
+ }
115
+
116
+ if diff .Diff () != test .Want {
117
+ t .Logf ("LHS: %+#v\n " , test .LHS )
118
+ t .Logf ("RHS: %+#v\n " , test .RHS )
119
+ t .Errorf ("Diff(%#v, %#v) = %q, expected %q" , test .LHS , test .RHS , diff .Diff (), test .Want )
120
+ }
121
+ }
122
+ }
78
123
func TestTypeString (t * testing.T ) {
79
124
for _ , test := range []struct {
80
125
Input Type
@@ -223,7 +268,7 @@ func TestSlice(t *testing.T) {
223
268
Type : ContentDiffer ,
224
269
},
225
270
} {
226
- typ , err := newSlice (test .LHS , test .RHS , & visited {})
271
+ typ , err := newSlice (defaultConfig (), test .LHS , test .RHS , & visited {})
227
272
228
273
if err != nil {
229
274
t .Errorf ("NewSlice(%+v, %+v): unexpected error: %q" , test .LHS , test .RHS , err )
@@ -238,7 +283,7 @@ func TestSlice(t *testing.T) {
238
283
testStrings ("TestSlice" , t , test , ss , indented )
239
284
}
240
285
241
- invalid , err := newSlice (nil , nil , & visited {})
286
+ invalid , err := newSlice (defaultConfig (), nil , nil , & visited {})
242
287
if invalidErr , ok := err .(errInvalidType ); ok {
243
288
if ! strings .Contains (invalidErr .Error (), "nil" ) {
244
289
t .Errorf ("NewSlice(nil, nil): unexpected format for InvalidType error: got %s" , err )
@@ -256,7 +301,7 @@ func TestSlice(t *testing.T) {
256
301
t .Errorf ("invalidSlice.StringIndent(%q, %q, %+v) = %q, expected %q" , testKey , testPrefix , testOutput , indented , "" )
257
302
}
258
303
259
- invalid , err = newSlice ([]int {}, nil , & visited {})
304
+ invalid , err = newSlice (defaultConfig (), []int {}, nil , & visited {})
260
305
if invalidErr , ok := err .(errInvalidType ); ok {
261
306
if ! strings .Contains (invalidErr .Error (), "nil" ) {
262
307
t .Errorf ("NewSlice([]int{}, nil): unexpected format for InvalidType error: got %s" , err )
@@ -275,6 +320,113 @@ func TestSlice(t *testing.T) {
275
320
}
276
321
}
277
322
323
+ func TestSliceMyers (t * testing.T ) {
324
+ c := defaultConfig ()
325
+ c = UseSliceMyers ()(c )
326
+
327
+ for _ , test := range []stringTest {
328
+ {
329
+ LHS : []int {1 , 2 },
330
+ RHS : []int {1 , 2 },
331
+ Want : [][]string {
332
+ {"int" , "1" , "2" },
333
+ },
334
+ Type : Identical ,
335
+ },
336
+ {
337
+ LHS : []int {1 },
338
+ RHS : []int {},
339
+ Want : [][]string {
340
+ {},
341
+ {"-" , "int" , "1" },
342
+ {},
343
+ },
344
+ Type : ContentDiffer ,
345
+ },
346
+ {
347
+ LHS : []int {},
348
+ RHS : []int {2 },
349
+ Want : [][]string {
350
+ {},
351
+ {"+" , "int" , "2" },
352
+ {},
353
+ },
354
+ Type : ContentDiffer ,
355
+ },
356
+ {
357
+ LHS : []int {1 , 2 },
358
+ RHS : []float64 {1.1 , 2.1 },
359
+ Want : [][]string {
360
+ {"-" , "int" , "1" , "2" },
361
+ {"+" , "float64" , "1.1" , "2.1" },
362
+ },
363
+ Type : TypesDiffer ,
364
+ },
365
+ {
366
+ LHS : []int {1 , 3 },
367
+ RHS : []int {1 , 2 },
368
+ Want : [][]string {
369
+ {},
370
+ {"int" , "1" },
371
+ {"-" , "int" , "3" },
372
+ {"+" , "int" , "2" },
373
+ {},
374
+ },
375
+ Type : ContentDiffer ,
376
+ },
377
+ } {
378
+ typ , err := c .sliceFn (c , test .LHS , test .RHS , & visited {})
379
+
380
+ if err != nil {
381
+ t .Errorf ("NewMyersSlice(%+v, %+v): unexpected error: %q" , test .LHS , test .RHS , err )
382
+ continue
383
+ }
384
+ if typ .Diff () != test .Type {
385
+ t .Errorf ("Types.Diff() = %q, expected %q" , typ .Diff (), test .Type )
386
+ }
387
+
388
+ ss := typ .Strings ()
389
+ indented := typ .StringIndent (testKey , testPrefix , testOutput )
390
+ testStrings ("TestSlice" , t , test , ss , indented )
391
+ }
392
+
393
+ invalid , err := c .sliceFn (c , nil , nil , & visited {})
394
+ if invalidErr , ok := err .(errInvalidType ); ok {
395
+ if ! strings .Contains (invalidErr .Error (), "nil" ) {
396
+ t .Errorf ("NewMyersSlice(nil, nil): unexpected format for InvalidType error: got %s" , err )
397
+ }
398
+ } else {
399
+ t .Errorf ("NewMyersSlice(nil, nil): expected InvalidType error, got %s" , err )
400
+ }
401
+ ss := invalid .Strings ()
402
+ if len (ss ) != 0 {
403
+ t .Errorf ("len(invalidSlice.Strings()) = %d, expected 0" , len (ss ))
404
+ }
405
+
406
+ indented := invalid .StringIndent (testKey , testPrefix , testOutput )
407
+ if indented != "" {
408
+ t .Errorf ("invalidSlice.StringIndent(%q, %q, %+v) = %q, expected %q" , testKey , testPrefix , testOutput , indented , "" )
409
+ }
410
+
411
+ invalid , err = c .sliceFn (c , []int {}, nil , & visited {})
412
+ if invalidErr , ok := err .(errInvalidType ); ok {
413
+ if ! strings .Contains (invalidErr .Error (), "nil" ) {
414
+ t .Errorf ("NewMyersSlice([]int{}, nil): unexpected format for InvalidType error: got %s" , err )
415
+ }
416
+ } else {
417
+ t .Errorf ("NewMyersSlice([]int{}, nil): expected InvalidType error, got %s" , err )
418
+ }
419
+ ss = invalid .Strings ()
420
+ if len (ss ) != 0 {
421
+ t .Errorf ("len(invalidSlice.Strings()) = %d, expected 0" , len (ss ))
422
+ }
423
+
424
+ indented = invalid .StringIndent (testKey , testPrefix , testOutput )
425
+ if indented != "" {
426
+ t .Errorf ("invalidSlice.StringIndent(%q, %q, %+v) = %q, expected %q" , testKey , testPrefix , testOutput , indented , "" )
427
+ }
428
+ }
429
+
278
430
func TestMap (t * testing.T ) {
279
431
for i , test := range []stringTest {
280
432
{
@@ -338,7 +490,7 @@ func TestMap(t *testing.T) {
338
490
Type : ContentDiffer ,
339
491
},
340
492
} {
341
- m , err := newMap (test .LHS , test .RHS , & visited {})
493
+ m , err := newMap (defaultConfig (), test .LHS , test .RHS , & visited {})
342
494
343
495
if err != nil {
344
496
t .Errorf ("NewMap(%+v, %+v): unexpected error: %q" , test .LHS , test .RHS , err )
@@ -353,7 +505,7 @@ func TestMap(t *testing.T) {
353
505
testStrings (fmt .Sprintf ("TestMap[%d]" , i ), t , test , ss , indented )
354
506
}
355
507
356
- invalid , err := newMap (nil , nil , & visited {})
508
+ invalid , err := newMap (defaultConfig (), nil , nil , & visited {})
357
509
if invalidErr , ok := err .(errInvalidType ); ok {
358
510
if ! strings .Contains (invalidErr .Error (), "nil" ) {
359
511
t .Errorf ("NewMap(nil, nil): unexpected format for InvalidType error: got %s" , err )
@@ -371,7 +523,7 @@ func TestMap(t *testing.T) {
371
523
t .Errorf ("invalidMap.StringIndent(%q, %q, %+v) = %q, expected %q" , testKey , testPrefix , testOutput , indented , "" )
372
524
}
373
525
374
- invalid , err = newMap (map [int ]int {}, nil , & visited {})
526
+ invalid , err = newMap (defaultConfig (), map [int ]int {}, nil , & visited {})
375
527
if invalidErr , ok := err .(errInvalidType ); ok {
376
528
if ! strings .Contains (invalidErr .Error (), "nil" ) {
377
529
t .Errorf ("NewMap(map[int]int{}, nil): unexpected format for InvalidType error: got %s" , err )
0 commit comments