File: map_test.go

package info (click to toggle)
golang-github-maxatome-go-testdeep 1.14.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,416 kB
  • sloc: perl: 1,012; yacc: 130; makefile: 2
file content (123 lines) | stat: -rw-r--r-- 2,816 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Copyright (c) 2018, Maxime Soulé
// All rights reserved.
//
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree.

package tdutil_test

import (
	"reflect"
	"sort"
	"testing"

	"github.com/maxatome/go-testdeep/helpers/tdutil"
)

func TestMap(t *testing.T) {
	m := map[string]int{"a": 1, "b": 2, "c": 3}

	t.Run("MapEach", func(t *testing.T) {
		type kv struct {
			key   string
			value int
		}
		var s []kv
		ok := tdutil.MapEach(reflect.ValueOf(m), func(k, v reflect.Value) bool {
			s = append(s, kv{
				key:   k.Interface().(string),
				value: v.Interface().(int),
			})
			return true
		})
		if !ok {
			t.Error("MapEach returned false")
		}

		sort.Slice(s, func(i, j int) bool { return s[i].key < s[j].key })

		if len(s) != 3 ||
			s[0] != (kv{key: "a", value: 1}) ||
			s[1] != (kv{key: "b", value: 2}) ||
			s[2] != (kv{key: "c", value: 3}) {
			t.Errorf("MapEach failed: %v", s)
		}
	})

	t.Run("MapEach short circuit", func(t *testing.T) {
		called := 0
		ok := tdutil.MapEach(reflect.ValueOf(m), func(k, v reflect.Value) bool {
			called++
			return false
		})
		if ok {
			t.Error("MapEach returned true")
		}
		if called != 1 {
			t.Errorf("MapEach callback called %d times instead of 1", called)
		}
	})

	t.Run("MapEachValue", func(t *testing.T) {
		var s []int
		ok := tdutil.MapEachValue(reflect.ValueOf(m), func(v reflect.Value) bool {
			s = append(s, v.Interface().(int))
			return true
		})
		if !ok {
			t.Error("MapEachValue returned false")
		}

		sort.Ints(s)

		if len(s) != 3 || s[0] != 1 || s[1] != 2 || s[2] != 3 {
			t.Errorf("MapEachValue failed: %v", s)
		}
	})

	t.Run("MapEachValue short circuit", func(t *testing.T) {
		called := 0
		ok := tdutil.MapEachValue(reflect.ValueOf(m), func(v reflect.Value) bool {
			called++
			return false
		})
		if ok {
			t.Error("MapEachValue returned true")
		}
		if called != 1 {
			t.Errorf("MapEachValue callback called %d times instead of 1", called)
		}
	})

	t.Run("MapSortedValues", func(t *testing.T) {
		vs := tdutil.MapSortedValues(reflect.ValueOf(m))

		if len(vs) != 3 ||
			vs[0].Int() != 1 || vs[1].Int() != 2 || vs[2].Int() != 3 {
			t.Errorf("MapSortedValues failed: %v", vs)
		}

		// nil map
		var mn map[string]int
		vs = tdutil.MapSortedKeys(reflect.ValueOf(mn))
		if len(vs) != 0 {
			t.Errorf("MapSortedValues failed: %v", vs)
		}
	})

	t.Run("MapSortedKeys", func(t *testing.T) {
		ks := tdutil.MapSortedKeys(reflect.ValueOf(m))

		if len(ks) != 3 ||
			ks[0].String() != "a" || ks[1].String() != "b" || ks[2].String() != "c" {
			t.Errorf("MapSortedKeys failed: %v", ks)
		}

		// nil map
		var mn map[string]int
		ks = tdutil.MapSortedKeys(reflect.ValueOf(mn))
		if len(ks) != 0 {
			t.Errorf("MapSortedKeys failed: %v", ks)
		}
	})
}