Debian Codegraph @

server/grouped_error_test.go

raw
1 package server
2
3 import (
4 "errors"
5 "fmt"
6 "testing"
7 )
8
9 func TestGroupedError(t *testing.T) {
10 var gErr GroupedError
11 gErr.Errors = append(gErr.Errors, errors.New("error1"))
12 gErr.Errors = append(gErr.Errors, errors.New("error2"))
13 gErr.Errors = append(gErr.Errors, fmt.Errorf("fmted error"))
14 gErr.Errors = append(gErr.Errors, fmt.Errorf("wrapped: %w", errors.New("error3")))
15 gErr.Prefix = "failed action:"
16
17 expected := `failed action:
18 error1
19 error2
20 fmted error
21 wrapped: error3`
22
23 if gErr.Error() != expected {
24 t.Errorf("error string output (%s) did not match expected (%s)",
25 gErr.Error(), expected)
26 }
27 }
28
29 func TestEmptyGroupedError(t *testing.T) {
30 outErr := GroupedError{Prefix: "foo:", Errors: []error{}}
31 if outErr.Error() != fatalError {
32 t.Errorf("error string output (%s) did not match fatal error (%s)",
33 outErr.Error(), fatalError)
34 }
35 }
36
37 func TestCreateGroupedErrorFail(t *testing.T) {
38 outErr := createGroupedError("foo:", []error{})
39 if outErr != nil {
40 t.Errorf("expected nil error!")
41 }
42 }
43
44 func TestContainsOnlySubstring(t *testing.T) {
45 wholeString := "err error errorz"
46 err := errors.New(wholeString)
47 outErr := GroupedError{Prefix: "foo:", Errors: []error{err}}
48 if !outErr.containsOnlySubstring("error") {
49 t.Errorf("expected a match for substring")
50 }
51 if !outErr.containsOnlySubstring("err") {
52 t.Errorf("expected a match for substring")
53 }
54 if !outErr.containsOnlySubstring("") {
55 t.Errorf("expected a match for substring")
56 }
57 if !outErr.containsOnlySubstring(wholeString) {
58 t.Errorf("expected a match for substring")
59 }
60 }
61
62 func TestContainsOnlySubstringsFalse(t *testing.T) {
63 wholeString := "err error errorz"
64 err := errors.New(wholeString)
65 outErr := GroupedError{Prefix: "foo:", Errors: []error{err}}
66
67 tests := []struct {
68 name string
69 substring string
70 }{
71 {"AdditionalCharacterStart", "." + wholeString},
72 {"AdditionalCharacterEnd", wholeString + "."},
73 {"RemovedCharacter", wholeString[:5] + wholeString[6:]},
74 {"ReplacedCharacter", wholeString[:5] + "." + wholeString[6:]},
75 }
76
77 for _, test := range tests {
78 t.Run(test.name, func(t *testing.T) {
79 if outErr.containsOnlySubstring(test.substring) {
80 t.Errorf("expected failed matching for substring")
81 }
82
83 })
84 }
85 }
86
87 func TestContainsKnownSubstrings(t *testing.T) {
88 err := errors.New("err error errorz")
89 err2 := errors.New("new newww newzz")
90 err3 := errors.New("iss issue issues")
91 outErr := GroupedError{Prefix: "foo:", Errors: []error{err, err2, err3}}
92 if !outErr.containsKnownSubstrings([]string{"error", " newzz", " issue "}) {
93 t.Errorf("expected a match for known substrings")
94 }
95 }
96
97 func TestContainsKnownSubstringsFalse(t *testing.T) {
98 err := errors.New("err error errorz")
99 err2 := errors.New("new newww newzz")
100 err3 := errors.New("iss issue issues")
101 outErr := GroupedError{Prefix: "foo:", Errors: []error{err, err2, err3}}
102
103 tests := []struct {
104 name string
105 substrings []string
106 }{
107 {"NoSubstrings", []string{}},
108 {"OneEmptySubstring", []string{""}},
109 // Should fail, since there is overlap between substrings.
110 {"AllEmptySubstrings", []string{"", "", ""}},
111 {"FewerSubstrings", []string{"err"}},
112 {"FewerSubstrings2", []string{"error", " issue "}},
113 {"MoreSubstrings", []string{"error", " newzz", " issue ", " issues"}},
114 {"MoreSubstrings5", []string{"error", " newzz", " issue ", " issues", "err"}},
115 {"OverlappingSubstrings", []string{"error", " err", " issue "}},
116 }
117
118 for _, test := range tests {
119 t.Run(test.name, func(t *testing.T) {
120 if outErr.containsKnownSubstrings(test.substrings) {
121 t.Errorf("expected failed matching for known substrings")
122 }
123
124 })
125 }
126 }