| 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 |
"fmt" |
| 11 |
"testing" |
| 12 |
|
| 13 |
"github.com/maxatome/go-testdeep/internal/test" |
| 14 |
"github.com/maxatome/go-testdeep/td" |
| 15 |
) |
| 16 |
|
| 17 |
func TestSet(t *testing.T) { |
| 18 |
type MyArray [5]int |
| 19 |
type MySlice []int |
| 20 |
|
| 21 |
for idx, got := range []any{ |
| 22 |
[]int{1, 3, 4, 4, 5}, |
| 23 |
[...]int{1, 3, 4, 4, 5}, |
| 24 |
MySlice{1, 3, 4, 4, 5}, |
| 25 |
MyArray{1, 3, 4, 4, 5}, |
| 26 |
&MySlice{1, 3, 4, 4, 5}, |
| 27 |
&MyArray{1, 3, 4, 4, 5}, |
| 28 |
} { |
| 29 |
testName := fmt.Sprintf("Test #%d → %v", idx, got) |
| 30 |
|
| 31 |
// |
| 32 |
// Set |
| 33 |
checkOK(t, got, td.Set(5, 4, 1, 3), testName) |
| 34 |
checkOK(t, got, |
| 35 |
td.Set(5, 4, 1, 3, 3, 3, 3), testName) // duplicated fields |
| 36 |
checkOK(t, got, |
| 37 |
td.Set( |
| 38 |
td.Between(0, 5), |
| 39 |
td.Between(0, 5), |
| 40 |
td.Between(0, 5))) // dup too |
| 41 |
|
| 42 |
checkError(t, got, td.Set(5, 4), |
| 43 |
expectedError{ |
| 44 |
Message: mustBe("comparing %% as a Set"), |
| 45 |
Path: mustBe("DATA"), |
| 46 |
// items are sorted |
| 47 |
Summary: mustBe(`Extra 2 items: (1, |
| 48 |
3)`), |
| 49 |
}, |
| 50 |
testName) |
| 51 |
|
| 52 |
checkError(t, got, td.Set(5, 4, 1, 3, 66), |
| 53 |
expectedError{ |
| 54 |
Message: mustBe("comparing %% as a Set"), |
| 55 |
Path: mustBe("DATA"), |
| 56 |
Summary: mustBe("Missing item: (66)"), |
| 57 |
}, |
| 58 |
testName) |
| 59 |
|
| 60 |
checkError(t, got, td.Set(5, 66, 4, 1, 3), |
| 61 |
expectedError{ |
| 62 |
Message: mustBe("comparing %% as a Set"), |
| 63 |
Path: mustBe("DATA"), |
| 64 |
Summary: mustBe("Missing item: (66)"), |
| 65 |
}, |
| 66 |
testName) |
| 67 |
|
| 68 |
checkError(t, got, td.Set(5, 67, 4, 1, 3, 66), |
| 69 |
expectedError{ |
| 70 |
Message: mustBe("comparing %% as a Set"), |
| 71 |
Path: mustBe("DATA"), |
| 72 |
Summary: mustBe("Missing 2 items: (66,\n 67)"), |
| 73 |
}, |
| 74 |
testName) |
| 75 |
|
| 76 |
checkError(t, got, td.Set(5, 66, 4, 3), |
| 77 |
expectedError{ |
| 78 |
Message: mustBe("comparing %% as a Set"), |
| 79 |
Path: mustBe("DATA"), |
| 80 |
Summary: mustBe("Missing item: (66)\n Extra item: (1)"), |
| 81 |
}, |
| 82 |
testName) |
| 83 |
|
| 84 |
checkError(t, got, td.Set(1, td.JSON("{")), |
| 85 |
expectedError{ |
| 86 |
Message: mustBe("bad usage of JSON operator"), |
| 87 |
Path: mustBe(`DATA`), |
| 88 |
Summary: mustContain("JSON unmarshal error"), |
| 89 |
Under: mustContain("under operator JSON at "), |
| 90 |
}, |
| 91 |
testName+": erroneous operator expected") |
| 92 |
|
| 93 |
// Lax |
| 94 |
checkOK(t, got, td.Lax(td.Set(5, float64(4), 1, 3)), testName) |
| 95 |
|
| 96 |
// |
| 97 |
// SubSetOf |
| 98 |
checkOK(t, got, td.SubSetOf(5, 4, 1, 3), testName) |
| 99 |
checkOK(t, got, td.SubSetOf(5, 4, 1, 3, 66), testName) |
| 100 |
|
| 101 |
checkError(t, got, td.SubSetOf(5, 66, 4, 3), |
| 102 |
expectedError{ |
| 103 |
Message: mustBe("comparing %% as a SubSetOf"), |
| 104 |
Path: mustBe("DATA"), |
| 105 |
Summary: mustBe("Extra item: (1)"), |
| 106 |
}, |
| 107 |
testName) |
| 108 |
|
| 109 |
checkError(t, got, td.SubSetOf(1, td.JSON("{")), |
| 110 |
expectedError{ |
| 111 |
Message: mustBe("bad usage of JSON operator"), |
| 112 |
Path: mustBe(`DATA`), |
| 113 |
Summary: mustContain("JSON unmarshal error"), |
| 114 |
Under: mustContain("under operator JSON at "), |
| 115 |
}, |
| 116 |
testName+": erroneous operator expected") |
| 117 |
|
| 118 |
// Lax |
| 119 |
checkOK(t, got, td.Lax(td.SubSetOf(5, float64(4), 1, 3)), testName) |
| 120 |
|
| 121 |
// |
| 122 |
// SuperSetOf |
| 123 |
checkOK(t, got, td.SuperSetOf(5, 4, 1, 3), testName) |
| 124 |
checkOK(t, got, td.SuperSetOf(5, 4), testName) |
| 125 |
|
| 126 |
checkError(t, got, td.SuperSetOf(5, 66, 4, 1, 3), |
| 127 |
expectedError{ |
| 128 |
Message: mustBe("comparing %% as a SuperSetOf"), |
| 129 |
Path: mustBe("DATA"), |
| 130 |
Summary: mustBe("Missing item: (66)"), |
| 131 |
}, |
| 132 |
testName) |
| 133 |
|
| 134 |
checkError(t, got, td.SuperSetOf(1, td.JSON("{")), |
| 135 |
expectedError{ |
| 136 |
Message: mustBe("bad usage of JSON operator"), |
| 137 |
Path: mustBe(`DATA`), |
| 138 |
Summary: mustContain("JSON unmarshal error"), |
| 139 |
Under: mustContain("under operator JSON at "), |
| 140 |
}, |
| 141 |
testName+": erroneous operator expected") |
| 142 |
|
| 143 |
// Lax |
| 144 |
checkOK(t, got, td.Lax(td.SuperSetOf(5, float64(4), 1, 3)), testName) |
| 145 |
|
| 146 |
// |
| 147 |
// NotAny |
| 148 |
checkOK(t, got, td.NotAny(10, 20, 30), testName) |
| 149 |
|
| 150 |
checkError(t, got, td.NotAny(3, 66), |
| 151 |
expectedError{ |
| 152 |
Message: mustBe("comparing %% as a NotAny"), |
| 153 |
Path: mustBe("DATA"), |
| 154 |
Summary: mustBe("Extra item: (3)"), |
| 155 |
}, |
| 156 |
testName) |
| 157 |
|
| 158 |
checkError(t, got, td.NotAny(10, td.JSON("{")), |
| 159 |
expectedError{ |
| 160 |
Message: mustBe("bad usage of JSON operator"), |
| 161 |
Path: mustBe(`DATA`), |
| 162 |
Summary: mustContain("JSON unmarshal error"), |
| 163 |
Under: mustContain("under operator JSON at "), |
| 164 |
}, |
| 165 |
testName+": erroneous operator expected") |
| 166 |
|
| 167 |
// Lax |
| 168 |
checkOK(t, got, td.NotAny(float64(3)), testName) |
| 169 |
|
| 170 |
checkError(t, got, td.Lax(td.NotAny(float64(3))), |
| 171 |
expectedError{ |
| 172 |
Message: mustBe("comparing %% as a NotAny"), |
| 173 |
Path: mustBe("DATA"), |
| 174 |
Summary: mustBe("Extra item: (3.0)"), |
| 175 |
}, |
| 176 |
testName) |
| 177 |
} |
| 178 |
|
| 179 |
checkOK(t, []any{123, "foo", nil, "bar", nil}, |
| 180 |
td.Set("foo", "bar", 123, nil)) |
| 181 |
|
| 182 |
var nilSlice MySlice |
| 183 |
for idx, got := range []any{([]int)(nil), &nilSlice} { |
| 184 |
testName := fmt.Sprintf("Test #%d", idx) |
| 185 |
|
| 186 |
checkOK(t, got, td.Set(), testName) |
| 187 |
checkOK(t, got, td.SubSetOf(), testName) |
| 188 |
checkOK(t, got, td.SubSetOf(1, 2), testName) |
| 189 |
checkOK(t, got, td.SuperSetOf(), testName) |
| 190 |
checkOK(t, got, td.NotAny(), testName) |
| 191 |
checkOK(t, got, td.NotAny(1, 2), testName) |
| 192 |
} |
| 193 |
|
| 194 |
for idx, set := range []td.TestDeep{ |
| 195 |
td.Set(123), |
| 196 |
td.SubSetOf(123), |
| 197 |
td.SuperSetOf(123), |
| 198 |
td.NotAny(123), |
| 199 |
} { |
| 200 |
testName := fmt.Sprintf("Test #%d → %s", idx, set) |
| 201 |
|
| 202 |
checkError(t, 123, set, |
| 203 |
expectedError{ |
| 204 |
Message: mustBe("bad kind"), |
| 205 |
Path: mustBe("DATA"), |
| 206 |
Got: mustBe("int"), |
| 207 |
Expected: mustBe("slice OR array OR *slice OR *array"), |
| 208 |
}, |
| 209 |
testName) |
| 210 |
|
| 211 |
num := 123 |
| 212 |
checkError(t, &num, set, |
| 213 |
expectedError{ |
| 214 |
Message: mustBe("bad kind"), |
| 215 |
Path: mustBe("DATA"), |
| 216 |
Got: mustBe("*int"), |
| 217 |
Expected: mustBe("slice OR array OR *slice OR *array"), |
| 218 |
}, |
| 219 |
testName) |
| 220 |
|
| 221 |
var list *MySlice |
| 222 |
checkError(t, list, set, |
| 223 |
expectedError{ |
| 224 |
Message: mustBe("nil pointer"), |
| 225 |
Path: mustBe("DATA"), |
| 226 |
Got: mustBe("nil *slice (*td_test.MySlice type)"), |
| 227 |
Expected: mustBe("non-nil *slice OR *array"), |
| 228 |
}, |
| 229 |
testName) |
| 230 |
|
| 231 |
checkError(t, nil, set, |
| 232 |
expectedError{ |
| 233 |
Message: mustBe("bad kind"), |
| 234 |
Path: mustBe("DATA"), |
| 235 |
Got: mustBe("nil"), |
| 236 |
Expected: mustBe("slice OR array OR *slice OR *array"), |
| 237 |
}, |
| 238 |
testName) |
| 239 |
} |
| 240 |
|
| 241 |
// |
| 242 |
// String |
| 243 |
test.EqualStr(t, td.Set(1).String(), "Set(1)") |
| 244 |
test.EqualStr(t, td.Set(1, 2).String(), "Set(1,\n 2)") |
| 245 |
|
| 246 |
test.EqualStr(t, td.SubSetOf(1).String(), "SubSetOf(1)") |
| 247 |
test.EqualStr(t, td.SubSetOf(1, 2).String(), "SubSetOf(1,\n 2)") |
| 248 |
|
| 249 |
test.EqualStr(t, td.SuperSetOf(1).String(), "SuperSetOf(1)") |
| 250 |
test.EqualStr(t, td.SuperSetOf(1, 2).String(), |
| 251 |
"SuperSetOf(1,\n 2)") |
| 252 |
|
| 253 |
test.EqualStr(t, td.NotAny(1).String(), "NotAny(1)") |
| 254 |
test.EqualStr(t, td.NotAny(1, 2).String(), "NotAny(1,\n 2)") |
| 255 |
} |
| 256 |
|
| 257 |
func TestSetTypeBehind(t *testing.T) { |
| 258 |
equalTypes(t, td.Set(6, 5), ([]int)(nil)) |
| 259 |
equalTypes(t, td.Set(6, "foo"), nil) |
| 260 |
|
| 261 |
equalTypes(t, td.SubSetOf(6, 5), ([]int)(nil)) |
| 262 |
equalTypes(t, td.SubSetOf(6, "foo"), nil) |
| 263 |
|
| 264 |
equalTypes(t, td.SuperSetOf(6, 5), ([]int)(nil)) |
| 265 |
equalTypes(t, td.SuperSetOf(6, "foo"), nil) |
| 266 |
|
| 267 |
equalTypes(t, td.NotAny(6, 5), ([]int)(nil)) |
| 268 |
equalTypes(t, td.NotAny(6, "foo"), nil) |
| 269 |
|
| 270 |
// Always the same non-interface type (even if we encounter several |
| 271 |
// interface types) |
| 272 |
equalTypes(t, |
| 273 |
td.Set( |
| 274 |
td.Empty(), |
| 275 |
5, |
| 276 |
td.Isa((*error)(nil)), // interface type (in fact pointer to ...) |
| 277 |
td.All(6, 7), |
| 278 |
td.Isa((*fmt.Stringer)(nil)), // interface type |
| 279 |
8), |
| 280 |
([]int)(nil)) |
| 281 |
|
| 282 |
// Only one interface type |
| 283 |
equalTypes(t, |
| 284 |
td.Set( |
| 285 |
td.Isa((*error)(nil)), |
| 286 |
td.Isa((*error)(nil)), |
| 287 |
td.Isa((*error)(nil)), |
| 288 |
), |
| 289 |
([]*error)(nil)) |
| 290 |
|
| 291 |
// Several interface types, cannot be sure |
| 292 |
equalTypes(t, |
| 293 |
td.Set( |
| 294 |
td.Isa((*error)(nil)), |
| 295 |
td.Isa((*fmt.Stringer)(nil)), |
| 296 |
), |
| 297 |
nil) |
| 298 |
} |