LEFT | RIGHT |
1 // Copied with small adaptations from the reflect package in the | 1 // Copied with small adaptations from the reflect package in the |
2 // Go source tree. We use testing rather than gocheck to preserve | 2 // Go source tree. We use testing rather than gocheck to preserve |
3 // as much source equivalence as possible. | 3 // as much source equivalence as possible. |
4 | 4 |
5 // TODO tests for error messages | 5 // TODO tests for error messages |
6 | 6 |
7 // Copyright 2009 The Go Authors. All rights reserved. | 7 // Copyright 2009 The Go Authors. All rights reserved. |
8 // Use of this source code is governed by a BSD-style | 8 // Use of this source code is governed by a BSD-style |
9 // license that can be found in the LICENSE file. | 9 // license that can be found in the LICENSE file. |
10 | 10 |
11 package checkers_test | 11 package checkers_test |
12 | 12 |
13 import ( | 13 import ( |
14 "launchpad.net/juju-core/testing/checkers" | 14 "launchpad.net/juju-core/testing/checkers" |
| 15 "regexp" |
15 "testing" | 16 "testing" |
16 ) | 17 ) |
17 | 18 |
18 func deepEqual(a1, a2 interface{}) bool { | 19 func deepEqual(a1, a2 interface{}) bool { |
19 ok, _ := checkers.DeepEqual(a1, a2) | 20 ok, _ := checkers.DeepEqual(a1, a2) |
20 return ok | 21 return ok |
21 } | 22 } |
22 | 23 |
23 type Basic struct { | 24 type Basic struct { |
24 x int | 25 x int |
25 y float32 | 26 y float32 |
26 } | 27 } |
27 | 28 |
28 type NotBasic Basic | 29 type NotBasic Basic |
29 | 30 |
30 type DeepEqualTest struct { | 31 type DeepEqualTest struct { |
31 a, b interface{} | 32 a, b interface{} |
32 eq bool | 33 eq bool |
| 34 msg string |
33 } | 35 } |
34 | 36 |
35 // Simple functions for DeepEqual tests. | 37 // Simple functions for DeepEqual tests. |
36 var ( | 38 var ( |
37 fn1 func() // nil. | 39 fn1 func() // nil. |
38 fn2 func() // nil. | 40 fn2 func() // nil. |
39 fn3 = func() { fn1() } // Not nil. | 41 fn3 = func() { fn1() } // Not nil. |
40 ) | 42 ) |
41 | 43 |
42 var deepEqualTests = []DeepEqualTest{ | 44 var deepEqualTests = []DeepEqualTest{ |
43 // Equalities | 45 // Equalities |
44 » {nil, nil, true}, | 46 » {nil, nil, true, ""}, |
45 » {1, 1, true}, | 47 » {1, 1, true, ""}, |
46 » {int32(1), int32(1), true}, | 48 » {int32(1), int32(1), true, ""}, |
47 » {0.5, 0.5, true}, | 49 » {0.5, 0.5, true, ""}, |
48 » {float32(0.5), float32(0.5), true}, | 50 » {float32(0.5), float32(0.5), true, ""}, |
49 » {"hello", "hello", true}, | 51 » {"hello", "hello", true, ""}, |
50 » {make([]int, 10), make([]int, 10), true}, | 52 » {make([]int, 10), make([]int, 10), true, ""}, |
51 » {&[3]int{1, 2, 3}, &[3]int{1, 2, 3}, true}, | 53 » {&[3]int{1, 2, 3}, &[3]int{1, 2, 3}, true, ""}, |
52 » {Basic{1, 0.5}, Basic{1, 0.5}, true}, | 54 » {Basic{1, 0.5}, Basic{1, 0.5}, true, ""}, |
53 » {error(nil), error(nil), true}, | 55 » {error(nil), error(nil), true, ""}, |
54 » {map[int]string{1: "one", 2: "two"}, map[int]string{2: "two", 1: "one"},
true}, | 56 » {map[int]string{1: "one", 2: "two"}, map[int]string{2: "two", 1: "one"},
true, ""}, |
55 » {fn1, fn2, true}, | 57 » {fn1, fn2, true, ""}, |
56 | 58 |
57 // Inequalities | 59 // Inequalities |
58 » {1, 2, false}, | 60 » {1, 2, false, `mismatch at top level: unequal; obtained 1; expected 2`}, |
59 » {int32(1), int32(2), false}, | 61 » {int32(1), int32(2), false, `mismatch at top level: unequal; obtained 1;
expected 2`}, |
60 » {0.5, 0.6, false}, | 62 » {0.5, 0.6, false, `mismatch at top level: unequal; obtained 0\.5; expect
ed 0\.6`}, |
61 » {float32(0.5), float32(0.6), false}, | 63 » {float32(0.5), float32(0.6), false, `mismatch at top level: unequal; obt
ained 0\.5; expected 0\.6`}, |
62 » {"hello", "hey", false}, | 64 » {"hello", "hey", false, `mismatch at top level: unequal; obtained "hello
"; expected "hey"`}, |
63 » {make([]int, 10), make([]int, 11), false}, | 65 » {make([]int, 10), make([]int, 11), false, `mismatch at top level: length
mismatch, 10 vs 11; obtained \[\]int\{0, 0, 0, 0, 0, 0, 0, 0, 0, 0\}; expected
\[\]int\{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0\}`}, |
64 » {&[3]int{1, 2, 3}, &[3]int{1, 2, 4}, false}, | 66 » {&[3]int{1, 2, 3}, &[3]int{1, 2, 4}, false, `mismatch at \(\*\)\[2\]: un
equal; obtained 3; expected 4`}, |
65 » {Basic{1, 0.5}, Basic{1, 0.6}, false}, | 67 » {Basic{1, 0.5}, Basic{1, 0.6}, false, `mismatch at \.y: unequal; obtaine
d 0\.5; expected 0\.6`}, |
66 » {Basic{1, 0}, Basic{2, 0}, false}, | 68 » {Basic{1, 0}, Basic{2, 0}, false, `mismatch at \.x: unequal; obtained 1;
expected 2`}, |
67 » {map[int]string{1: "one", 3: "two"}, map[int]string{2: "two", 1: "one"},
false}, | 69 » {map[int]string{1: "one", 3: "two"}, map[int]string{2: "two", 1: "one"},
false, `mismatch at \[3\]: validity mismatch; obtained "two"; expected <nil>`}, |
68 » {map[int]string{1: "one", 2: "txo"}, map[int]string{2: "two", 1: "one"},
false}, | 70 » {map[int]string{1: "one", 2: "txo"}, map[int]string{2: "two", 1: "one"},
false, `mismatch at \[2\]: unequal; obtained "txo"; expected "two"`}, |
69 » {map[int]string{1: "one"}, map[int]string{2: "two", 1: "one"}, false}, | 71 » {map[int]string{1: "one"}, map[int]string{2: "two", 1: "one"}, false, `m
ismatch at top level: length mismatch, 1 vs 2; obtained map\[int\]string\{1:"one
"\}; expected map\[int\]string\{2:"two", 1:"one"\}`}, |
70 » {map[int]string{2: "two", 1: "one"}, map[int]string{1: "one"}, false}, | 72 » {map[int]string{2: "two", 1: "one"}, map[int]string{1: "one"}, false, `m
ismatch at top level: length mismatch, 2 vs 1; obtained map\[int\]string\{2:"two
", 1:"one"\}; expected map\[int\]string\{1:"one"\}`}, |
71 » {nil, 1, false}, | 73 » {nil, 1, false, `mismatch at top level: nil vs non-nil mismatch; obtaine
d <nil>; expected 1`}, |
72 » {1, nil, false}, | 74 » {1, nil, false, `mismatch at top level: nil vs non-nil mismatch; obtaine
d 1; expected <nil>`}, |
73 » {fn1, fn3, false}, | 75 » {fn1, fn3, false, `mismatch at top level: non-nil functions; obtained \(
func\(\)\)\(nil\); expected \(func\(\)\)\(0x[0-9a-f]+\)`}, |
74 » {fn3, fn3, false}, | 76 » {fn3, fn3, false, `mismatch at top level: non-nil functions; obtained \(
func\(\)\)\(0x[0-9a-f]+\); expected \(func\(\)\)\(0x[0-9a-f]+\)`}, |
75 | 77 |
76 // Nil vs empty: they're the same (difference from normal DeepEqual) | 78 // Nil vs empty: they're the same (difference from normal DeepEqual) |
77 » {[]int{}, []int(nil), true}, | 79 » {[]int{}, []int(nil), true, ""}, |
78 » {[]int{}, []int{}, true}, | 80 » {[]int{}, []int{}, true, ""}, |
79 » {[]int(nil), []int(nil), true}, | 81 » {[]int(nil), []int(nil), true, ""}, |
80 | 82 |
81 // Mismatched types | 83 // Mismatched types |
82 » {1, 1.0, false}, | 84 » {1, 1.0, false, `mismatch at top level: type mismatch int vs float64; ob
tained 1; expected 1`}, |
83 » {int32(1), int64(1), false}, | 85 » {int32(1), int64(1), false, `mismatch at top level: type mismatch int32
vs int64; obtained 1; expected 1`}, |
84 » {0.5, "hello", false}, | 86 » {0.5, "hello", false, `mismatch at top level: type mismatch float64 vs s
tring; obtained 0\.5; expected "hello"`}, |
85 » {[]int{1, 2, 3}, [3]int{1, 2, 3}, false}, | 87 » {[]int{1, 2, 3}, [3]int{1, 2, 3}, false, `mismatch at top level: type mi
smatch \[\]int vs \[3\]int; obtained \[\]int\{1, 2, 3\}; expected \[3\]int\{1, 2
, 3\}`}, |
86 » {&[3]interface{}{1, 2, 4}, &[3]interface{}{1, 2, "s"}, false}, | 88 » {&[3]interface{}{1, 2, 4}, &[3]interface{}{1, 2, "s"}, false, `mismatch
at \(\*\)\[2\]: type mismatch int vs string; obtained 4; expected "s"`}, |
87 » {Basic{1, 0.5}, NotBasic{1, 0.5}, false}, | 89 » {Basic{1, 0.5}, NotBasic{1, 0.5}, false, `mismatch at top level: type mi
smatch checkers_test\.Basic vs checkers_test\.NotBasic; obtained checkers_test\.
Basic\{x:1, y:0\.5\}; expected checkers_test\.NotBasic\{x:1, y:0\.5\}`}, |
88 » {map[uint]string{1: "one", 2: "two"}, map[int]string{2: "two", 1: "one"}
, false}, | 90 » {map[uint]string{1: "one", 2: "two"}, map[int]string{2: "two", 1: "one"}
, false, `mismatch at top level: type mismatch map\[uint\]string vs map\[int\]st
ring; obtained map\[uint\]string\{0x1:"one", 0x2:"two"\}; expected map\[int\]str
ing\{2:"two", 1:"one"\}`}, |
89 } | 91 } |
90 | 92 |
91 func TestDeepEqual(t *testing.T) { | 93 func TestDeepEqual(t *testing.T) { |
92 » for _, test := range deepEqualTests { | 94 » for i, test := range deepEqualTests { |
93 r, err := checkers.DeepEqual(test.a, test.b) | 95 r, err := checkers.DeepEqual(test.a, test.b) |
94 if r != test.eq { | 96 if r != test.eq { |
95 t.Errorf("deepEqual(%v, %v) = %v, want %v", test.a, test
.b, r, test.eq) | 97 t.Errorf("deepEqual(%v, %v) = %v, want %v", test.a, test
.b, r, test.eq) |
96 } | 98 } |
97 » » if err != nil { | 99 » » if test.eq { |
98 » » » err.Error() | 100 » » » if err != nil { |
| 101 » » » » t.Errorf("deepEqual(%v, %v): unexpected error me
ssage %q when equal", test.a, test.b, i, err) |
| 102 » » » } |
| 103 » » } else { |
| 104 » » » if ok, _ := regexp.MatchString(test.msg, err.Error()); !
ok { |
| 105 » » » » t.Errorf("deepEqual(%v, %v); unexpected error %q
, want %q", test.a, test.b, err.Error(), test.msg) |
| 106 » » » } |
99 } | 107 } |
100 } | 108 } |
101 } | 109 } |
102 | 110 |
103 type Recursive struct { | 111 type Recursive struct { |
104 x int | 112 x int |
105 r *Recursive | 113 r *Recursive |
106 } | 114 } |
107 | 115 |
108 func TestDeepEqualRecursiveStruct(t *testing.T) { | 116 func TestDeepEqualRecursiveStruct(t *testing.T) { |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
153 x2 := UnexpT{map[int]int{1: 2}} | 161 x2 := UnexpT{map[int]int{1: 2}} |
154 if !deepEqual(&x1, &x2) { | 162 if !deepEqual(&x1, &x2) { |
155 t.Error("deepEqual(x1, x2) = false, want true") | 163 t.Error("deepEqual(x1, x2) = false, want true") |
156 } | 164 } |
157 | 165 |
158 y1 := UnexpT{map[int]int{2: 3}} | 166 y1 := UnexpT{map[int]int{2: 3}} |
159 if deepEqual(&x1, &y1) { | 167 if deepEqual(&x1, &y1) { |
160 t.Error("deepEqual(x1, y1) = true, want false") | 168 t.Error("deepEqual(x1, y1) = true, want false") |
161 } | 169 } |
162 } | 170 } |
LEFT | RIGHT |