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

Function TestAttributesRelativeAttrOneOf

interpreter/attributes_test.go:141–184  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

139}
140
141func TestAttributesRelativeAttrOneOf(t *testing.T) {
142 reg := newTestRegistry(t)
143 cont, err := containers.NewContainer(containers.Name("acme.ns"))
144 if err != nil {
145 t.Fatal(err)
146 }
147 attrs := NewAttributeFactory(cont, reg, reg)
148 data := map[string]any{
149 "a": map[int]any{
150 -1: []int32{2, 42},
151 },
152 "acme.b": 1,
153 }
154 vars, _ := NewActivation(data)
155
156 // The relative attribute under test is applied to a map literal:
157 // {
158 // a: {-1: [2, 42], b: 1}
159 // b: 1
160 // }
161 //
162 // The expression being evaluated is: <map-literal>.a[-1][b] -> 42
163 //
164 // However, since the test is validating what happens with maybe attributes
165 // the attribute resolution must also consider the following variations:
166 // - <map-literal>.a[-1][acme.ns.b]
167 // - <map-literal>.a[-1][acme.b]
168 //
169 // The correct behavior should yield the value of the last alternative.
170 op := NewConstValue(1, reg.NativeToValue(data))
171 attr := attrs.RelativeAttribute(1, op)
172 qualA := makeQualifier(t, attrs, nil, 2, "a")
173 qualNeg1 := makeQualifier(t, attrs, nil, 3, int64(-1))
174 attr.AddQualifier(qualA)
175 attr.AddQualifier(qualNeg1)
176 attr.AddQualifier(attrs.MaybeAttribute(4, "b"))
177 out, err := attr.Resolve(vars)
178 if err != nil {
179 t.Fatal(err)
180 }
181 if out != types.Int(42) {
182 t.Errorf("Got %v (%T), wanted 42", out, out)
183 }
184}
185
186func TestAttributesRelativeAttrConditional(t *testing.T) {
187 reg := newTestRegistry(t)

Callers

nothing calls this directly

Calls 13

RelativeAttributeMethod · 0.95
MaybeAttributeMethod · 0.95
NewContainerFunction · 0.92
NameFunction · 0.92
IntTypeAlias · 0.92
NewAttributeFactoryFunction · 0.85
NewConstValueFunction · 0.85
makeQualifierFunction · 0.85
newTestRegistryFunction · 0.70
NewActivationFunction · 0.70
NativeToValueMethod · 0.65
AddQualifierMethod · 0.65

Tested by

no test coverage detected