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