-
Notifications
You must be signed in to change notification settings - Fork 2
/
tree_test.go
108 lines (91 loc) · 2.37 KB
/
tree_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
package errortree
import (
"errors"
"testing"
"github.com/stretchr/testify/require"
)
func TestNew(t *testing.T) {
// Validate default options
tree := New()
require.NotNil(t, tree)
require.EqualValues(t, DefaultDelimiter, tree.Delimiter)
require.NotNil(t, tree.Formatter)
require.NotNil(t, tree.Errors)
// Misc: validate that getErrors (internal function) initializes the Errors map
tree = &Tree{}
require.NotNil(t, tree.getErrors())
require.NotNil(t, tree.Errors)
}
func TestTree_ErrorOrNil(t *testing.T) {
// Create new error
tree := &Tree{}
require.NotNil(t, tree)
require.Nil(t, tree.ErrorOrNil())
// Set t to nil, this should not panic but rather return nil
tree = nil
require.Nil(t, tree.ErrorOrNil())
tree = &Tree{
Errors: map[string]error{
"test": errors.New("test"),
},
}
require.NotNil(t, tree.ErrorOrNil())
}
func TestTree_WrappedErrors(t *testing.T) {
tree := &Tree{
// Use keys and error messages that would be sorted differently.
// This is required to ensure that sorting is done using the keys.
Errors: map[string]error{
"a": errors.New("c"),
"b": errors.New("a"),
"c": errors.New("b"),
},
}
wrappedErrors := tree.WrappedErrors()
require.Len(t, wrappedErrors, 3)
require.EqualValues(t, wrappedErrors, []error{
errors.New("c"),
errors.New("a"),
errors.New("b"),
})
}
func TestTree_Error(t *testing.T) {
treeErrors := map[string]error{
"a": errors.New("c"),
"b": &Tree{
Errors: map[string]error{
"0": errors.New("test0"),
"1": errors.New("test1"),
},
},
"c": errors.New("b"),
}
flattenedErrors := map[string]error{
"a": errors.New("c"),
"b.0": errors.New("test0"),
"b.1": errors.New("test1"),
"c": errors.New("b"),
}
formatter := func(errorMap map[string]error) string {
// Validate that we receive the expected flattened error map into the formatter
require.EqualValues(t, flattenedErrors, errorMap)
return "formatter_called"
}
tree := &Tree{
Formatter: formatter,
Errors: treeErrors,
Delimiter: ".",
}
// Call the formatter, but only require that formatter_called is returned
require.EqualValues(t, "formatter_called", tree.Error())
}
func TestGetTree(t *testing.T) {
err := errors.New("Test")
tree, isTree := GetTree(err)
require.Nil(t, tree)
require.False(t, isTree)
err = &Tree{}
tree, isTree = GetTree(err)
require.EqualValues(t, err, tree)
require.True(t, isTree)
}