-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrf_test.go
More file actions
63 lines (59 loc) · 1.99 KB
/
Copy patherrf_test.go
File metadata and controls
63 lines (59 loc) · 1.99 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
package fmt
import "testing"
// Errf used to truncate any message that had literal text before a verb: the formatted
// value and everything after it vanished, with no error reported.
//
// Cause: wrFormat writes into dest, and for Errf dest IS BuffErr. After formatting a value
// it checked `hasContent(BuffErr)` to detect a formatting failure — but the literal prefix
// already sitting in BuffErr made that check a false positive, so it bailed out early.
//
// The failure was SILENT: no panic, no error, just a diagnostic message with its subject
// missing. `Errf("filetype: %s is not allowed", "PDF")` returned "filetype: ".
func TestErrfKeepsTextAroundVerbs(t *testing.T) {
tests := []struct {
name string
got string
expect string
}{
{
name: "literal text before the verb",
got: Errf("filetype: %s is not allowed", "PDF").Error(),
expect: "filetype: PDF is not allowed",
},
{
name: "verb first (this case always worked: BuffErr was still empty)",
got: Errf("%s is not allowed", "PDF").Error(),
expect: "PDF is not allowed",
},
{
name: "no verbs at all",
got: Errf("plain text, no verbs").Error(),
expect: "plain text, no verbs",
},
{
name: "several verbs with text between them",
got: Errf("db: row %d of table %s failed", 7, "users").Error(),
expect: "db: row 7 of table users failed",
},
{
name: "trailing text after the last verb",
got: Errf("port %d is busy", 8080).Error(),
expect: "port 8080 is busy",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.got != tt.expect {
t.Errorf("Errf() = %q, want %q", tt.got, tt.expect)
}
})
}
}
// An unsupported verb must still report loudly — fixing the truncation must not turn a
// noisy failure into a silent one.
func TestErrfUnsupportedVerbStillReports(t *testing.T) {
got := Errf("scan: %T", 1).Error()
if got == "scan: " || got == "" {
t.Fatalf("Errf() = %q — an unsupported verb must not fail silently", got)
}
}