-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfields_test.go
65 lines (51 loc) · 1.56 KB
/
fields_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
package whoops
import "github.com/stretchr/testify/assert"
import "testing"
import "errors"
type testStruct struct {
A string
B int
}
func TestErrorEnrichingWithFields(t *testing.T) {
var (
f1 Field[string] = "field1"
f2 Field[testStruct] = "field2"
f3 Field[int] = "field which is not used"
f4 Field[bool] = "something"
ts = testStruct{
A: "A",
B: 2,
}
enrichedErr error
)
anyErr := errors.New("any error")
t.Run("Enrich enriches an error", func(t *testing.T) {
enrichedErr = Enrich(anyErr, f1.Val("random value"), f2.Val(ts))
assert.Equal(t, "original error: any error\nenriched fields:\nfield1[string] = \"random value\"\nfield2[whoops.testStruct] = whoops.testStruct{A:\"A\", B:2}\n", enrichedErr.Error())
})
t.Run("ensure that fields can get extracted", func(t *testing.T) {
extractedField1, found := f1.GetFrom(enrichedErr)
assert.True(t, found)
assert.Equal(t, "random value", extractedField1)
extractedField2, found := f2.GetFrom(enrichedErr)
assert.True(t, found)
assert.Equal(t, ts, extractedField2)
_, found = f3.GetFrom(enrichedErr)
assert.False(t, found)
})
newEnriched := Enrich(enrichedErr, f4.Val(true))
t.Run("enriching an enriched error", func(t *testing.T) {
// f1, f2 and f4 must exist on newWe
var ok bool
_, ok = f1.GetFrom(newEnriched)
assert.True(t, ok)
_, ok = f2.GetFrom(newEnriched)
assert.True(t, ok)
_, ok = f4.GetFrom(newEnriched)
assert.True(t, ok)
// f4 does not exist on old we
v, ok := f4.GetFrom(enrichedErr)
assert.False(t, ok)
assert.False(t, v)
})
}