-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack_test.go
141 lines (119 loc) · 3.17 KB
/
stack_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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package uhoh
import (
"errors"
"testing"
)
func TestStack(t *testing.T) {
originalErr := errors.New("original error")
describeErr := errors.New("describe error")
tests := []struct {
err *Err
name string
file string
function string
line int
str string
}{
{
err: New(originalErr).SetDescribe(describeErr),
name: "stack0",
file: "stack_test.go",
function: "TestStack",
line: 20,
str: "stack_test.go:20 TestStack",
},
{
err: New(originalErr).SetDescribe(describeErr),
name: "stack1",
file: "stack_test.go",
function: "TestStack",
line: 28,
str: "stack_test.go:28 TestStack",
},
{
err: NewStackLevel(originalErr, 1).SetDescribe(describeErr),
name: "stack1",
file: "stack_test.go",
function: "TestStack",
line: 36,
str: "stack_test.go:36 TestStack",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := tt.err.FirstFrame()
if s.File != tt.file {
t.Errorf("TestStack File = %s, want %s", s.File, tt.file)
}
if s.Function != tt.function {
t.Errorf("TestStack Function = %s, want %s", s.Function, tt.function)
}
if s.Line != tt.line {
t.Errorf("TestStack Line = %d, want %d", s.Line, tt.line)
}
if s.String() != tt.str {
t.Errorf("Stack.String() = %v, want %v", s.String(), tt.str)
}
})
}
}
func TestFirstFrameZeroFrameStackDepth(t *testing.T) {
ogStackDepth := stackDepth
SetStackDepth(0)
defer SetStackDepth(ogStackDepth)
err := New(errors.New("original error"))
ff := err.FirstFrame()
if ff != nil {
t.Error("FirstFrame should be nil")
}
// Test that the stack in Err is the correct length
if len(err.Stack) != 0 {
t.Error("Stack was not set correctly")
}
}
func TestStackLevelHigherThanStackDepth(t *testing.T) {
err := NewStackLevel(errors.New("original error"), 10)
// Test that stack is empty
if len(err.Stack) != 0 {
t.Error("Stack was not set correctly")
}
// Try to grab the first frame
ff := err.FirstFrame()
if ff != nil {
t.Error("FirstFrame should be nil")
}
// Get string representation of the stack
str := err.FirstFrame().String()
if str != "" {
t.Error("Stack was not set correctly")
}
}
func TestSetStackDepth(t *testing.T) {
ogStackDepth := stackDepth
SetStackDepth(2)
defer SetStackDepth(ogStackDepth)
err := New(errors.New("original error"))
// Test that the stack depth is set correctly
if stackDepth != 2 {
t.Error("Stack depth was not set correctly")
}
// Test that the stack in Err is the correct length
if len(err.Stack) != 2 {
t.Error("Stack was not set correctly")
}
}
// TestLargeStackDepth - Testing if the stack depth is set larger than the amount of stack frames there are
func TestLargeStackDepth(t *testing.T) {
ogStackDepth := stackDepth
SetStackDepth(10)
defer SetStackDepth(ogStackDepth)
err := New(errors.New("original error"))
// Test that the stack depth is set correctly
if stackDepth != 10 {
t.Error("Stack depth was not set correctly")
}
// Test that the stack in Err is the correct length
if len(err.Stack) > 5 {
t.Errorf("Stack was not set correctly. Depth %d", len(err.Stack))
}
}