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 | 0439a3e | 2015-04-01 02:01:05 +0900 | [diff] [blame] | 16 | |
| 17 | import ( |
| 18 | "reflect" |
| 19 | "testing" |
| 20 | ) |
| 21 | |
| 22 | func TestRuleParser(t *testing.T) { |
| 23 | for _, tc := range []struct { |
Fumitoshi Ukai | 953ce6f | 2015-04-04 00:38:53 +0900 | [diff] [blame] | 24 | in string |
Fumitoshi Ukai | 201df42 | 2015-07-07 17:31:05 +0900 | [diff] [blame] | 25 | tsv *assignAST |
Fumitoshi Ukai | b97be67 | 2015-07-02 15:12:48 +0900 | [diff] [blame] | 26 | rhs expr |
Fumitoshi Ukai | adc1444 | 2015-06-25 16:10:30 +0900 | [diff] [blame] | 27 | want rule |
Fumitoshi Ukai | 91ed5d7 | 2015-06-25 13:08:09 +0900 | [diff] [blame] | 28 | assign *assignAST |
Fumitoshi Ukai | 953ce6f | 2015-04-04 00:38:53 +0900 | [diff] [blame] | 29 | err string |
| 30 | }{ |
Shinichiro Hamaji | 0439a3e | 2015-04-01 02:01:05 +0900 | [diff] [blame] | 31 | { |
Fumitoshi Ukai | 953ce6f | 2015-04-04 00:38:53 +0900 | [diff] [blame] | 32 | in: "foo: bar", |
Fumitoshi Ukai | adc1444 | 2015-06-25 16:10:30 +0900 | [diff] [blame] | 33 | want: rule{ |
Shinichiro Hamaji | 0439a3e | 2015-04-01 02:01:05 +0900 | [diff] [blame] | 34 | outputs: []string{"foo"}, |
| 35 | inputs: []string{"bar"}, |
| 36 | }, |
| 37 | }, |
| 38 | { |
Fumitoshi Ukai | 953ce6f | 2015-04-04 00:38:53 +0900 | [diff] [blame] | 39 | in: "foo: bar baz", |
Fumitoshi Ukai | adc1444 | 2015-06-25 16:10:30 +0900 | [diff] [blame] | 40 | want: rule{ |
Shinichiro Hamaji | 0439a3e | 2015-04-01 02:01:05 +0900 | [diff] [blame] | 41 | outputs: []string{"foo"}, |
| 42 | inputs: []string{"bar", "baz"}, |
| 43 | }, |
| 44 | }, |
Shinichiro Hamaji | 21a9b5f | 2015-04-01 02:42:59 +0900 | [diff] [blame] | 45 | { |
Fumitoshi Ukai | 953ce6f | 2015-04-04 00:38:53 +0900 | [diff] [blame] | 46 | in: "foo:: bar", |
Fumitoshi Ukai | adc1444 | 2015-06-25 16:10:30 +0900 | [diff] [blame] | 47 | want: rule{ |
Fumitoshi Ukai | 953ce6f | 2015-04-04 00:38:53 +0900 | [diff] [blame] | 48 | outputs: []string{"foo"}, |
| 49 | inputs: []string{"bar"}, |
Shinichiro Hamaji | 21a9b5f | 2015-04-01 02:42:59 +0900 | [diff] [blame] | 50 | isDoubleColon: true, |
| 51 | }, |
| 52 | }, |
| 53 | { |
| 54 | in: "foo", |
| 55 | err: "*** missing separator.", |
| 56 | }, |
| 57 | { |
Fumitoshi Ukai | 953ce6f | 2015-04-04 00:38:53 +0900 | [diff] [blame] | 58 | in: "%.o: %.c", |
Fumitoshi Ukai | adc1444 | 2015-06-25 16:10:30 +0900 | [diff] [blame] | 59 | want: rule{ |
Fumitoshi Ukai | b97be67 | 2015-07-02 15:12:48 +0900 | [diff] [blame] | 60 | outputs: []string{}, |
Fumitoshi Ukai | 935de96 | 2015-04-28 17:08:20 +0900 | [diff] [blame] | 61 | outputPatterns: []pattern{pattern{suffix: ".o"}}, |
Shinichiro Hamaji | 21a9b5f | 2015-04-01 02:42:59 +0900 | [diff] [blame] | 62 | inputs: []string{"%.c"}, |
| 63 | }, |
| 64 | }, |
| 65 | { |
| 66 | in: "foo %.o: %.c", |
| 67 | err: "*** mixed implicit and normal rules: deprecated syntax", |
| 68 | }, |
| 69 | { |
Fumitoshi Ukai | 953ce6f | 2015-04-04 00:38:53 +0900 | [diff] [blame] | 70 | in: "foo.o: %.o: %.c %.h", |
Fumitoshi Ukai | adc1444 | 2015-06-25 16:10:30 +0900 | [diff] [blame] | 71 | want: rule{ |
Shinichiro Hamaji | 21a9b5f | 2015-04-01 02:42:59 +0900 | [diff] [blame] | 72 | outputs: []string{"foo.o"}, |
Fumitoshi Ukai | 935de96 | 2015-04-28 17:08:20 +0900 | [diff] [blame] | 73 | outputPatterns: []pattern{pattern{suffix: ".o"}}, |
Shinichiro Hamaji | 21a9b5f | 2015-04-01 02:42:59 +0900 | [diff] [blame] | 74 | 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 Hamaji | 5c53b57 | 2015-04-02 05:36:42 +0900 | [diff] [blame] | 93 | { |
Fumitoshi Ukai | 953ce6f | 2015-04-04 00:38:53 +0900 | [diff] [blame] | 94 | in: "foo: bar | baz", |
Fumitoshi Ukai | adc1444 | 2015-06-25 16:10:30 +0900 | [diff] [blame] | 95 | want: rule{ |
Shinichiro Hamaji | 5c53b57 | 2015-04-02 05:36:42 +0900 | [diff] [blame] | 96 | outputs: []string{"foo"}, |
| 97 | inputs: []string{"bar"}, |
| 98 | orderOnlyInputs: []string{"baz"}, |
| 99 | }, |
| 100 | }, |
Fumitoshi Ukai | 953ce6f | 2015-04-04 00:38:53 +0900 | [diff] [blame] | 101 | { |
Fumitoshi Ukai | b97be67 | 2015-07-02 15:12:48 +0900 | [diff] [blame] | 102 | in: "foo: CFLAGS =", |
| 103 | rhs: expr{literal("-g")}, |
Fumitoshi Ukai | adc1444 | 2015-06-25 16:10:30 +0900 | [diff] [blame] | 104 | want: rule{ |
Fumitoshi Ukai | 953ce6f | 2015-04-04 00:38:53 +0900 | [diff] [blame] | 105 | outputs: []string{"foo"}, |
| 106 | }, |
Fumitoshi Ukai | 91ed5d7 | 2015-06-25 13:08:09 +0900 | [diff] [blame] | 107 | assign: &assignAST{ |
Fumitoshi Ukai | 7c9aa9f | 2015-06-12 23:51:38 +0900 | [diff] [blame] | 108 | lhs: literal("CFLAGS"), |
| 109 | rhs: literal("-g"), |
Fumitoshi Ukai | 953ce6f | 2015-04-04 00:38:53 +0900 | [diff] [blame] | 110 | op: "=", |
| 111 | }, |
| 112 | }, |
Fumitoshi Ukai | 3ef5fcc | 2015-04-06 17:21:43 +0900 | [diff] [blame] | 113 | { |
Fumitoshi Ukai | 201df42 | 2015-07-07 17:31:05 +0900 | [diff] [blame] | 114 | 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 Ukai | b97be67 | 2015-07-02 15:12:48 +0900 | [diff] [blame] | 130 | in: "foo: CFLAGS=", |
| 131 | rhs: expr{literal("-g")}, |
Fumitoshi Ukai | adc1444 | 2015-06-25 16:10:30 +0900 | [diff] [blame] | 132 | want: rule{ |
Fumitoshi Ukai | 3ef5fcc | 2015-04-06 17:21:43 +0900 | [diff] [blame] | 133 | outputs: []string{"foo"}, |
| 134 | }, |
Fumitoshi Ukai | 91ed5d7 | 2015-06-25 13:08:09 +0900 | [diff] [blame] | 135 | assign: &assignAST{ |
Fumitoshi Ukai | 7c9aa9f | 2015-06-12 23:51:38 +0900 | [diff] [blame] | 136 | lhs: literal("CFLAGS"), |
| 137 | rhs: literal("-g"), |
Fumitoshi Ukai | 3ef5fcc | 2015-04-06 17:21:43 +0900 | [diff] [blame] | 138 | op: "=", |
| 139 | }, |
| 140 | }, |
| 141 | { |
Fumitoshi Ukai | b97be67 | 2015-07-02 15:12:48 +0900 | [diff] [blame] | 142 | in: "foo: CFLAGS :=", |
| 143 | rhs: expr{literal("-g")}, |
Fumitoshi Ukai | adc1444 | 2015-06-25 16:10:30 +0900 | [diff] [blame] | 144 | want: rule{ |
Fumitoshi Ukai | 3ef5fcc | 2015-04-06 17:21:43 +0900 | [diff] [blame] | 145 | outputs: []string{"foo"}, |
| 146 | }, |
Fumitoshi Ukai | 91ed5d7 | 2015-06-25 13:08:09 +0900 | [diff] [blame] | 147 | assign: &assignAST{ |
Fumitoshi Ukai | 7c9aa9f | 2015-06-12 23:51:38 +0900 | [diff] [blame] | 148 | lhs: literal("CFLAGS"), |
| 149 | rhs: literal("-g"), |
Fumitoshi Ukai | 3ef5fcc | 2015-04-06 17:21:43 +0900 | [diff] [blame] | 150 | op: ":=", |
| 151 | }, |
| 152 | }, |
Shinichiro Hamaji | a485d2f | 2015-04-16 14:03:24 +0900 | [diff] [blame] | 153 | { |
Fumitoshi Ukai | b97be67 | 2015-07-02 15:12:48 +0900 | [diff] [blame] | 154 | in: "%.o: CFLAGS :=", |
| 155 | rhs: expr{literal("-g")}, |
Fumitoshi Ukai | adc1444 | 2015-06-25 16:10:30 +0900 | [diff] [blame] | 156 | want: rule{ |
Fumitoshi Ukai | b97be67 | 2015-07-02 15:12:48 +0900 | [diff] [blame] | 157 | outputs: []string{}, |
Fumitoshi Ukai | 935de96 | 2015-04-28 17:08:20 +0900 | [diff] [blame] | 158 | outputPatterns: []pattern{pattern{suffix: ".o"}}, |
Shinichiro Hamaji | a485d2f | 2015-04-16 14:03:24 +0900 | [diff] [blame] | 159 | }, |
Fumitoshi Ukai | 91ed5d7 | 2015-06-25 13:08:09 +0900 | [diff] [blame] | 160 | assign: &assignAST{ |
Fumitoshi Ukai | 7c9aa9f | 2015-06-12 23:51:38 +0900 | [diff] [blame] | 161 | lhs: literal("CFLAGS"), |
| 162 | rhs: literal("-g"), |
Shinichiro Hamaji | a485d2f | 2015-04-16 14:03:24 +0900 | [diff] [blame] | 163 | op: ":=", |
| 164 | }, |
| 165 | }, |
Fumitoshi Ukai | 201df42 | 2015-07-07 17:31:05 +0900 | [diff] [blame] | 166 | { |
| 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 Hamaji | 21a9b5f | 2015-04-01 02:42:59 +0900 | [diff] [blame] | 183 | /* TODO |
| 184 | { |
| 185 | in: "foo.o: %.c: %.c", |
| 186 | err: "*** target 'foo.o' doesn't match the target pattern", |
| 187 | }, |
| 188 | */ |
Shinichiro Hamaji | 0439a3e | 2015-04-01 02:01:05 +0900 | [diff] [blame] | 189 | } { |
Fumitoshi Ukai | adc1444 | 2015-06-25 16:10:30 +0900 | [diff] [blame] | 190 | got := &rule{} |
Fumitoshi Ukai | 201df42 | 2015-07-07 17:31:05 +0900 | [diff] [blame] | 191 | assign, err := got.parse([]byte(tc.in), tc.tsv, tc.rhs) |
Fumitoshi Ukai | 953ce6f | 2015-04-04 00:38:53 +0900 | [diff] [blame] | 192 | if tc.err != "" { |
| 193 | if err == nil { |
Fumitoshi Ukai | b97be67 | 2015-07-02 15:12:48 +0900 | [diff] [blame] | 194 | t.Errorf(`r.parse(%q, %v)=_, <nil>, want _, %q`, tc.in, tc.rhs, tc.err) |
Fumitoshi Ukai | 953ce6f | 2015-04-04 00:38:53 +0900 | [diff] [blame] | 195 | continue |
| 196 | } |
| 197 | if got, want := err.Error(), tc.err; got != want { |
Fumitoshi Ukai | b97be67 | 2015-07-02 15:12:48 +0900 | [diff] [blame] | 198 | t.Errorf(`r.parse(%q, %v)=_, %s, want %s`, tc.in, tc.rhs, got, want) |
Fumitoshi Ukai | 953ce6f | 2015-04-04 00:38:53 +0900 | [diff] [blame] | 199 | } |
| 200 | continue |
Shinichiro Hamaji | 21a9b5f | 2015-04-01 02:42:59 +0900 | [diff] [blame] | 201 | } |
Fumitoshi Ukai | 953ce6f | 2015-04-04 00:38:53 +0900 | [diff] [blame] | 202 | if err != nil { |
Fumitoshi Ukai | b97be67 | 2015-07-02 15:12:48 +0900 | [diff] [blame] | 203 | t.Errorf(`r.parse(%q, %v)=_, %v; want nil error`, tc.in, tc.rhs, err) |
Fumitoshi Ukai | 953ce6f | 2015-04-04 00:38:53 +0900 | [diff] [blame] | 204 | continue |
| 205 | } |
| 206 | if !reflect.DeepEqual(*got, tc.want) { |
Fumitoshi Ukai | b97be67 | 2015-07-02 15:12:48 +0900 | [diff] [blame] | 207 | t.Errorf(`r.parse(%q, %v); r=%#v, want %#v`, tc.in, tc.rhs, *got, tc.want) |
Shinichiro Hamaji | 0439a3e | 2015-04-01 02:01:05 +0900 | [diff] [blame] | 208 | } |
Fumitoshi Ukai | 953ce6f | 2015-04-04 00:38:53 +0900 | [diff] [blame] | 209 | if tc.assign != nil { |
| 210 | if assign == nil { |
Fumitoshi Ukai | b97be67 | 2015-07-02 15:12:48 +0900 | [diff] [blame] | 211 | t.Errorf(`r.parse(%q, %v)=<nil>; want=%#v`, tc.in, tc.rhs, tc.assign) |
Fumitoshi Ukai | 953ce6f | 2015-04-04 00:38:53 +0900 | [diff] [blame] | 212 | continue |
| 213 | } |
| 214 | if got, want := assign, tc.assign; !reflect.DeepEqual(got, want) { |
Fumitoshi Ukai | b97be67 | 2015-07-02 15:12:48 +0900 | [diff] [blame] | 215 | t.Errorf(`r.parse(%q, %v)=%#v; want=%#v`, tc.in, tc.rhs, got, want) |
Fumitoshi Ukai | 953ce6f | 2015-04-04 00:38:53 +0900 | [diff] [blame] | 216 | } |
| 217 | continue |
| 218 | } |
| 219 | if assign != nil { |
Fumitoshi Ukai | b97be67 | 2015-07-02 15:12:48 +0900 | [diff] [blame] | 220 | t.Errorf(`r.parse(%q, %v)=%v; want=<nil>`, tc.in, tc.rhs, assign) |
Fumitoshi Ukai | 953ce6f | 2015-04-04 00:38:53 +0900 | [diff] [blame] | 221 | } |
Shinichiro Hamaji | 0439a3e | 2015-04-01 02:01:05 +0900 | [diff] [blame] | 222 | } |
| 223 | } |