| 1 | // Copyright 2019 Google LLC |
| 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 | |
| 15 | package parser |
| 16 | |
| 17 | import ( |
| 18 | "errors" |
| 19 | "strings" |
| 20 | "testing" |
| 21 | |
| 22 | "github.com/google/cel-go/common" |
| 23 | "github.com/google/cel-go/common/ast" |
| 24 | "github.com/google/cel-go/common/operators" |
| 25 | |
| 26 | "google.golang.org/protobuf/proto" |
| 27 | |
| 28 | exprpb "google.golang.org/genproto/googleapis/api/expr/v1alpha1" |
| 29 | ) |
| 30 | |
| 31 | func TestUnparse(t *testing.T) { |
| 32 | tests := []struct { |
| 33 | name string |
| 34 | in string |
| 35 | out interface{} |
| 36 | requiresMacroCalls bool |
| 37 | unparserOptions []UnparserOption |
| 38 | }{ |
| 39 | {name: "call_add", in: `a + b - c`}, |
| 40 | {name: "call_and", in: `a && b && c && d && e`}, |
| 41 | {name: "call_and_or", in: `a || b && (c || d) && e`}, |
| 42 | {name: "call_cond", in: `a ? b : c`}, |
| 43 | {name: "call_cond_nested_inner", in: `a ? (c ? d : e) : b`}, |
| 44 | {name: "call_cond_nested_outer", in: `a ? b : c ? d : e`, out: `a ? b : (c ? d : e)`}, |
| 45 | {name: "call_index", in: `a[1]["b"]`}, |
| 46 | {name: "call_index_eq", in: `x["a"].single_int32 == 23`}, |
| 47 | {name: "call_mul", in: `a * (b / c) % 0`}, |
| 48 | {name: "call_mul_add", in: `a + b * c`}, |
| 49 | {name: "call_mul_add_nested", in: `(a + b) * c / (d - e)`}, |
| 50 | {name: "call_mul_nested", in: `a * b / c % 0`}, |
| 51 | {name: "call_not", in: `!true`}, |
| 52 | {name: "call_neg", in: `-num`}, |
| 53 | {name: "call_or", in: `a || b || c || d || e`}, |
| 54 | {name: "call_neg_mult", in: `-(1 * 2)`}, |
| 55 | {name: "call_neg_add", in: `-(1 + 2)`}, |
| 56 | {name: "call_operator_precedence", in: `1 - (2 == -1)`}, |
| 57 | {name: "calc_distr_paren", in: `(1 + 2) * 3`}, |
| 58 | {name: "calc_distr_noparen", in: `1 + 2 * 3`}, |
| 59 | {name: "cond_tern_simple", in: `(x > 5) ? (x - 5) : 0`}, |
| 60 | {name: "cond_tern_neg_expr", in: `-((x > 5) ? (x - 5) : 0)`}, |
| 61 | {name: "cond_tern_neg_term", in: `-x ? (x - 5) : 0`}, |
| 62 | {name: "func_global", in: `size(a ? (b ? c : d) : e)`}, |
| 63 | {name: "func_member", in: `a.hello("world")`}, |
| 64 | {name: "func_no_arg", in: `zero()`}, |
| 65 | {name: "func_one_arg", in: `one("a")`}, |
| 66 | {name: "func_two_args", in: `and(d, 32u)`}, |
| 67 | {name: "func_var_args", in: `max(a, b, 100)`}, |
| 68 | {name: "func_neq", in: `x != "a"`}, |
| 69 | {name: "func_in", in: `a in b`}, |
| 70 | {name: "list_empty", in: `[]`}, |
| 71 | {name: "list_one", in: `[1]`}, |
| 72 | {name: "list_ints", in: `[1, 2, 3]`}, |
| 73 | {name: "list_doubles", in: `[1.0, 2.0, 3.0]`}, |
| 74 | {name: "list_doubles", in: `[1.1, 2.1, 3.1]`}, |
| 75 | {name: "list_uints", in: `[1u, 2u, 3u]`}, |
| 76 | {name: "list_numeric", in: `[1, 2.0, 3u]`}, |
| 77 | {name: "list_many", in: `["hello, world", "goodbye, world", "sure, why not?"]`}, |
| 78 | {name: "lit_bytes", in: `b"\303\203\302\277"`}, |
| 79 | {name: "lit_double", in: `-42.101`}, |
| 80 | {name: "lit_false", in: `false`}, |
| 81 | {name: "lit_int", in: `-405069`}, |
| 82 | {name: "lit_null", in: `null`}, |
| 83 | {name: "lit_string", in: `"hello:\t'world'"`}, |
| 84 | {name: "lit_string_quote", in: `"hello:\"world\""`}, |
| 85 | {name: "lit_true", in: `true`}, |
| 86 | {name: "lit_uint", in: `42u`}, |
| 87 | {name: "ident", in: `my_ident`}, |
| 88 | {name: "macro_has", in: `has(hello.world)`}, |
| 89 | {name: "map_empty", in: `{}`}, |
| 90 | {name: "map_lit_key", in: `{"a": a.b.c, b"\142": bytes(a.b.c)}`}, |
| 91 | {name: "map_expr_key", in: `{a: a, b: a.b, c: a.b.c, a ? b : c: false, a || b: true}`}, |
| 92 | {name: "msg_empty", in: `v1alpha1.Expr{}`}, |
| 93 | {name: "msg_fields", in: `v1alpha1.Expr{id: 1, call_expr: v1alpha1.Call_Expr{function: "name"}}`}, |
| 94 | {name: "select", in: `a.b.c`}, |
| 95 | {name: "idx_idx_sel", in: `a[b][c].name`}, |
| 96 | {name: "sel_expr_target", in: `(a + b).name`}, |
| 97 | {name: "sel_cond_target", in: `(a ? b : c).name`}, |
| 98 | {name: "idx_cond_target", in: `(a ? b : c)[0]`}, |
| 99 | {name: "cond_conj", in: `(a1 && a2) ? b : c`}, |
| 100 | {name: "cond_disj_conj", in: `a ? (b1 || b2) : (c1 && c2)`}, |
| 101 | {name: "call_cond_target", in: `(a ? b : c).method(d)`}, |
| 102 | {name: "cond_flat", in: `false && !true || false`}, |
| 103 | {name: "cond_paren", in: `false && (!true || false)`}, |
| 104 | {name: "cond_cond", in: `(false && !true || false) ? 2 : 3`}, |
| 105 | {name: "cond_binop", in: `(x < 5) ? x : 5`}, |
| 106 | {name: "cond_binop_binop", in: `(x > 5) ? (x - 5) : 0`}, |
| 107 | {name: "cond_cond_binop", in: `(x > 5) ? ((x > 10) ? (x - 10) : 5) : 0`}, |
| 108 | {name: "select_opt", in: `a.?b`}, |
| 109 | {name: "index_opt", in: `a[?b]`}, |
| 110 | {name: "list_lit_opt", in: `[?a, ?b, c]`}, |
| 111 | {name: "map_lit_opt", in: `{?a: b, c: d}`}, |
| 112 | {name: "msg_fields_opt", in: `v1alpha1.Expr{?id: id, call_expr: v1alpha1.Call_Expr{function: "name"}}`}, |
| 113 | {name: "select_quoted", in: "a.`b-c`"}, |
| 114 | {name: "opt_select_quoted", in: "a.?`b.c`"}, |
| 115 | {name: "message_create_quoted", in: "MyType{`in`: false}"}, |
| 116 | |
| 117 | // Equivalent expressions form unparse which do not match the originals. |
| 118 | {name: "call_add_equiv", in: `a+b-c`, out: `a + b - c`}, |
| 119 | {name: "call_cond_equiv", in: `a ? b : c`, out: `a ? b : c`}, |
| 120 | {name: "call_index_equiv", in: `a[ 1 ]["b"]`, out: `a[1]["b"]`}, |
| 121 | {name: "call_or_and_equiv", in: `(false && !true) || false`, out: `false && !true || false`}, |
| 122 | {name: "call_not_not_equiv", in: `!!true`, out: `true`}, |
| 123 | {name: "call_cond_equiv", in: `(a || b ? c : d).e`, out: `((a || b) ? c : d).e`}, |
| 124 | {name: "lit_quote_bytes_equiv", in: `b'aaa"bbb'`, out: `b"\141\141\141\042\142\142\142"`}, |
| 125 | {name: "select_equiv", in: `a . b . c`, out: `a.b.c`}, |
| 126 | |
| 127 | // These expressions require macro call tracking to be enabled. |
| 128 | { |
| 129 | name: "comp_all", |
| 130 | in: `[1, 2, 3].all(x, x > 0)`, |
| 131 | requiresMacroCalls: true, |
| 132 | }, |
| 133 | { |
| 134 | name: "comp_exists", |
| 135 | in: `[1, 2, 3].exists(x, x > 0)`, |
| 136 | requiresMacroCalls: true, |
| 137 | }, |
| 138 | { |
| 139 | name: "comp_map", |
| 140 | in: `[1, 2, 3].map(x, x >= 2, x * 4)`, |
| 141 | requiresMacroCalls: true, |
| 142 | }, |
| 143 | { |
| 144 | name: "comp_exists_one", |
| 145 | in: `[1, 2, 3].exists_one(x, x >= 2)`, |
| 146 | requiresMacroCalls: true, |
| 147 | }, |
| 148 | { |
| 149 | name: "comp_nested", |
| 150 | in: `[[1], [2], [3]].map(x, x.filter(y, y > 1))`, |
| 151 | requiresMacroCalls: true, |
| 152 | }, |
| 153 | { |
| 154 | name: "comp_chained", |
| 155 | in: `[1, 2, 3].map(x, x >= 2, x * 4).filter(x, x <= 10)`, |
| 156 | requiresMacroCalls: true, |
| 157 | }, |
| 158 | { |
| 159 | name: "comp_chained_opt", |
| 160 | in: `[?a, b[?0], c].map(x, x >= 2, x * 4).filter(x, x <= 10)`, |
| 161 | requiresMacroCalls: true, |
| 162 | }, |
| 163 | { |
| 164 | name: "comp_map_opt", |
| 165 | in: `{?a: b[?0]}.map(k, x >= 2, x * 4)`, |
| 166 | requiresMacroCalls: true, |
| 167 | }, |
| 168 | { |
| 169 | name: "comp_map_opt", |
| 170 | in: `{a: has(b.c)}.exists(k, k != "")`, |
| 171 | requiresMacroCalls: true, |
| 172 | }, |
| 173 | { |
| 174 | name: "comp_nested", |
| 175 | in: `{a: [1, 2].all(i > 0)}.exists(k, k != "")`, |
| 176 | requiresMacroCalls: true, |
| 177 | }, |
| 178 | |
| 179 | // These expressions will not be wrapped because they haven't met the |
| 180 | // conditions required by the provided unparser options |
| 181 | { |
| 182 | name: "call_no_wrap_no_operators", |
| 183 | in: "a + b + c + d", |
| 184 | out: "a + b + c + d", |
| 185 | unparserOptions: []UnparserOption{ |
| 186 | WrapOnColumn(3), |
| 187 | }, |
| 188 | }, |
| 189 | { |
| 190 | name: "call_no_wrap_column_limit_large_val", |
| 191 | in: "a + b + c + d", |
| 192 | out: "a + b + c + d", |
| 193 | unparserOptions: []UnparserOption{ |
| 194 | WrapOnColumn(1000), |
| 195 | WrapOnOperators(operators.Add), |
| 196 | }, |
| 197 | }, |
| 198 | { |
| 199 | name: "call_no_wrap_column_limit_equal_length_to_input", |
| 200 | in: "a + b + c + d", |
| 201 | out: "a + b + c + d", |
| 202 | unparserOptions: []UnparserOption{ |
| 203 | WrapOnColumn(13), |
| 204 | WrapOnOperators(operators.Add), |
| 205 | }, |
| 206 | }, |
| 207 | |
| 208 | // These expressions will be formatted based on the unparser options provided |
| 209 | { |
| 210 | name: "call_wrap_add", |
| 211 | in: "a + b - d * e", |
| 212 | out: "a +\nb - d * e", |
| 213 | unparserOptions: []UnparserOption{ |
| 214 | WrapOnColumn(3), |
| 215 | WrapOnOperators(operators.Add), |
| 216 | }, |
| 217 | }, |
| 218 | { |
| 219 | name: "call_wrap_add_subtract", |
| 220 | in: "a * b + c - d * e", |
| 221 | out: "a * b +\nc -\nd * e", |
| 222 | unparserOptions: []UnparserOption{ |
| 223 | WrapOnColumn(3), |
| 224 | WrapOnOperators(operators.Add, operators.Subtract), |
| 225 | }, |
| 226 | }, |
| 227 | { |
| 228 | name: "call_wrap_add_subtract", |
| 229 | in: "a * b + c - d * e", |
| 230 | out: "a * b +\nc -\nd * e", |
| 231 | unparserOptions: []UnparserOption{ |
| 232 | WrapOnColumn(3), |
| 233 | WrapOnOperators(operators.Add, operators.Subtract), |
| 234 | }, |
| 235 | }, |
| 236 | { |
| 237 | name: "call_wrap_logical_and", |
| 238 | in: "a && b && c && d && e", |
| 239 | out: "a &&\nb &&\nc &&\nd &&\ne", |
| 240 | unparserOptions: []UnparserOption{ |
| 241 | WrapOnColumn(3), |
| 242 | WrapOnOperators(operators.LogicalAnd), |
| 243 | }, |
| 244 | }, |
| 245 | { |
| 246 | name: "call_wrap_logical_and_2", |
| 247 | in: "a && b", |
| 248 | out: "a &&\nb", |
| 249 | unparserOptions: []UnparserOption{ |
| 250 | WrapOnColumn(3), |
| 251 | WrapOnOperators(operators.LogicalAnd), |
| 252 | }, |
| 253 | }, |
| 254 | { |
| 255 | name: "call_wrap_conditional", |
| 256 | in: "a ? b : c ? d : e", |
| 257 | out: "a ?\nb : (c ?\nd : e)", |
| 258 | unparserOptions: []UnparserOption{ |
| 259 | WrapOnColumn(3), |
| 260 | WrapOnOperators(operators.Conditional), |
| 261 | }, |
| 262 | }, |
| 263 | { |
| 264 | name: "call_wrap_or", |
| 265 | in: "a || b || c || d || e", |
| 266 | out: "a ||\nb ||\nc ||\nd ||\ne", |
| 267 | unparserOptions: []UnparserOption{ |
| 268 | WrapOnColumn(3), |
| 269 | WrapOnOperators(operators.LogicalOr), |
| 270 | }, |
| 271 | }, |
| 272 | { |
| 273 | name: "call_wrap_equals", |
| 274 | in: "a == b == c == d == e", |
| 275 | out: "a ==\nb ==\nc ==\nd ==\ne", |
| 276 | unparserOptions: []UnparserOption{ |
| 277 | WrapOnColumn(3), |
| 278 | WrapOnOperators(operators.Equals), |
| 279 | }, |
| 280 | }, |
| 281 | { |
| 282 | name: "call_wrap_greater", |
| 283 | in: "a > b > c > d > e", |
| 284 | out: "a >\nb >\nc >\nd >\ne", |
| 285 | unparserOptions: []UnparserOption{ |
| 286 | WrapOnColumn(3), |
| 287 | WrapOnOperators(operators.Greater), |
| 288 | }, |
| 289 | }, |
| 290 | { |
| 291 | name: "call_wrap_greater_equals", |
| 292 | in: "a >= b >= c >= d >= e", |
| 293 | out: "a >=\nb >=\nc >=\nd >=\ne", |
| 294 | unparserOptions: []UnparserOption{ |
| 295 | WrapOnColumn(3), |
| 296 | WrapOnOperators(operators.GreaterEquals), |
| 297 | }, |
| 298 | }, |
| 299 | { |
| 300 | name: "call_wrap_in", |
| 301 | in: "a in b in c in d in e", |
| 302 | out: "a in\nb in\nc in\nd in\ne", |
| 303 | unparserOptions: []UnparserOption{ |
| 304 | WrapOnColumn(3), |
| 305 | WrapOnOperators(operators.In), |
| 306 | }, |
| 307 | }, |
| 308 | { |
| 309 | name: "call_wrap_less", |
| 310 | in: "a < b < c < d < e", |
| 311 | out: "a <\nb <\nc <\nd <\ne", |
| 312 | unparserOptions: []UnparserOption{ |
| 313 | WrapOnColumn(3), |
| 314 | WrapOnOperators(operators.Less), |
| 315 | }, |
| 316 | }, |
| 317 | { |
| 318 | name: "call_wrap_less_equals", |
| 319 | in: "a <= b <= c <= d <= e", |
| 320 | out: "a <=\nb <=\nc <=\nd <=\ne", |
| 321 | unparserOptions: []UnparserOption{ |
| 322 | WrapOnColumn(3), |
| 323 | WrapOnOperators(operators.LessEquals), |
| 324 | }, |
| 325 | }, |
| 326 | { |
| 327 | name: "call_wrap_not_equals", |
| 328 | in: "a != b != c != d != e", |
| 329 | out: "a !=\nb !=\nc !=\nd !=\ne", |
| 330 | unparserOptions: []UnparserOption{ |
| 331 | WrapOnColumn(3), |
| 332 | WrapOnOperators(operators.NotEquals), |
| 333 | }, |
| 334 | }, |
| 335 | { |
| 336 | name: "call_wrap_divide", |
| 337 | in: "a / b / c / d / e", |
| 338 | out: "a /\nb /\nc /\nd /\ne", |
| 339 | unparserOptions: []UnparserOption{ |
| 340 | WrapOnColumn(3), |
| 341 | WrapOnOperators(operators.Divide), |
| 342 | }, |
| 343 | }, |
| 344 | { |
| 345 | name: "call_wrap_modulo", |
| 346 | in: "a % b % c % d % e", |
| 347 | out: "a %\nb %\nc %\nd %\ne", |
| 348 | unparserOptions: []UnparserOption{ |
| 349 | WrapOnColumn(3), |
| 350 | WrapOnOperators(operators.Modulo), |
| 351 | }, |
| 352 | }, |
| 353 | { |
| 354 | name: "call_wrap_multiply", |
| 355 | in: "a * b * c * d * e", |
| 356 | out: "a *\nb *\nc *\nd *\ne", |
| 357 | unparserOptions: []UnparserOption{ |
| 358 | WrapOnColumn(3), |
| 359 | WrapOnOperators(operators.Multiply), |
| 360 | }, |
| 361 | }, |
| 362 | { |
| 363 | name: "call_wrap_logical_and_long_variables", |
| 364 | in: "longVariableA && longVariableB && longVariableC", |
| 365 | out: "longVariableA &&\nlongVariableB &&\nlongVariableC", |
| 366 | unparserOptions: []UnparserOption{ |
| 367 | WrapOnColumn(3), |
| 368 | WrapOnOperators(operators.LogicalAnd), |
| 369 | }, |
| 370 | }, |
| 371 | { |
| 372 | name: "comp_chained_wrap_comparisons", |
| 373 | in: "[1, 2, 3].map(x, x >= 2, x * 4).filter(x, x <= 10)", |
| 374 | out: "[1, 2, 3].map(x, x >=\n2, x * 4).filter(x, x <=\n10)", |
| 375 | requiresMacroCalls: true, |
| 376 | unparserOptions: []UnparserOption{ |
| 377 | WrapOnColumn(3), |
| 378 | WrapOnOperators(operators.GreaterEquals, operators.LessEquals), |
| 379 | }, |
| 380 | }, |
| 381 | { |
| 382 | name: "call_wrap_before_add", |
| 383 | in: "a + b - d * e", |
| 384 | out: "a\n+ b - d * e", |
| 385 | unparserOptions: []UnparserOption{ |
| 386 | WrapOnColumn(3), |
| 387 | WrapOnOperators(operators.Add), |
| 388 | WrapAfterColumnLimit(false), |
| 389 | }, |
| 390 | }, |
| 391 | { |
| 392 | name: "call_wrap_before_add_subtract", |
| 393 | in: "a * b + c - d * e", |
| 394 | out: "a * b\n+ c\n- d * e", |
| 395 | unparserOptions: []UnparserOption{ |
| 396 | WrapOnColumn(3), |
| 397 | WrapOnOperators(operators.Add, operators.Subtract), |
| 398 | WrapAfterColumnLimit(false), |
| 399 | }, |
| 400 | }, |
| 401 | { |
| 402 | name: "call_wrap_logical_and_long_variables", |
| 403 | in: "longVariableA && longVariableB && longVariableC", |
| 404 | out: "longVariableA\n&& longVariableB\n&& longVariableC", |
| 405 | unparserOptions: []UnparserOption{ |
| 406 | WrapOnColumn(3), |
| 407 | WrapOnOperators(operators.LogicalAnd), |
| 408 | WrapAfterColumnLimit(false), |
| 409 | }, |
| 410 | }, |
| 411 | { |
| 412 | name: "call_wrap_logical_and_long_input", |
| 413 | in: `"my-principal-group" in request.auth.claims && request.auth.claims.iat > now - duration("5m")`, |
| 414 | out: `"my-principal-group" in request.auth.claims &&` + "\n" + `request.auth.claims.iat > now - duration("5m")`, |
| 415 | unparserOptions: []UnparserOption{ |
| 416 | WrapOnColumn(40), |
| 417 | WrapOnOperators(operators.LogicalAnd), |
| 418 | }, |
| 419 | }, |
| 420 | { |
| 421 | name: "call_wrap_before_logical_and_long_input", |
| 422 | in: `"my-principal-group" in request.auth.claims && request.auth.claims.iat > now - duration("5m")`, |
| 423 | out: `"my-principal-group" in request.auth.claims` + "\n" + `&& request.auth.claims.iat > now - duration("5m")`, |
| 424 | unparserOptions: []UnparserOption{ |
| 425 | WrapOnColumn(40), |
| 426 | WrapOnOperators(operators.LogicalAnd), |
| 427 | WrapAfterColumnLimit(false), |
| 428 | }, |
| 429 | }, |
| 430 | { |
| 431 | // By default: |
| 432 | // - Column limit is at 80 |
| 433 | // - && and || are wrapped |
| 434 | // - Wrapping occurs after the symbol |
| 435 | name: "call_wrap_default", |
| 436 | in: `jwt.extra_claims.filter(c, c.startsWith("group")).all(c, jwt.extra_claims[c].all(g, g.endsWith("@acme.co"))) && jwt.extra_claims.exists(c, c.startsWith("group")) || request.auth.claims.group == "admin" || request.auth.principal == "user:[email protected]"`, |
| 437 | out: `jwt.extra_claims.filter(c, c.startsWith("group")).all(c, jwt.extra_claims[c].all(g, g.endsWith("@acme.co"))) &&` + |
| 438 | "\n" + |
| 439 | `jwt.extra_claims.exists(c, c.startsWith("group")) || request.auth.claims.group == "admin" ||` + |
| 440 | "\n" + |
| 441 | `request.auth.principal == "user:[email protected]"`, |
| 442 | requiresMacroCalls: true, |
| 443 | }, |
| 444 | { |
| 445 | // && and || are wrapped by default if only the column limit is specified |
| 446 | name: "call_wrap_default_operators", |
| 447 | in: "longVariableA && longVariableB || longVariableC + longVariableD - longVariableE", |
| 448 | out: "longVariableA &&\nlongVariableB ||\nlongVariableC + longVariableD - longVariableE", |
| 449 | unparserOptions: []UnparserOption{ |
| 450 | WrapOnColumn(3), |
| 451 | }, |
| 452 | }, |
| 453 | } |
| 454 | |
| 455 | for _, tst := range tests { |
| 456 | tc := tst |
| 457 | t.Run(tc.name, func(t *testing.T) { |
| 458 | prsr, err := NewParser( |
| 459 | Macros(AllMacros...), |
| 460 | PopulateMacroCalls(tc.requiresMacroCalls), |
| 461 | EnableOptionalSyntax(true), |
| 462 | EnableIdentEscapeSyntax(true), |
| 463 | ) |
| 464 | if err != nil { |
| 465 | t.Fatalf("NewParser() failed: %v", err) |
| 466 | } |
| 467 | p, iss := prsr.Parse(common.NewTextSource(tc.in)) |
| 468 | if len(iss.GetErrors()) > 0 { |
| 469 | t.Fatalf("parser.Parse(%s) failed: %v", tc.in, iss.ToDisplayString()) |
| 470 | } |
| 471 | out, err := Unparse(p.Expr(), p.SourceInfo(), tc.unparserOptions...) |
| 472 | |
| 473 | if err != nil { |
| 474 | t.Fatalf("Unparse(%s) failed: %v", tc.in, err) |
| 475 | } |
| 476 | var want interface{} = tc.in |
| 477 | if tc.out != nil { |
| 478 | want = tc.out |
| 479 | } |
| 480 | if out != want { |
| 481 | t.Errorf("Unparse() got '%s', wanted '%s'", out, want) |
| 482 | } |
| 483 | p2, iss := prsr.Parse(common.NewTextSource(out)) |
| 484 | if len(iss.GetErrors()) > 0 { |
| 485 | t.Fatalf("parser.Parse(%s) roundtrip failed: %v", tc.in, iss.ToDisplayString()) |
| 486 | } |
| 487 | before, err := ast.ExprToProto(p.Expr()) |
| 488 | if err != nil { |
| 489 | t.Fatalf("ast.ExprToProto() failed: %v", err) |
| 490 | } |
| 491 | after, err := ast.ExprToProto(p2.Expr()) |
| 492 | if err != nil { |
| 493 | t.Fatalf("ast.ExprToProto() failed: %v", err) |
| 494 | } |
| 495 | if !proto.Equal(before, after) { |
| 496 | t.Errorf("Roundtrip Parse() differs from original. Got '%v', wanted '%v'", before, after) |
| 497 | } |
| 498 | }) |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | func TestUnparseErrors(t *testing.T) { |
| 503 | validConstantExpression := &exprpb.Expr{ |
| 504 | ExprKind: &exprpb.Expr_ConstExpr{ |
| 505 | ConstExpr: &exprpb.Constant{ |
| 506 | ConstantKind: &exprpb.Constant_NullValue{}, |
| 507 | }, |
| 508 | }, |
| 509 | } |
| 510 | tests := []struct { |
| 511 | name string |
| 512 | in *exprpb.Expr |
| 513 | err error |
| 514 | unparserOptions []UnparserOption |
| 515 | }{ |
| 516 | {name: "empty_expr", in: &exprpb.Expr{}, err: errors.New("unsupported expression")}, |
| 517 | { |
| 518 | name: "bad_args", |
| 519 | in: &exprpb.Expr{ |
| 520 | ExprKind: &exprpb.Expr_CallExpr{ |
| 521 | CallExpr: &exprpb.Expr_Call{ |
| 522 | Function: "_&&_", |
| 523 | Args: []*exprpb.Expr{{}, {}}, |
| 524 | }, |
| 525 | }, |
| 526 | }, |
| 527 | err: errors.New("unsupported expression"), |
| 528 | }, |
| 529 | { |
| 530 | name: "bad_struct", |
| 531 | in: &exprpb.Expr{ |
| 532 | ExprKind: &exprpb.Expr_StructExpr{ |
| 533 | StructExpr: &exprpb.Expr_CreateStruct{ |
| 534 | MessageName: "Msg", |
| 535 | Entries: []*exprpb.Expr_CreateStruct_Entry{ |
| 536 | {KeyKind: &exprpb.Expr_CreateStruct_Entry_FieldKey{FieldKey: "field"}}, |
| 537 | }, |
| 538 | }, |
| 539 | }, |
| 540 | }, |
| 541 | err: errors.New("unsupported expression"), |
| 542 | }, |
| 543 | { |
| 544 | name: "bad_map", |
| 545 | in: &exprpb.Expr{ |
| 546 | ExprKind: &exprpb.Expr_StructExpr{ |
| 547 | StructExpr: &exprpb.Expr_CreateStruct{ |
| 548 | Entries: []*exprpb.Expr_CreateStruct_Entry{ |
| 549 | {KeyKind: &exprpb.Expr_CreateStruct_Entry_FieldKey{FieldKey: "field"}}, |
| 550 | }, |
| 551 | }, |
| 552 | }, |
| 553 | }, |
| 554 | err: errors.New("unsupported expression"), |
| 555 | }, |
| 556 | { |
| 557 | name: "bad_index", |
| 558 | in: &exprpb.Expr{ |
| 559 | ExprKind: &exprpb.Expr_CallExpr{ |
| 560 | CallExpr: &exprpb.Expr_Call{ |
| 561 | Function: "_[_]", |
| 562 | Args: []*exprpb.Expr{{}, {}}, |
| 563 | }, |
| 564 | }, |
| 565 | }, |
| 566 | err: errors.New("unsupported expression"), |
| 567 | }, |
| 568 | { |
| 569 | name: "bad_unparser_option_wrap_column_zero", |
| 570 | in: validConstantExpression, |
| 571 | err: errors.New("Invalid unparser option. Wrap column value must be greater than or equal to 1. Got 0 instead"), |
| 572 | unparserOptions: []UnparserOption{ |
| 573 | WrapOnColumn(0), |
| 574 | }, |
| 575 | }, |
| 576 | { |
| 577 | name: "bad_unparser_option_wrap_column_negative", |
| 578 | in: validConstantExpression, |
| 579 | err: errors.New("Invalid unparser option. Wrap column value must be greater than or equal to 1. Got -1 instead"), |
| 580 | unparserOptions: []UnparserOption{ |
| 581 | WrapOnColumn(-1), |
| 582 | }, |
| 583 | }, |
| 584 | { |
| 585 | name: "bad_unparser_option_unsupported_operator", |
| 586 | in: validConstantExpression, |
| 587 | err: errors.New("Invalid unparser option. Unsupported operator: bogus"), |
| 588 | unparserOptions: []UnparserOption{ |
| 589 | WrapOnOperators("bogus"), |
| 590 | }, |
| 591 | }, |
| 592 | { |
| 593 | name: "bad_unparser_option_unary_operator", |
| 594 | in: validConstantExpression, |
| 595 | err: errors.New("Invalid unparser option. Unary operators are unsupported: " + operators.Negate), |
| 596 | unparserOptions: []UnparserOption{ |
| 597 | WrapOnOperators(operators.Negate), |
| 598 | }, |
| 599 | }, |
| 600 | } |
| 601 | |
| 602 | for _, tst := range tests { |
| 603 | tc := tst |
| 604 | t.Run(tc.name, func(t *testing.T) { |
| 605 | info := ast.NewSourceInfo(nil) |
| 606 | e, err := ast.ProtoToExpr(tc.in) |
| 607 | if err != nil { |
| 608 | t.Fatalf("ast.ProtoToExpr(%v) failed: %v", tc.in, err) |
| 609 | } |
| 610 | out, err := Unparse(e, info, tc.unparserOptions...) |
| 611 | if err == nil { |
| 612 | t.Fatalf("Unparse(%v) got %v, wanted error %v", tc.in, out, tc.err) |
| 613 | } |
| 614 | if !strings.Contains(err.Error(), tc.err.Error()) { |
| 615 | t.Errorf("Unparse(%v) got unexpected error: %v, wanted %v", tc.in, err, tc.err) |
| 616 | } |
| 617 | }) |
| 618 | } |
| 619 | } |