| 1 |
// Copyright (c) 2018, 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 TestArray(t *testing.T) { |
| 17 |
type MyArray [5]int |
| 18 |
|
| 19 |
// |
| 20 |
// Simple array |
| 21 |
checkOK(t, [5]int{}, td.Array([5]int{})) |
| 22 |
checkOK(t, [5]int{}, td.Array([5]int{}, nil)) |
| 23 |
checkOK(t, [5]int{0, 0, 0, 4}, td.Array([5]int{0, 0, 0, 4})) |
| 24 |
checkOK(t, [5]int{1, 0, 3}, |
| 25 |
td.Array([5]int{}, td.ArrayEntries{2: 3, 0: 1})) |
| 26 |
checkOK(t, [5]int{1, 2, 3}, |
| 27 |
td.Array([5]int{0, 2}, td.ArrayEntries{2: 3, 0: 1})) |
| 28 |
|
| 29 |
checkOK(t, [5]any{1, 2, nil, 4, nil}, |
| 30 |
td.Array([5]any{nil, 2, nil, 4}, td.ArrayEntries{0: 1, 2: nil})) |
| 31 |
|
| 32 |
checkOK(t, [5]any{1, 2, nil, 4, nil}, |
| 33 |
td.Array([5]any{nil, 2, nil, 4}, |
| 34 |
td.ArrayEntries{0: 3, 2: 28}, |
| 35 |
td.ArrayEntries{0: 2, 2: "pipo"}, |
| 36 |
td.ArrayEntries{0: 1, 2: nil}, |
| 37 |
)) |
| 38 |
|
| 39 |
zero, one, two := 0, 1, 2 |
| 40 |
checkOK(t, [5]*int{nil, &zero, &one, &two}, |
| 41 |
td.Array( |
| 42 |
[5]*int{}, td.ArrayEntries{1: &zero, 2: &one, 3: &two, 4: nil})) |
| 43 |
|
| 44 |
gotArray := [...]int{1, 2, 3, 4, 5} |
| 45 |
|
| 46 |
checkError(t, gotArray, td.Array(MyArray{}), |
| 47 |
expectedError{ |
| 48 |
Message: mustBe("type mismatch"), |
| 49 |
Path: mustBe("DATA"), |
| 50 |
Got: mustBe("[5]int"), |
| 51 |
Expected: mustBe("td_test.MyArray"), |
| 52 |
}) |
| 53 |
checkError(t, gotArray, td.Array([5]int{1, 2, 3, 4, 6}), |
| 54 |
expectedError{ |
| 55 |
Message: mustBe("values differ"), |
| 56 |
Path: mustBe("DATA[4]"), |
| 57 |
Got: mustBe("5"), |
| 58 |
Expected: mustBe("6"), |
| 59 |
}) |
| 60 |
checkError(t, gotArray, |
| 61 |
td.Array([5]int{1, 2, 3, 4}, td.ArrayEntries{4: 6}), |
| 62 |
expectedError{ |
| 63 |
Message: mustBe("values differ"), |
| 64 |
Path: mustBe("DATA[4]"), |
| 65 |
Got: mustBe("5"), |
| 66 |
Expected: mustBe("6"), |
| 67 |
}) |
| 68 |
|
| 69 |
checkError(t, nil, |
| 70 |
td.Array([1]int{42}), |
| 71 |
expectedError{ |
| 72 |
Message: mustBe("values differ"), |
| 73 |
Path: mustBe("DATA"), |
| 74 |
Got: mustBe("nil"), |
| 75 |
Expected: mustContain("Array("), |
| 76 |
}) |
| 77 |
|
| 78 |
// |
| 79 |
// Array type |
| 80 |
checkOK(t, MyArray{}, td.Array(MyArray{})) |
| 81 |
checkOK(t, MyArray{}, td.Array(MyArray{}, nil)) |
| 82 |
checkOK(t, MyArray{0, 0, 0, 4}, td.Array(MyArray{0, 0, 0, 4})) |
| 83 |
checkOK(t, MyArray{1, 0, 3}, |
| 84 |
td.Array(MyArray{}, td.ArrayEntries{2: 3, 0: 1})) |
| 85 |
checkOK(t, MyArray{1, 2, 3}, |
| 86 |
td.Array(MyArray{0, 2}, td.ArrayEntries{2: 3, 0: 1})) |
| 87 |
|
| 88 |
checkOK(t, &MyArray{}, td.Array(&MyArray{})) |
| 89 |
checkOK(t, &MyArray{0, 0, 0, 4}, td.Array(&MyArray{0, 0, 0, 4})) |
| 90 |
checkOK(t, &MyArray{1, 0, 3}, |
| 91 |
td.Array(&MyArray{}, td.ArrayEntries{2: 3, 0: 1})) |
| 92 |
checkOK(t, &MyArray{1, 0, 3}, |
| 93 |
td.Array((*MyArray)(nil), td.ArrayEntries{2: 3, 0: 1})) |
| 94 |
checkOK(t, &MyArray{1, 2, 3}, |
| 95 |
td.Array(&MyArray{0, 2}, td.ArrayEntries{2: 3, 0: 1})) |
| 96 |
|
| 97 |
gotTypedArray := MyArray{1, 2, 3, 4, 5} |
| 98 |
|
| 99 |
checkError(t, 123, td.Array(&MyArray{}, td.ArrayEntries{}), |
| 100 |
expectedError{ |
| 101 |
Message: mustBe("type mismatch"), |
| 102 |
Path: mustBe("DATA"), |
| 103 |
Got: mustBe("int"), |
| 104 |
Expected: mustBe("*td_test.MyArray"), |
| 105 |
}) |
| 106 |
|
| 107 |
checkError(t, &MyStruct{}, |
| 108 |
td.Array(&MyArray{}, td.ArrayEntries{}), |
| 109 |
expectedError{ |
| 110 |
Message: mustBe("type mismatch"), |
| 111 |
Path: mustBe("DATA"), |
| 112 |
Got: mustBe("*td_test.MyStruct"), |
| 113 |
Expected: mustBe("*td_test.MyArray"), |
| 114 |
}) |
| 115 |
|
| 116 |
checkError(t, gotTypedArray, td.Array([5]int{}), |
| 117 |
expectedError{ |
| 118 |
Message: mustBe("type mismatch"), |
| 119 |
Path: mustBe("DATA"), |
| 120 |
Got: mustBe("td_test.MyArray"), |
| 121 |
Expected: mustBe("[5]int"), |
| 122 |
}) |
| 123 |
checkError(t, gotTypedArray, td.Array(MyArray{1, 2, 3, 4, 6}), |
| 124 |
expectedError{ |
| 125 |
Message: mustBe("values differ"), |
| 126 |
Path: mustBe("DATA[4]"), |
| 127 |
Got: mustBe("5"), |
| 128 |
Expected: mustBe("6"), |
| 129 |
}) |
| 130 |
checkError(t, gotTypedArray, |
| 131 |
td.Array(MyArray{1, 2, 3, 4}, td.ArrayEntries{4: 6}), |
| 132 |
expectedError{ |
| 133 |
Message: mustBe("values differ"), |
| 134 |
Path: mustBe("DATA[4]"), |
| 135 |
Got: mustBe("5"), |
| 136 |
Expected: mustBe("6"), |
| 137 |
}) |
| 138 |
|
| 139 |
checkError(t, &gotTypedArray, td.Array([5]int{}), |
| 140 |
expectedError{ |
| 141 |
Message: mustBe("type mismatch"), |
| 142 |
Path: mustBe("DATA"), |
| 143 |
Got: mustBe("*td_test.MyArray"), |
| 144 |
Expected: mustBe("[5]int"), |
| 145 |
}) |
| 146 |
checkError(t, &gotTypedArray, td.Array(&MyArray{1, 2, 3, 4, 6}), |
| 147 |
expectedError{ |
| 148 |
Message: mustBe("values differ"), |
| 149 |
Path: mustBe("DATA[4]"), |
| 150 |
Got: mustBe("5"), |
| 151 |
Expected: mustBe("6"), |
| 152 |
}) |
| 153 |
checkError(t, &gotTypedArray, |
| 154 |
td.Array(&MyArray{1, 2, 3, 4}, td.ArrayEntries{4: 6}), |
| 155 |
expectedError{ |
| 156 |
Message: mustBe("values differ"), |
| 157 |
Path: mustBe("DATA[4]"), |
| 158 |
Got: mustBe("5"), |
| 159 |
Expected: mustBe("6"), |
| 160 |
}) |
| 161 |
|
| 162 |
// Be lax... |
| 163 |
// Without Lax → error |
| 164 |
checkError(t, MyArray{}, td.Array([5]int{}), |
| 165 |
expectedError{ |
| 166 |
Message: mustBe("type mismatch"), |
| 167 |
}) |
| 168 |
checkError(t, [5]int{}, td.Array(MyArray{}), |
| 169 |
expectedError{ |
| 170 |
Message: mustBe("type mismatch"), |
| 171 |
}) |
| 172 |
checkOK(t, MyArray{}, td.Lax(td.Array([5]int{}))) |
| 173 |
checkOK(t, [5]int{}, td.Lax(td.Array(MyArray{}))) |
| 174 |
|
| 175 |
// |
| 176 |
// Bad usage |
| 177 |
checkError(t, "never tested", |
| 178 |
td.Array("test"), |
| 179 |
expectedError{ |
| 180 |
Message: mustBe("bad usage of Array operator"), |
| 181 |
Path: mustBe("DATA"), |
| 182 |
Summary: mustBe("usage: Array(ARRAY|&ARRAY, EXPECTED_ENTRIES), but received string as 1st parameter"), |
| 183 |
}) |
| 184 |
|
| 185 |
checkError(t, "never tested", |
| 186 |
td.Array(&MyStruct{}), |
| 187 |
expectedError{ |
| 188 |
Message: mustBe("bad usage of Array operator"), |
| 189 |
Path: mustBe("DATA"), |
| 190 |
Summary: mustBe("usage: Array(ARRAY|&ARRAY, EXPECTED_ENTRIES), but received *td_test.MyStruct (ptr) as 1st parameter"), |
| 191 |
}) |
| 192 |
|
| 193 |
checkError(t, "never tested", |
| 194 |
td.Array([]int{}), |
| 195 |
expectedError{ |
| 196 |
Message: mustBe("bad usage of Array operator"), |
| 197 |
Path: mustBe("DATA"), |
| 198 |
Summary: mustBe("usage: Array(ARRAY|&ARRAY, EXPECTED_ENTRIES), but received []int (slice) as 1st parameter"), |
| 199 |
}) |
| 200 |
|
| 201 |
checkError(t, "never tested", |
| 202 |
td.Array([1]int{}, td.ArrayEntries{1: 34}), |
| 203 |
expectedError{ |
| 204 |
Message: mustBe("bad usage of Array operator"), |
| 205 |
Path: mustBe("DATA"), |
| 206 |
Summary: mustBe("array length is 1, so cannot have #1 expected index"), |
| 207 |
}) |
| 208 |
|
| 209 |
checkError(t, [3]int{}, |
| 210 |
td.Array([3]int{}, td.ArrayEntries{1: nil}), |
| 211 |
expectedError{ |
| 212 |
Message: mustBe("values differ"), |
| 213 |
Path: mustBe("DATA[1]"), |
| 214 |
Got: mustBe("0"), |
| 215 |
Expected: mustBe("nil"), |
| 216 |
}) |
| 217 |
|
| 218 |
checkError(t, [3]int{}, |
| 219 |
td.Array([3]int{}, td.ArrayEntries{1: "bad"}), |
| 220 |
expectedError{ |
| 221 |
Message: mustBe("type mismatch"), |
| 222 |
Path: mustBe("DATA[1]"), |
| 223 |
Got: mustBe("int"), |
| 224 |
Expected: mustBe("string"), |
| 225 |
}) |
| 226 |
|
| 227 |
checkError(t, "never tested", |
| 228 |
td.Array([1]int{12}, td.ArrayEntries{0: 21}), |
| 229 |
expectedError{ |
| 230 |
Message: mustBe("bad usage of Array operator"), |
| 231 |
Path: mustBe("DATA"), |
| 232 |
Summary: mustBe("non zero #0 entry in model already exists in expectedEntries"), |
| 233 |
}) |
| 234 |
|
| 235 |
// |
| 236 |
// String |
| 237 |
test.EqualStr(t, |
| 238 |
td.Array(MyArray{0, 0, 4}, td.ArrayEntries{1: 3, 0: 2}).String(), |
| 239 |
`Array(td_test.MyArray{ |
| 240 |
0: 2 |
| 241 |
1: 3 |
| 242 |
2: 4 |
| 243 |
3: 0 |
| 244 |
4: 0 |
| 245 |
})`) |
| 246 |
|
| 247 |
test.EqualStr(t, |
| 248 |
td.Array(&MyArray{0, 0, 4}, td.ArrayEntries{1: 3, 0: 2}).String(), |
| 249 |
`Array(*td_test.MyArray{ |
| 250 |
0: 2 |
| 251 |
1: 3 |
| 252 |
2: 4 |
| 253 |
3: 0 |
| 254 |
4: 0 |
| 255 |
})`) |
| 256 |
|
| 257 |
test.EqualStr(t, td.Array([0]int{}, td.ArrayEntries{}).String(), |
| 258 |
`Array([0]int{})`) |
| 259 |
|
| 260 |
// Erroneous op |
| 261 |
test.EqualStr(t, td.Array(12).String(), "Array(<ERROR>)") |
| 262 |
} |
| 263 |
|
| 264 |
func TestArrayTypeBehind(t *testing.T) { |
| 265 |
type MyArray [12]int |
| 266 |
|
| 267 |
equalTypes(t, td.Array([12]int{}), [12]int{}) |
| 268 |
equalTypes(t, td.Array(MyArray{}), MyArray{}) |
| 269 |
equalTypes(t, td.Array(&MyArray{}), &MyArray{}) |
| 270 |
|
| 271 |
// Erroneous op |
| 272 |
equalTypes(t, td.Array(12), nil) |
| 273 |
} |
| 274 |
|
| 275 |
func TestSlice(t *testing.T) { |
| 276 |
type MySlice []int |
| 277 |
|
| 278 |
// |
| 279 |
// Simple slice |
| 280 |
checkOK(t, []int{}, td.Slice([]int{})) |
| 281 |
checkOK(t, []int{}, td.Slice([]int{}, nil)) |
| 282 |
checkOK(t, []int{0, 3}, td.Slice([]int{0, 3})) |
| 283 |
checkOK(t, []int{2, 3}, |
| 284 |
td.Slice([]int{}, td.ArrayEntries{1: 3, 0: 2})) |
| 285 |
checkOK(t, []int{2, 3}, |
| 286 |
td.Slice(([]int)(nil), td.ArrayEntries{1: 3, 0: 2})) |
| 287 |
checkOK(t, []int{2, 3, 4}, |
| 288 |
td.Slice([]int{0, 0, 4}, td.ArrayEntries{1: 3, 0: 2})) |
| 289 |
checkOK(t, []int{2, 3, 4}, |
| 290 |
td.Slice([]int{2, 3}, td.ArrayEntries{2: 4})) |
| 291 |
checkOK(t, []int{2, 3, 4, 0, 6}, |
| 292 |
td.Slice([]int{2, 3}, td.ArrayEntries{2: 4, 4: 6})) |
| 293 |
|
| 294 |
gotSlice := []int{2, 3, 4} |
| 295 |
|
| 296 |
checkError(t, gotSlice, td.Slice(MySlice{}), |
| 297 |
expectedError{ |
| 298 |
Message: mustBe("type mismatch"), |
| 299 |
Path: mustBe("DATA"), |
| 300 |
Got: mustBe("[]int"), |
| 301 |
Expected: mustBe("td_test.MySlice"), |
| 302 |
}) |
| 303 |
checkError(t, gotSlice, td.Slice([]int{2, 3, 5}), |
| 304 |
expectedError{ |
| 305 |
Message: mustBe("values differ"), |
| 306 |
Path: mustBe("DATA[2]"), |
| 307 |
Got: mustBe("4"), |
| 308 |
Expected: mustBe("5"), |
| 309 |
}) |
| 310 |
checkError(t, gotSlice, |
| 311 |
td.Slice([]int{2, 3}, td.ArrayEntries{2: 5}), |
| 312 |
expectedError{ |
| 313 |
Message: mustBe("values differ"), |
| 314 |
Path: mustBe("DATA[2]"), |
| 315 |
Got: mustBe("4"), |
| 316 |
Expected: mustBe("5"), |
| 317 |
}) |
| 318 |
|
| 319 |
checkError(t, nil, |
| 320 |
td.Slice([]int{2, 3}), |
| 321 |
expectedError{ |
| 322 |
Message: mustBe("values differ"), |
| 323 |
Path: mustBe("DATA"), |
| 324 |
Got: mustBe("nil"), |
| 325 |
Expected: mustContain("Slice("), |
| 326 |
}) |
| 327 |
|
| 328 |
// |
| 329 |
// Slice type |
| 330 |
checkOK(t, MySlice{}, td.Slice(MySlice{})) |
| 331 |
checkOK(t, MySlice{}, td.Slice(MySlice{}, nil)) |
| 332 |
checkOK(t, MySlice{0, 3}, td.Slice(MySlice{0, 3})) |
| 333 |
checkOK(t, MySlice{2, 3}, |
| 334 |
td.Slice(MySlice{}, td.ArrayEntries{1: 3, 0: 2})) |
| 335 |
checkOK(t, MySlice{2, 3}, |
| 336 |
td.Slice((MySlice)(nil), td.ArrayEntries{1: 3, 0: 2})) |
| 337 |
checkOK(t, MySlice{2, 3, 4}, |
| 338 |
td.Slice(MySlice{0, 0, 4}, td.ArrayEntries{1: 3, 0: 2})) |
| 339 |
checkOK(t, MySlice{2, 3, 4, 0, 6}, |
| 340 |
td.Slice(MySlice{2, 3}, td.ArrayEntries{2: 4, 4: 6})) |
| 341 |
|
| 342 |
checkOK(t, &MySlice{}, td.Slice(&MySlice{})) |
| 343 |
checkOK(t, &MySlice{0, 3}, td.Slice(&MySlice{0, 3})) |
| 344 |
checkOK(t, &MySlice{2, 3}, |
| 345 |
td.Slice(&MySlice{}, td.ArrayEntries{1: 3, 0: 2})) |
| 346 |
checkOK(t, &MySlice{2, 3}, |
| 347 |
td.Slice((*MySlice)(nil), td.ArrayEntries{1: 3, 0: 2})) |
| 348 |
checkOK(t, &MySlice{2, 3, 4}, |
| 349 |
td.Slice(&MySlice{0, 0, 4}, td.ArrayEntries{1: 3, 0: 2})) |
| 350 |
checkOK(t, &MySlice{2, 3, 4, 0, 6}, |
| 351 |
td.Slice(&MySlice{2, 3}, td.ArrayEntries{2: 4, 4: 6})) |
| 352 |
|
| 353 |
gotTypedSlice := MySlice{2, 3, 4} |
| 354 |
|
| 355 |
checkError(t, 123, td.Slice(&MySlice{}, td.ArrayEntries{}), |
| 356 |
expectedError{ |
| 357 |
Message: mustBe("type mismatch"), |
| 358 |
Path: mustBe("DATA"), |
| 359 |
Got: mustBe("int"), |
| 360 |
Expected: mustBe("*td_test.MySlice"), |
| 361 |
}) |
| 362 |
|
| 363 |
checkError(t, &MyStruct{}, |
| 364 |
td.Slice(&MySlice{}, td.ArrayEntries{}), |
| 365 |
expectedError{ |
| 366 |
Message: mustBe("type mismatch"), |
| 367 |
Path: mustBe("DATA"), |
| 368 |
Got: mustBe("*td_test.MyStruct"), |
| 369 |
Expected: mustBe("*td_test.MySlice"), |
| 370 |
}) |
| 371 |
|
| 372 |
checkError(t, gotTypedSlice, td.Slice([]int{}), |
| 373 |
expectedError{ |
| 374 |
Message: mustBe("type mismatch"), |
| 375 |
Path: mustBe("DATA"), |
| 376 |
Got: mustBe("td_test.MySlice"), |
| 377 |
Expected: mustBe("[]int"), |
| 378 |
}) |
| 379 |
checkError(t, gotTypedSlice, td.Slice(MySlice{2, 3, 5}), |
| 380 |
expectedError{ |
| 381 |
Message: mustBe("values differ"), |
| 382 |
Path: mustBe("DATA[2]"), |
| 383 |
Got: mustBe("4"), |
| 384 |
Expected: mustBe("5"), |
| 385 |
}) |
| 386 |
checkError(t, gotTypedSlice, |
| 387 |
td.Slice(MySlice{2, 3}, td.ArrayEntries{2: 5}), |
| 388 |
expectedError{ |
| 389 |
Message: mustBe("values differ"), |
| 390 |
Path: mustBe("DATA[2]"), |
| 391 |
Got: mustBe("4"), |
| 392 |
Expected: mustBe("5"), |
| 393 |
}) |
| 394 |
checkError(t, gotTypedSlice, |
| 395 |
td.Slice(MySlice{2, 3, 4}, td.ArrayEntries{3: 5}), |
| 396 |
expectedError{ |
| 397 |
Message: mustBe("expected value out of range"), |
| 398 |
Path: mustBe("DATA[3]"), |
| 399 |
Got: mustBe("<non-existent value>"), |
| 400 |
Expected: mustBe("5"), |
| 401 |
}) |
| 402 |
checkError(t, gotTypedSlice, td.Slice(MySlice{2, 3}), |
| 403 |
expectedError{ |
| 404 |
Message: mustBe("got value out of range"), |
| 405 |
Path: mustBe("DATA[2]"), |
| 406 |
Got: mustBe("4"), |
| 407 |
Expected: mustBe("<non-existent value>"), |
| 408 |
}) |
| 409 |
|
| 410 |
checkError(t, &gotTypedSlice, td.Slice([]int{}), |
| 411 |
expectedError{ |
| 412 |
Message: mustBe("type mismatch"), |
| 413 |
Path: mustBe("DATA"), |
| 414 |
Got: mustBe("*td_test.MySlice"), |
| 415 |
Expected: mustBe("[]int"), |
| 416 |
}) |
| 417 |
checkError(t, &gotTypedSlice, td.Slice(&MySlice{2, 3, 5}), |
| 418 |
expectedError{ |
| 419 |
Message: mustBe("values differ"), |
| 420 |
Path: mustBe("DATA[2]"), |
| 421 |
Got: mustBe("4"), |
| 422 |
Expected: mustBe("5"), |
| 423 |
}) |
| 424 |
checkError(t, &gotTypedSlice, |
| 425 |
td.Slice(&MySlice{2, 3}, td.ArrayEntries{2: 5}), |
| 426 |
expectedError{ |
| 427 |
Message: mustBe("values differ"), |
| 428 |
Path: mustBe("DATA[2]"), |
| 429 |
Got: mustBe("4"), |
| 430 |
Expected: mustBe("5"), |
| 431 |
}) |
| 432 |
checkError(t, &gotTypedSlice, td.Slice(&MySlice{2, 3}), |
| 433 |
expectedError{ |
| 434 |
Message: mustBe("got value out of range"), |
| 435 |
Path: mustBe("DATA[2]"), |
| 436 |
Got: mustBe("4"), |
| 437 |
Expected: mustBe("<non-existent value>"), |
| 438 |
}) |
| 439 |
|
| 440 |
// |
| 441 |
// nil cases |
| 442 |
var ( |
| 443 |
gotNilSlice []int |
| 444 |
gotNilTypedSlice MySlice |
| 445 |
) |
| 446 |
|
| 447 |
checkOK(t, gotNilSlice, td.Slice([]int{})) |
| 448 |
checkOK(t, gotNilTypedSlice, td.Slice(MySlice{})) |
| 449 |
checkOK(t, &gotNilTypedSlice, td.Slice(&MySlice{})) |
| 450 |
|
| 451 |
// Be lax... |
| 452 |
// Without Lax → error |
| 453 |
checkError(t, MySlice{}, td.Slice([]int{}), |
| 454 |
expectedError{ |
| 455 |
Message: mustBe("type mismatch"), |
| 456 |
}) |
| 457 |
checkError(t, []int{}, td.Slice(MySlice{}), |
| 458 |
expectedError{ |
| 459 |
Message: mustBe("type mismatch"), |
| 460 |
}) |
| 461 |
checkOK(t, MySlice{}, td.Lax(td.Slice([]int{}))) |
| 462 |
checkOK(t, []int{}, td.Lax(td.Slice(MySlice{}))) |
| 463 |
|
| 464 |
// |
| 465 |
// Bad usage |
| 466 |
checkError(t, "never tested", |
| 467 |
td.Slice("test", nil), |
| 468 |
expectedError{ |
| 469 |
Message: mustBe("bad usage of Slice operator"), |
| 470 |
Path: mustBe("DATA"), |
| 471 |
Summary: mustBe("usage: Slice(SLICE|&SLICE, EXPECTED_ENTRIES), but received string as 1st parameter"), |
| 472 |
}) |
| 473 |
|
| 474 |
checkError(t, "never tested", |
| 475 |
td.Slice(&MyStruct{}), |
| 476 |
expectedError{ |
| 477 |
Message: mustBe("bad usage of Slice operator"), |
| 478 |
Path: mustBe("DATA"), |
| 479 |
Summary: mustBe("usage: Slice(SLICE|&SLICE, EXPECTED_ENTRIES), but received *td_test.MyStruct (ptr) as 1st parameter"), |
| 480 |
}) |
| 481 |
|
| 482 |
checkError(t, "never tested", |
| 483 |
td.Slice([0]int{}), |
| 484 |
expectedError{ |
| 485 |
Message: mustBe("bad usage of Slice operator"), |
| 486 |
Path: mustBe("DATA"), |
| 487 |
Summary: mustBe("usage: Slice(SLICE|&SLICE, EXPECTED_ENTRIES), but received [0]int (array) as 1st parameter"), |
| 488 |
}) |
| 489 |
|
| 490 |
checkError(t, []int{0, 5}, |
| 491 |
td.Slice([]int{}, td.ArrayEntries{1: "bad"}), |
| 492 |
expectedError{ |
| 493 |
Message: mustBe("type mismatch"), |
| 494 |
Path: mustBe("DATA[1]"), |
| 495 |
Got: mustBe("int"), |
| 496 |
Expected: mustBe("string"), |
| 497 |
}) |
| 498 |
|
| 499 |
checkError(t, "never tested", |
| 500 |
td.Slice([]int{12}, td.ArrayEntries{0: 21}), |
| 501 |
expectedError{ |
| 502 |
Message: mustBe("bad usage of Slice operator"), |
| 503 |
Path: mustBe("DATA"), |
| 504 |
Summary: mustBe("non zero #0 entry in model already exists in expectedEntries"), |
| 505 |
}) |
| 506 |
|
| 507 |
// |
| 508 |
// String |
| 509 |
test.EqualStr(t, |
| 510 |
td.Slice(MySlice{0, 0, 4}, td.ArrayEntries{1: 3, 0: 2}).String(), |
| 511 |
`Slice(td_test.MySlice{ |
| 512 |
0: 2 |
| 513 |
1: 3 |
| 514 |
2: 4 |
| 515 |
})`) |
| 516 |
|
| 517 |
test.EqualStr(t, |
| 518 |
td.Slice(&MySlice{0, 0, 4}, td.ArrayEntries{1: 3, 0: 2}).String(), |
| 519 |
`Slice(*td_test.MySlice{ |
| 520 |
0: 2 |
| 521 |
1: 3 |
| 522 |
2: 4 |
| 523 |
})`) |
| 524 |
|
| 525 |
test.EqualStr(t, td.Slice(&MySlice{}, td.ArrayEntries{}).String(), |
| 526 |
`Slice(*td_test.MySlice{})`) |
| 527 |
|
| 528 |
// Erroneous op |
| 529 |
test.EqualStr(t, td.Slice(12).String(), "Slice(<ERROR>)") |
| 530 |
} |
| 531 |
|
| 532 |
func TestSliceTypeBehind(t *testing.T) { |
| 533 |
type MySlice []int |
| 534 |
|
| 535 |
equalTypes(t, td.Slice([]int{}), []int{}) |
| 536 |
equalTypes(t, td.Slice(MySlice{}), MySlice{}) |
| 537 |
equalTypes(t, td.Slice(&MySlice{}), &MySlice{}) |
| 538 |
|
| 539 |
// Erroneous op |
| 540 |
equalTypes(t, td.Slice(12), nil) |
| 541 |
} |
| 542 |
|
| 543 |
func TestSuperSliceOf(t *testing.T) { |
| 544 |
t.Run("interface array", func(t *testing.T) { |
| 545 |
got := [5]any{"foo", "bar", nil, 666, 777} |
| 546 |
|
| 547 |
checkOK(t, got, |
| 548 |
td.SuperSliceOf([5]any{1: "bar"}, td.ArrayEntries{2: td.Nil()})) |
| 549 |
checkOK(t, got, |
| 550 |
td.SuperSliceOf([5]any{1: "bar"}, td.ArrayEntries{2: nil})) |
| 551 |
checkOK(t, got, |
| 552 |
td.SuperSliceOf([5]any{1: "bar"}, td.ArrayEntries{3: 666})) |
| 553 |
checkOK(t, got, |
| 554 |
td.SuperSliceOf([5]any{1: "bar"}, td.ArrayEntries{3: td.Between(665, 667)})) |
| 555 |
checkOK(t, &got, |
| 556 |
td.SuperSliceOf(&[5]any{1: "bar"}, td.ArrayEntries{3: td.Between(665, 667)})) |
| 557 |
|
| 558 |
checkOK(t, got, |
| 559 |
td.SuperSliceOf([5]any{1: "bar"}, |
| 560 |
td.ArrayEntries{3: 42}, |
| 561 |
td.ArrayEntries{3: "pipo"}, |
| 562 |
td.ArrayEntries{3: 666}, |
| 563 |
)) |
| 564 |
|
| 565 |
checkError(t, got, |
| 566 |
td.SuperSliceOf([5]any{1: "foo"}, td.ArrayEntries{2: td.Nil()}), |
| 567 |
expectedError{ |
| 568 |
Message: mustBe("values differ"), |
| 569 |
Path: mustBe("DATA[1]"), |
| 570 |
Got: mustBe(`"bar"`), |
| 571 |
Expected: mustBe(`"foo"`), |
| 572 |
}) |
| 573 |
checkError(t, got, |
| 574 |
td.SuperSliceOf([5]any{1: 666}, td.ArrayEntries{2: td.Nil()}), |
| 575 |
expectedError{ |
| 576 |
Message: mustBe("type mismatch"), |
| 577 |
Path: mustBe("DATA[1]"), |
| 578 |
Got: mustBe("string"), |
| 579 |
Expected: mustBe("int"), |
| 580 |
}) |
| 581 |
checkError(t, &got, |
| 582 |
td.SuperSliceOf([5]any{1: 666}, td.ArrayEntries{2: td.Nil()}), |
| 583 |
expectedError{ |
| 584 |
Message: mustBe("type mismatch"), |
| 585 |
Path: mustBe("DATA"), |
| 586 |
Got: mustBe("*[5]interface {}"), |
| 587 |
Expected: mustBe("[5]interface {}"), |
| 588 |
}) |
| 589 |
checkError(t, got, |
| 590 |
td.SuperSliceOf(&[5]any{1: 666}, td.ArrayEntries{2: td.Nil()}), |
| 591 |
expectedError{ |
| 592 |
Message: mustBe("type mismatch"), |
| 593 |
Path: mustBe("DATA"), |
| 594 |
Got: mustBe("[5]interface {}"), |
| 595 |
Expected: mustBe("*[5]interface {}"), |
| 596 |
}) |
| 597 |
}) |
| 598 |
|
| 599 |
t.Run("ints array", func(t *testing.T) { |
| 600 |
type MyArray [5]int |
| 601 |
|
| 602 |
checkOK(t, MyArray{}, td.SuperSliceOf(MyArray{})) |
| 603 |
|
| 604 |
got := MyArray{3: 4} |
| 605 |
checkOK(t, got, td.SuperSliceOf(MyArray{})) |
| 606 |
checkOK(t, got, td.SuperSliceOf(MyArray{3: 4})) |
| 607 |
checkOK(t, got, td.SuperSliceOf(MyArray{}, td.ArrayEntries{3: 4})) |
| 608 |
|
| 609 |
checkError(t, got, |
| 610 |
td.SuperSliceOf(MyArray{}, td.ArrayEntries{1: 666}), |
| 611 |
expectedError{ |
| 612 |
Message: mustBe("values differ"), |
| 613 |
Path: mustBe("DATA[1]"), |
| 614 |
Got: mustBe(`0`), |
| 615 |
Expected: mustBe(`666`), |
| 616 |
}) |
| 617 |
|
| 618 |
// Be lax... |
| 619 |
// Without Lax → error |
| 620 |
checkError(t, got, |
| 621 |
td.SuperSliceOf([5]int{}, td.ArrayEntries{3: 4}), |
| 622 |
expectedError{ |
| 623 |
Message: mustBe("type mismatch"), |
| 624 |
Path: mustBe("DATA"), |
| 625 |
Got: mustBe(`td_test.MyArray`), |
| 626 |
Expected: mustBe(`[5]int`), |
| 627 |
}) |
| 628 |
checkOK(t, got, td.Lax(td.SuperSliceOf([5]int{}, td.ArrayEntries{3: 4}))) |
| 629 |
checkError(t, [5]int{3: 4}, |
| 630 |
td.SuperSliceOf(MyArray{}, td.ArrayEntries{3: 4}), |
| 631 |
expectedError{ |
| 632 |
Message: mustBe("type mismatch"), |
| 633 |
Path: mustBe("DATA"), |
| 634 |
Got: mustBe(`[5]int`), |
| 635 |
Expected: mustBe(`td_test.MyArray`), |
| 636 |
}) |
| 637 |
checkOK(t, [5]int{3: 4}, |
| 638 |
td.Lax(td.SuperSliceOf(MyArray{}, td.ArrayEntries{3: 4}))) |
| 639 |
|
| 640 |
checkError(t, "never tested", |
| 641 |
td.SuperSliceOf(MyArray{}, td.ArrayEntries{8: 34}), |
| 642 |
expectedError{ |
| 643 |
Message: mustBe("bad usage of SuperSliceOf operator"), |
| 644 |
Path: mustBe("DATA"), |
| 645 |
Summary: mustBe("array length is 5, so cannot have #8 expected index"), |
| 646 |
}) |
| 647 |
}) |
| 648 |
|
| 649 |
t.Run("ints slice", func(t *testing.T) { |
| 650 |
type MySlice []int |
| 651 |
|
| 652 |
checkOK(t, MySlice{}, td.SuperSliceOf(MySlice{})) |
| 653 |
checkOK(t, MySlice(nil), td.SuperSliceOf(MySlice{})) |
| 654 |
|
| 655 |
got := MySlice{3: 4} |
| 656 |
|
| 657 |
checkOK(t, got, td.SuperSliceOf(MySlice{}, td.ArrayEntries{3: td.N(5, 1)})) |
| 658 |
checkOK(t, got, td.SuperSliceOf(MySlice{3: 4}, td.ArrayEntries{2: 0})) |
| 659 |
|
| 660 |
checkError(t, got, |
| 661 |
td.SuperSliceOf(MySlice{}, td.ArrayEntries{1: 666}), |
| 662 |
expectedError{ |
| 663 |
Message: mustBe("values differ"), |
| 664 |
Path: mustBe("DATA[1]"), |
| 665 |
Got: mustBe(`0`), |
| 666 |
Expected: mustBe(`666`), |
| 667 |
}) |
| 668 |
checkError(t, got, |
| 669 |
td.SuperSliceOf(MySlice{}, td.ArrayEntries{3: 0}), |
| 670 |
expectedError{ |
| 671 |
Message: mustBe("values differ"), |
| 672 |
Path: mustBe("DATA[3]"), |
| 673 |
Got: mustBe(`4`), |
| 674 |
Expected: mustBe(`0`), |
| 675 |
}) |
| 676 |
checkError(t, got, |
| 677 |
td.SuperSliceOf(MySlice{}, td.ArrayEntries{28: 666}), |
| 678 |
expectedError{ |
| 679 |
Message: mustBe("expected value out of range"), |
| 680 |
Path: mustBe("DATA[28]"), |
| 681 |
Got: mustBe(`<non-existent value>`), |
| 682 |
Expected: mustBe(`666`), |
| 683 |
}) |
| 684 |
checkError(t, got, |
| 685 |
td.SuperSliceOf(MySlice{28: 666}), |
| 686 |
expectedError{ |
| 687 |
Message: mustBe("expected value out of range"), |
| 688 |
Path: mustBe("DATA[28]"), |
| 689 |
Got: mustBe(`<non-existent value>`), |
| 690 |
Expected: mustBe(`666`), |
| 691 |
}) |
| 692 |
|
| 693 |
// Be lax... |
| 694 |
// Without Lax → error |
| 695 |
checkError(t, got, |
| 696 |
td.SuperSliceOf([]int{}, td.ArrayEntries{3: 4}), |
| 697 |
expectedError{ |
| 698 |
Message: mustBe("type mismatch"), |
| 699 |
Path: mustBe("DATA"), |
| 700 |
Got: mustBe(`td_test.MySlice`), |
| 701 |
Expected: mustBe(`[]int`), |
| 702 |
}) |
| 703 |
checkOK(t, got, td.Lax(td.SuperSliceOf([]int{}, td.ArrayEntries{3: 4}))) |
| 704 |
checkError(t, []int{3: 4}, |
| 705 |
td.SuperSliceOf(MySlice{}, td.ArrayEntries{3: 4}), |
| 706 |
expectedError{ |
| 707 |
Message: mustBe("type mismatch"), |
| 708 |
Path: mustBe("DATA"), |
| 709 |
Got: mustBe(`[]int`), |
| 710 |
Expected: mustBe(`td_test.MySlice`), |
| 711 |
}) |
| 712 |
checkOK(t, []int{3: 4}, |
| 713 |
td.Lax(td.SuperSliceOf(MySlice{}, td.ArrayEntries{3: 4}))) |
| 714 |
}) |
| 715 |
|
| 716 |
// |
| 717 |
// Bad usage |
| 718 |
checkError(t, "never tested", |
| 719 |
td.SuperSliceOf("test", nil), |
| 720 |
expectedError{ |
| 721 |
Message: mustBe("bad usage of SuperSliceOf operator"), |
| 722 |
Path: mustBe("DATA"), |
| 723 |
Summary: mustBe("usage: SuperSliceOf(ARRAY|&ARRAY|SLICE|&SLICE, EXPECTED_ENTRIES), but received string as 1st parameter"), |
| 724 |
}) |
| 725 |
|
| 726 |
checkError(t, "never tested", |
| 727 |
td.SuperSliceOf(&MyStruct{}), |
| 728 |
expectedError{ |
| 729 |
Message: mustBe("bad usage of SuperSliceOf operator"), |
| 730 |
Path: mustBe("DATA"), |
| 731 |
Summary: mustBe("usage: SuperSliceOf(ARRAY|&ARRAY|SLICE|&SLICE, EXPECTED_ENTRIES), but received *td_test.MyStruct (ptr) as 1st parameter"), |
| 732 |
}) |
| 733 |
|
| 734 |
checkError(t, []int{0, 1}, |
| 735 |
td.SuperSliceOf([]int{}, td.ArrayEntries{1: "bad"}), |
| 736 |
expectedError{ |
| 737 |
Message: mustBe("type mismatch"), |
| 738 |
Path: mustBe("DATA[1]"), |
| 739 |
Got: mustBe("int"), |
| 740 |
Expected: mustBe("string"), |
| 741 |
}) |
| 742 |
|
| 743 |
checkError(t, "never tested", |
| 744 |
td.SuperSliceOf([]int{12}, td.ArrayEntries{0: 21}), |
| 745 |
expectedError{ |
| 746 |
Message: mustBe("bad usage of SuperSliceOf operator"), |
| 747 |
Path: mustBe("DATA"), |
| 748 |
Summary: mustBe("non zero #0 entry in model already exists in expectedEntries"), |
| 749 |
}) |
| 750 |
|
| 751 |
// Erroneous op |
| 752 |
test.EqualStr(t, td.SuperSliceOf(12).String(), "SuperSliceOf(<ERROR>)") |
| 753 |
} |
| 754 |
|
| 755 |
func TestSuperSliceOfTypeBehind(t *testing.T) { |
| 756 |
type MySlice []int |
| 757 |
|
| 758 |
equalTypes(t, td.SuperSliceOf([]int{}), []int{}) |
| 759 |
equalTypes(t, td.SuperSliceOf(MySlice{}), MySlice{}) |
| 760 |
equalTypes(t, td.SuperSliceOf(&MySlice{}), &MySlice{}) |
| 761 |
|
| 762 |
type MyArray [12]int |
| 763 |
|
| 764 |
equalTypes(t, td.SuperSliceOf([12]int{}), [12]int{}) |
| 765 |
equalTypes(t, td.SuperSliceOf(MyArray{}), MyArray{}) |
| 766 |
equalTypes(t, td.SuperSliceOf(&MyArray{}), &MyArray{}) |
| 767 |
|
| 768 |
// Erroneous op |
| 769 |
equalTypes(t, td.SuperSliceOf(12), nil) |
| 770 |
} |