MCPcopy Create free account
hub / github.com/cel-expr/cel-go / TestStruct

Function TestStruct

common/ast/expr_test.go:416–448  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

414}
415
416func 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
450func TestStructNil(t *testing.T) {
451 expr := nilTestExpr(t)

Callers

nothing calls this directly

Calls 15

NewStructMethod · 0.95
NewStructFieldMethod · 0.95
NewLiteralMethod · 0.95
NewExprFactoryFunction · 0.92
testIDGenFunction · 0.85
AsStructMethod · 0.65
TypeNameMethod · 0.65
FieldsMethod · 0.65
RenumberIDsMethod · 0.65
IDMethod · 0.65
AsStructFieldMethod · 0.65
ValueMethod · 0.65

Tested by

no test coverage detected