-
Notifications
You must be signed in to change notification settings - Fork 1
/
kind_struct_test.go
229 lines (211 loc) · 5.32 KB
/
kind_struct_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
package dapper_test
import (
"strings"
"testing"
"unsafe"
. "github.com/dogmatiq/dapper"
)
// This test verifies that empty structs are rendered on a single line.
func TestPrinter_EmptyStruct(t *testing.T) {
type empty struct{}
test(t, "empty struct", empty{}, "github.com/dogmatiq/dapper_test.empty{}")
test(t, "empty anonymous struct", struct{}{}, "{}")
}
// This test verifies the inclusion or omission of type information for fields
// in various nested depths of anonymous and named structs.
func TestPrinter_StructFieldTypes(t *testing.T) {
type named struct {
Int int
Iface any
}
type anonymous struct {
Anon struct {
Int int
}
}
test(
t,
"types are only included for interface fields of named struct",
named{
Int: 100,
Iface: 200,
},
"github.com/dogmatiq/dapper_test.named{",
" Int: 100",
" Iface: int(200)",
"}",
)
test(
t,
"types are always included fields of anonymous struct",
struct {
Int int
Iface any
}{
Int: 100,
Iface: 200,
},
"{",
" Int: int(100)",
" Iface: int(200)",
"}",
)
test(
t,
"types are only included for interface fields of anonymous struct inside a named struct",
anonymous{
Anon: struct{ Int int }{
Int: 100,
},
},
"github.com/dogmatiq/dapper_test.anonymous{",
" Anon: {",
" Int: 100",
" }",
"}",
)
}
// Verifies not exported fields in a struct are omitted when configured to do so
func TestPrinter_WithUnexportedStructFields(t *testing.T) {
printer := NewPrinter(WithUnexportedStructFields(false))
writer := &strings.Builder{}
_, err := printer.Write(writer, struct {
notExported bool
Exported bool
}{})
if err != nil {
t.Fatal(err)
}
expected := "{\n Exported: false\n}"
result := writer.String()
if expected != result {
t.Errorf("Expected \n'%s' but got \n'%s'", expected, result)
}
}
// This test verifies that all types can be formatted when obtained from
// unexported fields.
//
// This is important because reflect.Value().Interface() panics if called on
// such a value.
func TestPrinter_StructUnexportedFields(t *testing.T) {
type unexported struct {
vString string
vBool bool
vInt int
vInt8 int8
vInt16 int16
vInt32 int32
vInt64 int64
vUint uint
vUint8 uint8
vUint16 uint16
vUint32 uint32
vUint64 uint64
vComplex64 complex64
vComplex128 complex128
vFloat32 float32
vFloat64 float64
vUintptr uintptr
vUnsafePointer unsafe.Pointer
vChannel chan string
vFunc func(int, string) (bool, error)
vIface any
vStruct struct{}
vPtr *int
vSlice []int
vArray [1]int
vMap map[int]int
}
test(
t,
"unexported fields can be formatted",
unexported{
vString: shallowValues.String,
vBool: shallowValues.Bool,
vInt: shallowValues.Int,
vInt8: shallowValues.Int8,
vInt16: shallowValues.Int16,
vInt32: shallowValues.Int32,
vInt64: shallowValues.Int64,
vUint: shallowValues.Uint,
vUint8: shallowValues.Uint8,
vUint16: shallowValues.Uint16,
vUint32: shallowValues.Uint32,
vUint64: shallowValues.Uint64,
vComplex64: shallowValues.Complex64,
vComplex128: shallowValues.Complex128,
vFloat32: shallowValues.Float32,
vFloat64: shallowValues.Float64,
vUintptr: shallowValues.Uintptr,
vUnsafePointer: shallowValues.UnsafePointer,
vChannel: shallowValues.Channel,
vFunc: shallowValues.Func,
vIface: 100,
vStruct: struct{}{},
vPtr: &pointerTarget,
vSlice: []int{100},
vArray: [1]int{200},
vMap: map[int]int{300: 400},
},
"github.com/dogmatiq/dapper_test.unexported{",
` vString: "foo\nbar"`,
" vBool: true",
" vInt: -100",
" vInt8: -100",
" vInt16: -100",
" vInt32: -100",
" vInt64: -100",
" vUint: 100",
" vUint8: 100",
" vUint16: 100",
" vUint32: 100",
" vUint64: 100",
" vComplex64: 100+5i",
" vComplex128: 100+5i",
" vFloat32: 1.2300000190734863",
" vFloat64: 1.23",
" vUintptr: 0xabcd",
" vUnsafePointer: "+pointerTargetHex,
" vChannel: "+channelHex,
" vFunc: "+funcHex,
" vIface: int(100)",
" vStruct: {}",
" vPtr: 123",
" vSlice: {",
" 100",
" }",
" vArray: {",
" 200",
" }",
" vMap: {",
" 300: 400",
" }",
"}",
)
}
// This test verifies that zero-value structs are rendered without all of their
// nested fields only if they are not anonymous.
func TestPrinter_ZeroValueStruct(t *testing.T) {
type named struct {
Int int
Iface any
}
test(
t,
"no fields are rendered for named zero-value structs",
named{},
"github.com/dogmatiq/dapper_test.named{<zero>}",
)
test(
t,
"fields are always rendered for anonymous zero-value structs",
struct {
Int int
Iface any
}{},
"{",
" Int: int(0)",
" Iface: any(nil)",
"}",
)
}