| 58 | ) |
| 59 | |
| 60 | func Test_ExampleWithBuiltins(t *testing.T) { |
| 61 | // Variables used within this expression environment. |
| 62 | env, err := NewEnv( |
| 63 | Variable("i", StringType), |
| 64 | Variable("you", StringType), |
| 65 | ) |
| 66 | if err != nil { |
| 67 | t.Fatalf("environment creation error: %s\n", err) |
| 68 | } |
| 69 | |
| 70 | // Compile the expression. |
| 71 | ast, iss := env.Compile(`"Hello " + you + "! I'm " + i + "."`) |
| 72 | if iss.Err() != nil { |
| 73 | t.Fatal(iss.Err()) |
| 74 | } |
| 75 | |
| 76 | // Create the program, and evaluate it against some input. |
| 77 | prg, err := env.Program(ast) |
| 78 | if err != nil { |
| 79 | t.Fatalf("program creation error: %s\n", err) |
| 80 | } |
| 81 | |
| 82 | // If the Eval() call were provided with cel.EvalOptions(OptTrackState) the details response |
| 83 | // (2nd return) would be non-nil. |
| 84 | out, _, err := prg.Eval( |
| 85 | map[string]any{ |
| 86 | "i": "CEL", |
| 87 | "you": "world", |
| 88 | }, |
| 89 | ) |
| 90 | if err != nil { |
| 91 | t.Fatalf("runtime error: %s\n", err) |
| 92 | } |
| 93 | |
| 94 | // Hello world! I'm CEL. |
| 95 | if out.Equal(types.String("Hello world! I'm CEL.")) != types.True { |
| 96 | t.Errorf(`got '%v', wanted "Hello world! I'm CEL."`, out.Value()) |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | func TestExtendCheckerParity(t *testing.T) { |
| 101 | // Base environment carrying standard library functions |