-
Notifications
You must be signed in to change notification settings - Fork 1
/
annotator_test.go
178 lines (171 loc) · 3.33 KB
/
annotator_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
package dapper_test
import (
"errors"
"reflect"
"testing"
. "github.com/dogmatiq/dapper"
)
func TestPrinter_Annotator(t *testing.T) {
cases := []struct {
Name string
Value any
Annotators []Annotator
Output []string
}{
{
Name: "empty annotation",
Value: 123,
Annotators: []Annotator{
func(Value) string { return "" },
},
Output: []string{
`int(123)`,
},
},
{
Name: "annotated nil value",
Value: nil,
Annotators: []Annotator{
func(Value) string { return "this is nil" },
},
Output: []string{
`any(nil) <<this is nil>>`,
},
},
{
Name: "annotated non-nil value",
Value: 123,
Annotators: []Annotator{
func(Value) string { return "this is not nil" },
},
Output: []string{
"int(123) <<this is not nil>>",
},
},
{
Name: "multiple annotations",
Value: 123,
Annotators: []Annotator{
func(Value) string { return "first" },
func(Value) string { return "" },
func(Value) string { return "second" },
},
Output: []string{
`int(123) <<first, second>>`,
},
},
{
Name: "multiline rendered value",
Value: struct {
Value int
}{},
Annotators: []Annotator{
func(v Value) string {
if v.DynamicType.Kind() == reflect.Struct {
return "an anonymous struct"
}
return ""
},
},
Output: []string{
`{`,
` Value: int(0)`,
`} <<an anonymous struct>>`,
},
},
{
Name: "annotation of nested values",
Value: struct{ Value int }{},
Annotators: []Annotator{
func(v Value) string {
if v.DynamicType.Kind() == reflect.Struct {
return "outer"
}
return "inner"
},
},
Output: []string{
`{`,
` Value: int(0) <<inner>>`,
`} <<outer>>`,
},
},
{
Name: "annotation of value that is rendered by a filter",
Value: errors.New("<error>"),
Annotators: []Annotator{
func(v Value) string {
if v.Value.CanInterface() {
if _, ok := v.Value.Interface().(error); ok {
return "an annotated error"
}
}
return ""
},
},
Output: []string{
`*errors.errorString{`,
` s: "<error>"`,
`} [<error>] <<an annotated error>>`,
},
},
{
Name: "annotation of recursion marker",
Value: func() any {
type T struct {
Self *T
Other int
}
var v T
v.Self = &v
return &v
}(),
Annotators: []Annotator{
func(v Value) string {
if v.DynamicType.String() == "*dapper_test.T" {
return "a recursive value"
}
return ""
},
},
Output: []string{
`*github.com/dogmatiq/dapper_test.T{`,
` Self: <recursion> <<a recursive value>>`,
` Other: 0`,
`} <<a recursive value>>`,
},
},
{
Name: "annotation of zero value marker",
Value: func() any {
type named struct {
Value int
}
return named{}
}(),
Annotators: []Annotator{
func(v Value) string {
return "a zero value"
},
},
Output: []string{
`github.com/dogmatiq/dapper_test.named{<zero>} <<a zero value>>`,
},
},
}
for _, c := range cases {
t.Run(c.Name, func(t *testing.T) {
var options []Option
for _, a := range c.Annotators {
options = append(options, WithAnnotator(a))
}
testWithPrinter(
t,
NewPrinter(options...),
c.Name,
c.Value,
c.Output...,
)
})
}
}