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

Function TestNetwork_CompileErrors

ext/network_test.go:502–567  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

500}
501
502func TestNetwork_CompileErrors(t *testing.T) {
503 tests := []struct {
504 name string
505 expr string
506 errContains string
507 }{
508 {
509 name: "ip constructor invalid literal",
510 expr: "ip('999.999.999.999')",
511 errContains: "invalid ip argument",
512 },
513 {
514 name: "cidr constructor invalid literal",
515 expr: "cidr('1.2.3.4')",
516 errContains: "invalid cidr argument",
517 },
518 {
519 name: "cidr constructor invalid mask literal",
520 expr: "cidr('10.0.0.0/999')",
521 errContains: "invalid cidr argument",
522 },
523 {
524 name: "ip constructor valid literal",
525 expr: "ip('127.0.0.1')",
526 errContains: "",
527 },
528 {
529 name: "cidr constructor valid literal",
530 expr: "cidr('10.0.0.0/8')",
531 errContains: "",
532 },
533 }
534
535 env, err := cel.NewEnv(Network())
536 if err != nil {
537 t.Fatalf("cel.NewEnv(Network()) failed: %v", err)
538 }
539
540 for _, tst := range tests {
541 t.Run(tst.name, func(t *testing.T) {
542 _, iss := env.Compile(tst.expr)
543 if tst.errContains != "" {
544 if iss.Err() == nil {
545 t.Errorf("Expected compile error for %q, got nil", tst.expr)
546 return
547 }
548 gotErr := iss.Err().Error()
549 // Simple string contains check
550 found := false
551 for i := 0; i < len(gotErr)-len(tst.errContains)+1; i++ {
552 if gotErr[i:i+len(tst.errContains)] == tst.errContains {
553 found = true
554 break
555 }
556 }
557 if !found {
558 t.Errorf("Expected compile error containing %q, got %q", tst.errContains, gotErr)
559 }

Callers

nothing calls this directly

Calls 5

CompileMethod · 0.95
NewEnvFunction · 0.92
NetworkFunction · 0.85
ErrMethod · 0.80
ErrorMethod · 0.45

Tested by

no test coverage detected