-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patherrstack_test.go
112 lines (83 loc) · 2.4 KB
/
errstack_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
package errstack
import (
"strings"
"testing"
)
func TestBasic(t *testing.T) {
// 1. First error
e := New("password field is missing")
// 2. And then the second error occured.
e.Append("company name is missing")
// 3. And then the third error occured.
e.Append("username is too short")
if len(e.stack) != 3 {
t.Fatalf("There should be 3 errors. Got: %v", len(e.stack))
}
if e.Error() == "" {
t.Fatalf("error string should not be empty")
}
for i, err := range e.GetAll() {
if err.filename == "" {
t.Fatalf("The filename metadata should not be empty")
}
if err.line == 0 {
t.Fatalf("The line metadata should not be empty")
}
if err.err == "" {
t.Fatalf("The error string should not be empty")
}
if i == 0 && !strings.Contains(err.Error(), "username is too short") {
t.Fatalf("The last error should be printed first")
}
if i == 2 && !strings.Contains(err.Error(), "password field is missing") {
t.Fatalf("The first error should be printed last")
}
}
}
func TestPopAll(t *testing.T) {
e := New("password field is missing")
// 2. And then the second error occured.
e.Append("company name is missing")
e.PopAll()
if len(e.stack) != 0 {
t.Fatal("after PopAll, the stack should be empty")
}
}
func TestNoMetadata(t *testing.T) {
// 1. First error
e := New("password field is missing")
// 2. And then the second error occured.
e.Append("company name is missing")
// 3. And then the third error occured.
e.Append("username is too short")
if len(e.stack) != 3 {
t.Fatalf("There should be 3 errors. Got: %v", len(e.stack))
}
e.SetShowMetadata(false)
if e.showMetadata != false {
t.Fatalf("Failed to set showMetadata")
}
expected := "username is too short, company name is missing, password field is missing"
if e.Error() != expected {
t.Fatalf("Error string incorrect.\nexpected: %v,\ngot: %v", expected, e.Error())
}
}
func TestTrimFilename(t *testing.T) {
// 1. First error
e := New("password field is missing")
// 2. And then the second error occured.
e.Append("company name is missing")
// 3. And then the third error occured.
e.Append("username is too short")
if len(e.stack) != 3 {
t.Fatalf("There should be 3 errors. Got: %v", len(e.stack))
}
e.SetTrimFilename(true)
if e.trimFilename != true {
t.Fatalf("Failed to set trimFilename")
}
e.SetTrimFilename(false)
if e.trimFilename != false {
t.Fatalf("Failed to set trimFilename")
}
}