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

Function TestPopulateMacroCalls

parser/parser_test.go:3054–3117  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

3052}
3053
3054func TestPopulateMacroCalls(t *testing.T) {
3055 for _, pratt := range []bool{false, true} {
3056 t.Run(fmt.Sprintf("enablePrattParser=%t", pratt), func(t *testing.T) {
3057 t.Run("DisabledByDefault", func(t *testing.T) {
3058 p, err := NewParser(Macros(AllMacros...), PopulateMacroCalls(false), EnablePrattParser(pratt))
3059 if err != nil {
3060 t.Fatalf("NewParser() failed: %v", err)
3061 }
3062 parsed, errs := p.Parse(common.NewTextSource("has(a.b)"))
3063 if len(errs.GetErrors()) > 0 {
3064 t.Fatalf("unexpected error: %s", errs.ToDisplayString())
3065 }
3066 if len(parsed.SourceInfo().MacroCalls()) != 0 {
3067 t.Errorf("expected 0 macro calls, got %d", len(parsed.SourceInfo().MacroCalls()))
3068 }
3069 })
3070
3071 t.Run("GlobalMacroCallRecorded", func(t *testing.T) {
3072 p, err := NewParser(Macros(AllMacros...), PopulateMacroCalls(true), EnablePrattParser(pratt))
3073 if err != nil {
3074 t.Fatalf("NewParser() failed: %v", err)
3075 }
3076 parsed, errs := p.Parse(common.NewTextSource("has(a.b)"))
3077 if len(errs.GetErrors()) > 0 {
3078 t.Fatalf("unexpected error: %s", errs.ToDisplayString())
3079 }
3080 macroCalls := parsed.SourceInfo().MacroCalls()
3081 if len(macroCalls) != 1 {
3082 t.Fatalf("expected 1 macro call, got %d", len(macroCalls))
3083 }
3084 })
3085
3086 t.Run("ReceiverMacroCallRecorded", func(t *testing.T) {
3087 p, err := NewParser(Macros(AllMacros...), PopulateMacroCalls(true), EnablePrattParser(pratt))
3088 if err != nil {
3089 t.Fatalf("NewParser() failed: %v", err)
3090 }
3091 parsed, errs := p.Parse(common.NewTextSource("[1, 2].exists(x, x > 0)"))
3092 if len(errs.GetErrors()) > 0 {
3093 t.Fatalf("unexpected error: %s", errs.ToDisplayString())
3094 }
3095 macroCalls := parsed.SourceInfo().MacroCalls()
3096 if len(macroCalls) != 1 {
3097 t.Fatalf("expected 1 macro call, got %d", len(macroCalls))
3098 }
3099 })
3100
3101 t.Run("NestedMacroCallsRecorded", func(t *testing.T) {
3102 p, err := NewParser(Macros(AllMacros...), PopulateMacroCalls(true), EnablePrattParser(pratt))
3103 if err != nil {
3104 t.Fatalf("NewParser() failed: %v", err)
3105 }
3106 parsed, errs := p.Parse(common.NewTextSource("[1, 2].all(x, has(x.b))"))
3107 if len(errs.GetErrors()) > 0 {
3108 t.Fatalf("unexpected error: %s", errs.ToDisplayString())
3109 }
3110 macroCalls := parsed.SourceInfo().MacroCalls()
3111 if len(macroCalls) != 2 {

Callers

nothing calls this directly

Calls 10

ParseMethod · 0.95
NewTextSourceFunction · 0.92
PopulateMacroCallsFunction · 0.85
EnablePrattParserFunction · 0.85
GetErrorsMethod · 0.80
NewParserFunction · 0.70
MacrosFunction · 0.70
ToDisplayStringMethod · 0.45
MacroCallsMethod · 0.45
SourceInfoMethod · 0.45

Tested by

no test coverage detected