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

Function TestRegexOptimizer

cel/cel_test.go:2705–2780  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

2703}
2704
2705func TestRegexOptimizer(t *testing.T) {
2706 stringTests := []struct {
2707 expr string
2708 optimizeRegex bool
2709 progErr string
2710 err string
2711 parseOnly bool
2712 }{
2713 {expr: `"123 abc 456".matches('[0-9]*')`},
2714 {expr: `"123 abc 456".matches('[0-9]' + '*')`},
2715 {expr: `"123 abc 456".matches('[0-9]*')`, optimizeRegex: true},
2716 {expr: `"123 abc 456".matches('[0-9]' + '*')`, optimizeRegex: true},
2717 {
2718 // Verify that a regex compilation error for an optimized regex is
2719 // reported at program creation time.
2720 expr: `"123 abc 456".matches(')[0-9]*')`, optimizeRegex: true,
2721 progErr: "error parsing regexp: unexpected ): `)[0-9]*`",
2722 },
2723 {
2724 expr: `"123 abc 456".matches(')[0-9]*')`,
2725 err: "error parsing regexp: unexpected ): `)[0-9]*`",
2726 },
2727 }
2728
2729 env := testEnv(t)
2730 for i, tst := range stringTests {
2731 tc := tst
2732 t.Run(fmt.Sprintf("[%d]", i), func(tt *testing.T) {
2733 var asts []*Ast
2734 pAst, iss := env.Parse(tc.expr)
2735 if iss.Err() != nil {
2736 tt.Fatal(iss.Err())
2737 }
2738 asts = append(asts, pAst)
2739 if !tc.parseOnly {
2740 cAst, iss := env.Check(pAst)
2741 if iss.Err() != nil {
2742 tt.Fatal(iss.Err())
2743 }
2744 asts = append(asts, cAst)
2745 }
2746 for _, ast := range asts {
2747 var opts []ProgramOption
2748 if tc.optimizeRegex {
2749 opts = append(opts, EvalOptions(OptOptimize))
2750 }
2751 prg, progErr := env.Program(ast, opts...)
2752 if tc.progErr != "" {
2753 if progErr == nil {
2754 tt.Fatalf("wanted error %s for expr: %s", tc.progErr, tc.expr)
2755 }
2756 if tc.progErr != progErr.Error() {
2757 tt.Errorf("got error %v, wanted error %s for expr: %s", progErr, tc.progErr, tc.expr)
2758 }
2759 continue
2760 } else if progErr != nil {
2761 tt.Fatal(progErr)
2762 }

Callers

nothing calls this directly

Calls 10

EvalOptionsFunction · 0.85
NoVarsFunction · 0.85
ErrMethod · 0.80
CheckMethod · 0.80
ProgramMethod · 0.80
testEnvFunction · 0.70
ParseMethod · 0.65
EvalMethod · 0.65
ValueMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected