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
|
package messagediff
import (
"testing"
"time"
"github.com/d4l3k/messagediff/testdata"
)
type testStruct struct {
A, b int
C []int
D [3]int
}
type RecursiveStruct struct {
Key int
Child *RecursiveStruct
}
func newRecursiveStruct(key int) *RecursiveStruct {
a := &RecursiveStruct{
Key: key,
}
b := &RecursiveStruct{
Key: key,
Child: a,
}
a.Child = b
return a
}
type testCase struct {
a, b interface{}
diff string
equal bool
}
func checkTestCases(t *testing.T, testData []testCase) {
for i, td := range testData {
diff, equal := PrettyDiff(td.a, td.b)
if diff != td.diff {
t.Errorf("%d. PrettyDiff(%#v, %#v) diff = %#v; not %#v", i, td.a, td.b, diff, td.diff)
}
if equal != td.equal {
t.Errorf("%d. PrettyDiff(%#v, %#v) equal = %#v; not %#v", i, td.a, td.b, equal, td.equal)
}
}
}
func TestPrettyDiff(t *testing.T) {
testData := []testCase{
{
true,
false,
"modified: = false\n",
false,
},
{
true,
0,
"modified: = 0\n",
false,
},
{
[]int{0, 1, 2},
[]int{0, 1, 2, 3},
"added: [3] = 3\n",
false,
},
{
[]int{0, 1, 2, 3},
[]int{0, 1, 2},
"removed: [3] = 3\n",
false,
},
{
[]int{0},
[]int{1},
"modified: [0] = 1\n",
false,
},
{
&[]int{0},
&[]int{1},
"modified: [0] = 1\n",
false,
},
{
map[string]int{"a": 1, "b": 2},
map[string]int{"b": 4, "c": 3},
"added: [\"c\"] = 3\nmodified: [\"b\"] = 4\nremoved: [\"a\"] = 1\n",
false,
},
{
testStruct{1, 2, []int{1}, [3]int{4, 5, 6}},
testStruct{1, 3, []int{1, 2}, [3]int{4, 5, 6}},
"added: .C[1] = 2\nmodified: .b = 3\n",
false,
},
{
nil,
nil,
"",
true,
},
{
&struct{}{},
nil,
"modified: = <nil>\n",
false,
},
{
nil,
&struct{}{},
"modified: = &struct {}{}\n",
false,
},
{
time.Time{},
time.Time{},
"",
true,
},
{
testdata.MakeTest(10, "duck"),
testdata.MakeTest(20, "foo"),
"modified: .a = 20\nmodified: .b = \"foo\"\n",
false,
},
}
checkTestCases(t, testData)
}
func TestPrettyDiffRecursive(t *testing.T) {
testData := []testCase{
{
newRecursiveStruct(1),
newRecursiveStruct(1),
"",
true,
},
{
newRecursiveStruct(1),
newRecursiveStruct(2),
"modified: .Child.Key = 2\nmodified: .Key = 2\n",
false,
},
}
checkTestCases(t, testData)
}
func TestPathString(t *testing.T) {
testData := []struct {
in Path
want string
}{{
Path{StructField("test"), SliceIndex(1), MapKey{"blue"}, MapKey{12.3}},
".test[1][\"blue\"][12.3]",
}}
for i, td := range testData {
if out := td.in.String(); out != td.want {
t.Errorf("%d. %#v.String() = %#v; not %#v", i, td.in, out, td.want)
}
}
}
type ignoreStruct struct {
A int `testdiff:"ignore"`
a int
B [3]int `testdiff:"ignore"`
b [3]int
}
func TestIgnoreTag(t *testing.T) {
s1 := ignoreStruct{1, 1, [3]int{1, 2, 3}, [3]int{4, 5, 6}}
s2 := ignoreStruct{2, 1, [3]int{1, 8, 3}, [3]int{4, 5, 6}}
diff, equal := PrettyDiff(s1, s2)
if !equal {
t.Errorf("Expected structs to be equal. Diff:\n%s", diff)
}
s2 = ignoreStruct{2, 2, [3]int{1, 8, 3}, [3]int{4, 9, 6}}
diff, equal = PrettyDiff(s1, s2)
if equal {
t.Errorf("Expected structs NOT to be equal.")
}
expect := "modified: .a = 2\nmodified: .b[1] = 9\n"
if diff != expect {
t.Errorf("Expected diff to be:\n%v\nbut got:\n%v", expect, diff)
}
}
|