Shinichiro Hamaji | c5686fd | 2015-04-01 03:30:34 +0900 | [diff] [blame] | 1 | package main |
| 2 | |
| 3 | import ( |
Shinichiro Hamaji | 98e910d | 2015-04-11 10:38:44 +0900 | [diff] [blame] | 4 | "fmt" |
Shinichiro Hamaji | 248e754 | 2015-04-09 18:12:06 +0900 | [diff] [blame] | 5 | "reflect" |
Shinichiro Hamaji | c5686fd | 2015-04-01 03:30:34 +0900 | [diff] [blame] | 6 | "testing" |
| 7 | ) |
| 8 | |
Shinichiro Hamaji | 248e754 | 2015-04-09 18:12:06 +0900 | [diff] [blame] | 9 | func 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 Hamaji | c5686fd | 2015-04-01 03:30:34 +0900 | [diff] [blame] | 42 | func TestSubstPattern(t *testing.T) { |
| 43 | for _, tc := range []struct { |
| 44 | pat string |
| 45 | repl string |
| 46 | in string |
| 47 | want string |
Shinichiro Hamaji | 248e754 | 2015-04-09 18:12:06 +0900 | [diff] [blame] | 48 | }{ |
Shinichiro Hamaji | c5686fd | 2015-04-01 03:30:34 +0900 | [diff] [blame] | 49 | { |
| 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 Hamaji | 98e910d | 2015-04-11 10:38:44 +0900 | [diff] [blame] | 102 | |
| 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 Hamaji | c5686fd | 2015-04-01 03:30:34 +0900 | [diff] [blame] | 108 | } |
| 109 | } |