Shinichiro Hamaji | b69bf8a | 2015-06-10 14:52:06 +0900 | [diff] [blame] | 1 | // Copyright 2015 Google Inc. All rights reserved |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
Fumitoshi Ukai | 744bb2b | 2015-06-25 00:10:52 +0900 | [diff] [blame^] | 15 | package kati |
Shinichiro Hamaji | c5686fd | 2015-04-01 03:30:34 +0900 | [diff] [blame] | 16 | |
| 17 | import ( |
Shinichiro Hamaji | 98e910d | 2015-04-11 10:38:44 +0900 | [diff] [blame] | 18 | "fmt" |
Shinichiro Hamaji | 248e754 | 2015-04-09 18:12:06 +0900 | [diff] [blame] | 19 | "reflect" |
Shinichiro Hamaji | c5686fd | 2015-04-01 03:30:34 +0900 | [diff] [blame] | 20 | "testing" |
| 21 | ) |
| 22 | |
Shinichiro Hamaji | 248e754 | 2015-04-09 18:12:06 +0900 | [diff] [blame] | 23 | func TestSplitSpaces(t *testing.T) { |
| 24 | for _, tc := range []struct { |
| 25 | in string |
| 26 | want []string |
| 27 | }{ |
| 28 | { |
| 29 | in: "foo", |
| 30 | want: []string{"foo"}, |
| 31 | }, |
| 32 | { |
| 33 | in: " ", |
| 34 | want: nil, |
| 35 | }, |
| 36 | { |
| 37 | in: " foo bar ", |
| 38 | want: []string{"foo", "bar"}, |
| 39 | }, |
| 40 | { |
| 41 | in: " foo bar", |
| 42 | want: []string{"foo", "bar"}, |
| 43 | }, |
| 44 | { |
| 45 | in: "foo bar ", |
| 46 | want: []string{"foo", "bar"}, |
| 47 | }, |
| 48 | } { |
| 49 | got := splitSpaces(tc.in) |
| 50 | if !reflect.DeepEqual(got, tc.want) { |
| 51 | t.Errorf(`splitSpaces(%q)=%q, want %q`, tc.in, got, tc.want) |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | |
Fumitoshi Ukai | 9da19f6 | 2015-05-08 14:39:08 +0900 | [diff] [blame] | 56 | func TestWordScanner(t *testing.T) { |
| 57 | for _, tc := range []struct { |
| 58 | in string |
| 59 | want []string |
| 60 | }{ |
| 61 | { |
| 62 | in: "foo", |
| 63 | want: []string{"foo"}, |
| 64 | }, |
| 65 | { |
| 66 | in: " ", |
| 67 | want: nil, |
| 68 | }, |
| 69 | { |
| 70 | in: " foo bar ", |
| 71 | want: []string{"foo", "bar"}, |
| 72 | }, |
| 73 | { |
| 74 | in: " foo bar", |
| 75 | want: []string{"foo", "bar"}, |
| 76 | }, |
| 77 | { |
| 78 | in: "foo bar ", |
| 79 | want: []string{"foo", "bar"}, |
| 80 | }, |
| 81 | } { |
| 82 | ws := newWordScanner([]byte(tc.in)) |
| 83 | var got []string |
| 84 | for ws.Scan() { |
| 85 | got = append(got, string(ws.Bytes())) |
| 86 | } |
| 87 | if !reflect.DeepEqual(got, tc.want) { |
| 88 | t.Errorf(`wordScanner(%q)=%q, want %q`, tc.in, got, tc.want) |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | |
Shinichiro Hamaji | c5686fd | 2015-04-01 03:30:34 +0900 | [diff] [blame] | 93 | func TestSubstPattern(t *testing.T) { |
Fumitoshi Ukai | 77411fb | 2015-06-19 15:46:21 +0900 | [diff] [blame] | 94 | concatStr := func(pre, subst, post []byte) string { |
| 95 | var s []byte |
| 96 | s = append(s, pre...) |
| 97 | s = append(s, subst...) |
| 98 | s = append(s, post...) |
| 99 | return string(s) |
| 100 | } |
| 101 | |
Shinichiro Hamaji | c5686fd | 2015-04-01 03:30:34 +0900 | [diff] [blame] | 102 | for _, tc := range []struct { |
| 103 | pat string |
| 104 | repl string |
| 105 | in string |
| 106 | want string |
Shinichiro Hamaji | 248e754 | 2015-04-09 18:12:06 +0900 | [diff] [blame] | 107 | }{ |
Shinichiro Hamaji | c5686fd | 2015-04-01 03:30:34 +0900 | [diff] [blame] | 108 | { |
| 109 | pat: "%.c", |
| 110 | repl: "%.o", |
| 111 | in: "x.c", |
| 112 | want: "x.o", |
| 113 | }, |
| 114 | { |
| 115 | pat: "c.%", |
| 116 | repl: "o.%", |
| 117 | in: "c.x", |
| 118 | want: "o.x", |
| 119 | }, |
| 120 | { |
| 121 | pat: "%.c", |
| 122 | repl: "%.o", |
| 123 | in: "x.c.c", |
| 124 | want: "x.c.o", |
| 125 | }, |
| 126 | { |
| 127 | pat: "%.c", |
| 128 | repl: "%.o", |
| 129 | in: "x.x y.c", |
| 130 | want: "x.x y.o", |
| 131 | }, |
| 132 | { |
| 133 | pat: "%.%.c", |
| 134 | repl: "OK", |
| 135 | in: "x.%.c", |
| 136 | want: "OK", |
| 137 | }, |
| 138 | { |
| 139 | pat: "x.c", |
| 140 | repl: "XX", |
| 141 | in: "x.c", |
| 142 | want: "XX", |
| 143 | }, |
| 144 | { |
| 145 | pat: "x.c", |
| 146 | repl: "XX", |
| 147 | in: "x.c.c", |
| 148 | want: "x.c.c", |
| 149 | }, |
| 150 | { |
| 151 | pat: "x.c", |
| 152 | repl: "XX", |
| 153 | in: "x.x.c", |
| 154 | want: "x.x.c", |
| 155 | }, |
| 156 | } { |
| 157 | got := substPattern(tc.pat, tc.repl, tc.in) |
| 158 | if got != tc.want { |
| 159 | t.Errorf(`substPattern(%q,%q,%q)=%q, want %q`, tc.pat, tc.repl, tc.in, got, tc.want) |
| 160 | } |
Shinichiro Hamaji | 98e910d | 2015-04-11 10:38:44 +0900 | [diff] [blame] | 161 | |
Fumitoshi Ukai | 77411fb | 2015-06-19 15:46:21 +0900 | [diff] [blame] | 162 | got = concatStr(substPatternBytes([]byte(tc.pat), []byte(tc.repl), []byte(tc.in))) |
Shinichiro Hamaji | 98e910d | 2015-04-11 10:38:44 +0900 | [diff] [blame] | 163 | if got != tc.want { |
| 164 | fmt.Printf("substPatternBytes(%q,%q,%q)=%q, want %q\n", tc.pat, tc.repl, tc.in, got, tc.want) |
| 165 | t.Errorf(`substPatternBytes(%q,%q,%q)=%q, want %q`, tc.pat, tc.repl, tc.in, got, tc.want) |
| 166 | } |
Shinichiro Hamaji | c5686fd | 2015-04-01 03:30:34 +0900 | [diff] [blame] | 167 | } |
| 168 | } |