blob: be120411fe0236b4cf884cd73a9eea51e82c2c82 [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 Hamaji0439a3e2015-04-01 02:01:05 +090016
17import (
18 "reflect"
19 "testing"
20)
21
22func TestRuleParser(t *testing.T) {
23 for _, tc := range []struct {
Fumitoshi Ukai953ce6f2015-04-04 00:38:53 +090024 in string
Fumitoshi Ukai201df422015-07-07 17:31:05 +090025 tsv *assignAST
Fumitoshi Ukaib97be672015-07-02 15:12:48 +090026 rhs expr
Fumitoshi Ukaiadc14442015-06-25 16:10:30 +090027 want rule
Fumitoshi Ukai91ed5d72015-06-25 13:08:09 +090028 assign *assignAST
Fumitoshi Ukai953ce6f2015-04-04 00:38:53 +090029 err string
30 }{
Shinichiro Hamaji0439a3e2015-04-01 02:01:05 +090031 {
Fumitoshi Ukai953ce6f2015-04-04 00:38:53 +090032 in: "foo: bar",
Fumitoshi Ukaiadc14442015-06-25 16:10:30 +090033 want: rule{
Shinichiro Hamaji0439a3e2015-04-01 02:01:05 +090034 outputs: []string{"foo"},
35 inputs: []string{"bar"},
36 },
37 },
38 {
Fumitoshi Ukai953ce6f2015-04-04 00:38:53 +090039 in: "foo: bar baz",
Fumitoshi Ukaiadc14442015-06-25 16:10:30 +090040 want: rule{
Shinichiro Hamaji0439a3e2015-04-01 02:01:05 +090041 outputs: []string{"foo"},
42 inputs: []string{"bar", "baz"},
43 },
44 },
Shinichiro Hamaji21a9b5f2015-04-01 02:42:59 +090045 {
Fumitoshi Ukai953ce6f2015-04-04 00:38:53 +090046 in: "foo:: bar",
Fumitoshi Ukaiadc14442015-06-25 16:10:30 +090047 want: rule{
Fumitoshi Ukai953ce6f2015-04-04 00:38:53 +090048 outputs: []string{"foo"},
49 inputs: []string{"bar"},
Shinichiro Hamaji21a9b5f2015-04-01 02:42:59 +090050 isDoubleColon: true,
51 },
52 },
53 {
54 in: "foo",
55 err: "*** missing separator.",
56 },
57 {
Fumitoshi Ukai953ce6f2015-04-04 00:38:53 +090058 in: "%.o: %.c",
Fumitoshi Ukaiadc14442015-06-25 16:10:30 +090059 want: rule{
Fumitoshi Ukaib97be672015-07-02 15:12:48 +090060 outputs: []string{},
Fumitoshi Ukai935de962015-04-28 17:08:20 +090061 outputPatterns: []pattern{pattern{suffix: ".o"}},
Shinichiro Hamaji21a9b5f2015-04-01 02:42:59 +090062 inputs: []string{"%.c"},
63 },
64 },
65 {
66 in: "foo %.o: %.c",
67 err: "*** mixed implicit and normal rules: deprecated syntax",
68 },
69 {
Fumitoshi Ukai953ce6f2015-04-04 00:38:53 +090070 in: "foo.o: %.o: %.c %.h",
Fumitoshi Ukaiadc14442015-06-25 16:10:30 +090071 want: rule{
Shinichiro Hamaji21a9b5f2015-04-01 02:42:59 +090072 outputs: []string{"foo.o"},
Fumitoshi Ukai935de962015-04-28 17:08:20 +090073 outputPatterns: []pattern{pattern{suffix: ".o"}},
Shinichiro Hamaji21a9b5f2015-04-01 02:42:59 +090074 inputs: []string{"%.c", "%.h"},
75 },
76 },
77 {
78 in: "%.x: %.y: %.z",
79 err: "*** mixed implicit and normal rules: deprecated syntax",
80 },
81 {
82 in: "foo.o: : %.c",
83 err: "*** missing target pattern.",
84 },
85 {
86 in: "foo.o: %.o %.o: %.c",
87 err: "*** multiple target patterns.",
88 },
89 {
90 in: "foo.o: foo.o: %.c",
91 err: "*** target pattern contains no '%'.",
92 },
Shinichiro Hamaji5c53b572015-04-02 05:36:42 +090093 {
Fumitoshi Ukai953ce6f2015-04-04 00:38:53 +090094 in: "foo: bar | baz",
Fumitoshi Ukaiadc14442015-06-25 16:10:30 +090095 want: rule{
Shinichiro Hamaji5c53b572015-04-02 05:36:42 +090096 outputs: []string{"foo"},
97 inputs: []string{"bar"},
98 orderOnlyInputs: []string{"baz"},
99 },
100 },
Fumitoshi Ukai953ce6f2015-04-04 00:38:53 +0900101 {
Fumitoshi Ukaib97be672015-07-02 15:12:48 +0900102 in: "foo: CFLAGS =",
103 rhs: expr{literal("-g")},
Fumitoshi Ukaiadc14442015-06-25 16:10:30 +0900104 want: rule{
Fumitoshi Ukai953ce6f2015-04-04 00:38:53 +0900105 outputs: []string{"foo"},
106 },
Fumitoshi Ukai91ed5d72015-06-25 13:08:09 +0900107 assign: &assignAST{
Fumitoshi Ukai7c9aa9f2015-06-12 23:51:38 +0900108 lhs: literal("CFLAGS"),
109 rhs: literal("-g"),
Fumitoshi Ukai953ce6f2015-04-04 00:38:53 +0900110 op: "=",
111 },
112 },
Fumitoshi Ukai3ef5fcc2015-04-06 17:21:43 +0900113 {
Fumitoshi Ukai201df422015-07-07 17:31:05 +0900114 in: "foo:",
115 tsv: &assignAST{
116 lhs: literal("CFLAGS"),
117 rhs: literal("-g"),
118 op: "=",
119 },
120 want: rule{
121 outputs: []string{"foo"},
122 },
123 assign: &assignAST{
124 lhs: literal("CFLAGS"),
125 rhs: literal("-g"),
126 op: "=",
127 },
128 },
129 {
Fumitoshi Ukaib97be672015-07-02 15:12:48 +0900130 in: "foo: CFLAGS=",
131 rhs: expr{literal("-g")},
Fumitoshi Ukaiadc14442015-06-25 16:10:30 +0900132 want: rule{
Fumitoshi Ukai3ef5fcc2015-04-06 17:21:43 +0900133 outputs: []string{"foo"},
134 },
Fumitoshi Ukai91ed5d72015-06-25 13:08:09 +0900135 assign: &assignAST{
Fumitoshi Ukai7c9aa9f2015-06-12 23:51:38 +0900136 lhs: literal("CFLAGS"),
137 rhs: literal("-g"),
Fumitoshi Ukai3ef5fcc2015-04-06 17:21:43 +0900138 op: "=",
139 },
140 },
141 {
Fumitoshi Ukaib97be672015-07-02 15:12:48 +0900142 in: "foo: CFLAGS :=",
143 rhs: expr{literal("-g")},
Fumitoshi Ukaiadc14442015-06-25 16:10:30 +0900144 want: rule{
Fumitoshi Ukai3ef5fcc2015-04-06 17:21:43 +0900145 outputs: []string{"foo"},
146 },
Fumitoshi Ukai91ed5d72015-06-25 13:08:09 +0900147 assign: &assignAST{
Fumitoshi Ukai7c9aa9f2015-06-12 23:51:38 +0900148 lhs: literal("CFLAGS"),
149 rhs: literal("-g"),
Fumitoshi Ukai3ef5fcc2015-04-06 17:21:43 +0900150 op: ":=",
151 },
152 },
Shinichiro Hamajia485d2f2015-04-16 14:03:24 +0900153 {
Fumitoshi Ukaib97be672015-07-02 15:12:48 +0900154 in: "%.o: CFLAGS :=",
155 rhs: expr{literal("-g")},
Fumitoshi Ukaiadc14442015-06-25 16:10:30 +0900156 want: rule{
Fumitoshi Ukaib97be672015-07-02 15:12:48 +0900157 outputs: []string{},
Fumitoshi Ukai935de962015-04-28 17:08:20 +0900158 outputPatterns: []pattern{pattern{suffix: ".o"}},
Shinichiro Hamajia485d2f2015-04-16 14:03:24 +0900159 },
Fumitoshi Ukai91ed5d72015-06-25 13:08:09 +0900160 assign: &assignAST{
Fumitoshi Ukai7c9aa9f2015-06-12 23:51:38 +0900161 lhs: literal("CFLAGS"),
162 rhs: literal("-g"),
Shinichiro Hamajia485d2f2015-04-16 14:03:24 +0900163 op: ":=",
164 },
165 },
Fumitoshi Ukai201df422015-07-07 17:31:05 +0900166 {
167 in: "%.o:",
168 tsv: &assignAST{
169 lhs: literal("CFLAGS"),
170 rhs: literal("-g"),
171 op: ":=",
172 },
173 want: rule{
174 outputs: []string{},
175 outputPatterns: []pattern{pattern{suffix: ".o"}},
176 },
177 assign: &assignAST{
178 lhs: literal("CFLAGS"),
179 rhs: literal("-g"),
180 op: ":=",
181 },
182 },
Shinichiro Hamaji21a9b5f2015-04-01 02:42:59 +0900183 /* TODO
184 {
185 in: "foo.o: %.c: %.c",
186 err: "*** target 'foo.o' doesn't match the target pattern",
187 },
188 */
Shinichiro Hamaji0439a3e2015-04-01 02:01:05 +0900189 } {
Fumitoshi Ukaiadc14442015-06-25 16:10:30 +0900190 got := &rule{}
Fumitoshi Ukai201df422015-07-07 17:31:05 +0900191 assign, err := got.parse([]byte(tc.in), tc.tsv, tc.rhs)
Fumitoshi Ukai953ce6f2015-04-04 00:38:53 +0900192 if tc.err != "" {
193 if err == nil {
Fumitoshi Ukaib97be672015-07-02 15:12:48 +0900194 t.Errorf(`r.parse(%q, %v)=_, <nil>, want _, %q`, tc.in, tc.rhs, tc.err)
Fumitoshi Ukai953ce6f2015-04-04 00:38:53 +0900195 continue
196 }
197 if got, want := err.Error(), tc.err; got != want {
Fumitoshi Ukaib97be672015-07-02 15:12:48 +0900198 t.Errorf(`r.parse(%q, %v)=_, %s, want %s`, tc.in, tc.rhs, got, want)
Fumitoshi Ukai953ce6f2015-04-04 00:38:53 +0900199 }
200 continue
Shinichiro Hamaji21a9b5f2015-04-01 02:42:59 +0900201 }
Fumitoshi Ukai953ce6f2015-04-04 00:38:53 +0900202 if err != nil {
Fumitoshi Ukaib97be672015-07-02 15:12:48 +0900203 t.Errorf(`r.parse(%q, %v)=_, %v; want nil error`, tc.in, tc.rhs, err)
Fumitoshi Ukai953ce6f2015-04-04 00:38:53 +0900204 continue
205 }
206 if !reflect.DeepEqual(*got, tc.want) {
Fumitoshi Ukaib97be672015-07-02 15:12:48 +0900207 t.Errorf(`r.parse(%q, %v); r=%#v, want %#v`, tc.in, tc.rhs, *got, tc.want)
Shinichiro Hamaji0439a3e2015-04-01 02:01:05 +0900208 }
Fumitoshi Ukai953ce6f2015-04-04 00:38:53 +0900209 if tc.assign != nil {
210 if assign == nil {
Fumitoshi Ukaib97be672015-07-02 15:12:48 +0900211 t.Errorf(`r.parse(%q, %v)=<nil>; want=%#v`, tc.in, tc.rhs, tc.assign)
Fumitoshi Ukai953ce6f2015-04-04 00:38:53 +0900212 continue
213 }
214 if got, want := assign, tc.assign; !reflect.DeepEqual(got, want) {
Fumitoshi Ukaib97be672015-07-02 15:12:48 +0900215 t.Errorf(`r.parse(%q, %v)=%#v; want=%#v`, tc.in, tc.rhs, got, want)
Fumitoshi Ukai953ce6f2015-04-04 00:38:53 +0900216 }
217 continue
218 }
219 if assign != nil {
Fumitoshi Ukaib97be672015-07-02 15:12:48 +0900220 t.Errorf(`r.parse(%q, %v)=%v; want=<nil>`, tc.in, tc.rhs, assign)
Fumitoshi Ukai953ce6f2015-04-04 00:38:53 +0900221 }
Shinichiro Hamaji0439a3e2015-04-01 02:01:05 +0900222 }
223}