blob: 56bfe87aa1e3f12964a455d1a9e3cf8b9dcf2a94 [file] [log] [blame]
Shinichiro Hamajib69bf8a2015-06-10 14:52:06 +09001// 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 Ukai744bb2b2015-06-25 00:10:52 +090015package kati
Shinichiro Hamajic5686fd2015-04-01 03:30:34 +090016
17import (
Shinichiro Hamaji98e910d2015-04-11 10:38:44 +090018 "fmt"
Shinichiro Hamaji248e7542015-04-09 18:12:06 +090019 "reflect"
Shinichiro Hamajic5686fd2015-04-01 03:30:34 +090020 "testing"
21)
22
Shinichiro Hamaji248e7542015-04-09 18:12:06 +090023func 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 Ukai9da19f62015-05-08 14:39:08 +090056func 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 Hamajic5686fd2015-04-01 03:30:34 +090093func TestSubstPattern(t *testing.T) {
Fumitoshi Ukai77411fb2015-06-19 15:46:21 +090094 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 Hamajic5686fd2015-04-01 03:30:34 +0900102 for _, tc := range []struct {
103 pat string
104 repl string
105 in string
106 want string
Shinichiro Hamaji248e7542015-04-09 18:12:06 +0900107 }{
Shinichiro Hamajic5686fd2015-04-01 03:30:34 +0900108 {
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 Hamaji98e910d2015-04-11 10:38:44 +0900161
Fumitoshi Ukai77411fb2015-06-19 15:46:21 +0900162 got = concatStr(substPatternBytes([]byte(tc.pat), []byte(tc.repl), []byte(tc.in)))
Shinichiro Hamaji98e910d2015-04-11 10:38:44 +0900163 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 Hamajic5686fd2015-04-01 03:30:34 +0900167 }
168}
Fumitoshi Ukaib97be672015-07-02 15:12:48 +0900169
170func TestRemoveComment(t *testing.T) {
171 for _, tc := range []struct {
172 in string
173 want string
174 removed bool
175 }{
176 {
177 in: "foo",
178 want: "foo",
179 },
180 {
181 in: "foo #bar",
182 want: "foo ",
183 removed: true,
184 },
185 {
186 in: `foo \#bar`,
187 want: "foo #bar",
188 },
189 {
190 in: `foo \#bar # baz`,
191 want: `foo #bar `,
192 removed: true,
193 },
194 {
195 in: `foo \\ \# \: \; \% \= \a \? \+`,
196 want: `foo \\ # \: \; \% \= \a \? \+`,
197 },
198 {
199 in: `foo \\#bar`,
200 want: `foo \`,
201 removed: true,
202 },
203 {
204 in: `foo \\\#bar`,
205 want: `foo \#bar`,
206 },
207 {
208 in: `PASS:=\#PASS`,
209 want: `PASS:=#PASS`,
210 },
211 } {
212 got, removed := removeComment([]byte(tc.in))
213 if string(got) != tc.want {
214 t.Errorf("removeComment(%q)=%q, _; want=%q, _", tc.in, got, tc.want)
215 }
216 if removed != tc.removed {
217 t.Errorf("removeComment(%q)=_, %t; want=_, %t", tc.in, removed, tc.removed)
218 }
219 }
220}
221
222func TestConcatline(t *testing.T) {
223 for _, tc := range []struct {
224 in string
225 want string
226 }{
227 {
228 in: "foo",
229 want: "foo",
230 },
231 {
232 in: "foo \\\n\t bar",
233 want: "foo bar",
234 },
235 {
236 in: "foo \\\n \\\n\t bar",
237 want: "foo bar",
238 },
Fumitoshi Ukai2a2deb32015-07-08 11:46:14 +0900239 {
240 in: `foo \`,
241 want: `foo `,
242 },
243 {
244 in: `foo \\`,
245 want: `foo \\`,
246 },
Fumitoshi Ukaib97be672015-07-02 15:12:48 +0900247 } {
248 got := string(concatline([]byte(tc.in)))
249 if got != tc.want {
250 t.Errorf("concatline(%q)=%q; want=%q\n", tc.in, got, tc.want)
251 }
252 }
253}