-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrors_test.go
76 lines (69 loc) · 1.92 KB
/
errors_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
package errors
import (
"bytes"
"fmt"
"testing"
)
func TestNew(t *testing.T) {
tests := []struct {
name string
msg string
}{
{name: "new error", msg: "error"},
{name: "not found", msg: "file not found"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := New(tt.msg)
if err.Error() != tt.msg {
t.Errorf("unmatch Error() message. out=%s, want=%s", err.Error(), tt.msg)
} else {
t.Log(err.Error())
}
// check implements fmt.Formatter
var buf bytes.Buffer
if _, err := buf.WriteString(fmt.Sprintf("%s\n", err)); err != nil {
t.Errorf("error unexpected. err=%s", err)
}
if _, err := buf.WriteString(fmt.Sprintf("%v", err)); err != nil {
t.Errorf("error unexpected. err=%v", err)
}
// output log
t.Log(buf.String())
})
}
}
func TestErrorf(t *testing.T) {
type args struct {
format string
args []any
}
tests := []struct {
name string
args args
}{
{name: "string format", args: args{format: "%s", args: []any{"error"}}},
{name: "number format", args: args{format: "%d", args: []any{123}}},
{name: "struct format", args: args{format: "%v", args: []any{struct{ msg string }{msg: "error"}}}},
{name: "multiple format", args: args{format: "%s %d %v", args: []any{"error", 123, struct{ msg string }{msg: "error"}}}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := Errorf(tt.args.format, tt.args.args...)
msg := fmt.Sprintf(tt.args.format, tt.args.args...)
if err.Error() != msg {
t.Errorf("unmatch Error() message. out=%s, want=%s", err.Error(), msg)
}
// check implements fmt.Formatter
var buf bytes.Buffer
if _, err := buf.WriteString(fmt.Sprintf("%s\n", err)); err != nil {
t.Errorf("error unexpected. err=%s", err)
}
if _, err := buf.WriteString(fmt.Sprintf("%v", err)); err != nil {
t.Errorf("error unexpected. err=%v", err)
}
// output log
t.Log(buf.String())
})
}
}