| 1 |
// Copyright (c) 2019, 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 TestKeysValues(t *testing.T) { |
| 17 |
var m map[string]int |
| 18 |
|
| 19 |
// |
| 20 |
t.Run("nil map", func(t *testing.T) { |
| 21 |
checkOK(t, m, td.Keys([]string{})) |
| 22 |
checkOK(t, m, td.Values([]int{})) |
| 23 |
|
| 24 |
checkOK(t, m, td.Keys(td.Empty())) |
| 25 |
checkOK(t, m, td.Values(td.Empty())) |
| 26 |
|
| 27 |
checkError(t, m, td.Keys(td.NotEmpty()), |
| 28 |
expectedError{ |
| 29 |
Message: mustBe("empty"), |
| 30 |
Path: mustBe("keys(DATA)"), |
| 31 |
Expected: mustBe("not empty"), |
| 32 |
}) |
| 33 |
checkError(t, m, td.Values(td.NotEmpty()), |
| 34 |
expectedError{ |
| 35 |
Message: mustBe("empty"), |
| 36 |
Path: mustBe("values(DATA)"), |
| 37 |
Expected: mustBe("not empty"), |
| 38 |
}) |
| 39 |
}) |
| 40 |
|
| 41 |
// |
| 42 |
t.Run("non-nil but empty map", func(t *testing.T) { |
| 43 |
m = map[string]int{} |
| 44 |
checkOK(t, m, td.Keys([]string{})) |
| 45 |
checkOK(t, m, td.Values([]int{})) |
| 46 |
|
| 47 |
checkOK(t, m, td.Keys(td.Empty())) |
| 48 |
checkOK(t, m, td.Values(td.Empty())) |
| 49 |
|
| 50 |
checkError(t, m, td.Keys(td.NotEmpty()), |
| 51 |
expectedError{ |
| 52 |
Message: mustBe("empty"), |
| 53 |
Path: mustBe("keys(DATA)"), |
| 54 |
Expected: mustBe("not empty"), |
| 55 |
}) |
| 56 |
checkError(t, m, td.Values(td.NotEmpty()), |
| 57 |
expectedError{ |
| 58 |
Message: mustBe("empty"), |
| 59 |
Path: mustBe("values(DATA)"), |
| 60 |
Expected: mustBe("not empty"), |
| 61 |
}) |
| 62 |
}) |
| 63 |
|
| 64 |
// |
| 65 |
t.Run("Filled map", func(t *testing.T) { |
| 66 |
m = map[string]int{"a": 1, "b": 2, "c": 3, "d": 4, "e": 5, "f": 6} |
| 67 |
checkOK(t, m, td.Keys([]string{"a", "b", "c", "d", "e", "f"})) |
| 68 |
checkOK(t, m, td.Values([]int{1, 2, 3, 4, 5, 6})) |
| 69 |
|
| 70 |
checkOK(t, m, td.Keys(td.Bag("a", "b", "c", "d", "e", "f"))) |
| 71 |
checkOK(t, m, td.Values(td.Bag(1, 2, 3, 4, 5, 6))) |
| 72 |
|
| 73 |
checkOK(t, m, td.Keys(td.ArrayEach(td.Between("a", "f")))) |
| 74 |
checkOK(t, m, td.Values(td.ArrayEach(td.Between(1, 6)))) |
| 75 |
|
| 76 |
checkError(t, m, td.Keys(td.Empty()), |
| 77 |
expectedError{ |
| 78 |
Message: mustBe("not empty"), |
| 79 |
Path: mustBe("keys(DATA)"), |
| 80 |
Expected: mustBe("empty"), |
| 81 |
}) |
| 82 |
checkError(t, m, td.Values(td.Empty()), |
| 83 |
expectedError{ |
| 84 |
Message: mustBe("not empty"), |
| 85 |
Path: mustBe("values(DATA)"), |
| 86 |
Expected: mustBe("empty"), |
| 87 |
}) |
| 88 |
}) |
| 89 |
|
| 90 |
// |
| 91 |
t.Run("Errors", func(t *testing.T) { |
| 92 |
checkError(t, nil, td.Keys([]int{1, 2, 3}), |
| 93 |
expectedError{ |
| 94 |
Message: mustBe("values differ"), |
| 95 |
Path: mustBe("DATA"), |
| 96 |
Got: mustBe("nil"), |
| 97 |
Expected: mustContain("keys=([]int)"), |
| 98 |
}) |
| 99 |
checkError(t, nil, td.Values([]int{1, 2, 3}), |
| 100 |
expectedError{ |
| 101 |
Message: mustBe("values differ"), |
| 102 |
Path: mustBe("DATA"), |
| 103 |
Got: mustBe("nil"), |
| 104 |
Expected: mustContain("values=([]int)"), |
| 105 |
}) |
| 106 |
|
| 107 |
checkError(t, nil, td.Keys(td.Empty()), |
| 108 |
expectedError{ |
| 109 |
Message: mustBe("values differ"), |
| 110 |
Path: mustBe("DATA"), |
| 111 |
Got: mustBe("nil"), |
| 112 |
Expected: mustBe("keys: Empty()"), |
| 113 |
}) |
| 114 |
checkError(t, nil, td.Values(td.Empty()), |
| 115 |
expectedError{ |
| 116 |
Message: mustBe("values differ"), |
| 117 |
Path: mustBe("DATA"), |
| 118 |
Got: mustBe("nil"), |
| 119 |
Expected: mustBe("values: Empty()"), |
| 120 |
}) |
| 121 |
|
| 122 |
checkError(t, 123, td.Keys(td.Empty()), |
| 123 |
expectedError{ |
| 124 |
Message: mustBe("bad kind"), |
| 125 |
Path: mustBe("DATA"), |
| 126 |
Got: mustBe("int"), |
| 127 |
Expected: mustBe("map"), |
| 128 |
}) |
| 129 |
checkError(t, 123, td.Values(td.Empty()), |
| 130 |
expectedError{ |
| 131 |
Message: mustBe("bad kind"), |
| 132 |
Path: mustBe("DATA"), |
| 133 |
Got: mustBe("int"), |
| 134 |
Expected: mustBe("map"), |
| 135 |
}) |
| 136 |
}) |
| 137 |
|
| 138 |
// |
| 139 |
t.Run("Bad usage", func(t *testing.T) { |
| 140 |
checkError(t, "never tested", |
| 141 |
td.Keys(12), |
| 142 |
expectedError{ |
| 143 |
Message: mustBe("bad usage of Keys operator"), |
| 144 |
Path: mustBe("DATA"), |
| 145 |
Summary: mustBe("usage: Keys(TESTDEEP_OPERATOR|SLICE), but received int as 1st parameter"), |
| 146 |
}) |
| 147 |
|
| 148 |
checkError(t, "never tested", |
| 149 |
td.Values(12), |
| 150 |
expectedError{ |
| 151 |
Message: mustBe("bad usage of Values operator"), |
| 152 |
Path: mustBe("DATA"), |
| 153 |
Summary: mustBe("usage: Values(TESTDEEP_OPERATOR|SLICE), but received int as 1st parameter"), |
| 154 |
}) |
| 155 |
}) |
| 156 |
|
| 157 |
// Erroneous op |
| 158 |
test.EqualStr(t, td.Keys(12).String(), "Keys(<ERROR>)") |
| 159 |
test.EqualStr(t, td.Values(12).String(), "Values(<ERROR>)") |
| 160 |
} |
| 161 |
|
| 162 |
func TestKeysValuesTypeBehind(t *testing.T) { |
| 163 |
equalTypes(t, td.Keys([]string{}), nil) |
| 164 |
equalTypes(t, td.Values([]string{}), nil) |
| 165 |
|
| 166 |
// Erroneous op |
| 167 |
equalTypes(t, td.Keys(12), nil) |
| 168 |
equalTypes(t, td.Values(12), nil) |
| 169 |
} |