data_test.go 15 KB

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