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

Function TestSourceInfoPositions

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

Source from the content-addressed store, hash-verified

2963}
2964
2965func TestSourceInfoPositions(t *testing.T) {
2966 for _, pratt := range []bool{false, true} {
2967 t.Run(fmt.Sprintf("enablePrattParser=%t", pratt), func(t *testing.T) {
2968 t.Run("ASCII", func(t *testing.T) {
2969 src := common.NewTextSource("a + b")
2970 p, err := NewParser(EnablePrattParser(pratt))
2971 if err != nil {
2972 t.Fatalf("NewParser() failed: %v", err)
2973 }
2974 parsed, errs := p.Parse(src)
2975 if len(errs.GetErrors()) > 0 {
2976 t.Fatalf("Parse() failed: %s", errs.ToDisplayString())
2977 }
2978 sourceInfo := parsed.SourceInfo()
2979 root := parsed.Expr()
2980 if sourceInfo.GetStartLocation(root.ID()).Column() != 2 {
2981 t.Errorf("expected root column 2, got %d", sourceInfo.GetStartLocation(root.ID()).Column())
2982 }
2983 args := root.AsCall().Args()
2984 if len(args) != 2 {
2985 t.Fatalf("expected 2 args, got %d", len(args))
2986 }
2987 if sourceInfo.GetStartLocation(args[0].ID()).Column() != 0 {
2988 t.Errorf("expected arg[0] column 0, got %d", sourceInfo.GetStartLocation(args[0].ID()).Column())
2989 }
2990 if sourceInfo.GetStartLocation(args[1].ID()).Column() != 4 {
2991 t.Errorf("expected arg[1] column 4, got %d", sourceInfo.GetStartLocation(args[1].ID()).Column())
2992 }
2993 })
2994
2995 t.Run("MixedUnicodeMultiByteAndMultiLine", func(t *testing.T) {
2996 // Mix of 1-byte ASCII (a, b, +), 2-byte Unicode ("ñ"), 3-byte Unicode ("❤"), and 4-byte Unicode ("🚀")
2997 expr := "a + \"ñ\" +\n\"🚀\" + \"❤\" + b"
2998 src := common.NewTextSource(expr)
2999 p, err := NewParser(EnablePrattParser(pratt))
3000 if err != nil {
3001 t.Fatalf("NewParser() failed: %v", err)
3002 }
3003 parsed, errs := p.Parse(src)
3004 if len(errs.GetErrors()) > 0 {
3005 t.Fatalf("Parse() failed: %s", errs.ToDisplayString())
3006 }
3007 sinfo := parsed.SourceInfo()
3008
3009 // AST hierarchy:
3010 // expr4: [expr3] + [b] (line 2, col 10)
3011 // expr3: [expr2] + ["❤"] (line 2, col 4)
3012 // expr2: [expr1] + ["🚀"] (line 1, col 8)
3013 // expr1: [a] + ["ñ"] (line 1, col 2)
3014 expr4 := parsed.Expr()
3015 call4 := expr4.AsCall()
3016 bExpr := call4.Args()[1]
3017
3018 expr3 := call4.Args()[0]
3019 call3 := expr3.AsCall()
3020 heartExpr := call3.Args()[1]
3021
3022 expr2 := call3.Args()[0]

Callers

nothing calls this directly

Calls 14

ParseMethod · 0.95
NewTextSourceFunction · 0.92
EnablePrattParserFunction · 0.85
GetErrorsMethod · 0.80
GetStartLocationMethod · 0.80
NewParserFunction · 0.70
ExprMethod · 0.65
ColumnMethod · 0.65
IDMethod · 0.65
ArgsMethod · 0.65
AsCallMethod · 0.65
LineMethod · 0.65

Tested by

no test coverage detected