-
Notifications
You must be signed in to change notification settings - Fork 1
/
kind_shallow_test.go
242 lines (224 loc) · 7.95 KB
/
kind_shallow_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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package dapper_test
import (
"fmt"
"reflect"
"testing"
"unsafe"
)
// shallow is a test struct containing fields for each type of "shallow" value.
type shallow struct {
String string
Bool bool
Int int
Int8 int8
Int16 int16
Int32 int32
Int64 int64
Uint uint
Uint8 uint8
Uint16 uint16
Uint32 uint32
Uint64 uint64
Complex64 complex64
Complex128 complex128
Float32 float32
Float64 float64
Uintptr uintptr
UnsafePointer unsafe.Pointer
Channel chan string
Func func(int, string) (bool, error)
}
var shallowValues = shallow{
String: "foo\nbar",
Bool: true,
Int: -100,
Int8: -100,
Int16: -100,
Int32: -100,
Int64: -100,
Uint: 100,
Uint8: 100,
Uint16: 100,
Uint32: 100,
Uint64: 100,
Complex64: 100 + 5i,
Complex128: 100 + 5i,
Float32: 1.23,
Float64: 1.23,
Uintptr: 0xABCD,
UnsafePointer: unsafe.Pointer(&pointerTarget),
Channel: make(chan string),
Func: func(int, string) (bool, error) {
panic("not implemented")
},
}
var (
pointerTarget int = 123
pointerTargetHex = fmt.Sprintf("0x%x", &pointerTarget)
channelHex = fmt.Sprintf("0x%x", shallowValues.Channel)
funcHex = fmt.Sprintf("0x%x", reflect.ValueOf(shallowValues.Func).Pointer())
)
// This test verifies the formatting of "shallow" values.
//
// It verifies that type information is included, as the type information can
// not be inferred from context.
//
// Note that the type name is never rendered for strings or booleans, as these
// literals are not ambiguous as is.
func TestPrinter_ShallowValues(t *testing.T) {
test(t, "string", shallowValues.String, `"foo\nbar"`)
test(t, "bool", shallowValues.Bool, "true")
test(t, "int", shallowValues.Int, "int(-100)")
test(t, "int8", shallowValues.Int8, "int8(-100)")
test(t, "int16", shallowValues.Int16, "int16(-100)")
test(t, "int32", shallowValues.Int32, "int32(-100)")
test(t, "int64", shallowValues.Int64, "int64(-100)")
test(t, "uint", shallowValues.Uint, "uint(100)")
test(t, "uint8", shallowValues.Uint8, "uint8(100)")
test(t, "uint16", shallowValues.Uint16, "uint16(100)")
test(t, "uint32", shallowValues.Uint32, "uint32(100)")
test(t, "uint64", shallowValues.Uint64, "uint64(100)")
test(t, "complex64", shallowValues.Complex64, "complex64(100+5i)")
test(t, "complex128", shallowValues.Complex128, "complex128(100+5i)")
test(t, "float32", shallowValues.Float32, "float32(1.2300000190734863)")
test(t, "float64", shallowValues.Float64, "float64(1.23)")
test(t, "uintptr", shallowValues.Uintptr, "uintptr(0xabcd)")
test(t, "unsafe.Pointer", shallowValues.UnsafePointer, "unsafe.Pointer("+pointerTargetHex+")")
test(t, "channel", shallowValues.Channel, "(chan string)("+channelHex+")")
test(t, "func", shallowValues.Func, "(func(int, string) (bool, error))("+funcHex+")")
}
// This test verifies the formatting of "shallow" values when the type
// information omitted because it can be inferred from the context in which the
// values are rendered.
func TestPrinter_ShallowValuesInNamedStruct(t *testing.T) {
test(
t,
"it does not include the scalar types",
shallowValues,
"github.com/dogmatiq/dapper_test.shallow{",
` String: "foo\nbar"`,
" Bool: true",
" Int: -100",
" Int8: -100",
" Int16: -100",
" Int32: -100",
" Int64: -100",
" Uint: 100",
" Uint8: 100",
" Uint16: 100",
" Uint32: 100",
" Uint64: 100",
" Complex64: 100+5i",
" Complex128: 100+5i",
" Float32: 1.2300000190734863",
" Float64: 1.23",
" Uintptr: 0xabcd",
" UnsafePointer: "+pointerTargetHex,
" Channel: "+channelHex,
" Func: "+funcHex,
"}",
)
}
// This test verifies the formatting of "shallow" values when the type
// information included because it can not be inferred from the context in which
// the values are rendered because they are in an anonymous struct.
func TestPrinter_ShallowValuesInAnonymousStruct(t *testing.T) {
anon := struct {
String string
Bool bool
Int int
Int8 int8
Int16 int16
Int32 int32
Int64 int64
Uint uint
Uint8 uint8
Uint16 uint16
Uint32 uint32
Uint64 uint64
Complex64 complex64
Complex128 complex128
Float32 float32
Float64 float64
Uintptr uintptr
UnsafePointer unsafe.Pointer
Channel chan string
Func func(int, string) (bool, error)
}{}
anon = shallowValues // rely on the same layout
test(
t,
"it does not include the scalar types",
anon,
"{",
` String: "foo\nbar"`,
" Bool: true",
" Int: int(-100)",
" Int8: int8(-100)",
" Int16: int16(-100)",
" Int32: int32(-100)",
" Int64: int64(-100)",
" Uint: uint(100)",
" Uint8: uint8(100)",
" Uint16: uint16(100)",
" Uint32: uint32(100)",
" Uint64: uint64(100)",
" Complex64: complex64(100+5i)",
" Complex128: complex128(100+5i)",
" Float32: float32(1.2300000190734863)",
" Float64: float64(1.23)",
" Uintptr: uintptr(0xabcd)",
" UnsafePointer: unsafe.Pointer("+pointerTargetHex+")",
" Channel: (chan string)("+channelHex+")",
" Func: (func(int, string) (bool, error))("+funcHex+")",
"}",
)
}
// This test provides additional tests for untyped pointer rendering.
func TestPrinter_UntypedPointer(t *testing.T) {
test(t, "zero uintptr", uintptr(0), "uintptr(0)")
test(t, "nil unsafe.Pointer", unsafe.Pointer(nil), "unsafe.Pointer(nil)")
}
// This test provides additional tests for channel rendering.
func TestPrinter_Channel(t *testing.T) {
type named chan int
type local struct{}
test(t, "nil channel", (chan string)(nil), "(chan string)(nil)")
test(t, "recv-only channel", (<-chan string)(nil), "(<-chan string)(nil)")
test(t, "send-only channel", (chan<- string)(nil), "(chan<- string)(nil)")
test(t, "named channel", named(nil), "github.com/dogmatiq/dapper_test.named(nil)")
test(t, "package path", (chan local)(nil), "(chan github.com/dogmatiq/dapper_test.local)(nil)")
// a buffered channel will show it's "usage" ratio
ch := make(chan string, 10)
ch <- ""
ch <- ""
ch <- ""
test(
t,
"buffered channel",
ch,
fmt.Sprintf("(chan string)(0x%x 3/10)", ch),
)
}
// This test provides additional tests for function rendering.
func TestPrinter_Func(t *testing.T) {
type named func(int) bool
type local struct{}
test(t, "no inputs or outputs", (func())(nil), "(func())(nil)")
test(t, "single input", (func(int))(nil), "(func(int))(nil)")
test(t, "multiple inputs", (func(int, bool))(nil), "(func(int, bool))(nil)")
test(t, "variadic", (func(...int))(nil), "(func(...int))(nil)")
test(t, "variadic with multiple inputs", (func(string, ...int))(nil), "(func(string, ...int))(nil)")
test(t, "single return value", (func() int)(nil), "(func() int)(nil)")
test(t, "multiple return values", (func() (int, bool))(nil), "(func() (int, bool))(nil)")
test(t, "named function", named(nil), "github.com/dogmatiq/dapper_test.named(nil)")
test(t, "package path", (func(local))(nil), "(func(github.com/dogmatiq/dapper_test.local))(nil)")
test(t, "everything", (func(local, ...int) (int, bool))(nil), "(func(github.com/dogmatiq/dapper_test.local, ...int) (int, bool))(nil)")
}
// See https://github.com/dogmatiq/dapper/issues/6
func TestPrinter_StringAndBoolTypeNames(t *testing.T) {
type MyString string
type MyBool bool
test(t, "typed string", MyString("foo\nbar"), `github.com/dogmatiq/dapper_test.MyString("foo\nbar")`)
test(t, "typed bool", MyBool(true), `github.com/dogmatiq/dapper_test.MyBool(true)`)
}