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

Function TestAttributesNarrowMapKeyQualifier

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

Source from the content-addressed store, hash-verified

494}
495
496func TestAttributesNarrowMapKeyQualifier(t *testing.T) {
497 reg := newTestRegistry(t)
498 attrs := NewAttributeFactory(containers.DefaultContainer, reg, reg)
499 vars, err := NewActivation(map[string]any{
500 "i32": map[int32]any{0: "zero"},
501 "u32": map[uint32]any{0: "zero"},
502 })
503 if err != nil {
504 t.Fatalf("NewActivation() failed: %v", err)
505 }
506 // An index outside the key type's range must not be truncated into a
507 // matching key. int32(1<<32) and uint32(1<<32) both wrap to 0.
508 tests := []struct {
509 varName string
510 qual any
511 out any
512 err error
513 }{
514 {varName: "i32", qual: int64(1) << 32, err: errors.New("no such key: 4294967296")},
515 {varName: "u32", qual: uint64(1) << 32, err: errors.New("no such key: 4294967296")},
516 {varName: "i32", qual: int64(0), out: "zero"},
517 {varName: "u32", qual: uint64(0), out: "zero"},
518 }
519 for i, tst := range tests {
520 tc := tst
521 t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
522 attr := attrs.AbsoluteAttribute(1, tc.varName)
523 attr.AddQualifier(makeQualifier(t, attrs, nil, 2, tc.qual))
524 out, err := attr.Resolve(vars)
525 if err != nil {
526 if tc.err == nil {
527 t.Fatalf("attr.Resolve() failed: %v", err)
528 }
529 if tc.err.Error() != err.Error() {
530 t.Fatalf("attr.Resolve() errored with %v, wanted error %v", err, tc.err)
531 }
532 return
533 }
534 if tc.err != nil {
535 t.Fatalf("attr.Resolve() got %v, wanted error %v", out, tc.err)
536 }
537 if out != tc.out {
538 t.Errorf("attr.Resolve() got %v, wanted %v", out, tc.out)
539 }
540 })
541 }
542}
543
544func TestAttributesOptional(t *testing.T) {
545 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