| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 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 |
|
| 28 |
|
| 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 |
} |