-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformat_test.go
More file actions
109 lines (92 loc) · 2.53 KB
/
Copy pathformat_test.go
File metadata and controls
109 lines (92 loc) · 2.53 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
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
package faultline
import (
"errors"
"fmt"
"strings"
"testing"
)
func TestFormatNew(t *testing.T) {
err := New("test message")
tests := []struct {
format string
want string
}{
{"%s", "test message"},
{"%v", "test message"},
{"%q", `"test message"`},
}
for _, tt := range tests {
got := fmt.Sprintf(tt.format, err)
if got != tt.want {
t.Errorf("fmt.Sprintf(%q, New(...)): got %q, want %q", tt.format, got, tt.want)
}
}
// %+v should contain both message and stack
pv := fmt.Sprintf("%+v", err)
if !strings.HasPrefix(pv, "test message") {
t.Errorf("%%+v should start with message, got %q", pv)
}
if !strings.Contains(pv, "TestFormatNew") {
t.Errorf("%%+v should contain function name, got %q", pv)
}
}
func TestFormatWrap(t *testing.T) {
base := errors.New("base error")
err := Wrap(base, "wrapped")
tests := []struct {
format string
want string
}{
{"%s", "wrapped: base error"},
{"%v", "wrapped: base error"},
{"%q", `"wrapped: base error"`},
}
for _, tt := range tests {
got := fmt.Sprintf(tt.format, err)
if got != tt.want {
t.Errorf("fmt.Sprintf(%q, Wrap(...)): got %q, want %q", tt.format, got, tt.want)
}
}
// %+v should contain both messages and stack
pv := fmt.Sprintf("%+v", err)
if !strings.Contains(pv, "wrapped") {
t.Errorf("%%+v should contain wrap message, got %q", pv)
}
if !strings.Contains(pv, "base error") {
t.Errorf("%%+v should contain base message, got %q", pv)
}
}
func TestFormatWithStack(t *testing.T) {
base := errors.New("base")
err := WithStack(base)
if got := fmt.Sprintf("%s", err); got != "base" {
t.Errorf("%%s: got %q, want %q", got, "base")
}
if got := fmt.Sprintf("%v", err); got != "base" {
t.Errorf("%%v: got %q, want %q", got, "base")
}
pv := fmt.Sprintf("%+v", err)
if !strings.Contains(pv, "base") {
t.Errorf("%%+v should contain message, got %q", pv)
}
if !strings.Contains(pv, "TestFormatWithStack") {
t.Errorf("%%+v should contain function name, got %q", pv)
}
}
func TestFormatWithMessage(t *testing.T) {
base := New("base")
err := WithMessage(base, "context")
if got := fmt.Sprintf("%s", err); got != "context: base" {
t.Errorf("%%s: got %q, want %q", got, "context: base")
}
if got := fmt.Sprintf("%v", err); got != "context: base" {
t.Errorf("%%v: got %q, want %q", got, "context: base")
}
pv := fmt.Sprintf("%+v", err)
if !strings.Contains(pv, "context") {
t.Errorf("%%+v should contain context message, got %q", pv)
}
if !strings.Contains(pv, "base") {
t.Errorf("%%+v should contain base message, got %q", pv)
}
}