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

Function TestCompose_Unnest

policy/composer_test.go:55–96  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

53}
54
55func TestCompose_Unnest(t *testing.T) {
56 policyYAML := `name: unnest
57rule:
58 match:
59 - condition: "2 == 1"
60 output: "'hi'"
61 - output: "'hello'"
62`
63 src := StringSource(policyYAML, "unnest.yaml")
64 parser, err := NewParser()
65 if err != nil {
66 t.Fatalf("NewParser() failed: %v", err)
67 }
68 policy, iss := parser.Parse(src)
69 if iss.Err() != nil {
70 t.Fatalf("parser.Parse() failed: %v", iss.Err())
71 }
72
73 env, err := cel.NewEnv(cel.OptionalTypes(), ext.Bindings())
74 if err != nil {
75 t.Fatalf("cel.NewEnv() failed: %v", err)
76 }
77 compiledRule, iss := CompileRule(env, policy)
78 if iss.Err() != nil {
79 t.Fatalf("CompileRule() failed: %v", iss.Err())
80 }
81
82 composer, err := NewRuleComposer(env, ExpressionUnnestHeight(1))
83 if err != nil {
84 t.Fatalf("NewRuleComposer() failed: %v", err)
85 }
86 compAST, iss := composer.Compose(compiledRule)
87 if iss.Err() != nil {
88 t.Fatalf("composer.Compose() failed: %v", iss.Err())
89 }
90
91 verifySourceInfoTransfer(t, compiledRule, compAST)
92 if t.Failed() {
93 t.Logf("composed AST: %s", debug.ToDebugStringWithIDs(compAST.NativeRep().Expr()))
94 t.Logf("SourceInfo: %v", compAST.NativeRep().SourceInfo().OffsetRanges())
95 }
96}
97
98// verifySourceInfoTransfer checks that each offset range in the compiledRule has a corresponding node in composed
99func verifySourceInfoTransfer(t *testing.T, compiledRule *CompiledRule, composed *cel.Ast) {

Callers

nothing calls this directly

Calls 15

ParseMethod · 0.95
ComposeMethod · 0.95
NewEnvFunction · 0.92
OptionalTypesFunction · 0.92
BindingsFunction · 0.92
ToDebugStringWithIDsFunction · 0.92
StringSourceFunction · 0.85
CompileRuleFunction · 0.85
NewRuleComposerFunction · 0.85
ExpressionUnnestHeightFunction · 0.85
verifySourceInfoTransferFunction · 0.85
ErrMethod · 0.80

Tested by

no test coverage detected