forked from google/go-tpm-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrouped_error_test.go
126 lines (110 loc) · 3.54 KB
/
grouped_error_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
package server
import (
"errors"
"fmt"
"testing"
)
func TestGroupedError(t *testing.T) {
var gErr GroupedError
gErr.Errors = append(gErr.Errors, errors.New("error1"))
gErr.Errors = append(gErr.Errors, errors.New("error2"))
gErr.Errors = append(gErr.Errors, fmt.Errorf("fmted error"))
gErr.Errors = append(gErr.Errors, fmt.Errorf("wrapped: %w", errors.New("error3")))
gErr.Prefix = "failed action:"
expected := `failed action:
error1
error2
fmted error
wrapped: error3`
if gErr.Error() != expected {
t.Errorf("error string output (%s) did not match expected (%s)",
gErr.Error(), expected)
}
}
func TestEmptyGroupedError(t *testing.T) {
outErr := GroupedError{Prefix: "foo:", Errors: []error{}}
if outErr.Error() != fatalError {
t.Errorf("error string output (%s) did not match fatal error (%s)",
outErr.Error(), fatalError)
}
}
func TestCreateGroupedErrorFail(t *testing.T) {
outErr := createGroupedError("foo:", []error{})
if outErr != nil {
t.Errorf("expected nil error!")
}
}
func TestContainsOnlySubstring(t *testing.T) {
wholeString := "err error errorz"
err := errors.New(wholeString)
outErr := GroupedError{Prefix: "foo:", Errors: []error{err}}
if !outErr.containsOnlySubstring("error") {
t.Errorf("expected a match for substring")
}
if !outErr.containsOnlySubstring("err") {
t.Errorf("expected a match for substring")
}
if !outErr.containsOnlySubstring("") {
t.Errorf("expected a match for substring")
}
if !outErr.containsOnlySubstring(wholeString) {
t.Errorf("expected a match for substring")
}
}
func TestContainsOnlySubstringsFalse(t *testing.T) {
wholeString := "err error errorz"
err := errors.New(wholeString)
outErr := GroupedError{Prefix: "foo:", Errors: []error{err}}
tests := []struct {
name string
substring string
}{
{"AdditionalCharacterStart", "." + wholeString},
{"AdditionalCharacterEnd", wholeString + "."},
{"RemovedCharacter", wholeString[:5] + wholeString[6:]},
{"ReplacedCharacter", wholeString[:5] + "." + wholeString[6:]},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if outErr.containsOnlySubstring(test.substring) {
t.Errorf("expected failed matching for substring")
}
})
}
}
func TestContainsKnownSubstrings(t *testing.T) {
err := errors.New("err error errorz")
err2 := errors.New("new newww newzz")
err3 := errors.New("iss issue issues")
outErr := GroupedError{Prefix: "foo:", Errors: []error{err, err2, err3}}
if !outErr.containsKnownSubstrings([]string{"error", " newzz", " issue "}) {
t.Errorf("expected a match for known substrings")
}
}
func TestContainsKnownSubstringsFalse(t *testing.T) {
err := errors.New("err error errorz")
err2 := errors.New("new newww newzz")
err3 := errors.New("iss issue issues")
outErr := GroupedError{Prefix: "foo:", Errors: []error{err, err2, err3}}
tests := []struct {
name string
substrings []string
}{
{"NoSubstrings", []string{}},
{"OneEmptySubstring", []string{""}},
// Should fail, since there is overlap between substrings.
{"AllEmptySubstrings", []string{"", "", ""}},
{"FewerSubstrings", []string{"err"}},
{"FewerSubstrings2", []string{"error", " issue "}},
{"MoreSubstrings", []string{"error", " newzz", " issue ", " issues"}},
{"MoreSubstrings5", []string{"error", " newzz", " issue ", " issues", "err"}},
{"OverlappingSubstrings", []string{"error", " err", " issue "}},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if outErr.containsKnownSubstrings(test.substrings) {
t.Errorf("expected failed matching for known substrings")
}
})
}
}