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

Function TestAttributesNarrowMapKeyQualifier

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

Source from the content-addressed store, hash-verified

397}
398
399func TestAttributesNarrowMapKeyQualifier(t *testing.T) {
400 reg := newTestRegistry(t)
401 attrs := NewAttributeFactory(containers.DefaultContainer, reg, reg)
402 vars, err := NewActivation(map[string]any{
403 "i32": map[int32]any{0: "zero"},
404 "u32": map[uint32]any{0: "zero"},
405 })
406 if err != nil {
407 t.Fatalf("NewActivation() failed: %v", err)
408 }
409 // An index outside the key type's range must not be truncated into a
410 // matching key. int32(1<<32) and uint32(1<<32) both wrap to 0.
411 tests := []struct {
412 varName string
413 qual any
414 out any
415 err error
416 }{
417 {varName: "i32", qual: int64(1) << 32, err: errors.New("no such key: 4294967296")},
418 {varName: "u32", qual: uint64(1) << 32, err: errors.New("no such key: 4294967296")},
419 {varName: "i32", qual: int64(0), out: "zero"},
420 {varName: "u32", qual: uint64(0), out: "zero"},
421 }
422 for i, tst := range tests {
423 tc := tst
424 t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
425 attr := attrs.AbsoluteAttribute(1, tc.varName)
426 attr.AddQualifier(makeQualifier(t, attrs, nil, 2, tc.qual))
427 out, err := attr.Resolve(vars)
428 if err != nil {
429 if tc.err == nil {
430 t.Fatalf("attr.Resolve() failed: %v", err)
431 }
432 if tc.err.Error() != err.Error() {
433 t.Fatalf("attr.Resolve() errored with %v, wanted error %v", err, tc.err)
434 }
435 return
436 }
437 if tc.err != nil {
438 t.Fatalf("attr.Resolve() got %v, wanted error %v", out, tc.err)
439 }
440 if out != tc.out {
441 t.Errorf("attr.Resolve() got %v, wanted %v", out, tc.out)
442 }
443 })
444 }
445}
446
447func TestAttributesOptional(t *testing.T) {
448 reg := newTestRegistry(t, types.ProtoTypeDefs(&proto3pb.TestAllTypes{}))

Callers

nothing calls this directly

Calls 9

AbsoluteAttributeMethod · 0.95
NewAttributeFactoryFunction · 0.85
makeQualifierFunction · 0.85
NewMethod · 0.80
newTestRegistryFunction · 0.70
NewActivationFunction · 0.70
AddQualifierMethod · 0.65
ResolveMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected