blob: e6641115d63650554e6c1644b81bdb193a00d6a1 [file] [log] [blame]
Shinichiro Hamajic5686fd2015-04-01 03:30:34 +09001package main
2
3import (
Shinichiro Hamaji98e910d2015-04-11 10:38:44 +09004 "fmt"
Shinichiro Hamaji248e7542015-04-09 18:12:06 +09005 "reflect"
Shinichiro Hamajic5686fd2015-04-01 03:30:34 +09006 "testing"
7)
8
Shinichiro Hamaji248e7542015-04-09 18:12:06 +09009func TestSplitSpaces(t *testing.T) {
10 for _, tc := range []struct {
11 in string
12 want []string
13 }{
14 {
15 in: "foo",
16 want: []string{"foo"},
17 },
18 {
19 in: " ",
20 want: nil,
21 },
22 {
23 in: " foo bar ",
24 want: []string{"foo", "bar"},
25 },
26 {
27 in: " foo bar",
28 want: []string{"foo", "bar"},
29 },
30 {
31 in: "foo bar ",
32 want: []string{"foo", "bar"},
33 },
34 } {
35 got := splitSpaces(tc.in)
36 if !reflect.DeepEqual(got, tc.want) {
37 t.Errorf(`splitSpaces(%q)=%q, want %q`, tc.in, got, tc.want)
38 }
39 }
40}
41
Shinichiro Hamajic5686fd2015-04-01 03:30:34 +090042func TestSubstPattern(t *testing.T) {
43 for _, tc := range []struct {
44 pat string
45 repl string
46 in string
47 want string
Shinichiro Hamaji248e7542015-04-09 18:12:06 +090048 }{
Shinichiro Hamajic5686fd2015-04-01 03:30:34 +090049 {
50 pat: "%.c",
51 repl: "%.o",
52 in: "x.c",
53 want: "x.o",
54 },
55 {
56 pat: "c.%",
57 repl: "o.%",
58 in: "c.x",
59 want: "o.x",
60 },
61 {
62 pat: "%.c",
63 repl: "%.o",
64 in: "x.c.c",
65 want: "x.c.o",
66 },
67 {
68 pat: "%.c",
69 repl: "%.o",
70 in: "x.x y.c",
71 want: "x.x y.o",
72 },
73 {
74 pat: "%.%.c",
75 repl: "OK",
76 in: "x.%.c",
77 want: "OK",
78 },
79 {
80 pat: "x.c",
81 repl: "XX",
82 in: "x.c",
83 want: "XX",
84 },
85 {
86 pat: "x.c",
87 repl: "XX",
88 in: "x.c.c",
89 want: "x.c.c",
90 },
91 {
92 pat: "x.c",
93 repl: "XX",
94 in: "x.x.c",
95 want: "x.x.c",
96 },
97 } {
98 got := substPattern(tc.pat, tc.repl, tc.in)
99 if got != tc.want {
100 t.Errorf(`substPattern(%q,%q,%q)=%q, want %q`, tc.pat, tc.repl, tc.in, got, tc.want)
101 }
Shinichiro Hamaji98e910d2015-04-11 10:38:44 +0900102
103 got = string(substPatternBytes([]byte(tc.pat), []byte(tc.repl), []byte(tc.in)))
104 if got != tc.want {
105 fmt.Printf("substPatternBytes(%q,%q,%q)=%q, want %q\n", tc.pat, tc.repl, tc.in, got, tc.want)
106 t.Errorf(`substPatternBytes(%q,%q,%q)=%q, want %q`, tc.pat, tc.repl, tc.in, got, tc.want)
107 }
Shinichiro Hamajic5686fd2015-04-01 03:30:34 +0900108 }
109}