-
Notifications
You must be signed in to change notification settings - Fork 5
/
leftpad_test.go
54 lines (51 loc) · 1.19 KB
/
leftpad_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 leftpad
import "testing"
func TestPad(t *testing.T) {
for _, testcase := range []struct {
s string
n int
want string
}{
{"foo", 2, "foo"},
{"foo", 3, "foo"},
{"foo", 4, " foo"},
{"foo", 5, " foo"},
} {
have, err := Pad(testcase.s, testcase.n)
if err != nil {
t.Errorf("Pad(%q, %d): %v", testcase.s, testcase.n, err)
continue
}
if want := testcase.want; want != have {
t.Errorf("Pad(%q, %d): want %q, have %q", testcase.s, testcase.n, want, have)
continue
}
}
}
func TestPadChar(t *testing.T) {
for _, testcase := range []struct {
s string
n int
r rune
want string
}{
{"foo", 2, 'X', "foo"},
{"foo", 3, 'X', "foo"},
{"foo", 4, 'X', "Xfoo"},
{"foo", 5, 'X', "XXfoo"},
} {
have, err := PadChar(testcase.s, testcase.n, testcase.r)
if err != nil {
t.Errorf("PadChar(%q, %d, %c): %v", testcase.s, testcase.n, testcase.r, err)
continue
}
if want := testcase.want; want != have {
t.Errorf("Pad(%q, %d, %c): want %q, have %q", testcase.s, testcase.n, testcase.r, want, have)
continue
}
}
have, err := PadChar("foo", -1, 'X')
if err == nil || have != ""{
t.Error("PadChar with negative number should be an error")
}
}