blob: 362a09d65b050f3893424d67cbae79780a9c20d0 [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}