| 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 | // https://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 ext |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
| 19 | "testing" |
| 20 | |
| 21 | "github.com/google/cel-go/cel" |
| 22 | "github.com/google/cel-go/common/env" |
| 23 | ) |
| 24 | |
| 25 | func TestExtensionOptionFactoryInvalidExtension(t *testing.T) { |
| 26 | invalidExtension := "invalid extension" |
| 27 | _, validExtension := ExtensionOptionFactory(invalidExtension) |
| 28 | if validExtension { |
| 29 | t.Fatalf("ExtensionOptionFactory(%s) returned valid extension for invalid input", invalidExtension) |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | func TestExtensionOptionFactoryInvalidExtensionName(t *testing.T) { |
| 34 | e := &env.Extension{Name: "invalid extension name"} |
| 35 | _, validExtension := ExtensionOptionFactory(e) |
| 36 | if validExtension { |
| 37 | t.Fatalf("ExtensionOptionFactory(%s) returned valid extension for invalid extension name", e.Name) |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | func TestExtensionOptionFactoryInvalidExtensionVersion(t *testing.T) { |
| 42 | e := &env.Extension{Name: "bindings", Version: "invalid version"} |
| 43 | opt, validExtension := ExtensionOptionFactory(e) |
| 44 | if !validExtension { |
| 45 | t.Fatalf("ExtensionOptionFactory(%s) returned invalid extension", e.Name) |
| 46 | } |
| 47 | _, err := cel.NewCustomEnv(opt) |
| 48 | if err == nil || err.Error() != fmt.Sprintf("invalid extension version: %s - %s", e.Name, e.Version) { |
| 49 | t.Fatalf("ExtensionOptionFactory(%s) returned invalid extension version", e.Name) |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | func TestExtensionOptionFactoryValidBindingsExtension(t *testing.T) { |
| 54 | e := &env.Extension{Name: "cel.lib.ext.cel.bindings", Version: "latest"} |
| 55 | opt, validExtension := ExtensionOptionFactory(e) |
| 56 | if !validExtension { |
| 57 | t.Fatalf("ExtensionOptionFactory(%s) returned invalid extension", e.Name) |
| 58 | } |
| 59 | en, err := cel.NewCustomEnv(opt) |
| 60 | if err != nil { |
| 61 | t.Fatalf("ExtensionOptionFactory(%s) returned invalid extension", e.Name) |
| 62 | } |
| 63 | cfg, err := en.ToConfig("test config") |
| 64 | if err != nil { |
| 65 | t.Fatalf("ToConfig(%s) returned error: %v", e.Name, err) |
| 66 | } |
| 67 | if len(cfg.Extensions) != 1 || cfg.Extensions[0].Name != "cel.lib.ext.cel.bindings" || cfg.Extensions[0].Version != "latest" { |
| 68 | t.Fatalf("ExtensionOptionFactory(%s) returned invalid extension", e.Name) |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | func TestExtensionOptionFactoryValidBindingsExtensionAlias(t *testing.T) { |
| 73 | e := &env.Extension{Name: "bindings", Version: "latest"} |
| 74 | opt, validExtension := ExtensionOptionFactory(e) |
| 75 | if !validExtension { |
| 76 | t.Fatalf("ExtensionOptionFactory(%s) returned invalid extension", e.Name) |
| 77 | } |
| 78 | en, err := cel.NewCustomEnv(opt) |
| 79 | if err != nil { |
| 80 | t.Fatalf("ExtensionOptionFactory(%s) returned invalid extension", e.Name) |
| 81 | } |
| 82 | cfg, err := en.ToConfig("test config") |
| 83 | if err != nil { |
| 84 | t.Fatalf("ToConfig(%s) returned error: %v", e.Name, err) |
| 85 | } |
| 86 | if len(cfg.Extensions) != 1 || cfg.Extensions[0].Name != "cel.lib.ext.cel.bindings" || cfg.Extensions[0].Version != "latest" { |
| 87 | t.Fatalf("ExtensionOptionFactory(%s) returned invalid extension", e.Name) |
| 88 | } |
| 89 | } |