-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelp_test.go
54 lines (47 loc) · 1.58 KB
/
help_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
package stackfmt
import (
"fmt"
"regexp"
"strings"
"testing"
)
func testFormatRegexp(t *testing.T, n int, arg interface{}, format, wantAll string) {
t.Helper()
got := fmt.Sprintf(format, arg)
gotLines := strings.SplitN(got, "\n", -1)
wantLines := strings.SplitN(wantAll, "\n", -1)
if len(wantLines) > len(gotLines) {
t.Errorf("test %d: wantLines(%d) > gotLines(%d):\n got: %q\nwant: %q", n+1, len(wantLines), len(gotLines), got, wantLines)
return
}
for i, wantLine := range wantLines {
want := wantLine
got := gotLines[i]
adjustedGot := regexp.MustCompile(`\S.*/stackfmt/`).ReplaceAllString(got, `github.com/gregwebs/stackfmt/`)
match, err := regexp.MatchString(want, adjustedGot)
if err != nil {
t.Fatal(err)
}
if !match {
t.Errorf("test %d: line %d: fmt.Sprintf(%q, err):\n got: %q\nwant: %q", n+1, i+1, format, adjustedGot, want)
}
}
}
func testFormatString(t *testing.T, n int, arg interface{}, format, wantAll string) {
t.Helper()
got := fmt.Sprintf(format, arg)
gotLines := strings.SplitN(got, "\n", -1)
wantLines := strings.SplitN(wantAll, "\n", -1)
if len(wantLines) > len(gotLines) {
t.Errorf("test %d: wantLines(%d) > gotLines(%d):\n got: %q\nwant: %q", n+1, len(wantLines), len(gotLines), got, wantLines)
return
}
for i, wantLine := range wantLines {
want := wantLine
got := gotLines[i]
adjustedGot := regexp.MustCompile(`\S.*/stackfmt/`).ReplaceAllString(got, `github.com/gregwebs/stackfmt/`)
if want != adjustedGot {
t.Errorf("test %d: line %d: fmt.Sprintf(%q, err):\n got: %q\nwant: %q", n+1, i+1, format, adjustedGot, want)
}
}
}