| 1 | // Copyright 2025 Google LLC |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package parser |
| 16 | |
| 17 | import ( |
| 18 | "testing" |
| 19 | |
| 20 | "github.com/google/cel-go/common" |
| 21 | "github.com/google/cel-go/common/ast" |
| 22 | ) |
| 23 | |
| 24 | func TestReceiverVarArgMacro(t *testing.T) { |
| 25 | noopExpander := func(meh ExprHelper, target ast.Expr, args []ast.Expr) (ast.Expr, *common.Error) { |
| 26 | return nil, nil |
| 27 | } |
| 28 | varArgMacro := NewReceiverVarArgMacro("varargs", noopExpander, |
| 29 | MacroDocs(`convert variable argument lists to a list literal`), |
| 30 | MacroExamples(`varargs(1,2,3) // [1, 2, 3]`)) |
| 31 | if varArgMacro.ArgCount() != 0 { |
| 32 | t.Errorf("ArgCount() got %d, wanted 0", varArgMacro.ArgCount()) |
| 33 | } |
| 34 | if varArgMacro.Function() != "varargs" { |
| 35 | t.Errorf("Function() got %q, wanted 'varargs'", varArgMacro.Function()) |
| 36 | } |
| 37 | if varArgMacro.MacroKey() != "varargs:*:true" { |
| 38 | t.Errorf("MacroKey() got %q, wanted 'varargs:*:true'", varArgMacro.MacroKey()) |
| 39 | } |
| 40 | if !varArgMacro.IsReceiverStyle() { |
| 41 | t.Errorf("IsReceiverStyle() got %t, wanted true", varArgMacro.IsReceiverStyle()) |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | func TestDocumentation(t *testing.T) { |
| 46 | noopExpander := func(meh ExprHelper, target ast.Expr, args []ast.Expr) (ast.Expr, *common.Error) { |
| 47 | return nil, nil |
| 48 | } |
| 49 | varArgMacro := NewReceiverVarArgMacro("varargs", noopExpander, |
| 50 | MacroDocs(`convert variable argument lists to a list literal`), |
| 51 | MacroExamples(`varargs(1,2,3) // [1, 2, 3]`)) |
| 52 | doc, ok := varArgMacro.(common.Documentor) |
| 53 | if !ok { |
| 54 | t.Fatal("macro does not implement Documenter interface") |
| 55 | } |
| 56 | d := doc.Documentation() |
| 57 | if d.Kind != common.DocMacro { |
| 58 | t.Errorf("Documentation() got kind %v, wanted DocMacro", d.Kind) |
| 59 | } |
| 60 | if d.Name != varArgMacro.Function() { |
| 61 | t.Errorf("Documentation() got name %q, wanted %q", d.Name, varArgMacro.Function()) |
| 62 | } |
| 63 | if d.Description != `convert variable argument lists to a list literal` { |
| 64 | t.Errorf("Documentation() got description %q, wanted %q", d.Description, `convert variable argument lists to a list literal`) |
| 65 | } |
| 66 | if len(d.Children) != 1 { |
| 67 | t.Fatalf("macro documentation children got: %d", len(d.Children)) |
| 68 | } |
| 69 | if d.Children[0].Description != `varargs(1,2,3) // [1, 2, 3]` { |
| 70 | t.Errorf("macro documentation Children[0] got %s, wanted %s", d.Children[0].Description, `varargs(1,2,3) // [1, 2, 3]`) |
| 71 | } |
| 72 | } |