Debian Codegraph @

testutil/testutil_test.go

raw symbols SCIP
1 // Copyright (c) The EfficientGo Authors.
2 // Licensed under the Apache License 2.0.
3
4 package testutil
5
6 import (
7 "math"
8 "testing"
9
10 "github.com/google/go-cmp/cmp"
11 "github.com/google/go-cmp/cmp/cmpopts"
12 )
13
14 func TestContains(t *testing.T) {
15 tests := map[string]struct {
16 haystack []string
17 needle []string
18 shouldMatch bool
19 }{
20 "empty haystack": {
21 haystack: []string{},
22 needle: []string{"key1"},
23 shouldMatch: false,
24 },
25
26 "empty needle": {
27 haystack: []string{"key1", "key2", "key3"},
28 needle: []string{},
29 shouldMatch: false,
30 },
31
32 "single value needle": {
33 haystack: []string{"key1", "key2", "key3"},
34 needle: []string{"key1"},
35 shouldMatch: true,
36 },
37
38 "multiple value needle": {
39 haystack: []string{"key1", "key2", "key3"},
40 needle: []string{"key1", "key2"},
41 shouldMatch: true,
42 },
43
44 "same size needle as haystack": {
45 haystack: []string{"key1", "key2", "key3"},
46 needle: []string{"key1", "key2", "key3"},
47 shouldMatch: true,
48 },
49
50 "larger needle than haystack": {
51 haystack: []string{"key1", "key2", "key3"},
52 needle: []string{"key1", "key2", "key3", "key4"},
53 shouldMatch: false,
54 },
55
56 "needle not contained": {
57 haystack: []string{"key1", "key2", "key3"},
58 needle: []string{"key4"},
59 shouldMatch: false,
60 },
61
62 "haystack ends before needle": {
63 haystack: []string{"key1", "key2", "key3"},
64 needle: []string{"key3", "key4"},
65 shouldMatch: false,
66 },
67 }
68
69 for testName, testData := range tests {
70 t.Run(testName, func(t *testing.T) {
71 if testData.shouldMatch != contains(testData.haystack, testData.needle) {
72 t.Fatalf("unexpected result testing contains() with %#v", testData)
73 }
74 })
75 }
76 }
77
78 type child struct {
79 Val float64
80 }
81
82 type parent struct {
83 C child
84 }
85
86 type unexp struct {
87 val float64
88 }
89
90 func TestEqualsWithNaN(t *testing.T) {
91 for _, tc := range []struct {
92 name string
93 a interface{}
94 b interface{}
95 opts cmp.Options
96 }{
97 {
98 a: math.NaN(),
99 b: math.NaN(),
100 name: "Simple NaN value comparison",
101 opts: cmp.Options{cmpopts.EquateNaNs()},
102 },
103 {
104 a: child{Val: math.NaN()},
105 b: child{Val: math.NaN()},
106 name: "NaN value as struct member comparison",
107 opts: cmp.Options{cmpopts.EquateNaNs()},
108 },
109 {
110 a: parent{C: child{Val: math.NaN()}},
111 b: parent{C: child{Val: math.NaN()}},
112 name: "NaN value in nested struct comparison",
113 opts: cmp.Options{cmpopts.EquateNaNs()},
114 },
115 {
116 a: unexp{val: math.NaN()},
117 b: unexp{val: math.NaN()},
118 name: "NaN value as unexported struct member comparison",
119 opts: cmp.Options{cmp.AllowUnexported(unexp{}), cmpopts.EquateNaNs()},
120 },
121 } {
122 t.Run(tc.name, func(t *testing.T) {
123 WithGoCmp(tc.opts).Equals(t, tc.a, tc.b)
124 })
125 }
126 }