Debian Codegraph @

helpers/tdutil/map_test.go

raw
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 tdutil_test
8
9 import (
10 "reflect"
11 "sort"
12 "testing"
13
14 "github.com/maxatome/go-testdeep/helpers/tdutil"
15 )
16
17 func TestMap(t *testing.T) {
18 m := map[string]int{"a": 1, "b": 2, "c": 3}
19
20 t.Run("MapEach", func(t *testing.T) {
21 type kv struct {
22 key string
23 value int
24 }
25 var s []kv
26 ok := tdutil.MapEach(reflect.ValueOf(m), func(k, v reflect.Value) bool {
27 s = append(s, kv{
28 key: k.Interface().(string),
29 value: v.Interface().(int),
30 })
31 return true
32 })
33 if !ok {
34 t.Error("MapEach returned false")
35 }
36
37 sort.Slice(s, func(i, j int) bool { return s[i].key < s[j].key })
38
39 if len(s) != 3 ||
40 s[0] != (kv{key: "a", value: 1}) ||
41 s[1] != (kv{key: "b", value: 2}) ||
42 s[2] != (kv{key: "c", value: 3}) {
43 t.Errorf("MapEach failed: %v", s)
44 }
45 })
46
47 t.Run("MapEach short circuit", func(t *testing.T) {
48 called := 0
49 ok := tdutil.MapEach(reflect.ValueOf(m), func(k, v reflect.Value) bool {
50 called++
51 return false
52 })
53 if ok {
54 t.Error("MapEach returned true")
55 }
56 if called != 1 {
57 t.Errorf("MapEach callback called %d times instead of 1", called)
58 }
59 })
60
61 t.Run("MapEachValue", func(t *testing.T) {
62 var s []int
63 ok := tdutil.MapEachValue(reflect.ValueOf(m), func(v reflect.Value) bool {
64 s = append(s, v.Interface().(int))
65 return true
66 })
67 if !ok {
68 t.Error("MapEachValue returned false")
69 }
70
71 sort.Ints(s)
72
73 if len(s) != 3 || s[0] != 1 || s[1] != 2 || s[2] != 3 {
74 t.Errorf("MapEachValue failed: %v", s)
75 }
76 })
77
78 t.Run("MapEachValue short circuit", func(t *testing.T) {
79 called := 0
80 ok := tdutil.MapEachValue(reflect.ValueOf(m), func(v reflect.Value) bool {
81 called++
82 return false
83 })
84 if ok {
85 t.Error("MapEachValue returned true")
86 }
87 if called != 1 {
88 t.Errorf("MapEachValue callback called %d times instead of 1", called)
89 }
90 })
91
92 t.Run("MapSortedValues", func(t *testing.T) {
93 vs := tdutil.MapSortedValues(reflect.ValueOf(m))
94
95 if len(vs) != 3 ||
96 vs[0].Int() != 1 || vs[1].Int() != 2 || vs[2].Int() != 3 {
97 t.Errorf("MapSortedValues failed: %v", vs)
98 }
99
100 // nil map
101 var mn map[string]int
102 vs = tdutil.MapSortedKeys(reflect.ValueOf(mn))
103 if len(vs) != 0 {
104 t.Errorf("MapSortedValues failed: %v", vs)
105 }
106 })
107
108 t.Run("MapSortedKeys", func(t *testing.T) {
109 ks := tdutil.MapSortedKeys(reflect.ValueOf(m))
110
111 if len(ks) != 3 ||
112 ks[0].String() != "a" || ks[1].String() != "b" || ks[2].String() != "c" {
113 t.Errorf("MapSortedKeys failed: %v", ks)
114 }
115
116 // nil map
117 var mn map[string]int
118 ks = tdutil.MapSortedKeys(reflect.ValueOf(mn))
119 if len(ks) != 0 {
120 t.Errorf("MapSortedKeys failed: %v", ks)
121 }
122 })
123 }