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

Function TestExprHelperCopy

parser/helper_test.go:26–65  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

24)
25
26func TestExprHelperCopy(t *testing.T) {
27 src := common.NewStringSource(
28 `noop([1, 2, 3].map(i, Msg{first: 1 + 2, second: a.b, third: {true: true}}))`,
29 "")
30 p, err := NewParser(
31 PopulateMacroCalls(true),
32 Macros(
33 MapMacro,
34 NewGlobalMacro("noop", 1,
35 func(eh ExprHelper, target ast.Expr, args []ast.Expr) (ast.Expr, *common.Error) {
36 return eh.Copy(args[0]), nil
37 },
38 ),
39 ),
40 )
41 if err != nil {
42 t.Fatalf("NewParser() failed: %v", err)
43 }
44 parsed, errs := p.Parse(src)
45 if errs != nil && len(errs.GetErrors()) != 0 {
46 t.Fatalf("p.Parse(%v) failed: %v", src, errs.ToDisplayString())
47 }
48 // id generation is consistent between runs, and in this case '27' refers to the
49 // macro expression that's the sole argument to the noop() macro.
50 macroTarget, found := parsed.SourceInfo().GetMacroCall(27)
51 if !found {
52 t.Fatal("GetMacroCall(27) failed to find call")
53 }
54 macroExpr, err := ast.ExprToProto(macroTarget)
55 if err != nil {
56 t.Fatalf("ast.ExprToProto() failed: %v", err)
57 }
58 protoExpr, err := ast.ExprToProto(parsed.Expr())
59 if err != nil {
60 t.Fatalf("ast.ExprToProto() failed: %v", err)
61 }
62 if proto.Equal(protoExpr, macroExpr) {
63 t.Errorf("Copy() failed to provide a unique ids: %v", macroExpr)
64 }
65}

Callers

nothing calls this directly

Calls 14

ParseMethod · 0.95
NewStringSourceFunction · 0.92
ExprToProtoFunction · 0.92
PopulateMacroCallsFunction · 0.85
GetErrorsMethod · 0.80
GetMacroCallMethod · 0.80
NewParserFunction · 0.70
MacrosFunction · 0.70
NewGlobalMacroFunction · 0.70
CopyMethod · 0.65
ExprMethod · 0.65
EqualMethod · 0.65

Tested by

no test coverage detected