github.com/Psiphon-Inc/goarista@v0.0.0-20160825065156-d002785f4c67/test/data_test.go (about) 1 // Copyright (C) 2015 Arista Networks, Inc. 2 // Use of this source code is governed by the Apache License 2.0 3 // that can be found in the COPYING file. 4 5 package test 6 7 import ( 8 "testing" 9 10 "github.com/aristanetworks/goarista/key" 11 ) 12 13 type builtinCompare struct { 14 a uint32 15 b string 16 } 17 18 type complexCompare struct { 19 m map[builtinCompare]int8 20 p *complexCompare 21 } 22 23 type partialCompare struct { 24 a uint32 25 b string `deepequal:"ignore"` 26 } 27 28 type deepEqualTestCase struct { 29 a, b interface{} 30 diff string 31 } 32 33 type code int32 34 35 type message string 36 37 func getDeepEqualTests(t *testing.T) []deepEqualTestCase { 38 var deepEqualNullMapString map[string]interface{} 39 recursive := &complexCompare{} 40 recursive.p = recursive 41 return []deepEqualTestCase{{ 42 a: nil, 43 b: nil, 44 }, { 45 a: uint8(5), 46 b: uint8(5), 47 }, { 48 a: nil, 49 b: uint8(5), 50 diff: "expected nil but got a uint8: 0x5", 51 }, { 52 a: uint8(5), 53 b: nil, 54 diff: "expected a uint8 (0x5) but got nil", 55 }, { 56 a: uint16(1), 57 b: uint16(2), 58 diff: "uint16(1) != uint16(2)", 59 }, { 60 a: int8(1), 61 b: int16(1), 62 diff: "expected a int8 but got a int16", 63 }, { 64 a: true, 65 b: true, 66 }, { 67 a: float32(3.1415), 68 b: float32(3.1415), 69 }, { 70 a: float32(3.1415), 71 b: float32(3.1416), 72 diff: "float32(3.1415) != float32(3.1416)", 73 }, { 74 a: float64(3.14159265), 75 b: float64(3.14159265), 76 }, { 77 a: float64(3.14159265), 78 b: float64(3.14159266), 79 diff: "float64(3.14159265) != float64(3.14159266)", 80 }, { 81 a: deepEqualNullMapString, 82 b: deepEqualNullMapString, 83 }, { 84 a: &deepEqualNullMapString, 85 b: &deepEqualNullMapString, 86 }, { 87 a: deepEqualNullMapString, 88 b: &deepEqualNullMapString, 89 diff: "expected a map[string]interface {} but got a *map[string]interface {}", 90 }, { 91 a: &deepEqualNullMapString, 92 b: deepEqualNullMapString, 93 diff: "expected a *map[string]interface {} but got a map[string]interface {}", 94 }, { 95 a: map[string]interface{}{"a": uint32(42)}, 96 b: map[string]interface{}{"a": uint32(42)}, 97 }, { 98 a: map[string]interface{}{"a": int32(42)}, 99 b: map[string]interface{}{"a": int32(51)}, 100 diff: `for key "a" in map, values are different: int32(42) != int32(51)`, 101 }, { 102 a: map[string]interface{}{"a": uint32(42)}, 103 b: map[string]interface{}{}, 104 diff: `Maps have different size: 1 != 0 (missing key: "a")`, 105 }, { 106 a: map[string]interface{}{}, 107 b: map[string]interface{}{"a": uint32(42)}, 108 diff: `Maps have different size: 0 != 1 (extra key: "a")`, 109 }, { 110 a: map[string]interface{}{"a": uint64(42), "b": "extra"}, 111 b: map[string]interface{}{"a": uint64(42)}, 112 diff: `Maps have different size: 2 != 1 (missing key: "b")`, 113 }, { 114 a: map[string]interface{}{"a": uint64(42)}, 115 b: map[string]interface{}{"a": uint64(42), "b": "extra"}, 116 diff: `Maps have different size: 1 != 2 (extra key: "b")`, 117 }, { 118 a: map[uint64]interface{}{uint64(42): "foo"}, 119 b: map[uint64]interface{}{uint64(42): "foo"}, 120 }, { 121 a: map[uint64]interface{}{uint64(42): "foo"}, 122 b: map[uint64]interface{}{uint64(51): "foo"}, 123 diff: "key uint64(42) in map is missing in the actual map", 124 }, { 125 a: map[uint64]interface{}{uint64(42): "foo"}, 126 b: map[uint64]interface{}{uint64(42): "foo", uint64(51): "bar"}, 127 diff: `Maps have different size: 1 != 2 (extra key: uint64(51))`, 128 }, { 129 a: map[uint64]interface{}{uint64(42): "foo"}, 130 b: map[interface{}]interface{}{uint32(42): "foo"}, 131 diff: "expected a map[uint64]interface {} but got a map[interface {}]interface {}", 132 }, { 133 a: map[interface{}]interface{}{"a": uint32(42)}, 134 b: map[string]interface{}{"a": uint32(42)}, 135 diff: "expected a map[interface {}]interface {} but got a map[string]interface {}", 136 }, { 137 a: map[interface{}]interface{}{}, 138 b: map[interface{}]interface{}{}, 139 }, { 140 a: &map[interface{}]interface{}{}, 141 b: &map[interface{}]interface{}{}, 142 }, { 143 a: map[interface{}]interface{}{ 144 &map[string]interface{}{"a": "foo", "b": int16(8)}: "foo"}, 145 b: map[interface{}]interface{}{ 146 &map[string]interface{}{"a": "foo", "b": int16(8)}: "foo"}, 147 }, { 148 a: map[interface{}]interface{}{ 149 &map[string]interface{}{"a": "foo", "b": uint32(8)}: "foo"}, 150 b: map[interface{}]interface{}{ 151 &map[string]interface{}{"a": "foo", "b": uint32(8)}: "fox"}, 152 diff: `for complex key *map[string]interface {}{"a":"foo", "b":uint32(8)}` + 153 ` in map, values are different: string(foo) != string(fox)`, 154 }, { 155 a: map[interface{}]interface{}{ 156 &map[string]interface{}{"a": "foo", "b": uint32(8)}: "foo"}, 157 b: map[interface{}]interface{}{ 158 &map[string]interface{}{"a": "foo", "b": uint32(5)}: "foo"}, 159 diff: `complex key *map[string]interface {}{"a":"foo", "b":uint32(8)}` + 160 ` in map is missing in the actual map`, 161 }, { 162 a: map[interface{}]interface{}{ 163 &map[string]interface{}{"a": "foo", "b": uint32(8)}: "foo"}, 164 b: map[interface{}]interface{}{ 165 &map[string]interface{}{"a": "foo"}: "foo"}, 166 diff: `complex key *map[string]interface {}{"a":"foo", "b":uint32(8)}` + 167 ` in map is missing in the actual map`, 168 }, { 169 a: map[interface{}]interface{}{ 170 &map[string]interface{}{"a": "foo", "b": int16(8)}: "foo", 171 &map[string]interface{}{"a": "foo", "b": int8(8)}: "foo", 172 }, 173 b: map[interface{}]interface{}{ 174 &map[string]interface{}{"a": "foo", "b": int16(8)}: "foo", 175 &map[string]interface{}{"a": "foo", "b": int8(8)}: "foo", 176 }, 177 }, { 178 a: map[interface{}]interface{}{ 179 &map[string]interface{}{"a": "foo", "b": int16(8)}: "foo", 180 &map[string]interface{}{"a": "foo", "b": int8(8)}: "foo", 181 }, 182 b: map[interface{}]interface{}{ 183 &map[string]interface{}{"a": "foo", "b": int16(8)}: "foo", 184 &map[string]interface{}{"a": "foo", "b": int8(5)}: "foo", 185 }, 186 diff: `complex key *map[string]interface {}{"a":"foo", "b":int8(8)}` + 187 ` in map is missing in the actual map`, 188 }, { 189 a: map[interface{}]interface{}{ 190 &map[string]interface{}{"a": "foo", "b": int16(8)}: "foo", 191 &map[string]interface{}{"a": "foo", "b": int8(8)}: "foo", 192 }, 193 b: map[interface{}]interface{}{ 194 &map[string]interface{}{"a": "foo", "b": int16(8)}: "foo", 195 &map[string]interface{}{"a": "foo", "b": int32(8)}: "foo", 196 }, 197 diff: `complex key *map[string]interface {}{"a":"foo", "b":int8(8)}` + 198 ` in map is missing in the actual map`, 199 }, { 200 a: map[interface{}]interface{}{ 201 &map[string]interface{}{"a": "foo", "b": int16(8)}: "foo", 202 &map[string]interface{}{"a": "foo", "b": int8(8)}: "foo", 203 }, 204 b: map[interface{}]interface{}{ 205 &map[string]interface{}{"a": "foo", "b": int16(8)}: "foo", 206 }, 207 diff: `Maps have different size: 2 != 1` + 208 ` (extra key: *map[string]interface {}{"a":"foo", "b":int16(8)},` + 209 ` missing key: *map[string]interface {}{"a":"foo", "b":int16(8)},` + 210 ` missing key: *map[string]interface {}{"a":"foo", "b":int8(8)})`, 211 }, { 212 a: map[interface{}]interface{}{ 213 &map[string]interface{}{"a": "foo", "b": int16(8)}: "foo", 214 }, 215 b: map[interface{}]interface{}{ 216 &map[string]interface{}{"a": "foo", "b": int16(8)}: "foo", 217 &map[string]interface{}{"a": "foo", "b": int8(8)}: "foo", 218 }, 219 diff: `Maps have different size: 1 != 2` + 220 ` (extra key: *map[string]interface {}{"a":"foo", "b":int16(8)},` + 221 ` extra key: *map[string]interface {}{"a":"foo", "b":int8(8)},` + 222 ` missing key: *map[string]interface {}{"a":"foo", "b":int16(8)})`, 223 }, { 224 a: []string{}, 225 b: []string{}, 226 }, { 227 a: []string{"foo", "bar"}, 228 b: []string{"foo", "bar"}, 229 }, { 230 a: []string{"foo", "bar"}, 231 b: []string{"foo"}, 232 diff: "Expected an array of size 2 but got 1", 233 }, { 234 a: []string{"foo"}, 235 b: []string{"foo", "bar"}, 236 diff: "Expected an array of size 1 but got 2", 237 }, { 238 a: []string{"foo", "bar"}, 239 b: []string{"bar", "foo"}, 240 diff: `In arrays, values are different at index 0:` + 241 ` string(foo) != string(bar)`, 242 }, { 243 a: &[]string{}, 244 b: []string{}, 245 diff: "expected a *[]string but got a []string", 246 }, { 247 a: &[]string{}, 248 b: &[]string{}, 249 }, { 250 a: &[]string{"foo", "bar"}, 251 b: &[]string{"foo", "bar"}, 252 }, { 253 a: &[]string{"foo", "bar"}, 254 b: &[]string{"foo"}, 255 diff: "Expected an array of size 2 but got 1", 256 }, { 257 a: &[]string{"foo"}, 258 b: &[]string{"foo", "bar"}, 259 diff: "Expected an array of size 1 but got 2", 260 }, { 261 a: &[]string{"foo", "bar"}, 262 b: &[]string{"bar", "foo"}, 263 diff: `In arrays, values are different at index 0:` + 264 ` string(foo) != string(bar)`, 265 }, { 266 a: []uint32{42, 51}, 267 b: []uint32{42, 51}, 268 }, { 269 a: []uint32{42, 51}, 270 b: []uint32{42, 88}, 271 diff: "In arrays, values are different at index 1: uint32(51) != uint32(88)", 272 }, { 273 a: []uint32{42, 51}, 274 b: []uint32{42}, 275 diff: "Expected an array of size 2 but got 1", 276 }, { 277 a: []uint32{42, 51}, 278 b: []uint64{42, 51}, 279 diff: "expected a []uint32 but got a []uint64", 280 }, { 281 a: []uint64{42, 51}, 282 b: []uint32{42, 51}, 283 diff: "expected a []uint64 but got a []uint32", 284 }, { 285 a: []uint64{42, 51}, 286 b: []uint64{42, 51}, 287 }, { 288 a: []uint64{42, 51}, 289 b: []uint64{42}, 290 diff: "Expected an array of size 2 but got 1", 291 }, { 292 a: []uint64{42, 51}, 293 b: []uint64{42, 88}, 294 diff: "In arrays, values are different at index 1: uint64(51) != uint64(88)", 295 }, { 296 a: []interface{}{"foo", uint32(42)}, 297 b: []interface{}{"foo", uint32(42)}, 298 }, { 299 a: []interface{}{"foo", uint32(42)}, 300 b: []interface{}{"foo"}, 301 diff: "Expected an array of size 2 but got 1", 302 }, { 303 a: []interface{}{"foo"}, 304 b: []interface{}{"foo", uint32(42)}, 305 diff: "Expected an array of size 1 but got 2", 306 }, { 307 a: []interface{}{"foo", uint32(42)}, 308 b: []interface{}{"foo", uint8(42)}, 309 diff: "In arrays, values are different at index 1:" + 310 " expected a uint32 but got a uint8", 311 }, { 312 a: []interface{}{"foo", "bar"}, 313 b: []string{"foo", "bar"}, 314 diff: "expected a []interface {} but got a []string", 315 }, { 316 a: &[]interface{}{"foo", uint32(42)}, 317 b: &[]interface{}{"foo", uint32(42)}, 318 }, { 319 a: &[]interface{}{"foo", uint32(42)}, 320 b: []interface{}{"foo", uint32(42)}, 321 diff: "expected a *[]interface {} but got a []interface {}", 322 }, { 323 a: comparableStruct{a: 42}, 324 b: comparableStruct{a: 42}, 325 }, { 326 a: comparableStruct{a: 42, t: t}, 327 b: comparableStruct{a: 42}, 328 }, { 329 a: comparableStruct{a: 42}, 330 b: comparableStruct{a: 42, t: t}, 331 }, { 332 a: comparableStruct{a: 42}, 333 b: comparableStruct{a: 51}, 334 diff: "Comparable types are different: test.comparableStruct{a:" + 335 "uint32(42), t:*nil} vs test.comparableStruct{a:uint32(51), t:*nil}", 336 }, { 337 a: builtinCompare{a: 42, b: "foo"}, 338 b: builtinCompare{a: 42, b: "foo"}, 339 }, { 340 a: builtinCompare{a: 42, b: "foo"}, 341 b: builtinCompare{a: 42, b: "bar"}, 342 diff: `attributes "b" are different: string(foo) != string(bar)`, 343 }, { 344 a: map[int8]int8{2: 3, 3: 4}, 345 b: map[int8]int8{2: 3, 3: 4}, 346 }, { 347 a: map[int8]int8{2: 3, 3: 4}, 348 b: map[int8]int8{2: 3, 3: 5}, 349 diff: "for key int8(3) in map, values are different: int8(4) != int8(5)", 350 }, { 351 a: complexCompare{}, 352 b: complexCompare{}, 353 }, { 354 a: complexCompare{ 355 m: map[builtinCompare]int8{builtinCompare{1, "foo"}: 42}}, 356 b: complexCompare{ 357 m: map[builtinCompare]int8{builtinCompare{1, "foo"}: 42}}, 358 }, { 359 a: complexCompare{ 360 m: map[builtinCompare]int8{builtinCompare{1, "foo"}: 42}}, 361 b: complexCompare{ 362 m: map[builtinCompare]int8{builtinCompare{1, "foo"}: 51}}, 363 diff: `attributes "m" are different: for key test.builtinCompare{a:uint32(1),` + 364 ` b:"foo"} in map, values are different: int8(42) != int8(51)`, 365 }, { 366 a: complexCompare{ 367 m: map[builtinCompare]int8{builtinCompare{1, "foo"}: 42}}, 368 b: complexCompare{ 369 m: map[builtinCompare]int8{builtinCompare{1, "bar"}: 42}}, 370 diff: `attributes "m" are different: key test.builtinCompare{a:uint32(1),` + 371 ` b:"foo"} in map is missing in the actual map`, 372 }, { 373 a: recursive, 374 b: recursive, 375 }, { 376 a: complexCompare{p: recursive}, 377 b: complexCompare{p: recursive}, 378 }, { 379 a: complexCompare{p: &complexCompare{p: recursive}}, 380 b: complexCompare{p: &complexCompare{p: recursive}}, 381 }, { 382 a: []complexCompare{{p: &complexCompare{p: recursive}}}, 383 b: []complexCompare{{p: &complexCompare{p: recursive}}}, 384 }, { 385 a: []complexCompare{{p: &complexCompare{p: recursive}}}, 386 b: []complexCompare{{p: &complexCompare{p: nil}}}, 387 diff: `In arrays, values are different at index 0: attributes "p" are` + 388 ` different: attributes "p" are different: got nil instead of ` + 389 `*test.complexCompare{m:map[test.builtinCompare]int8{},` + 390 ` p:*test.complexCompare{<circular dependency>}}`, 391 }, { 392 a: partialCompare{a: 42}, 393 b: partialCompare{a: 42}, 394 }, { 395 a: partialCompare{a: 42}, 396 b: partialCompare{a: 51}, 397 diff: `attributes "a" are different: uint32(42) != uint32(51)`, 398 }, { 399 a: partialCompare{a: 42, b: "foo"}, 400 b: partialCompare{a: 42, b: "bar"}, 401 }, { 402 a: map[*builtinCompare]uint32{&builtinCompare{1, "foo"}: 42}, 403 b: map[*builtinCompare]uint32{&builtinCompare{1, "foo"}: 42}, 404 }, { 405 a: map[*builtinCompare]uint32{&builtinCompare{1, "foo"}: 42}, 406 b: map[*builtinCompare]uint32{&builtinCompare{2, "foo"}: 42}, 407 diff: `complex key *test.builtinCompare{a:uint32(1), b:"foo"}` + 408 ` in map is missing in the actual map`, 409 }, { 410 a: map[*builtinCompare]uint32{&builtinCompare{1, "foo"}: 42}, 411 b: map[*builtinCompare]uint32{&builtinCompare{1, "foo"}: 51}, 412 diff: `for complex key *test.builtinCompare{a:uint32(1), b:"foo"}` + 413 ` in map, values are different: uint32(42) != uint32(51)`, 414 }, { 415 a: key.New("a"), 416 b: key.New("a"), 417 }, { 418 a: map[key.Key]string{key.New("a"): "b"}, 419 b: map[key.Key]string{key.New("a"): "b"}, 420 }, { 421 a: map[key.Key]string{key.New(map[string]interface{}{"a": true}): "b"}, 422 b: map[key.Key]string{key.New(map[string]interface{}{"a": true}): "b"}, 423 }, { 424 a: key.New(map[string]interface{}{ 425 "a": map[key.Key]interface{}{key.New(map[string]interface{}{"k": 42}): true}}), 426 b: key.New(map[string]interface{}{ 427 "a": map[key.Key]interface{}{key.New(map[string]interface{}{"k": 42}): true}}), 428 }, { 429 a: key.New(map[string]interface{}{ 430 "a": map[key.Key]interface{}{key.New(map[string]interface{}{"k": 42}): true}}), 431 b: key.New(map[string]interface{}{ 432 "a": map[key.Key]interface{}{key.New(map[string]interface{}{"k": 51}): true}}), 433 diff: `Comparable types are different: ` + 434 `key.composite{sentinel:uintptr(18379810577513696751), m:map[string]interface {}` + 435 `{"a":map[key.Key]interface {}{<max_depth>:<max_depth>}}} vs` + 436 ` key.composite{sentinel:uintptr(18379810577513696751), m:map[string]interface {}` + 437 `{"a":map[key.Key]interface {}{<max_depth>:<max_depth>}}}`, 438 }, { 439 a: code(42), 440 b: code(42), 441 }, { 442 a: code(42), 443 b: code(51), 444 diff: "code(42) != code(51)", 445 }, { 446 a: message("foo"), 447 b: message("foo"), 448 }, { 449 a: message("foo"), 450 b: message("bar"), 451 diff: `message("foo") != message("bar")`, 452 }, { 453 a: []byte("foo"), 454 b: []byte("foo"), 455 }, { 456 a: []byte("foo"), 457 b: []byte("bar"), 458 diff: `[]byte("foo") != []byte("bar")`, 459 }} 460 }