| 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 test |
| 16 |
|
| 17 |
import ( |
| 18 |
"os" |
| 19 |
"testing" |
| 20 |
|
| 21 |
"github.com/google/cel-go/cel" |
| 22 |
"github.com/google/cel-go/common/types" |
| 23 |
"github.com/google/cel-go/common/types/ref" |
| 24 |
"github.com/google/cel-go/tools/celtest" |
| 25 |
) |
| 26 |
|
| 27 |
// TestCEL triggers the celtest test runner with a list of custom options which are used to set up |
| 28 |
// the compiler tool and test runner. |
| 29 |
func TestCEL(t *testing.T) { |
| 30 |
opts := []any{locationCodeEnvOption()} |
| 31 |
testResourcesDir := os.Getenv("RUNFILES_DIR") |
| 32 |
testRunnerOpt := celtest.TestRunnerOptionsFromFlags(testResourcesDir, nil, opts...) |
| 33 |
celtest.TriggerTests(t, testRunnerOpt) |
| 34 |
} |
| 35 |
|
| 36 |
func locationCodeEnvOption() cel.EnvOption { |
| 37 |
return cel.Function("locationCode", |
| 38 |
cel.Overload("locationCode_string", []*cel.Type{cel.StringType}, cel.StringType, |
| 39 |
cel.UnaryBinding(locationCode))) |
| 40 |
} |
| 41 |
|
| 42 |
func locationCode(ip ref.Val) ref.Val { |
| 43 |
switch ip.(types.String) { |
| 44 |
case "10.0.0.1": |
| 45 |
return types.String("us") |
| 46 |
case "10.0.0.2": |
| 47 |
return types.String("de") |
| 48 |
default: |
| 49 |
return types.String("ir") |
| 50 |
} |
| 51 |
} |