Test Setup Failed
Pull Request — master (#3)
by Christian
01:37
created

e2h_test.TestEnhancedError_FormatStdError_JSON   A

Complexity

Conditions 1

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nop 1
dl 0
loc 10
rs 10
c 0
b 0
f 0
1
/*
2
Package e2h_test its the test package of the Enhanced Error Handling module
3
*/
4
package e2h_test
5
6
import (
7
	"fmt"
8
	"os"
9
	"path/filepath"
10
	"runtime"
11
	"strings"
12
	"testing"
13
14
	"github.com/cdleo/go-e2h"
15
	e2hformat "github.com/cdleo/go-e2h/formatter"
16
	"github.com/stretchr/testify/require"
17
)
18
19
func TestEnhancedError_StdErr_RawFormatter_GetSource(t *testing.T) {
20
21
	// Setup
22
	stdErr := fmt.Errorf("This is a standard error")
23
	rawFormatter, err := e2hformat.NewFormatter(e2hformat.Format_Raw)
24
	require.Nil(t, err)
25
26
	// Execute
27
	output_raw := rawFormatter.Source(stdErr)
28
29
	// Check
30
	require.Equal(t, output_raw, stdErr.Error())
31
}
32
33
func TestEnhancedError_StdErr_JSONFormatter_GetSource(t *testing.T) {
34
35
	// Setup
36
	stdErr := fmt.Errorf("This is a standard error")
37
	jsonFormatter, err := e2hformat.NewFormatter(e2hformat.Format_JSON)
38
	require.Nil(t, err)
39
40
	// Execute
41
	output_json := jsonFormatter.Source(stdErr)
42
43
	// Check
44
	require.Equal(t, output_json, "{\"error\":\"This is a standard error\"}")
45
}
46
47
func TestEnhancedError_StdErr_RawFormatter_Format(t *testing.T) {
48
49
	// Setup
50
	stdErr := fmt.Errorf("This is a standard error")
51
	rawFormatter, _ := e2hformat.NewFormatter(e2hformat.Format_Raw)
52
	params := e2hformat.Params{}
53
54
	// Execute
55
	output := rawFormatter.Format(stdErr, params)
56
57
	// Check
58
	require.Equal(t, output, "This is a standard error")
59
}
60
61
func TestEnhancedError_StdErr_JSONFormatter_Format(t *testing.T) {
62
63
	// Setup
64
	stdErr := fmt.Errorf("This is a standard error")
65
	jsonFormatter, _ := e2hformat.NewFormatter(e2hformat.Format_JSON)
66
	params := e2hformat.Params{}
67
68
	// Execute
69
	outputJSON := jsonFormatter.Format(stdErr, params)
70
71
	// Check
72
	require.Equal(t, outputJSON, "{\"error\":\"This is a standard error\",\"stack_trace\":[]}")
73
}
74
75
func TestEnhancedError_EnhErr_RawFormatter_GetCause(t *testing.T) {
76
77
	// Setup
78
	enhancedErr := e2h.Tracem(fmt.Errorf("This is a standard error"), "Error wrapped with additional info")
79
	rawFormatter, _ := e2hformat.NewFormatter(e2hformat.Format_Raw)
80
81
	// Execute
82
	output := rawFormatter.Source(enhancedErr)
83
84
	// Check
85
	require.Equal(t, output, "This is a standard error [Error wrapped with additional info]")
86
}
87
88
func TestEnhancedError_EnhErr_JSONFormatter_GetCause(t *testing.T) {
89
90
	// Setup
91
	enhancedErr := e2h.Tracem(fmt.Errorf("This is a standard error"), "Error wrapped with additional info")
92
	jsonFormatter, _ := e2hformat.NewFormatter(e2hformat.Format_JSON)
93
94
	// Execute
95
	output := jsonFormatter.Source(enhancedErr)
96
97
	// Check
98
	require.Equal(t, output, "{\"error\":\"This is a standard error\",\"context\":\"Error wrapped with additional info\"}")
99
}
100
101
func TestEnhancedError_EnhErr_RawFormatter_Format(t *testing.T) {
102
103
	// Setup
104
	enhancedErr := e2h.Tracem(fmt.Errorf("This is a standard error"), "Error wrapped with additional info")
105
	rawFormatter, _ := e2hformat.NewFormatter(e2hformat.Format_Raw)
106
	_, b, _, _ := runtime.Caller(0)
107
	hideThisPath := filepath.Dir(b) + string(os.PathSeparator)
108
	params := e2hformat.Params{
109
		PathHidingMethod: e2hformat.HidingMethod_FullBaseline,
110
		PathHidingValue:  hideThisPath,
111
	}
112
113
	// Execute
114
	output := rawFormatter.Format(enhancedErr, params)
115
116
	// Check
117
	require.Equal(t, output, "This is a standard error; github.com/cdleo/go-e2h_test.TestEnhancedError_EnhErr_RawFormatter_Format (e2h_test.go:104) [Error wrapped with additional info];")
118
}
119
120
func TestEnhancedError_EnhErr_JSONFormatter_Format(t *testing.T) {
121
122
	// Setup
123
	enhancedErr := e2h.Tracem(fmt.Errorf("This is a standard error"), "Error wrapped with additional info")
124
	jsonFormatter, _ := e2hformat.NewFormatter(e2hformat.Format_JSON)
125
	_, b, _, _ := runtime.Caller(0)
126
	hideThisPath := filepath.Dir(b) + string(os.PathSeparator)
127
	params := e2hformat.Params{
128
		PathHidingMethod: e2hformat.HidingMethod_FullBaseline,
129
		PathHidingValue:  hideThisPath,
130
	}
131
132
	// Execute
133
	output := jsonFormatter.Format(enhancedErr, params)
134
135
	// Check
136
	require.Equal(t, output, "{\"error\":\"This is a standard error\",\"stack_trace\":[{\"func\":\"github.com/cdleo/go-e2h_test.TestEnhancedError_EnhErr_JSONFormatter_Format\",\"caller\":\"e2h_test.go:123\",\"context\":\"Error wrapped with additional info\"}]}")
137
}
138
139
func TestEnhancedError_EnhErr_RawFormatter_Format_Beautified(t *testing.T) {
140
141
	// Setup
142
	enhancedErr := e2h.Tracem(fmt.Errorf("This is a standard error"), "Error wrapped with additional info")
143
	rawFormatter, _ := e2hformat.NewFormatter(e2hformat.Format_Raw)
144
	_, b, _, _ := runtime.Caller(0)
145
	hideThisPath := filepath.Dir(b) + string(os.PathSeparator)
146
	params := e2hformat.Params{
147
		PathHidingMethod: e2hformat.HidingMethod_FullBaseline,
148
		PathHidingValue:  hideThisPath,
149
		Beautify:         true,
150
	}
151
152
	// Execute
153
	output := rawFormatter.Format(enhancedErr, params)
154
155
	// Check
156
	require.Equal(t, output, "This is a standard error\ngithub.com/cdleo/go-e2h_test.TestEnhancedError_EnhErr_RawFormatter_Format_Beautified (e2h_test.go:142)\n\tError wrapped with additional info")
157
}
158
159
func TestEnhancedError_EnhErr_JSONFormatter_Format_Beautified(t *testing.T) {
160
161
	// Setup
162
	enhancedErr := e2h.Tracem(fmt.Errorf("This is a standard error"), "Error wrapped with additional info")
163
	jsonFormatter, _ := e2hformat.NewFormatter(e2hformat.Format_JSON)
164
	_, b, _, _ := runtime.Caller(0)
165
	hideThisPath := filepath.Dir(b) + string(os.PathSeparator)
166
	params := e2hformat.Params{
167
		PathHidingMethod: e2hformat.HidingMethod_FullBaseline,
168
		PathHidingValue:  hideThisPath,
169
		Beautify:         true,
170
	}
171
172
	// Execute
173
	output := jsonFormatter.Format(enhancedErr, params)
174
175
	// Check
176
	require.Equal(t, output, "{\n\t\"error\": \"This is a standard error\",\n\t\"stack_trace\": [\n\t\t{\n\t\t\t\"func\": \"github.com/cdleo/go-e2h_test.TestEnhancedError_EnhErr_JSONFormatter_Format_Beautified\",\n\t\t\t\"caller\": \"e2h_test.go:162\",\n\t\t\t\"context\": \"Error wrapped with additional info\"\n\t\t}\n\t]\n}")
177
}
178
179
func TestEnhancedError_EnhErr_RawFormatter_Format_Inverted(t *testing.T) {
180
181
	// Setup
182
	enhancedErr := e2h.Tracem(fmt.Errorf("This is a standard error"), "Error wrapped with additional info")
183
	rawFormatter, _ := e2hformat.NewFormatter(e2hformat.Format_Raw)
184
	_, b, _, _ := runtime.Caller(0)
185
	hideThisPath := filepath.Dir(b) + string(os.PathSeparator)
186
	params := e2hformat.Params{
187
		PathHidingMethod: e2hformat.HidingMethod_FullBaseline,
188
		PathHidingValue:  hideThisPath,
189
		InvertCallstack:  true,
190
	}
191
192
	// Execute
193
	output := rawFormatter.Format(enhancedErr, params)
194
195
	// Check
196
	require.Equal(t, output, "github.com/cdleo/go-e2h_test.TestEnhancedError_EnhErr_RawFormatter_Format_Inverted (e2h_test.go:182) [Error wrapped with additional info]; This is a standard error;")
197
}
198
199
func TestEnhancedError_EnhErr_JSONFormatter_Format_Inverted(t *testing.T) {
200
201
	// Setup
202
	enhancedErr := e2h.Tracem(fmt.Errorf("This is a standard error"), "Error wrapped with additional info")
203
	jsonFormatter, _ := e2hformat.NewFormatter(e2hformat.Format_JSON)
204
	_, b, _, _ := runtime.Caller(0)
205
	hideThisPath := filepath.Dir(b) + string(os.PathSeparator)
206
	params := e2hformat.Params{
207
		PathHidingMethod: e2hformat.HidingMethod_FullBaseline,
208
		PathHidingValue:  hideThisPath,
209
		InvertCallstack:  true,
210
	}
211
212
	// Execute
213
	output := jsonFormatter.Format(enhancedErr, params)
214
215
	// Check
216
	require.Equal(t, output, "{\"error\":\"This is a standard error\",\"stack_trace\":[{\"func\":\"github.com/cdleo/go-e2h_test.TestEnhancedError_EnhErr_JSONFormatter_Format_Inverted\",\"caller\":\"e2h_test.go:202\",\"context\":\"Error wrapped with additional info\"}]}")
217
}
218
219
func TestEnhancedError_EnhErr_RawFormatter_Format_FullPathHidden(t *testing.T) {
220
221
	// Setup
222
	enhancedErr := e2h.Tracem(fmt.Errorf("This is a standard error"), "Error wrapped with additional info")
223
	rawFormatter, _ := e2hformat.NewFormatter(e2hformat.Format_Raw)
224
	_, b, _, _ := runtime.Caller(0)
225
	hideThisPath := filepath.Dir(b) + string(os.PathSeparator)
226
	params := e2hformat.Params{
227
		PathHidingMethod: e2hformat.HidingMethod_FullBaseline,
228
		PathHidingValue:  hideThisPath,
229
	}
230
231
	// Execute
232
	output := rawFormatter.Format(enhancedErr, params)
233
234
	// Check
235
	require.Equal(t, output, "This is a standard error; github.com/cdleo/go-e2h_test.TestEnhancedError_EnhErr_RawFormatter_Format_FullPathHidden (e2h_test.go:222) [Error wrapped with additional info];")
236
}
237
238
func TestEnhancedError_EnhErr_JSONFormatter_Format_FullPathHidden(t *testing.T) {
239
240
	// Setup
241
	enhancedErr := e2h.Tracem(fmt.Errorf("This is a standard error"), "Error wrapped with additional info")
242
	jsonFormatter, _ := e2hformat.NewFormatter(e2hformat.Format_JSON)
243
	_, b, _, _ := runtime.Caller(0)
244
	hideThisPath := filepath.Dir(b) + string(os.PathSeparator)
245
	params := e2hformat.Params{
246
		PathHidingMethod: e2hformat.HidingMethod_FullBaseline,
247
		PathHidingValue:  hideThisPath,
248
	}
249
250
	// Execute
251
	output := jsonFormatter.Format(enhancedErr, params)
252
253
	// Check
254
	require.Equal(t, output, "{\"error\":\"This is a standard error\",\"stack_trace\":[{\"func\":\"github.com/cdleo/go-e2h_test.TestEnhancedError_EnhErr_JSONFormatter_Format_FullPathHidden\",\"caller\":\"e2h_test.go:241\",\"context\":\"Error wrapped with additional info\"}]}")
255
}
256
257
func TestEnhancedError_EnhErr_RawFormatter_Format_PartialPathHidden(t *testing.T) {
258
259
	// Setup
260
	enhancedErr := e2h.Tracem(fmt.Errorf("This is a standard error"), "Error wrapped with additional info")
261
	rawFormatter, _ := e2hformat.NewFormatter(e2hformat.Format_Raw)
262
	_, b, _, _ := runtime.Caller(0)
263
	path := strings.Split(filepath.Dir(b), string(os.PathSeparator))
264
	params := e2hformat.Params{
265
		PathHidingMethod: e2hformat.HidingMethod_ToFolder,
266
		PathHidingValue:  path[len(path)-1],
267
	}
268
269
	// Execute
270
	output := rawFormatter.Format(enhancedErr, params)
271
272
	want := strings.ReplaceAll("This is a standard error; github.com/cdleo/go-e2h_test.TestEnhancedError_EnhErr_RawFormatter_Format_PartialPathHidden (<LAST_DIR>/e2h_test.go:260) [Error wrapped with additional info];",
273
		"<LAST_DIR>", params.PathHidingValue)
274
275
	// Check
276
	require.Equal(t, want, output)
277
}
278
279
func TestEnhancedError_EnhErr_JSONFormatter_Format_PartialPathHidden(t *testing.T) {
280
281
	// Setup
282
	enhancedErr := e2h.Tracem(fmt.Errorf("This is a standard error"), "Error wrapped with additional info")
283
	jsonFormatter, _ := e2hformat.NewFormatter(e2hformat.Format_JSON)
284
	_, b, _, _ := runtime.Caller(0)
285
	path := strings.Split(filepath.Dir(b), string(os.PathSeparator))
286
	params := e2hformat.Params{
287
		PathHidingMethod: e2hformat.HidingMethod_ToFolder,
288
		PathHidingValue:  path[len(path)-1],
289
	}
290
291
	// Execute
292
	output := jsonFormatter.Format(enhancedErr, params)
293
294
	want := strings.ReplaceAll("{\"error\":\"This is a standard error\",\"stack_trace\":[{\"func\":\"github.com/cdleo/go-e2h_test.TestEnhancedError_EnhErr_JSONFormatter_Format_PartialPathHidden\",\"caller\":\"<LAST_DIR>/e2h_test.go:282\",\"context\":\"Error wrapped with additional info\"}]}",
295
		"<LAST_DIR>", params.PathHidingValue)
296
297
	// Check
298
	require.Equal(t, want, output)
299
}
300
301
func TestEnhancedError_EnhError_JSONFormatter_Format_MultipleTraces(t *testing.T) {
302
303
	// Setup
304
	enhancedErr := e2h.Tracem(fmt.Errorf("This is a standard error"), "Error wrapped with additional info")
305
	enhancedErr = e2h.Tracef(enhancedErr, "This is the %dnd. stack level", 2)
306
	enhancedErr = e2h.Trace(enhancedErr)
307
	jsonFormatter, _ := e2hformat.NewFormatter(e2hformat.Format_JSON)
308
	_, b, _, _ := runtime.Caller(0)
309
	hideThisPath := filepath.Dir(b) + string(os.PathSeparator)
310
	params := e2hformat.Params{
311
		Beautify:         true,
312
		PathHidingMethod: e2hformat.HidingMethod_FullBaseline,
313
		PathHidingValue:  hideThisPath,
314
	}
315
316
	// Execute
317
	output := jsonFormatter.Format(enhancedErr, params)
318
319
	// Check
320
	require.Equal(t, output, "{\n\t\"error\": \"This is a standard error\",\n\t\"stack_trace\": [\n\t\t{\n\t\t\t\"func\": \"github.com/cdleo/go-e2h_test.TestEnhancedError_EnhError_JSONFormatter_Format_MultipleTraces\",\n\t\t\t\"caller\": \"e2h_test.go:304\",\n\t\t\t\"context\": \"Error wrapped with additional info\"\n\t\t},\n\t\t{\n\t\t\t\"func\": \"github.com/cdleo/go-e2h_test.TestEnhancedError_EnhError_JSONFormatter_Format_MultipleTraces\",\n\t\t\t\"caller\": \"e2h_test.go:305\",\n\t\t\t\"context\": \"This is the 2nd. stack level\"\n\t\t},\n\t\t{\n\t\t\t\"func\": \"github.com/cdleo/go-e2h_test.TestEnhancedError_EnhError_JSONFormatter_Format_MultipleTraces\",\n\t\t\t\"caller\": \"e2h_test.go:306\"\n\t\t}\n\t]\n}")
321
}
322