Debian Codegraph @

debian/missing-sources/github.com/google/cel-go/interpreter/activation_test.go

raw symbols SCIP
1 // Copyright 2018 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 interpreter
16
17 import (
18 "testing"
19 "time"
20
21 "github.com/google/cel-go/common/types"
22 "github.com/google/cel-go/common/types/ref"
23 )
24
25 func TestActivation(t *testing.T) {
26 act, err := NewActivation(map[string]any{"a": types.True})
27 if err != nil {
28 t.Fatalf("Got err: %v, wanted activation", err)
29 }
30 _, err = NewActivation(act)
31 if err != nil {
32 t.Fatalf("Got err: %v, wanted activation", err)
33 }
34 act3, err := NewActivation("")
35 if err == nil {
36 t.Fatalf("Got %v, wanted err", act3)
37 }
38 }
39
40 func TestActivation_Resolve(t *testing.T) {
41 activation, _ := NewActivation(map[string]any{"a": types.True})
42 if val, found := activation.ResolveName("a"); !found || val != types.True {
43 t.Error("Activation failed to resolve 'a'")
44 }
45 }
46
47 func TestActivation_ResolveLazy(t *testing.T) {
48 var v ref.Val
49 now := func() ref.Val {
50 if v == nil {
51 v = types.DefaultTypeAdapter.NativeToValue(time.Now().Unix())
52 }
53 return v
54 }
55 a, _ := NewActivation(map[string]any{
56 "now": now,
57 })
58 first, _ := a.ResolveName("now")
59 second, _ := a.ResolveName("now")
60 if first != second {
61 t.Errorf("Got different second, "+
62 "expected same as first: 1:%v 2:%v", first, second)
63 }
64 }
65
66 func TestActivation_ResolveLazyAny(t *testing.T) {
67 var v any
68 now := func() any {
69 if v == nil {
70 v = time.Now().Unix()
71 }
72 return v
73 }
74 a, _ := NewActivation(map[string]any{
75 "now": now,
76 })
77 first, _ := a.ResolveName("now")
78 second, _ := a.ResolveName("now")
79 if first != second {
80 t.Errorf("Got different second, "+
81 "expected same as first: 1:%v 2:%v", first, second)
82 }
83 }
84
85 func TestHierarchicalActivation(t *testing.T) {
86 // compose a parent with more properties than the child
87 parent, _ := NewActivation(map[string]any{
88 "a": types.String("world"),
89 "b": types.Int(-42),
90 })
91 // compose the child such that it shadows the parent
92 child, _ := NewActivation(map[string]any{
93 "a": types.True,
94 "c": types.String("universe"),
95 })
96 combined := NewHierarchicalActivation(parent, child)
97
98 // Resolve the shadowed child value.
99 if val, found := combined.ResolveName("a"); !found || val != types.True {
100 t.Error("Activation failed to resolve shadow value of 'a'")
101 }
102 // Resolve the parent only value.
103 if val, found := combined.ResolveName("b"); !found || val.(types.Int) != -42 {
104 t.Error("Activation failed to resolve parent value of 'b'")
105 }
106 // Resolve the child only value.
107 if val, found := combined.ResolveName("c"); !found || val.(types.String) != "universe" {
108 t.Error("Activation failed to resolve child value of 'c'")
109 }
110 }
111
112 func TestAsPartialActivation(t *testing.T) {
113 // compose a parent with more properties than the child
114 parent, _ := NewPartialActivation(map[string]any{
115 "a": types.String("world"),
116 "b": types.Int(-42),
117 }, NewAttributePattern("c"))
118 // compose the child such that it shadows the parent
119 child, _ := NewActivation(map[string]any{
120 "d": types.String("universe"),
121 })
122 combined := NewHierarchicalActivation(parent, child)
123
124 // Resolve the shadowed child value.
125 if part, found := AsPartialActivation(combined); found {
126 if part != parent {
127 t.Errorf("AsPartialActivation() got %v, wanted %v", part, parent)
128 }
129 } else {
130 t.Error("AsPartialActivation() failed, did not find parent partial activation")
131 }
132 }