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 |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 16 | |
| 17 | import ( |
| 18 | "reflect" |
| 19 | "testing" |
| 20 | ) |
| 21 | |
| 22 | func TestParseExpr(t *testing.T) { |
| 23 | for _, tc := range []struct { |
| 24 | in string |
| 25 | val Value |
| 26 | isErr bool |
| 27 | }{ |
| 28 | { |
| 29 | in: "foo", |
| 30 | val: literal("foo"), |
| 31 | }, |
| 32 | { |
| 33 | in: "(foo)", |
| 34 | val: literal("(foo)"), |
| 35 | }, |
| 36 | { |
| 37 | in: "{foo}", |
| 38 | val: literal("{foo}"), |
| 39 | }, |
| 40 | { |
| 41 | in: "$$", |
| 42 | val: literal("$"), |
| 43 | }, |
| 44 | { |
| 45 | in: "foo$$bar", |
| 46 | val: literal("foo$bar"), |
| 47 | }, |
| 48 | { |
| 49 | in: "$foo", |
Fumitoshi Ukai | b8acae9 | 2015-06-18 15:36:57 +0900 | [diff] [blame] | 50 | val: Expr{&varref{varname: literal("f")}, literal("oo")}, |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 51 | }, |
| 52 | { |
| 53 | in: "$(foo)", |
Fumitoshi Ukai | b8acae9 | 2015-06-18 15:36:57 +0900 | [diff] [blame] | 54 | val: &varref{varname: literal("foo")}, |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 55 | }, |
| 56 | { |
| 57 | in: "$(foo:.c=.o)", |
| 58 | val: varsubst{ |
| 59 | varname: literal("foo"), |
| 60 | pat: literal(".c"), |
| 61 | subst: literal(".o"), |
| 62 | }, |
| 63 | }, |
| 64 | { |
| 65 | in: "$(subst $(space),$(,),$(foo))/bar", |
| 66 | val: Expr{ |
| 67 | &funcSubst{ |
| 68 | fclosure: fclosure{ |
| 69 | args: []Value{ |
Fumitoshi Ukai | b2670d9 | 2015-04-16 10:28:27 +0900 | [diff] [blame] | 70 | literal("(subst"), |
Fumitoshi Ukai | b8acae9 | 2015-06-18 15:36:57 +0900 | [diff] [blame] | 71 | &varref{ |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 72 | varname: literal("space"), |
| 73 | }, |
Fumitoshi Ukai | b8acae9 | 2015-06-18 15:36:57 +0900 | [diff] [blame] | 74 | &varref{ |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 75 | varname: literal(","), |
| 76 | }, |
Fumitoshi Ukai | b8acae9 | 2015-06-18 15:36:57 +0900 | [diff] [blame] | 77 | &varref{ |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 78 | varname: literal("foo"), |
| 79 | }, |
| 80 | }, |
| 81 | }, |
| 82 | }, |
| 83 | literal("/bar"), |
| 84 | }, |
| 85 | }, |
| 86 | { |
| 87 | in: "$(subst $(space),$,,$(foo))", |
| 88 | val: &funcSubst{ |
| 89 | fclosure: fclosure{ |
| 90 | args: []Value{ |
Fumitoshi Ukai | b2670d9 | 2015-04-16 10:28:27 +0900 | [diff] [blame] | 91 | literal("(subst"), |
Fumitoshi Ukai | b8acae9 | 2015-06-18 15:36:57 +0900 | [diff] [blame] | 92 | &varref{ |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 93 | varname: literal("space"), |
| 94 | }, |
Fumitoshi Ukai | b8acae9 | 2015-06-18 15:36:57 +0900 | [diff] [blame] | 95 | &varref{ |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 96 | varname: literal(""), |
| 97 | }, |
| 98 | Expr{ |
| 99 | literal(","), |
Fumitoshi Ukai | b8acae9 | 2015-06-18 15:36:57 +0900 | [diff] [blame] | 100 | &varref{ |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 101 | varname: literal("foo"), |
| 102 | }, |
| 103 | }, |
| 104 | }, |
| 105 | }, |
| 106 | }, |
| 107 | }, |
| 108 | { |
| 109 | in: `$(shell echo '()')`, |
| 110 | val: &funcShell{ |
| 111 | fclosure: fclosure{ |
| 112 | args: []Value{ |
Fumitoshi Ukai | b2670d9 | 2015-04-16 10:28:27 +0900 | [diff] [blame] | 113 | literal("(shell"), |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 114 | literal("echo '()'"), |
| 115 | }, |
| 116 | }, |
| 117 | }, |
| 118 | }, |
| 119 | { |
Fumitoshi Ukai | ebf945c | 2015-04-10 17:30:04 +0900 | [diff] [blame] | 120 | in: `${shell echo '()'}`, |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 121 | val: &funcShell{ |
| 122 | fclosure: fclosure{ |
| 123 | args: []Value{ |
Fumitoshi Ukai | b2670d9 | 2015-04-16 10:28:27 +0900 | [diff] [blame] | 124 | literal("{shell"), |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 125 | literal("echo '()'"), |
| 126 | }, |
| 127 | }, |
| 128 | }, |
| 129 | }, |
| 130 | { |
| 131 | in: `$(shell echo ')')`, |
| 132 | val: Expr{ |
| 133 | &funcShell{ |
| 134 | fclosure: fclosure{ |
| 135 | args: []Value{ |
Fumitoshi Ukai | b2670d9 | 2015-04-16 10:28:27 +0900 | [diff] [blame] | 136 | literal("(shell"), |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 137 | literal("echo '"), |
| 138 | }, |
| 139 | }, |
| 140 | }, |
| 141 | literal("')"), |
| 142 | }, |
| 143 | }, |
| 144 | { |
| 145 | in: `${shell echo ')'}`, |
| 146 | val: &funcShell{ |
| 147 | fclosure: fclosure{ |
| 148 | args: []Value{ |
Fumitoshi Ukai | b2670d9 | 2015-04-16 10:28:27 +0900 | [diff] [blame] | 149 | literal("{shell"), |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 150 | literal("echo ')'"), |
| 151 | }, |
| 152 | }, |
| 153 | }, |
| 154 | }, |
| 155 | { |
| 156 | in: `${shell echo '}'}`, |
| 157 | val: Expr{ |
| 158 | &funcShell{ |
| 159 | fclosure: fclosure{ |
| 160 | args: []Value{ |
Fumitoshi Ukai | b2670d9 | 2015-04-16 10:28:27 +0900 | [diff] [blame] | 161 | literal("{shell"), |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 162 | literal("echo '"), |
| 163 | }, |
| 164 | }, |
| 165 | }, |
| 166 | literal("'}"), |
| 167 | }, |
| 168 | }, |
| 169 | { |
| 170 | in: `$(shell make --version | ruby -n0e 'puts $$_[/Make (\d)/,1]')`, |
| 171 | val: &funcShell{ |
| 172 | fclosure: fclosure{ |
| 173 | args: []Value{ |
Fumitoshi Ukai | b2670d9 | 2015-04-16 10:28:27 +0900 | [diff] [blame] | 174 | literal("(shell"), |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 175 | literal(`make --version | ruby -n0e 'puts $_[/Make (\d)/,1]'`), |
| 176 | }, |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 177 | }, |
| 178 | }, |
| 179 | }, |
Fumitoshi Ukai | ee5c6fc | 2015-04-16 13:13:10 +0900 | [diff] [blame] | 180 | { |
| 181 | in: `$(and ${TRUE}, $(X) )`, |
| 182 | val: &funcAnd{ |
| 183 | fclosure: fclosure{ |
| 184 | args: []Value{ |
| 185 | literal("(and"), |
Fumitoshi Ukai | b8acae9 | 2015-06-18 15:36:57 +0900 | [diff] [blame] | 186 | &varref{ |
Fumitoshi Ukai | ee5c6fc | 2015-04-16 13:13:10 +0900 | [diff] [blame] | 187 | varname: literal("TRUE"), |
| 188 | }, |
Fumitoshi Ukai | b8acae9 | 2015-06-18 15:36:57 +0900 | [diff] [blame] | 189 | &varref{ |
Fumitoshi Ukai | ee5c6fc | 2015-04-16 13:13:10 +0900 | [diff] [blame] | 190 | varname: literal("X"), |
| 191 | }, |
| 192 | }, |
| 193 | }, |
| 194 | }, |
| 195 | }, |
| 196 | { |
| 197 | in: `$(call func, \ |
| 198 | foo)`, |
| 199 | val: &funcCall{ |
| 200 | fclosure: fclosure{ |
| 201 | args: []Value{ |
| 202 | literal("(call"), |
| 203 | literal("func"), |
| 204 | literal(" foo"), |
| 205 | }, |
| 206 | }, |
| 207 | }, |
| 208 | }, |
| 209 | { |
| 210 | in: `$(call func, \)`, |
| 211 | val: &funcCall{ |
| 212 | fclosure: fclosure{ |
| 213 | args: []Value{ |
| 214 | literal("(call"), |
| 215 | literal("func"), |
| 216 | literal(` \`), |
| 217 | }, |
| 218 | }, |
| 219 | }, |
| 220 | }, |
Fumitoshi Ukai | da7f255 | 2015-04-16 13:33:37 +0900 | [diff] [blame] | 221 | { |
| 222 | in: `$(eval ## comment)`, |
| 223 | val: &funcNop{ |
| 224 | expr: `$(eval ## comment)`, |
| 225 | }, |
| 226 | }, |
Fumitoshi Ukai | 150c861 | 2015-04-16 15:35:44 +0900 | [diff] [blame] | 227 | { |
| 228 | in: `$(eval foo = bar)`, |
| 229 | val: &funcEvalAssign{ |
| 230 | lhs: "foo", |
| 231 | op: "=", |
| 232 | rhs: literal("bar"), |
| 233 | }, |
| 234 | }, |
| 235 | { |
| 236 | in: `$(eval foo :=)`, |
| 237 | val: &funcEvalAssign{ |
| 238 | lhs: "foo", |
| 239 | op: ":=", |
| 240 | rhs: literal(""), |
| 241 | }, |
| 242 | }, |
Fumitoshi Ukai | d8cec17 | 2015-04-28 00:08:09 +0900 | [diff] [blame] | 243 | { |
| 244 | in: `$(eval foo := $(bar))`, |
| 245 | val: &funcEvalAssign{ |
| 246 | lhs: "foo", |
| 247 | op: ":=", |
Fumitoshi Ukai | b8acae9 | 2015-06-18 15:36:57 +0900 | [diff] [blame] | 248 | rhs: &varref{ |
Fumitoshi Ukai | d8cec17 | 2015-04-28 00:08:09 +0900 | [diff] [blame] | 249 | literal("bar"), |
| 250 | }, |
| 251 | }, |
| 252 | }, |
| 253 | { |
| 254 | in: `$(eval foo := $$(bar))`, |
| 255 | val: &funcEvalAssign{ |
| 256 | lhs: "foo", |
| 257 | op: ":=", |
| 258 | rhs: literal("$(bar)"), |
| 259 | }, |
| 260 | }, |
| 261 | { |
| 262 | in: `$(strip $1)`, |
| 263 | val: &funcStrip{ |
| 264 | fclosure: fclosure{ |
| 265 | args: []Value{ |
| 266 | literal("(strip"), |
| 267 | paramref(1), |
Fumitoshi Ukai | f0a2ba7 | 2015-04-19 00:02:32 +0900 | [diff] [blame] | 268 | }, |
| 269 | }, |
| 270 | }, |
Fumitoshi Ukai | d8cec17 | 2015-04-28 00:08:09 +0900 | [diff] [blame] | 271 | }, |
| 272 | { |
| 273 | in: `$(strip $(1))`, |
| 274 | val: &funcStrip{ |
| 275 | fclosure: fclosure{ |
| 276 | args: []Value{ |
| 277 | literal("(strip"), |
| 278 | paramref(1), |
Fumitoshi Ukai | f0a2ba7 | 2015-04-19 00:02:32 +0900 | [diff] [blame] | 279 | }, |
| 280 | }, |
| 281 | }, |
Fumitoshi Ukai | d8cec17 | 2015-04-28 00:08:09 +0900 | [diff] [blame] | 282 | }, |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 283 | } { |
Fumitoshi Ukai | 7c9aa9f | 2015-06-12 23:51:38 +0900 | [diff] [blame] | 284 | val, _, err := parseExpr([]byte(tc.in), nil, true) |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 285 | if tc.isErr { |
| 286 | if err == nil { |
| 287 | t.Errorf(`parseExpr(%q)=_, _, nil; want error`, tc.in) |
| 288 | } |
| 289 | continue |
| 290 | } |
| 291 | if err != nil { |
| 292 | t.Errorf(`parseExpr(%q)=_, _, %v; want nil error`, tc.in, err) |
| 293 | continue |
| 294 | } |
| 295 | if got, want := val, tc.val; !reflect.DeepEqual(got, want) { |
| 296 | t.Errorf("parseExpr(%[1]q)=%[2]q %#[2]v, _, _;\n want %[3]q %#[3]v, _, _", tc.in, got, want) |
| 297 | } |
| 298 | } |
| 299 | } |