-
Notifications
You must be signed in to change notification settings - Fork 35
/
func_test.go
160 lines (148 loc) · 3.64 KB
/
func_test.go
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// Copyright 2019 GRAIL, Inc. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
package bigslice
import (
"testing"
"unsafe"
"github.com/grailbio/testutil/expect"
"github.com/grailbio/testutil/h"
)
type testStruct0 struct{ Field0 int }
// testStruct1 exists to avoid the problem of registering the same struct twice
// with gob. As a convenience, bigslice.Func registers its argument types.
// However, if you pass the same struct as a value and a pointer, we attempt to
// register the same type twice with different names, e.g.
// "github.com/grailbio/bigslice.testStruct0" and "*bigslice.testStruct0". This
// causes a panic in gob. Instead, we just use a different type altogether for
// our pointer-to-struct argument.
type testStruct1 struct{ Field1 int }
// Disable unused checking for testInterface, as we're just using it to make
// sure that our func typechecking works properly, and we don't need to
// call it to do so.
// nolint:unused
type testInterface interface{ FuncTestMethod() }
type testInterfaceImpl struct{}
func (s *testInterfaceImpl) FuncTestMethod() {}
var fnTestNilFuncArgs = Func(
func(int, string, []string, map[int]int,
testStruct0, *testStruct1, unsafe.Pointer,
testInterface) Slice {
return Const(1, []int{})
})
// TestNilFuncArgs verifies that Func invocation handles untyped nil arguments
// properly.
func TestNilFuncArgs(t *testing.T) {
ts0 := testStruct0{Field0: 0}
pts1 := &testStruct1{Field1: 0}
upts1 := unsafe.Pointer(pts1)
ptii := &testInterfaceImpl{}
for _, c := range []struct {
name string
args []interface{}
ok bool
}{
{
name: "all non-nil",
args: []interface{}{
0, "", []string{}, map[int]int{0: 0},
ts0, pts1, upts1, ptii,
},
ok: true,
},
{
name: "nil for types that can be nil",
args: []interface{}{
0, "", nil, nil,
ts0, nil, nil, nil,
},
ok: true,
},
{
name: "nil for int",
args: []interface{}{
nil, "", []string{}, map[int]int{0: 0},
ts0, pts1, upts1, ptii,
},
ok: false,
},
{
name: "nil for string",
args: []interface{}{
0, nil, []string{}, map[int]int{0: 0},
ts0, pts1, upts1, ptii,
},
ok: false,
},
{
name: "nil for struct",
args: []interface{}{
0, "", []string{}, map[int]int{0: 0},
nil, pts1, upts1, ptii,
},
ok: false,
},
} {
t.Run(c.name, func(t *testing.T) {
var matcher *h.Matcher
if c.ok {
matcher = h.Panics(h.Nil())
} else {
// Expect a panic with a message saying something about
// nil-ness.
matcher = h.Panics(h.HasSubstr("nil"))
}
invoke := func() { fnTestNilFuncArgs.Invocation("", c.args...) }
expect.That(t, invoke, matcher)
apply := func() { fnTestNilFuncArgs.Apply(c.args...) }
expect.That(t, apply, matcher)
})
}
}
func TestFuncLocationsDiff(t *testing.T) {
for _, c := range []struct {
lhs []string
rhs []string
diff []string
}{
{nil, nil, nil},
{[]string{"a"}, []string{"a"}, nil},
{
[]string{},
[]string{"a"},
[]string{"+ a"},
},
{
[]string{"a", "b"},
[]string{"a"},
[]string{"a", "- b"},
},
{
[]string{"a", "b"},
[]string{"b"},
[]string{"- a", "b"},
},
{
[]string{"a"},
[]string{"a", "b"},
[]string{"a", "+ b"},
},
{
[]string{"a", "c"},
[]string{"a", "b", "c", "d"},
[]string{"a", "+ b", "c", "+ d"},
},
{
[]string{"a", "b", "d"},
[]string{"a", "c", "d"},
[]string{"a", "- b", "+ c", "d"},
},
{
[]string{"a", "b", "c"},
[]string{"a", "c", "d", "e"},
[]string{"a", "- b", "c", "+ d", "+ e"},
},
} {
expect.That(t, FuncLocationsDiff(c.lhs, c.rhs), h.EQ(c.diff))
}
}