| 1 |
// Copyright (c) 2018-2022, Maxime Soulé |
| 2 |
// All rights reserved. |
| 3 |
// |
| 4 |
// This source code is licensed under the BSD-style license found in the |
| 5 |
// LICENSE file in the root directory of this source tree. |
| 6 |
|
| 7 |
package td_test |
| 8 |
|
| 9 |
import ( |
| 10 |
"testing" |
| 11 |
|
| 12 |
"github.com/maxatome/go-testdeep/internal/test" |
| 13 |
"github.com/maxatome/go-testdeep/td" |
| 14 |
) |
| 15 |
|
| 16 |
func TestZero(t *testing.T) { |
| 17 |
checkOK(t, 0, td.Zero()) |
| 18 |
checkOK(t, int64(0), td.Zero()) |
| 19 |
checkOK(t, float64(0), td.Zero()) |
| 20 |
checkOK(t, nil, td.Zero()) |
| 21 |
checkOK(t, (map[string]int)(nil), td.Zero()) |
| 22 |
checkOK(t, ([]int)(nil), td.Zero()) |
| 23 |
checkOK(t, [3]int{}, td.Zero()) |
| 24 |
checkOK(t, MyStruct{}, td.Zero()) |
| 25 |
checkOK(t, (*MyStruct)(nil), td.Zero()) |
| 26 |
checkOK(t, &MyStruct{}, td.Ptr(td.Zero())) |
| 27 |
checkOK(t, (chan int)(nil), td.Zero()) |
| 28 |
checkOK(t, (func())(nil), td.Zero()) |
| 29 |
checkOK(t, false, td.Zero()) |
| 30 |
|
| 31 |
checkError(t, 12, td.Zero(), |
| 32 |
expectedError{ |
| 33 |
Message: mustBe("values differ"), |
| 34 |
Path: mustBe("DATA"), |
| 35 |
Got: mustBe("12"), |
| 36 |
Expected: mustBe("0"), |
| 37 |
}) |
| 38 |
checkError(t, int64(12), td.Zero(), |
| 39 |
expectedError{ |
| 40 |
Message: mustBe("values differ"), |
| 41 |
Path: mustBe("DATA"), |
| 42 |
Got: mustBe("(int64) 12"), |
| 43 |
Expected: mustBe("(int64) 0"), |
| 44 |
}) |
| 45 |
checkError(t, float64(12), td.Zero(), |
| 46 |
expectedError{ |
| 47 |
Message: mustBe("values differ"), |
| 48 |
Path: mustBe("DATA"), |
| 49 |
Got: mustBe("12.0"), |
| 50 |
Expected: mustBe("0.0"), |
| 51 |
}) |
| 52 |
checkError(t, map[string]int{}, td.Zero(), |
| 53 |
expectedError{ |
| 54 |
Message: mustBe("nil map"), |
| 55 |
Path: mustBe("DATA"), |
| 56 |
Got: mustBe("not nil"), |
| 57 |
Expected: mustBe("nil"), |
| 58 |
}) |
| 59 |
checkError(t, []int{}, td.Zero(), |
| 60 |
expectedError{ |
| 61 |
Message: mustBe("nil slice"), |
| 62 |
Path: mustBe("DATA"), |
| 63 |
Got: mustBe("not nil"), |
| 64 |
Expected: mustBe("nil"), |
| 65 |
}) |
| 66 |
checkError(t, [3]int{0, 12}, td.Zero(), |
| 67 |
expectedError{ |
| 68 |
Message: mustBe("values differ"), |
| 69 |
Path: mustBe("DATA[1]"), |
| 70 |
Got: mustBe("12"), |
| 71 |
Expected: mustBe("0"), |
| 72 |
}) |
| 73 |
checkError(t, MyStruct{ValInt: 12}, td.Zero(), |
| 74 |
expectedError{ |
| 75 |
Message: mustBe("values differ"), |
| 76 |
Path: mustBe("DATA.ValInt"), |
| 77 |
Got: mustBe("12"), |
| 78 |
Expected: mustBe("0"), |
| 79 |
}) |
| 80 |
checkError(t, &MyStruct{}, td.Zero(), |
| 81 |
expectedError{ |
| 82 |
Message: mustBe("values differ"), |
| 83 |
Path: mustBe("*DATA"), |
| 84 |
// in fact, pointer on 0'ed struct contents |
| 85 |
Got: mustBe("(td_test.MyStruct) {\n}"), |
| 86 |
Expected: mustBe("nil"), |
| 87 |
}) |
| 88 |
checkError(t, true, td.Zero(), |
| 89 |
expectedError{ |
| 90 |
Message: mustBe("values differ"), |
| 91 |
Path: mustBe("DATA"), |
| 92 |
Got: mustBe("true"), |
| 93 |
Expected: mustBe("false"), |
| 94 |
}) |
| 95 |
|
| 96 |
// |
| 97 |
// String |
| 98 |
test.EqualStr(t, td.Zero().String(), "Zero()") |
| 99 |
} |
| 100 |
|
| 101 |
func TestNotZero(t *testing.T) { |
| 102 |
checkOK(t, 12, td.NotZero()) |
| 103 |
checkOK(t, int64(12), td.NotZero()) |
| 104 |
checkOK(t, float64(12), td.NotZero()) |
| 105 |
checkOK(t, map[string]int{}, td.NotZero()) |
| 106 |
checkOK(t, []int{}, td.NotZero()) |
| 107 |
checkOK(t, [3]int{1}, td.NotZero()) |
| 108 |
checkOK(t, MyStruct{ValInt: 1}, td.NotZero()) |
| 109 |
checkOK(t, &MyStruct{}, td.NotZero()) |
| 110 |
checkOK(t, make(chan int), td.NotZero()) |
| 111 |
checkOK(t, func() {}, td.NotZero()) |
| 112 |
checkOK(t, true, td.NotZero()) |
| 113 |
|
| 114 |
checkError(t, nil, td.NotZero(), |
| 115 |
expectedError{ |
| 116 |
Message: mustBe("zero value"), |
| 117 |
Path: mustBe("DATA"), |
| 118 |
Got: mustBe("nil"), |
| 119 |
Expected: mustBe("NotZero()"), |
| 120 |
}) |
| 121 |
checkError(t, 0, td.NotZero(), |
| 122 |
expectedError{ |
| 123 |
Message: mustBe("zero value"), |
| 124 |
Path: mustBe("DATA"), |
| 125 |
Got: mustBe("0"), |
| 126 |
Expected: mustBe("NotZero()"), |
| 127 |
}) |
| 128 |
checkError(t, int64(0), td.NotZero(), |
| 129 |
expectedError{ |
| 130 |
Message: mustBe("zero value"), |
| 131 |
Path: mustBe("DATA"), |
| 132 |
Got: mustBe("(int64) 0"), |
| 133 |
Expected: mustBe("NotZero()"), |
| 134 |
}) |
| 135 |
checkError(t, float64(0), td.NotZero(), |
| 136 |
expectedError{ |
| 137 |
Message: mustBe("zero value"), |
| 138 |
Path: mustBe("DATA"), |
| 139 |
Got: mustBe("0.0"), |
| 140 |
Expected: mustBe("NotZero()"), |
| 141 |
}) |
| 142 |
checkError(t, (map[string]int)(nil), td.NotZero(), |
| 143 |
expectedError{ |
| 144 |
Message: mustBe("zero value"), |
| 145 |
Path: mustBe("DATA"), |
| 146 |
Got: mustBe("(map[string]int) <nil>"), |
| 147 |
Expected: mustBe("NotZero()"), |
| 148 |
}) |
| 149 |
checkError(t, ([]int)(nil), td.NotZero(), |
| 150 |
expectedError{ |
| 151 |
Message: mustBe("zero value"), |
| 152 |
Path: mustBe("DATA"), |
| 153 |
Got: mustBe("([]int) <nil>"), |
| 154 |
Expected: mustBe("NotZero()"), |
| 155 |
}) |
| 156 |
checkError(t, [3]int{}, td.NotZero(), |
| 157 |
expectedError{ |
| 158 |
Message: mustBe("zero value"), |
| 159 |
Path: mustBe("DATA"), |
| 160 |
Got: mustContain("0"), |
| 161 |
Expected: mustBe("NotZero()"), |
| 162 |
}) |
| 163 |
checkError(t, MyStruct{}, td.NotZero(), |
| 164 |
expectedError{ |
| 165 |
Message: mustBe("zero value"), |
| 166 |
Path: mustBe("DATA"), |
| 167 |
Got: mustBe("(td_test.MyStruct) {\n}"), |
| 168 |
Expected: mustBe("NotZero()"), |
| 169 |
}) |
| 170 |
checkError(t, &MyStruct{}, td.Ptr(td.NotZero()), |
| 171 |
expectedError{ |
| 172 |
Message: mustBe("zero value"), |
| 173 |
Path: mustBe("*DATA"), |
| 174 |
// in fact, pointer on 0'ed struct contents |
| 175 |
Got: mustBe("(td_test.MyStruct) {\n}"), |
| 176 |
Expected: mustBe("NotZero()"), |
| 177 |
}) |
| 178 |
checkError(t, false, td.NotZero(), |
| 179 |
expectedError{ |
| 180 |
Message: mustBe("zero value"), |
| 181 |
Path: mustBe("DATA"), |
| 182 |
Got: mustBe("false"), |
| 183 |
Expected: mustBe("NotZero()"), |
| 184 |
}) |
| 185 |
|
| 186 |
// |
| 187 |
// String |
| 188 |
test.EqualStr(t, td.NotZero().String(), "NotZero()") |
| 189 |
} |
| 190 |
|
| 191 |
func TestZeroTypeBehind(t *testing.T) { |
| 192 |
equalTypes(t, td.Zero(), nil) |
| 193 |
equalTypes(t, td.NotZero(), nil) |
| 194 |
} |