| 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 cel |
| 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 TestGlobalVarArgMacro(t *testing.T) { |
| 25 | noopExpander := func(meh MacroExprFactory, target ast.Expr, args []ast.Expr) (ast.Expr, *common.Error) { |
| 26 | return nil, nil |
| 27 | } |
| 28 | varArgMacro := GlobalVarArgMacro("varargs", noopExpander) |
| 29 | if varArgMacro.ArgCount() != 0 { |
| 30 | t.Errorf("ArgCount() got %d, wanted 0", varArgMacro.ArgCount()) |
| 31 | } |
| 32 | if varArgMacro.Function() != "varargs" { |
| 33 | t.Errorf("Function() got %q, wanted 'varargs'", varArgMacro.Function()) |
| 34 | } |
| 35 | if varArgMacro.MacroKey() != "varargs:*:false" { |
| 36 | t.Errorf("MacroKey() got %q, wanted 'varargs:*:false'", varArgMacro.MacroKey()) |
| 37 | } |
| 38 | if varArgMacro.IsReceiverStyle() { |
| 39 | t.Errorf("IsReceiverStyle() got %t, wanted false", varArgMacro.IsReceiverStyle()) |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | func TestReceiverVarArgMacro(t *testing.T) { |
| 44 | noopExpander := func(meh MacroExprFactory, target ast.Expr, args []ast.Expr) (ast.Expr, *common.Error) { |
| 45 | return nil, nil |
| 46 | } |
| 47 | varArgMacro := ReceiverVarArgMacro("varargs", noopExpander) |
| 48 | if varArgMacro.ArgCount() != 0 { |
| 49 | t.Errorf("ArgCount() got %d, wanted 0", varArgMacro.ArgCount()) |
| 50 | } |
| 51 | if varArgMacro.Function() != "varargs" { |
| 52 | t.Errorf("Function() got %q, wanted 'varargs'", varArgMacro.Function()) |
| 53 | } |
| 54 | if varArgMacro.MacroKey() != "varargs:*:true" { |
| 55 | t.Errorf("MacroKey() got %q, wanted 'varargs:*:true'", varArgMacro.MacroKey()) |
| 56 | } |
| 57 | if !varArgMacro.IsReceiverStyle() { |
| 58 | t.Errorf("IsReceiverStyle() got %t, wanted true", varArgMacro.IsReceiverStyle()) |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | func TestDocumentation(t *testing.T) { |
| 63 | noopExpander := func(meh MacroExprFactory, target ast.Expr, args []ast.Expr) (ast.Expr, *common.Error) { |
| 64 | return nil, nil |
| 65 | } |
| 66 | varArgMacro := ReceiverVarArgMacro("varargs", noopExpander, |
| 67 | MacroDocs(`convert variable argument lists to a list literal`), |
| 68 | MacroExamples(`fn.varargs(1,2,3) // fn([1, 2, 3])`)) |
| 69 | doc, ok := varArgMacro.(common.Documentor) |
| 70 | if !ok { |
| 71 | t.Fatal("macro does not implement Documenter interface") |
| 72 | } |
| 73 | d := doc.Documentation() |
| 74 | if d.Kind != common.DocMacro { |
| 75 | t.Errorf("Documentation() got kind %v, wanted DocMacro", d.Kind) |
| 76 | } |
| 77 | if d.Name != varArgMacro.Function() { |
| 78 | t.Errorf("Documentation() got name %q, wanted %q", d.Name, varArgMacro.Function()) |
| 79 | } |
| 80 | if d.Description != `convert variable argument lists to a list literal` { |
| 81 | t.Errorf("Documentation() got description %q, wanted %q", d.Description, `convert variable argument lists to a list literal`) |
| 82 | } |
| 83 | if len(d.Children) != 1 { |
| 84 | t.Fatalf("macro documentation children got: %d", len(d.Children)) |
| 85 | } |
| 86 | if d.Children[0].Description != `fn.varargs(1,2,3) // fn([1, 2, 3])` { |
| 87 | t.Errorf("macro documentation Children[0] got %s, wanted %s", d.Children[0].Description, |
| 88 | `fn.varargs(1,2,3) // fn([1, 2, 3])`) |
| 89 | } |
| 90 | } |