-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror_test.go
More file actions
68 lines (59 loc) · 3.06 KB
/
error_test.go
File metadata and controls
68 lines (59 loc) · 3.06 KB
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
// =============================================================================
// Project: tinyfmt
// File: error_test.go
// Description: Test suite for error functions in tinyfmt package.
// Datasheet/Docs:
//
// Author: Jason Duffy
// Created on: 07/07/2024
//
// Copyright: (C) 2024, Jason Duffy
// License: See LICENSE file in the project root for full license information.
// Disclaimer: See DISCLAIMER file in the project root for full disclaimer.
// =============================================================================
// -------------------------------------------------------------------------- //
// Import Statement //
// -------------------------------------------------------------------------- //
package tinyfmt
import (
"errors"
"testing"
)
// -------------------------------------------------------------------------- //
// Public Functions //
// -------------------------------------------------------------------------- //
func TestErrorf(t *testing.T) {
testCases := []struct {
format string
arguments []interface{}
want string
}{
{"Error: %s", []interface{}{"something went wrong"}, "Error: something went wrong"}, // Test formatting a string error message
{"Code: %d", []interface{}{404}, "Code: 404"}, // Test formatting an integer error code
{"Invalid: %q", []interface{}{42}, "unsupported format specifier"}, // Test with unsupported format specifier
{"Missing arg: %d %d", []interface{}{42}, "missing argument for %d"}, // Test with missing argument
{"", []interface{}{}, ""}, // Test with empty format string
{"Nil arg: %v", []interface{}{nil}, "Nil arg: <unsupported>"}, // Test with nil argument
{"Multiple: %d, %s, %v", []interface{}{42, "test", true}, "Multiple: 42, test, true"}, // Test with multiple format specifiers
{"Unsupported type: %v", []interface{}{map[string]int{"key": 1}}, "Unsupported type: {key:1}"}, // Test with unsupported type
{"Percent sign: %%", []interface{}{}, "Percent sign: %"}, // Test with escaped percent sign
}
for _, testCase := range testCases {
err := Errorf(testCase.format, testCase.arguments...)
if err.Error() != testCase.want {
t.Errorf("Errorf(%q, %v) = %q, want %q", testCase.format, testCase.arguments, err.Error(), testCase.want)
}
}
}
// Additional test for compatibility with errors.New()
func TestErrorfWithErrorsNew(t *testing.T) {
err1 := Errorf("This is a custom error with value: %d", 123)
err2 := errors.New("This is a standard error")
if err1.Error() == err2.Error() {
t.Errorf("Errorf generated error should not match errors.New() error: got %q, want different", err1.Error())
}
expected := "This is a custom error with value: 123"
if err1.Error() != expected {
t.Errorf("Errorf generated error = %q, want %q", err1.Error(), expected)
}
}