| 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 TestArrayEach(t *testing.T) { |
| 17 |
type MyArray [3]int |
| 18 |
type MyEmptyArray [0]int |
| 19 |
type MySlice []int |
| 20 |
|
| 21 |
checkOKForEach(t, |
| 22 |
[]any{ |
| 23 |
[...]int{4, 4, 4}, |
| 24 |
[]int{4, 4, 4}, |
| 25 |
&[...]int{4, 4, 4}, |
| 26 |
&[]int{4, 4, 4}, |
| 27 |
MyArray{4, 4, 4}, |
| 28 |
MySlice{4, 4, 4}, |
| 29 |
&MyArray{4, 4, 4}, |
| 30 |
&MySlice{4, 4, 4}, |
| 31 |
}, |
| 32 |
td.ArrayEach(4)) |
| 33 |
|
| 34 |
// Empty slice/array |
| 35 |
checkOKForEach(t, |
| 36 |
[]any{ |
| 37 |
[0]int{}, |
| 38 |
[]int{}, |
| 39 |
&[0]int{}, |
| 40 |
&[]int{}, |
| 41 |
MyEmptyArray{}, |
| 42 |
MySlice{}, |
| 43 |
&MyEmptyArray{}, |
| 44 |
&MySlice{}, |
| 45 |
// nil cases |
| 46 |
([]int)(nil), |
| 47 |
MySlice(nil), |
| 48 |
}, |
| 49 |
td.ArrayEach(4)) |
| 50 |
|
| 51 |
checkError(t, (*MyArray)(nil), td.ArrayEach(4), |
| 52 |
expectedError{ |
| 53 |
Message: mustBe("nil pointer"), |
| 54 |
Path: mustBe("DATA"), |
| 55 |
Got: mustBe("nil *array (*td_test.MyArray type)"), |
| 56 |
Expected: mustBe("non-nil *slice OR *array"), |
| 57 |
}) |
| 58 |
checkError(t, (*MySlice)(nil), td.ArrayEach(4), |
| 59 |
expectedError{ |
| 60 |
Message: mustBe("nil pointer"), |
| 61 |
Path: mustBe("DATA"), |
| 62 |
Got: mustBe("nil *slice (*td_test.MySlice type)"), |
| 63 |
Expected: mustBe("non-nil *slice OR *array"), |
| 64 |
}) |
| 65 |
|
| 66 |
checkOKForEach(t, |
| 67 |
[]any{ |
| 68 |
[...]int{20, 22, 29}, |
| 69 |
[]int{20, 22, 29}, |
| 70 |
MyArray{20, 22, 29}, |
| 71 |
MySlice{20, 22, 29}, |
| 72 |
&MyArray{20, 22, 29}, |
| 73 |
&MySlice{20, 22, 29}, |
| 74 |
}, |
| 75 |
td.ArrayEach(td.Between(20, 30))) |
| 76 |
|
| 77 |
checkError(t, nil, td.ArrayEach(4), |
| 78 |
expectedError{ |
| 79 |
Message: mustBe("nil value"), |
| 80 |
Path: mustBe("DATA"), |
| 81 |
Got: mustBe("nil"), |
| 82 |
Expected: mustBe("slice OR array OR *slice OR *array"), |
| 83 |
}) |
| 84 |
|
| 85 |
checkErrorForEach(t, |
| 86 |
[]any{ |
| 87 |
[...]int{4, 5, 4}, |
| 88 |
[]int{4, 5, 4}, |
| 89 |
MyArray{4, 5, 4}, |
| 90 |
MySlice{4, 5, 4}, |
| 91 |
&MyArray{4, 5, 4}, |
| 92 |
&MySlice{4, 5, 4}, |
| 93 |
}, |
| 94 |
td.ArrayEach(4), |
| 95 |
expectedError{ |
| 96 |
Message: mustBe("values differ"), |
| 97 |
Path: mustBe("DATA[1]"), |
| 98 |
Got: mustBe("5"), |
| 99 |
Expected: mustBe("4"), |
| 100 |
}) |
| 101 |
|
| 102 |
checkError(t, 666, td.ArrayEach(4), |
| 103 |
expectedError{ |
| 104 |
Message: mustBe("bad kind"), |
| 105 |
Path: mustBe("DATA"), |
| 106 |
Got: mustBe("int"), |
| 107 |
Expected: mustBe("slice OR array OR *slice OR *array"), |
| 108 |
}) |
| 109 |
num := 666 |
| 110 |
checkError(t, &num, td.ArrayEach(4), |
| 111 |
expectedError{ |
| 112 |
Message: mustBe("bad kind"), |
| 113 |
Path: mustBe("DATA"), |
| 114 |
Got: mustBe("*int"), |
| 115 |
Expected: mustBe("slice OR array OR *slice OR *array"), |
| 116 |
}) |
| 117 |
|
| 118 |
checkOK(t, []any{nil, nil, nil}, td.ArrayEach(nil)) |
| 119 |
checkError(t, []any{nil, nil, nil, 66}, td.ArrayEach(nil), |
| 120 |
expectedError{ |
| 121 |
Message: mustBe("values differ"), |
| 122 |
Path: mustBe("DATA[3]"), |
| 123 |
Got: mustBe("66"), |
| 124 |
Expected: mustBe("nil"), |
| 125 |
}) |
| 126 |
|
| 127 |
// |
| 128 |
// String |
| 129 |
test.EqualStr(t, td.ArrayEach(4).String(), "ArrayEach(4)") |
| 130 |
test.EqualStr(t, td.ArrayEach(td.All(1, 2)).String(), |
| 131 |
`ArrayEach(All(1, |
| 132 |
2))`) |
| 133 |
} |
| 134 |
|
| 135 |
func TestArrayEachTypeBehind(t *testing.T) { |
| 136 |
equalTypes(t, td.ArrayEach(6), nil) |
| 137 |
} |