| 1 | // Copyright 2018 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 checker |
| 16 | |
| 17 | import ( |
| 18 | "strings" |
| 19 | "testing" |
| 20 | |
| 21 | "github.com/google/cel-go/common" |
| 22 | "github.com/google/cel-go/common/containers" |
| 23 | "github.com/google/cel-go/common/decls" |
| 24 | "github.com/google/cel-go/common/stdlib" |
| 25 | "github.com/google/cel-go/common/types" |
| 26 | "github.com/google/cel-go/parser" |
| 27 | ) |
| 28 | |
| 29 | func TestOverlappingMacro(t *testing.T) { |
| 30 | env := newStdEnv(t) |
| 31 | hasFn, err := decls.NewFunction("has", |
| 32 | decls.Overload("has", []*types.Type{types.StringType}, types.BoolType)) |
| 33 | if err != nil { |
| 34 | t.Fatalf("decls.NewFunction() failed: %v", err) |
| 35 | } |
| 36 | err = env.AddFunctions(hasFn) |
| 37 | if err == nil { |
| 38 | t.Error("Got nil, wanted error") |
| 39 | } else if !strings.Contains(err.Error(), "overlapping macro") { |
| 40 | t.Errorf("Got %v, wanted overlapping macro error", err) |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | func TestCopyDeclarations(t *testing.T) { |
| 45 | src := common.NewTextSource(`1 + 2 != 3 - 4`) |
| 46 | parsedAst, errors := parser.Parse(src) |
| 47 | if len(errors.GetErrors()) > 0 { |
| 48 | t.Fatalf("Unexpected parse errors: %v", errors.ToDisplayString()) |
| 49 | } |
| 50 | |
| 51 | env := newStdEnv(t) |
| 52 | _, errors = Check(parsedAst, src, env) |
| 53 | if len(errors.GetErrors()) > 0 { |
| 54 | t.Fatalf("Check(parsedAst, src, env): %v", errors.ToDisplayString()) |
| 55 | } |
| 56 | |
| 57 | copy, err := NewEnv(containers.DefaultContainer, newTestRegistry(t), ValidatedDeclarations(env)) |
| 58 | if err != nil { |
| 59 | t.Fatalf("NewEnv(container, registry, CopyDeclarations(env)) failed %v: ", err) |
| 60 | } |
| 61 | _, errors = Check(parsedAst, src, copy) |
| 62 | if len(errors.GetErrors()) > 0 { |
| 63 | t.Fatalf("Check(parsedAst, src, copy): %v", errors.ToDisplayString()) |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | func BenchmarkNewStdEnv(b *testing.B) { |
| 68 | for i := 0; i < b.N; i++ { |
| 69 | env, err := NewEnv(containers.DefaultContainer, newTestRegistry(b)) |
| 70 | if err != nil { |
| 71 | b.Fatalf("NewEnv() failed: %v", err) |
| 72 | } |
| 73 | err = env.AddFunctions(stdlib.Functions()...) |
| 74 | if err != nil { |
| 75 | b.Fatalf("env.AddFunctions(stdlib.Functions()...) failed: %v", err) |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | func BenchmarkCopyDeclarations(b *testing.B) { |
| 81 | env, err := NewEnv(containers.DefaultContainer, newTestRegistry(b)) |
| 82 | if err != nil { |
| 83 | b.Fatalf("NewEnv() failed: %v", err) |
| 84 | } |
| 85 | err = env.AddFunctions(stdlib.Functions()...) |
| 86 | if err != nil { |
| 87 | b.Fatalf("env.AddFunctions(stdlib.Functions()...) failed: %v", err) |
| 88 | } |
| 89 | for i := 0; i < b.N; i++ { |
| 90 | env.validatedDeclarations().Copy() |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | func newStdEnv(t *testing.T) *Env { |
| 95 | t.Helper() |
| 96 | env, err := NewEnv(containers.DefaultContainer, newTestRegistry(t)) |
| 97 | if err != nil { |
| 98 | t.Fatalf("NewEnv() failed: %v", err) |
| 99 | } |
| 100 | err = env.AddFunctions(stdlib.Functions()...) |
| 101 | if err != nil { |
| 102 | t.Fatalf("env.Add(stdlib.Functions()...) failed: %v", err) |
| 103 | } |
| 104 | return env |
| 105 | } |
| 106 | |
| 107 | func newTestRegistry(t testing.TB) *types.Registry { |
| 108 | t.Helper() |
| 109 | reg, err := types.NewRegistry() |
| 110 | if err != nil { |
| 111 | t.Fatalf("types.NewRegistry() failed: %v", err) |
| 112 | } |
| 113 | return reg |
| 114 | } |