| 1 | // Copyright 2023 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 ast_test |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
| 19 | "reflect" |
| 20 | "testing" |
| 21 | |
| 22 | "github.com/google/cel-go/common/ast" |
| 23 | "github.com/google/cel-go/common/overloads" |
| 24 | "github.com/google/cel-go/common/types" |
| 25 | ) |
| 26 | |
| 27 | func TestSetKindCase(t *testing.T) { |
| 28 | fac := ast.NewExprFactory() |
| 29 | tests := []ast.Expr{ |
| 30 | fac.NewUnspecifiedExpr(1), |
| 31 | fac.NewCall(1, "_==_", fac.NewLiteral(2, types.True), fac.NewLiteral(3, types.False)), |
| 32 | fac.NewMemberCall(1, overloads.Size, fac.NewLiteral(2, types.String("hello"))), |
| 33 | fac.NewComprehension(12, |
| 34 | fac.NewList(1, []ast.Expr{}, []int32{}), |
| 35 | "i", |
| 36 | "__result__", |
| 37 | fac.NewLiteral(5, types.False), |
| 38 | fac.NewCall(8, "@not_strictly_false", fac.NewCall(7, "!_", fac.NewAccuIdent(6))), |
| 39 | fac.NewCall(10, "_||_", fac.NewAccuIdent(9), fac.NewIdent(4, "i")), |
| 40 | fac.NewAccuIdent(11), |
| 41 | ), |
| 42 | fac.NewIdent(1, "a"), |
| 43 | fac.NewLiteral(1, types.Bytes("hello")), |
| 44 | fac.NewList(1, []ast.Expr{fac.NewIdent(2, "a"), fac.NewIdent(3, "b")}, []int32{}), |
| 45 | fac.NewMap(1, []ast.EntryExpr{ |
| 46 | fac.NewMapEntry(2, |
| 47 | fac.NewLiteral(3, types.String("string")), |
| 48 | fac.NewCall(6, "_?._", fac.NewIdent(4, "a"), fac.NewLiteral(5, types.String("b"))), |
| 49 | true), |
| 50 | }), |
| 51 | fac.NewSelect(2, fac.NewIdent(1, "a"), "b"), |
| 52 | fac.NewStruct(1, |
| 53 | "custom.StructType", |
| 54 | []ast.EntryExpr{ |
| 55 | fac.NewStructField(2, |
| 56 | "uint_field", |
| 57 | fac.NewLiteral(3, types.Uint(42)), |
| 58 | false), |
| 59 | }), |
| 60 | } |
| 61 | expr := nilTestExpr(t) |
| 62 | for i, tst := range tests { |
| 63 | t.Run(fmt.Sprintf("%d", i), func(t *testing.T) { |
| 64 | expr.SetKindCase(tst) |
| 65 | switch expr.Kind() { |
| 66 | case ast.CallKind: |
| 67 | if !reflect.DeepEqual(expr.AsCall(), tst.AsCall()) { |
| 68 | t.Errorf("got %v, wanted %v", expr.AsCall(), tst.AsCall()) |
| 69 | } |
| 70 | case ast.ComprehensionKind: |
| 71 | if !reflect.DeepEqual(expr.AsComprehension(), tst.AsComprehension()) { |
| 72 | t.Errorf("got %v, wanted %v", expr.AsComprehension(), tst.AsComprehension()) |
| 73 | } |
| 74 | case ast.IdentKind: |
| 75 | if !reflect.DeepEqual(expr.AsIdent(), tst.AsIdent()) { |
| 76 | t.Errorf("got %v, wanted %v", expr.AsIdent(), tst.AsIdent()) |
| 77 | } |
| 78 | case ast.LiteralKind: |
| 79 | if !reflect.DeepEqual(expr.AsLiteral(), tst.AsLiteral()) { |
| 80 | t.Errorf("got %v, wanted %v", expr.AsLiteral(), tst.AsLiteral()) |
| 81 | } |
| 82 | case ast.ListKind: |
| 83 | if !reflect.DeepEqual(expr.AsList(), tst.AsList()) { |
| 84 | t.Errorf("got %v, wanted %v", expr.AsList(), tst.AsList()) |
| 85 | } |
| 86 | case ast.UnspecifiedExprKind: |
| 87 | if !reflect.DeepEqual(expr, tst) { |
| 88 | t.Errorf("got %v, wanted %v", expr, tst) |
| 89 | } |
| 90 | case ast.MapKind: |
| 91 | if !reflect.DeepEqual(expr.AsMap(), tst.AsMap()) { |
| 92 | t.Errorf("got %v, wanted %v", expr, tst) |
| 93 | } |
| 94 | case ast.SelectKind: |
| 95 | if !reflect.DeepEqual(expr.AsSelect(), tst.AsSelect()) { |
| 96 | t.Errorf("got %v, wanted %v", expr, tst) |
| 97 | } |
| 98 | case ast.StructKind: |
| 99 | if !reflect.DeepEqual(expr.AsStruct(), tst.AsStruct()) { |
| 100 | t.Errorf("got %v, wanted %v", expr, tst) |
| 101 | } |
| 102 | default: |
| 103 | t.Errorf("unable to determine kind case: %v", tst) |
| 104 | } |
| 105 | }) |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | func TestCall(t *testing.T) { |
| 110 | fac := ast.NewExprFactory() |
| 111 | expr := fac.NewCall(1, "size", fac.NewLiteral(2, types.String("hello"))) |
| 112 | if expr.Kind() != ast.CallKind { |
| 113 | t.Fatalf("NewCall() produced non-call expression: %v", expr.Kind()) |
| 114 | } |
| 115 | call := expr.AsCall() |
| 116 | if call.FunctionName() != "size" { |
| 117 | t.Errorf("Function() got %s, wanted 'size''", call.FunctionName()) |
| 118 | } |
| 119 | if len(call.Args()) != 1 { |
| 120 | t.Errorf("Args() got %v, wanted one", call.Args()) |
| 121 | } |
| 122 | if call.IsMemberFunction() { |
| 123 | t.Error("IsMemberFunction() got true, wanted false") |
| 124 | } |
| 125 | if call.Target().ID() != 0 { |
| 126 | t.Errorf("empty Target() got %d, wanted 0", call.Target().ID()) |
| 127 | } |
| 128 | expr.RenumberIDs(testIDGen(100)) |
| 129 | if expr.ID() != 101 { |
| 130 | t.Errorf("expr.ID() got %d, wanted 101 after RenumberIDs", expr.ID()) |
| 131 | } |
| 132 | if expr.AsCall().Args()[0].ID() != 102 { |
| 133 | t.Errorf("Args()[0].ID() got %d, wanted 102 after RenumberIDs", expr.AsCall().Args()[0].ID()) |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | func TestMemberCall(t *testing.T) { |
| 138 | fac := ast.NewExprFactory() |
| 139 | expr := fac.NewMemberCall(1, "size", fac.NewLiteral(2, types.String("hello"))) |
| 140 | if expr.Kind() != ast.CallKind { |
| 141 | t.Fatalf("NewCall() produced non-call expression: %v", expr.Kind()) |
| 142 | } |
| 143 | call := expr.AsCall() |
| 144 | if call.FunctionName() != "size" { |
| 145 | t.Errorf("Function() got %s, wanted 'size''", call.FunctionName()) |
| 146 | } |
| 147 | if len(call.Args()) != 0 { |
| 148 | t.Errorf("Args() got %v, wanted zero", call.Args()) |
| 149 | } |
| 150 | if !call.IsMemberFunction() { |
| 151 | t.Error("IsMemberFunction() got false, wanted true") |
| 152 | } |
| 153 | if call.Target().ID() != 2 { |
| 154 | t.Errorf("Target() got %d, wanted 2", call.Target().ID()) |
| 155 | } |
| 156 | expr.RenumberIDs(testIDGen(100)) |
| 157 | if expr.ID() != 101 { |
| 158 | t.Errorf("expr.ID() got %d, wanted 101 after RenumberIDs", expr.ID()) |
| 159 | } |
| 160 | if expr.AsCall().Target().ID() != 102 { |
| 161 | t.Errorf("Target().ID() got %d, wanted 102 after RenumberIDs", expr.AsCall().Target().ID()) |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | func TestCallNil(t *testing.T) { |
| 166 | expr := nilTestExpr(t) |
| 167 | call := expr.AsCall() |
| 168 | if call.FunctionName() != "" { |
| 169 | t.Errorf("FunctionName() got %s, wanted empty value", call.FunctionName()) |
| 170 | } |
| 171 | if len(call.Args()) != 0 { |
| 172 | t.Errorf("Args() got %v, wanted zero", call.Args()) |
| 173 | } |
| 174 | if call.IsMemberFunction() { |
| 175 | t.Error("IsMemberFunction() got true, wanted false") |
| 176 | } |
| 177 | if call.Target().ID() != 0 { |
| 178 | t.Errorf("empty Target() got %d, wanted 0", call.Target().ID()) |
| 179 | } |
| 180 | expr.RenumberIDs(testIDGen(100)) |
| 181 | if expr.ID() != 101 { |
| 182 | t.Errorf("RenumberIDs() got %d, wanted 101", expr.ID()) |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | func TestComprehension(t *testing.T) { |
| 187 | fac := ast.NewExprFactory() |
| 188 | expr := fac.NewComprehension(1, |
| 189 | fac.NewList(2, []ast.Expr{ |
| 190 | fac.NewLiteral(3, types.Int(1)), |
| 191 | fac.NewLiteral(4, types.Int(2)), |
| 192 | }, []int32{}), |
| 193 | "i", |
| 194 | "__result__", |
| 195 | fac.NewLiteral(5, types.False), |
| 196 | fac.NewLiteral(6, types.True), |
| 197 | fac.NewCall(7, "_||_", |
| 198 | fac.NewAccuIdent(8), |
| 199 | fac.NewCall(9, "<", fac.NewIdent(10, "i"), fac.NewLiteral(11, types.Int(3))), |
| 200 | ), |
| 201 | fac.NewAccuIdent(12), |
| 202 | ) |
| 203 | comp := expr.AsComprehension() |
| 204 | if comp.IterRange().Kind() != ast.ListKind { |
| 205 | t.Errorf("IterRange() got %v, wanted list", comp.IterRange().Kind()) |
| 206 | } |
| 207 | if comp.AccuInit().Kind() != ast.LiteralKind { |
| 208 | t.Errorf("AccuInit() got %v, wanted literal", comp.AccuInit().Kind()) |
| 209 | } |
| 210 | if comp.LoopCondition().Kind() != ast.LiteralKind { |
| 211 | t.Errorf("LoopCondition() got %v, wanted literal", comp.LoopCondition().Kind()) |
| 212 | } |
| 213 | if comp.LoopStep().Kind() != ast.CallKind { |
| 214 | t.Errorf("LoopStep() got %v, wanted call", comp.LoopStep().Kind()) |
| 215 | } |
| 216 | if comp.Result().Kind() != ast.IdentKind { |
| 217 | t.Errorf("Result() got %v, wanted identifier", comp.Result().Kind()) |
| 218 | } |
| 219 | expr.RenumberIDs(testIDGen(100)) |
| 220 | if expr.ID() != 101 { |
| 221 | t.Errorf("Renumbering an unspecified expression mutated the value: %v", expr) |
| 222 | } |
| 223 | comp = expr.AsComprehension() |
| 224 | if comp.IterRange().ID() != 102 { |
| 225 | t.Errorf("IterRange() got %v, wanted list", comp.IterRange().ID()) |
| 226 | } |
| 227 | if comp.AccuInit().ID() != 105 { |
| 228 | t.Errorf("AccuInit() got %v, wanted literal", comp.AccuInit().ID()) |
| 229 | } |
| 230 | if comp.LoopCondition().ID() != 106 { |
| 231 | t.Errorf("LoopCondition() got %v, wanted literal", comp.LoopCondition().ID()) |
| 232 | } |
| 233 | if comp.LoopStep().ID() != 107 { |
| 234 | t.Errorf("LoopStep() got %v, wanted call", comp.LoopStep().ID()) |
| 235 | } |
| 236 | if comp.Result().ID() != 112 { |
| 237 | t.Errorf("Result() got %v, wanted identifier", comp.Result().ID()) |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | func TestComprehensionNil(t *testing.T) { |
| 242 | expr := nilTestExpr(t) |
| 243 | comp := expr.AsComprehension() |
| 244 | if comp.IterRange().Kind() != ast.UnspecifiedExprKind { |
| 245 | t.Errorf("IterRange() got %v, wanted unspecified", comp.IterRange().Kind()) |
| 246 | } |
| 247 | if comp.AccuInit().Kind() != ast.UnspecifiedExprKind { |
| 248 | t.Errorf("AccuInit() got %v, wanted unspecified", comp.AccuInit().Kind()) |
| 249 | } |
| 250 | if comp.LoopCondition().Kind() != ast.UnspecifiedExprKind { |
| 251 | t.Errorf("LoopCondition() got %v, wanted unspecified", comp.LoopCondition().Kind()) |
| 252 | } |
| 253 | if comp.LoopStep().Kind() != ast.UnspecifiedExprKind { |
| 254 | t.Errorf("LoopStep() got %v, wanted unspecified", comp.LoopStep().Kind()) |
| 255 | } |
| 256 | if comp.Result().Kind() != ast.UnspecifiedExprKind { |
| 257 | t.Errorf("Result() got %v, wanted unspecified", comp.Result().Kind()) |
| 258 | } |
| 259 | expr.RenumberIDs(testIDGen(100)) |
| 260 | if expr.ID() != 101 { |
| 261 | t.Errorf("RenumberIDs() got %d, wanted 101", expr.ID()) |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | func TestIdent(t *testing.T) { |
| 266 | fac := ast.NewExprFactory() |
| 267 | id := fac.NewIdent(1, "a") |
| 268 | if id.AsIdent() != "a" { |
| 269 | t.Errorf("id.AsIdent() got %s, wanted 'a'", id.AsIdent()) |
| 270 | } |
| 271 | id.RenumberIDs(testIDGen(50)) |
| 272 | if id.ID() != 51 { |
| 273 | t.Errorf("id.ID() got %d, wanted 51", id.ID()) |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | func TestIdentNil(t *testing.T) { |
| 278 | expr := nilTestExpr(t) |
| 279 | if expr.AsIdent() != "" { |
| 280 | t.Errorf("AsIdent() got %s, wanted ''", expr.AsIdent()) |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | func TestList(t *testing.T) { |
| 285 | fac := ast.NewExprFactory() |
| 286 | expr := fac.NewList(20, []ast.Expr{fac.NewLiteral(21, types.OptionalOf(types.True))}, []int32{0}) |
| 287 | list := expr.AsList() |
| 288 | if list.Size() != 1 { |
| 289 | t.Errorf("list.Size() got %s, wanted 'a'", expr.AsIdent()) |
| 290 | } |
| 291 | if list.Elements()[0].Kind() != ast.LiteralKind { |
| 292 | t.Errorf("list.Elements()[0] got %v, wanted true", list.Elements()[0]) |
| 293 | } |
| 294 | if list.OptionalIndices()[0] != 0 { |
| 295 | t.Errorf("list.OptionalIndices()[0] got %d, wanted 0", list.OptionalIndices()[0]) |
| 296 | } |
| 297 | expr.RenumberIDs(testIDGen(50)) |
| 298 | if expr.ID() != 51 { |
| 299 | t.Errorf("expr.ID() got %d, wanted 51", expr.ID()) |
| 300 | } |
| 301 | if list.Elements()[0].ID() != 52 { |
| 302 | t.Errorf("list.Elements()[0].ID() got %d, wanted 52", list.Elements()[0].ID()) |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | func TestListNil(t *testing.T) { |
| 307 | expr := nilTestExpr(t) |
| 308 | list := expr.AsList() |
| 309 | if list.Size() != 0 { |
| 310 | t.Errorf("nil list.Size() got %d, wanted 0", list.Size()) |
| 311 | } |
| 312 | if len(list.Elements()) != 0 { |
| 313 | t.Errorf("nil list.Elements() got %d, wanted 0", list.Elements()) |
| 314 | } |
| 315 | if len(list.OptionalIndices()) != 0 { |
| 316 | t.Errorf("nil list.OptionalIndices() got %d, wanted 0", list.OptionalIndices()) |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | func TestLiteralNil(t *testing.T) { |
| 321 | expr := nilTestExpr(t) |
| 322 | if expr.AsLiteral() != nil { |
| 323 | t.Errorf("AsLiteral() got %v, wanted nil", expr.AsLiteral()) |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | func TestMap(t *testing.T) { |
| 328 | fac := ast.NewExprFactory() |
| 329 | expr := fac.NewMap(1, |
| 330 | []ast.EntryExpr{ |
| 331 | fac.NewMapEntry(2, fac.NewIdent(3, "a"), fac.NewLiteral(4, types.True), true), |
| 332 | fac.NewMapEntry(5, fac.NewIdent(6, "b"), fac.NewLiteral(7, types.False), false), |
| 333 | }) |
| 334 | m := expr.AsMap() |
| 335 | if m.Size() != 2 { |
| 336 | t.Errorf("map.Size() got %d, wanted 2", m.Size()) |
| 337 | } |
| 338 | expr.RenumberIDs(testIDGen(50)) |
| 339 | if expr.ID() != 51 { |
| 340 | t.Errorf("expr.ID() got %d, wanted 51", expr.ID()) |
| 341 | } |
| 342 | if m.Entries()[0].ID() != 52 { |
| 343 | t.Errorf("m.Entries()[0].ID() got %d, wanted 52", m.Entries()[0].ID()) |
| 344 | } |
| 345 | entry := m.Entries()[0].AsMapEntry() |
| 346 | if key := entry.Key(); key.ID() != 53 { |
| 347 | t.Errorf("key.ID() got %d, wanted 53", key.ID()) |
| 348 | } |
| 349 | if val := entry.Value(); val.ID() != 54 { |
| 350 | t.Errorf("val.ID() got %d, wanted 54", val.ID()) |
| 351 | } |
| 352 | if !entry.IsOptional() { |
| 353 | t.Error("entry.IsOptional() got false, wanted true") |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | func TestMapNil(t *testing.T) { |
| 358 | expr := nilTestExpr(t) |
| 359 | m := expr.AsMap() |
| 360 | if m.Size() != 0 { |
| 361 | t.Errorf("nil map.Size() got %d, wanted 0", m.Size()) |
| 362 | } |
| 363 | if len(m.Entries()) != 0 { |
| 364 | t.Errorf("nil map.Entries() got %d, wanted 0", m.Entries()) |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | func TestMapEntryNil(t *testing.T) { |
| 369 | fac := ast.NewExprFactory() |
| 370 | field := fac.NewStructField(1, |
| 371 | "hello", fac.NewLiteral(3, types.String("world")), false) |
| 372 | |
| 373 | // intentionally convert the struct field to the wrong type |
| 374 | entry := field.AsMapEntry() |
| 375 | if entry.Key().Kind() != ast.UnspecifiedExprKind { |
| 376 | t.Errorf("entry.Key() got %s, wanted ''", entry.Key()) |
| 377 | } |
| 378 | if entry.Value().Kind() != ast.UnspecifiedExprKind { |
| 379 | t.Errorf("entry.Value() got %v, wanted unspecified", entry.Value()) |
| 380 | } |
| 381 | if entry.IsOptional() { |
| 382 | t.Errorf("entry.IsOptional() got %v, wanted ''", entry.IsOptional()) |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | func TestSelect(t *testing.T) { |
| 387 | fac := ast.NewExprFactory() |
| 388 | expr := fac.NewSelect(2, fac.NewIdent(1, "operand"), "field") |
| 389 | sel := expr.AsSelect() |
| 390 | if sel.FieldName() != "field" { |
| 391 | t.Errorf("sel.FieldName() got %s, wanted 'field'", sel.FieldName()) |
| 392 | } |
| 393 | if sel.Operand().AsIdent() != "operand" { |
| 394 | t.Errorf("sel.Operand().AsIdent() got %s, wanted 'operand'", sel.Operand().AsIdent()) |
| 395 | } |
| 396 | expr.RenumberIDs(testIDGen(20)) |
| 397 | if sel.Operand().ID() != 22 { |
| 398 | t.Errorf("Operand().ID() got %d, wanted 22", sel.Operand().ID()) |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | func TestSelectNil(t *testing.T) { |
| 403 | expr := nilTestExpr(t) |
| 404 | sel := expr.AsSelect() |
| 405 | if sel.FieldName() != "" { |
| 406 | t.Errorf("sel.FieldName() got %s, wanted ''", sel.FieldName()) |
| 407 | } |
| 408 | if sel.IsTestOnly() { |
| 409 | t.Error("sel.IsTestOnly() got true, wanted false") |
| 410 | } |
| 411 | if sel.Operand().Kind() != ast.UnspecifiedExprKind { |
| 412 | t.Errorf("sel.Operand() got %v, wanted unspecified", sel.Operand()) |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | func TestStruct(t *testing.T) { |
| 417 | fac := ast.NewExprFactory() |
| 418 | expr := fac.NewStruct(1, |
| 419 | "custom.StructType", |
| 420 | []ast.EntryExpr{ |
| 421 | fac.NewStructField(2, "a", fac.NewLiteral(3, types.True), true), |
| 422 | fac.NewStructField(4, "b", fac.NewLiteral(5, types.False), false), |
| 423 | }) |
| 424 | s := expr.AsStruct() |
| 425 | if s.TypeName() != "custom.StructType" { |
| 426 | t.Errorf("s.TypeName() got %q, wanted 'custom.StructType'", s.TypeName()) |
| 427 | } |
| 428 | if len(s.Fields()) != 2 { |
| 429 | t.Errorf("s.Fields() got %d, wanted 2", len(s.Fields())) |
| 430 | } |
| 431 | expr.RenumberIDs(testIDGen(50)) |
| 432 | if expr.ID() != 51 { |
| 433 | t.Errorf("expr.ID() got %d, wanted 51", expr.ID()) |
| 434 | } |
| 435 | if s.Fields()[0].ID() != 52 { |
| 436 | t.Errorf("s.Fields()[0].ID() got %d, wanted 52", s.Fields()[0].ID()) |
| 437 | } |
| 438 | field := s.Fields()[0].AsStructField() |
| 439 | if val := field.Value(); val.ID() != 53 { |
| 440 | t.Errorf("val.ID() got %d, wanted 53", val.ID()) |
| 441 | } |
| 442 | if field.Name() != "a" { |
| 443 | t.Errorf("field.Name() got %s, wanted 'a'", field.Name()) |
| 444 | } |
| 445 | if !field.IsOptional() { |
| 446 | t.Error("field.IsOptional() got false, wanted true") |
| 447 | } |
| 448 | } |
| 449 | |
| 450 | func TestStructNil(t *testing.T) { |
| 451 | expr := nilTestExpr(t) |
| 452 | s := expr.AsStruct() |
| 453 | if s.TypeName() != "" { |
| 454 | t.Errorf("nil struct.TypeName() got %s, wanted ''", s.TypeName()) |
| 455 | } |
| 456 | if len(s.Fields()) != 0 { |
| 457 | t.Errorf("nil struct.Fields() got %d, wanted 0", s.Fields()) |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | func TestStructFieldNil(t *testing.T) { |
| 462 | fac := ast.NewExprFactory() |
| 463 | entry := fac.NewMapEntry(1, |
| 464 | fac.NewLiteral(2, types.String("hello")), |
| 465 | fac.NewLiteral(3, types.String("world")), |
| 466 | false) |
| 467 | |
| 468 | // intentionally convert the map entry to the wrong type |
| 469 | field := entry.AsStructField() |
| 470 | if field.Name() != "" { |
| 471 | t.Errorf("field.FieldName() got %s, wanted ''", field.Name()) |
| 472 | } |
| 473 | if field.Value().Kind() != ast.UnspecifiedExprKind { |
| 474 | t.Errorf("field.Value() got %v, wanted unspecified", field.Value()) |
| 475 | } |
| 476 | if field.IsOptional() { |
| 477 | t.Errorf("field.IsOptional() got %v, wanted ''", field.IsOptional()) |
| 478 | } |
| 479 | } |
| 480 | |
| 481 | func TestRenumberIDs(t *testing.T) { |
| 482 | fac := ast.NewExprFactory() |
| 483 | e := fac.NewUnspecifiedExpr(10) |
| 484 | e.RenumberIDs(testIDGen(100)) |
| 485 | if e.ID() != 101 { |
| 486 | t.Errorf("RenumberIDs() got %d, wanted 101", e.ID()) |
| 487 | } |
| 488 | e = fac.NewLiteral(20, types.True) |
| 489 | e.RenumberIDs(testIDGen(200)) |
| 490 | if e.ID() != 201 { |
| 491 | t.Errorf("RenumberIDs() got %d, wanted 201", e.ID()) |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | func nilTestExpr(t testing.TB) ast.Expr { |
| 496 | t.Helper() |
| 497 | fac := ast.NewExprFactory() |
| 498 | expr := fac.NewLiteral(1, types.NullValue) |
| 499 | expr.SetKindCase(nil) |
| 500 | if expr.Kind() != ast.UnspecifiedExprKind { |
| 501 | t.Fatalf("SetKindCase(nil) did not produce an unspecified expr kind: %v", expr.Kind()) |
| 502 | } |
| 503 | return expr |
| 504 | } |
| 505 | |
| 506 | func testIDGen(seed int64) ast.IDGenerator { |
| 507 | seen := map[int64]int64{} |
| 508 | return func(originalID int64) int64 { |
| 509 | if id, found := seen[originalID]; found { |
| 510 | return id |
| 511 | } |
| 512 | seed++ |
| 513 | seen[originalID] = seed |
| 514 | return seed |
| 515 | } |
| 516 | } |