| 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 |
"errors" |
| 11 |
"fmt" |
| 12 |
"reflect" |
| 13 |
"testing" |
| 14 |
|
| 15 |
"github.com/maxatome/go-testdeep/td" |
| 16 |
) |
| 17 |
|
| 18 |
func TestContains(t *testing.T) { |
| 19 |
type ( |
| 20 |
MySlice []int |
| 21 |
MyArray [3]int |
| 22 |
MyMap map[string]int |
| 23 |
MyString string |
| 24 |
) |
| 25 |
|
| 26 |
for idx, got := range []any{ |
| 27 |
[]int{12, 34, 28}, |
| 28 |
MySlice{12, 34, 28}, |
| 29 |
[...]int{12, 34, 28}, |
| 30 |
MyArray{12, 34, 28}, |
| 31 |
map[string]int{"foo": 12, "bar": 34, "zip": 28}, |
| 32 |
MyMap{"foo": 12, "bar": 34, "zip": 28}, |
| 33 |
} { |
| 34 |
testName := fmt.Sprintf("#%d: got=%v", idx, got) |
| 35 |
|
| 36 |
checkOK(t, got, td.Contains(34), testName) |
| 37 |
checkOK(t, got, td.Contains(td.Between(30, 35)), testName) |
| 38 |
|
| 39 |
checkError(t, got, td.Contains(35), |
| 40 |
expectedError{ |
| 41 |
Message: mustBe("does not contain"), |
| 42 |
Path: mustBe("DATA"), |
| 43 |
Got: mustContain("34"), // as well as other items in fact... |
| 44 |
Expected: mustBe("Contains(35)"), |
| 45 |
}, testName) |
| 46 |
|
| 47 |
// Lax |
| 48 |
checkOK(t, got, td.Lax(td.Contains(float64(34))), testName) |
| 49 |
} |
| 50 |
|
| 51 |
for idx, got := range []any{ |
| 52 |
"foobar", |
| 53 |
MyString("foobar"), |
| 54 |
} { |
| 55 |
testName := fmt.Sprintf("#%d: got=%v", idx, got) |
| 56 |
|
| 57 |
checkOK(t, got, td.Contains(td.Between('n', 'p')), testName) |
| 58 |
|
| 59 |
checkError(t, got, td.Contains(td.Between('y', 'z')), |
| 60 |
expectedError{ |
| 61 |
Message: mustBe("does not contain"), |
| 62 |
Path: mustBe("DATA"), |
| 63 |
Got: mustContain(`"foobar"`), // as well as other items in fact... |
| 64 |
Expected: mustBe(fmt.Sprintf("Contains((int32) %d ≤ got ≤ (int32) %d)", 'y', 'z')), |
| 65 |
}, testName) |
| 66 |
} |
| 67 |
|
| 68 |
checkError(t, [...]any{1, 2, 3}, td.Contains(td.JSON("{")), |
| 69 |
expectedError{ |
| 70 |
Message: mustBe("bad usage of JSON operator"), |
| 71 |
Path: mustBe(`DATA`), |
| 72 |
Summary: mustContain("JSON unmarshal error"), |
| 73 |
Under: mustContain("under operator JSON at "), |
| 74 |
}, |
| 75 |
"erroneous operator expected") |
| 76 |
|
| 77 |
checkError(t, map[string]any{"foo": 123}, td.Contains(td.JSON("{")), |
| 78 |
expectedError{ |
| 79 |
Message: mustBe("bad usage of JSON operator"), |
| 80 |
Path: mustBe(`DATA`), |
| 81 |
Summary: mustContain("JSON unmarshal error"), |
| 82 |
Under: mustContain("under operator JSON at "), |
| 83 |
}, |
| 84 |
"erroneous operator expected") |
| 85 |
} |
| 86 |
|
| 87 |
// nil case. |
| 88 |
func TestContainsNil(t *testing.T) { |
| 89 |
type ( |
| 90 |
MyPtrSlice []*int |
| 91 |
MyPtrArray [3]*int |
| 92 |
MyPtrMap map[string]*int |
| 93 |
) |
| 94 |
|
| 95 |
num := 12345642 |
| 96 |
for idx, got := range []any{ |
| 97 |
[]*int{&num, nil}, |
| 98 |
MyPtrSlice{&num, nil}, |
| 99 |
[...]*int{&num, nil}, |
| 100 |
MyPtrArray{&num}, |
| 101 |
map[string]*int{"foo": &num, "bar": nil}, |
| 102 |
MyPtrMap{"foo": &num, "bar": nil}, |
| 103 |
} { |
| 104 |
testName := fmt.Sprintf("#%d: got=%v", idx, got) |
| 105 |
|
| 106 |
checkOK(t, got, td.Contains(nil), testName) |
| 107 |
checkOK(t, got, td.Contains((*int)(nil)), testName) |
| 108 |
checkOK(t, got, td.Contains(td.Nil()), testName) |
| 109 |
checkOK(t, got, td.Contains(td.NotNil()), testName) |
| 110 |
|
| 111 |
checkError(t, got, td.Contains((*uint8)(nil)), |
| 112 |
expectedError{ |
| 113 |
Message: mustBe("does not contain"), |
| 114 |
Path: mustBe("DATA"), |
| 115 |
Got: mustContain("12345642"), |
| 116 |
Expected: mustBe("Contains((*uint8)(<nil>))"), |
| 117 |
}, testName) |
| 118 |
} |
| 119 |
|
| 120 |
for idx, got := range []any{ |
| 121 |
[]any{nil, 12345642}, |
| 122 |
[]func(){nil, func() {}}, |
| 123 |
[][]int{{}, nil}, |
| 124 |
[...]any{nil, 12345642}, |
| 125 |
[...]func(){nil, func() {}}, |
| 126 |
[...][]int{{}, nil}, |
| 127 |
map[bool]any{true: nil, false: 12345642}, |
| 128 |
map[bool]func(){true: nil, false: func() {}}, |
| 129 |
map[bool][]int{true: {}, false: nil}, |
| 130 |
} { |
| 131 |
testName := fmt.Sprintf("#%d: got=%v", idx, got) |
| 132 |
|
| 133 |
checkOK(t, got, td.Contains(nil), testName) |
| 134 |
checkOK(t, got, td.Contains(td.Nil()), testName) |
| 135 |
checkOK(t, got, td.Contains(td.NotNil()), testName) |
| 136 |
} |
| 137 |
|
| 138 |
for idx, got := range []any{ |
| 139 |
[]int{1, 2, 3}, |
| 140 |
[...]int{1, 2, 3}, |
| 141 |
map[string]int{"foo": 12, "bar": 34, "zip": 28}, |
| 142 |
} { |
| 143 |
testName := fmt.Sprintf("#%d: got=%v", idx, got) |
| 144 |
|
| 145 |
checkError(t, got, td.Contains(nil), |
| 146 |
expectedError{ |
| 147 |
Message: mustBe("does not contain"), |
| 148 |
Path: mustBe("DATA"), |
| 149 |
// Got |
| 150 |
Expected: mustBe("Contains(nil)"), |
| 151 |
}, testName) |
| 152 |
} |
| 153 |
|
| 154 |
checkError(t, "foobar", td.Contains(nil), |
| 155 |
expectedError{ |
| 156 |
Message: mustBe("cannot check contains"), |
| 157 |
Path: mustBe("DATA"), |
| 158 |
Got: mustBe("string"), |
| 159 |
Expected: mustBe("Contains(nil)"), |
| 160 |
}) |
| 161 |
|
| 162 |
// Caught by deepValueEqual, before Match() call |
| 163 |
checkError(t, nil, td.Contains(nil), |
| 164 |
expectedError{ |
| 165 |
Message: mustBe("values differ"), |
| 166 |
Path: mustBe("DATA"), |
| 167 |
Got: mustBe("nil"), |
| 168 |
Expected: mustBe("Contains(nil)"), |
| 169 |
}) |
| 170 |
} |
| 171 |
|
| 172 |
func TestContainsString(t *testing.T) { |
| 173 |
type MyString string |
| 174 |
|
| 175 |
for idx, got := range []any{ |
| 176 |
"pipo bingo", |
| 177 |
MyString("pipo bingo"), |
| 178 |
[]byte("pipo bingo"), |
| 179 |
errors.New("pipo bingo"), // error interface |
| 180 |
MyStringer{}, // fmt.Stringer interface |
| 181 |
} { |
| 182 |
testName := fmt.Sprintf("#%d: got=%v", idx, got) |
| 183 |
|
| 184 |
checkOK(t, got, td.Contains("pipo"), testName) |
| 185 |
checkOK(t, got, td.Contains("po bi"), testName) |
| 186 |
checkOK(t, got, td.Contains("bingo"), testName) |
| 187 |
|
| 188 |
checkOK(t, got, td.Contains([]byte("pipo")), testName) |
| 189 |
checkOK(t, got, td.Contains([]byte("po bi")), testName) |
| 190 |
checkOK(t, got, td.Contains([]byte("bingo")), testName) |
| 191 |
|
| 192 |
checkOK(t, got, td.Contains('o'), testName) |
| 193 |
checkOK(t, got, td.Contains(byte('o')), testName) |
| 194 |
|
| 195 |
checkOK(t, got, td.Contains(""), testName) |
| 196 |
checkOK(t, got, td.Contains([]byte{}), testName) |
| 197 |
|
| 198 |
if _, ok := got.([]byte); ok { |
| 199 |
checkOK(t, got, |
| 200 |
td.Contains(td.Code(func(b byte) bool { return b == 'o' })), |
| 201 |
testName) |
| 202 |
} else { |
| 203 |
checkOK(t, got, |
| 204 |
td.Contains(td.Code(func(r rune) bool { return r == 'o' })), |
| 205 |
testName) |
| 206 |
} |
| 207 |
|
| 208 |
checkError(t, got, td.Contains("zip"), |
| 209 |
expectedError{ |
| 210 |
Message: mustBe("does not contain"), |
| 211 |
Path: mustBe("DATA"), |
| 212 |
Got: mustContain(`pipo bingo`), |
| 213 |
Expected: mustMatch(`^Contains\(.*"zip"`), |
| 214 |
}) |
| 215 |
|
| 216 |
checkError(t, got, td.Contains([]byte("zip")), |
| 217 |
expectedError{ |
| 218 |
Message: mustBe("does not contain"), |
| 219 |
Path: mustBe("DATA"), |
| 220 |
Got: mustContain(`pipo bingo`), |
| 221 |
Expected: mustMatch(`^(?s)Contains\(.*zip`), |
| 222 |
}) |
| 223 |
|
| 224 |
checkError(t, got, td.Contains('z'), |
| 225 |
expectedError{ |
| 226 |
Message: mustBe("does not contain"), |
| 227 |
Path: mustBe("DATA"), |
| 228 |
Got: mustContain(`pipo bingo`), |
| 229 |
Expected: mustBe(`Contains((int32) 122)`), |
| 230 |
}) |
| 231 |
|
| 232 |
checkError(t, got, td.Contains(byte('z')), |
| 233 |
expectedError{ |
| 234 |
Message: mustBe("does not contain"), |
| 235 |
Path: mustBe("DATA"), |
| 236 |
Got: mustContain(`pipo bingo`), |
| 237 |
Expected: mustBe(`Contains((uint8) 122)`), |
| 238 |
}) |
| 239 |
|
| 240 |
checkError(t, got, td.Contains(12), |
| 241 |
expectedError{ |
| 242 |
Message: mustBe("cannot check contains"), |
| 243 |
Path: mustBe("DATA"), |
| 244 |
Got: mustBe(reflect.TypeOf(got).String()), |
| 245 |
Expected: mustBe("int"), |
| 246 |
}) |
| 247 |
|
| 248 |
checkError(t, got, td.Contains([]int{1, 2, 3}), |
| 249 |
expectedError{ |
| 250 |
Message: mustBe("cannot check contains"), |
| 251 |
Path: mustBe("DATA"), |
| 252 |
Got: mustBe(reflect.TypeOf(got).String()), |
| 253 |
Expected: mustBe("[]int"), |
| 254 |
}) |
| 255 |
|
| 256 |
// Lax |
| 257 |
checkOK(t, got, |
| 258 |
td.Lax(td.Contains(td.Code(func(b int) bool { return b == 'o' }))), |
| 259 |
testName) |
| 260 |
} |
| 261 |
|
| 262 |
checkError(t, 12, td.Contains("bar"), |
| 263 |
expectedError{ |
| 264 |
Message: mustBe("bad type"), |
| 265 |
Path: mustBe("DATA"), |
| 266 |
Got: mustBe("int"), |
| 267 |
Expected: mustBe("string (convertible) OR []byte (convertible) OR fmt.Stringer OR error"), |
| 268 |
}) |
| 269 |
|
| 270 |
checkError(t, "pipo", td.Contains(td.Code(func(x int) bool { return true })), |
| 271 |
expectedError{ |
| 272 |
Message: mustBe("Code operator has to match rune in string, but it does not"), |
| 273 |
Path: mustBe("DATA"), |
| 274 |
Got: mustBe("int"), |
| 275 |
Expected: mustBe("rune"), |
| 276 |
}) |
| 277 |
|
| 278 |
checkError(t, "pipo", td.Contains(td.JSON("{")), |
| 279 |
expectedError{ |
| 280 |
Message: mustBe("bad usage of JSON operator"), |
| 281 |
Path: mustBe(`DATA`), |
| 282 |
Summary: mustContain("JSON unmarshal error"), |
| 283 |
Under: mustContain("under operator JSON at "), |
| 284 |
}, |
| 285 |
"erroneous operator expected") |
| 286 |
} |
| 287 |
|
| 288 |
func TestContainsSlice(t *testing.T) { |
| 289 |
got := []int{1, 2, 3, 4, 5, 6} |
| 290 |
|
| 291 |
// Empty slice is always OK |
| 292 |
checkOK(t, got, td.Contains([]int{})) |
| 293 |
|
| 294 |
// Expected length > got length |
| 295 |
checkError(t, got, td.Contains([]int{1, 2, 3, 4, 5, 6, 7}), |
| 296 |
expectedError{ |
| 297 |
Message: mustBe("does not contain"), |
| 298 |
Path: mustBe("DATA"), |
| 299 |
Got: mustContain(`([]int) (len=6)`), |
| 300 |
Expected: mustContain(`Contains(([]int) (len=7)`), |
| 301 |
}) |
| 302 |
|
| 303 |
// Same length |
| 304 |
checkOK(t, got, td.Contains([]int{1, 2, 3, 4, 5, 6})) |
| 305 |
checkError(t, got, td.Contains([]int{8, 8, 8, 8, 8, 8}), |
| 306 |
expectedError{ |
| 307 |
Message: mustBe("does not contain"), |
| 308 |
Path: mustBe("DATA"), |
| 309 |
Got: mustContain(`([]int) (len=6)`), |
| 310 |
Expected: mustContain(`Contains(([]int) (len=6)`), |
| 311 |
}) |
| 312 |
|
| 313 |
checkOK(t, got, td.Contains([]int{1, 2, 3})) |
| 314 |
checkOK(t, got, td.Contains([]int{3, 4, 5})) |
| 315 |
checkOK(t, got, td.Contains([]int{4, 5, 6})) |
| 316 |
|
| 317 |
checkError(t, got, td.Contains([]int{8, 8, 8}), |
| 318 |
expectedError{ |
| 319 |
Message: mustBe("does not contain"), |
| 320 |
Path: mustBe("DATA"), |
| 321 |
Got: mustContain(`([]int) (len=6)`), |
| 322 |
Expected: mustContain(`Contains(([]int) (len=3)`), |
| 323 |
}) |
| 324 |
|
| 325 |
checkError(t, []any{1, 2, 3}, td.Contains([]any{1, td.JSON("{")}), |
| 326 |
expectedError{ |
| 327 |
Message: mustBe("bad usage of JSON operator"), |
| 328 |
Path: mustMatch(`DATA\[1\]`), // always without .Iface |
| 329 |
Summary: mustContain("JSON unmarshal error"), |
| 330 |
Under: mustContain("under operator JSON at "), |
| 331 |
}, |
| 332 |
"erroneous operator expected") |
| 333 |
} |
| 334 |
|
| 335 |
func TestContainsTypeBehind(t *testing.T) { |
| 336 |
equalTypes(t, td.Contains("x"), nil) |
| 337 |
} |