-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers_test.go
183 lines (170 loc) · 3.68 KB
/
helpers_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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package disk
import (
"bytes"
"log"
"testing"
)
// Mock for os.Exit
var osExitCalled = false
var osExitCode = 0
var osExit = func(code int) {
osExitCalled = true
osExitCode = code
panic("os.Exit called")
}
func TestInArray(t *testing.T) {
tests := []struct {
name string
n int
array []int
expected bool
}{
{
name: "Empty array",
n: 5,
array: []int{},
expected: false,
},
{
name: "Single element array, element present",
n: 5,
array: []int{5},
expected: true,
},
{
name: "Single element array, element not present",
n: 5,
array: []int{3},
expected: false,
},
{
name: "Multiple elements, element present",
n: 5,
array: []int{1, 2, 3, 4, 5, 6, 7},
expected: true,
},
{
name: "Multiple elements, element not present",
n: 10,
array: []int{1, 2, 3, 4, 5, 6, 7},
expected: false,
},
{
name: "Duplicate elements, element present",
n: 5,
array: []int{1, 5, 2, 5, 3, 5, 4},
expected: true,
},
{
name: "Large array, element present",
n: 999,
array: func() []int {
arr := make([]int, 1000)
for i := range arr {
arr[i] = i
}
return arr
}(),
expected: true,
},
{
name: "Large array, element not present",
n: 1000,
array: func() []int {
arr := make([]int, 1000)
for i := range arr {
arr[i] = i
}
return arr
}(),
expected: false,
},
{
name: "Negative numbers, element present",
n: -5,
array: []int{-7, -6, -5, -4, -3},
expected: true,
},
{
name: "Negative numbers, element not present",
n: -8,
array: []int{-7, -6, -5, -4, -3},
expected: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := inArray(tt.n, tt.array)
if result != tt.expected {
t.Errorf("inArray(%d, %v) = %v; want %v", tt.n, tt.array, result, tt.expected)
}
})
}
}
func TestHandleError(t *testing.T) {
// Save the original log output and flags
originalOutput := log.Writer()
originalFlags := log.Flags()
defer func() {
// Restore the original log output and flags after the test
log.SetOutput(originalOutput)
log.SetFlags(originalFlags)
}()
// Create a buffer to capture log output
var buf bytes.Buffer
log.SetOutput(&buf)
// Remove timestamp from log output for easier testing
log.SetFlags(0)
// Override os.Exit to prevent the test from terminating
originalOsExit := osExit
defer func() { osExit = originalOsExit }()
var exitCode int
osExit = func(code int) {
exitCode = code
panic("os.Exit called")
}
tests := []struct {
name string
err error
expectedLog string
expectedPanic bool
}{
{
name: "Nil error",
err: nil,
expectedLog: "",
expectedPanic: false,
},
// todo
// {
// name: "Non-nil error",
// err: errors.New("test error"),
// expectedLog: "Error: test error\n",
// expectedPanic: true,
// },
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Clear the buffer before each test
buf.Reset()
exitCode = 0
// Use a function to capture panics
func() {
defer func() {
r := recover()
if (r != nil) != tt.expectedPanic {
t.Errorf("handleError() panic = %v, expectedPanic %v", r, tt.expectedPanic)
}
if r != nil && exitCode != 1 {
t.Errorf("Expected exit code 1, got %d", exitCode)
}
}()
handleError(tt.err)
}()
// Check the log output
if got := buf.String(); got != tt.expectedLog {
t.Errorf("handleError() log = %q, want %q", got, tt.expectedLog)
}
})
}
}