-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrait_autodescriber_test.go
216 lines (199 loc) · 6.68 KB
/
trait_autodescriber_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
package meep
import (
"fmt"
"strings"
"testing"
)
func TestReacharound(t *testing.T) {
type Woop struct {
TraitAutodescribing
Wonk string
}
var err error
err = &Woop{Wonk: "Bonk"}
err = New(err)
woop := err.(*Woop)
if woop.Wonk != "Bonk" {
t.Errorf("Bonk somehow became %q", woop.Wonk)
}
if woop.TraitAutodescribing.self == nil {
t.Errorf("No impact")
}
if woop.TraitAutodescribing.self != err {
t.Errorf("Drat")
}
}
func TestAutodescribeSimple(t *testing.T) {
type Woop struct {
TraitAutodescribing
Wonk string
}
err := New(&Woop{Wonk: "Bonk"})
expect := `Error[meep.Woop]: Wonk="Bonk";`
if expect != err.Error() {
t.Errorf("expected %q, got %q", expect, err.Error())
}
}
func TestAutodescribePlusCause(t *testing.T) {
type Woop struct {
TraitAutodescribing
TraitCausable
Wonk string
}
err := New(&Woop{
Wonk: "Bonk",
TraitCausable: TraitCausable{fmt.Errorf("lecause")},
})
expect := `Error[meep.Woop]: Wonk="Bonk";` + "\n"
expect += "\t" + `Caused by: lecause` + "\n"
actual := err.Error()
if expect != actual {
t.Errorf("mismatch:\n expected %q\n got %q", expect, actual)
}
t.Logf("this is what a very basic error with a nested cause looks like:\n>>>\n%s\n<<<\n", actual)
}
func TestAutodescribePlusTraceableCause(t *testing.T) {
type Woop struct {
TraitAutodescribing
TraitCausable
Wonk string
}
type Boop struct {
TraitTraceable
TraitAutodescribing
}
err := New(&Woop{
Wonk: "Bonk",
TraitCausable: TraitCausable{
New(&Boop{}),
},
})
expect := `Error[meep.Woop]: Wonk="Bonk";` + "\n"
expect += "\t" + `Caused by: Error[meep.Boop]:` + "\n"
expect += "\t\t" + `Stack trace:` + "\n"
expect += "\t\t\t" + `·> /trait_autodescriber_test.go:73: meep.TestAutodescribePlusTraceableCause` + "\n"
// Cleanup is fun...
actual := err.Error()
// First, remove the local build path for this project.
actual = stripCwd(actual)
// Lines we expect following this -- as of go1.4 -- are:
// """
// expect += "\t\t\t" + `·> /usr/local/go/src/testing/testing.go:447: testing.tRunner` + "\n"
// expect += "\t\t\t" + `·> /usr/local/go/src/runtime/asm_amd64.s:2232: runtime.goexit` + "\n"
// """
// And these are not universal or portable in *several* ways:
// - the line numbers aren't constant across go versions
// - the files aren't constant across platforms
// - indeed even the *number* of lines is not constant across platforms and versions
// - the prefix path may change if your GOROOT is unusual (as it is, on some CI platforms, even)
// So, we must simply truncate them.
actual = dropLastNLines(actual, 3) + "\n"
if expect != actual {
t.Errorf("mismatch:\n expected %q\n got %q", expect, actual)
}
t.Logf("this is what errors with causes that have stacktraces look like :D\n>>>\n%s\n<<<\n", err.Error())
}
func TestAutodescribePlusTraceableCauseDoubleTrouble(t *testing.T) {
type Woop struct {
TraitAutodescribing
TraitCausable
Wonk string
}
type Boop struct {
TraitAutodescribing
TraitCausable
TraitTraceable
}
err := New(&Woop{
Wonk: "Bonk",
TraitCausable: TraitCausable{
New(&Boop{
TraitCausable: TraitCausable{
New(&Boop{}),
},
}),
},
})
expect := `Error[meep.Woop]: Wonk="Bonk";` + "\n"
expect += "\t" + `Caused by: Error[meep.Boop]:` + "\n"
expect += "\t\t" + `Caused by: Error[meep.Boop]:` + "\n"
expect += "\t\t\t" + `Stack trace:` + "\n"
expect += "\t\t\t\t" + `·> /trait_autodescriber_test.go:120: meep.TestAutodescribePlusTraceableCauseDoubleTrouble` + "\n"
// variable: // expect += "\t\t\t\t" + `·> /usr/local/go/src/testing/testing.go:447: testing.tRunner` + "\n"
// variable: // expect += "\t\t\t\t" + `·> /usr/local/go/src/runtime/asm_amd64.s:2232: runtime.goexit` + "\n"
expect += "\t\t" + `Stack trace:` + "\n"
expect += "\t\t\t" + `·> /trait_autodescriber_test.go:122: meep.TestAutodescribePlusTraceableCauseDoubleTrouble` + "\n"
// variable: // expect += "\t\t\t" + `·> /usr/local/go/src/testing/testing.go:447: testing.tRunner` + "\n"
// variable: // expect += "\t\t\t" + `·> /usr/local/go/src/runtime/asm_amd64.s:2232: runtime.goexit` + "\n"
// Cleanup is fun...
actual := err.Error()
actual = stripCwd(actual)
actual = dropLinesContaining(actual, ": testing.")
actual = dropLinesContaining(actual, ": runtime.")
if expect != actual {
t.Errorf("mismatch:\n expected %q\n got %q", expect, actual)
}
// now again for printing (without the parts dropped for the assertion)
actual = err.Error()
actual = strings.Replace(actual, "\t", "\\t\t", -1)
actual = strings.Replace(actual, "\n", "\\n\n", -1)
t.Logf("this is what errors with causes that have stacktraces look like :D\n>>>\n%s\n<<<\n", actual)
}
func TestAutodescribeManyFields(t *testing.T) {
type ErrBananaPancakes struct {
TraitAutodescribing
Alpha string
Beta int
Gamma interface{}
Delta string
}
err := New(&ErrBananaPancakes{
Alpha: "unO",
Beta: 1,
Gamma: struct{}{},
Delta: "ca\ttorce",
})
expect := `Error[meep.ErrBananaPancakes]: Alpha="unO";Beta=1;Gamma=struct {}{};Delta="ca\ttorce";`
actual := err.Error()
if expect != actual {
t.Errorf("mismatch:\n expected %q\n got %q", expect, actual)
}
}
func TestIndirectEmbed(t *testing.T) {
type ErrBananaPancakes struct {
AllTraits
Alpha string
Beta int
}
err := New(&ErrBananaPancakes{
Alpha: "fwee",
Beta: 14,
}, Cause(fmt.Errorf("a cause")))
expect := `Error[meep.ErrBananaPancakes]: Alpha="fwee";Beta=14;` + "\n"
expect += "\t" + `Stack trace:` + "\n"
expect += "\t\t" + `·> /trait_autodescriber_test.go:184: meep.TestIndirectEmbed` + "\n"
expect += "\t" + `Caused by: a cause` + "\n"
// Cleanup is fun...
actual := err.Error()
actual = stripCwd(actual)
actual = dropLinesContaining(actual, ": testing.")
actual = dropLinesContaining(actual, ": runtime.")
// Ok, this *really* gets to me.
// Apparently a multi-line "New" func call:
// - in go1.4 and before, it's always seen from the last line.
// - in go1.5 and 1.6, is seen from the first line.
// - ... except when the race detector is on, in which case it's last line.
// - in go1.7 and beyond, it's always seen from the last line again.
// Wild, right? Whee.
// See https://travis-ci.org/polydawn/meep/builds/155778432 .
// So we'll just take that "first line" line number and uh. Touch it a little.
actual = strings.Replace(actual, ":181:", ":184:", 1)
if expect != actual {
t.Errorf("mismatch:\n expected %q\n got %q", expect, actual)
}
// now again for printing (without the parts dropped for the assertion)
actual = err.Error()
actual = strings.Replace(actual, "\t", "\\t\t", -1)
actual = strings.Replace(actual, "\n", "\\n\n", -1)
t.Logf("embedding meep.Meep shorthand looks about the same\n>>>\n%s\n<<<\n", actual)
}