| 1 | // Copyright 2020 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 ext |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
| 19 | "strings" |
| 20 | "testing" |
| 21 | |
| 22 | "github.com/google/cel-go/cel" |
| 23 | ) |
| 24 | |
| 25 | func TestEncoders(t *testing.T) { |
| 26 | var tests = []struct { |
| 27 | expr string |
| 28 | err string |
| 29 | parseOnly bool |
| 30 | }{ |
| 31 | {expr: "base64.decode('aGVsbG8=') == b'hello'"}, |
| 32 | {expr: "base64.decode('aGVsbG8') == b'hello'"}, |
| 33 | { |
| 34 | expr: "base64.decode(b'aGVsbG8=') == b'hello'", |
| 35 | err: "no such overload", |
| 36 | parseOnly: true, |
| 37 | }, |
| 38 | {expr: "base64.encode(b'hello') == 'aGVsbG8='"}, |
| 39 | { |
| 40 | expr: "base64.encode('hello') == b'aGVsbG8='", |
| 41 | err: "no such overload", |
| 42 | parseOnly: true, |
| 43 | }, |
| 44 | } |
| 45 | |
| 46 | env, err := cel.NewEnv(Encoders()) |
| 47 | if err != nil { |
| 48 | t.Fatalf("cel.NewEnv(Encoders()) failed: %v", err) |
| 49 | } |
| 50 | for i, tst := range tests { |
| 51 | tc := tst |
| 52 | t.Run(fmt.Sprintf("[%d]", i), func(t *testing.T) { |
| 53 | var asts []*cel.Ast |
| 54 | pAst, iss := env.Parse(tc.expr) |
| 55 | if iss.Err() != nil { |
| 56 | t.Fatalf("env.Parse(%v) failed: %v", tc.expr, iss.Err()) |
| 57 | } |
| 58 | asts = append(asts, pAst) |
| 59 | if !tc.parseOnly { |
| 60 | cAst, iss := env.Check(pAst) |
| 61 | if iss.Err() != nil { |
| 62 | t.Fatalf("env.Check(%v) failed: %v", tc.expr, iss.Err()) |
| 63 | } |
| 64 | asts = append(asts, cAst) |
| 65 | } |
| 66 | for _, ast := range asts { |
| 67 | prg, err := env.Program(ast) |
| 68 | if err != nil { |
| 69 | t.Fatal(err) |
| 70 | } |
| 71 | out, _, err := prg.Eval(cel.NoVars()) |
| 72 | if tc.err != "" { |
| 73 | if err == nil { |
| 74 | t.Fatalf("got %v, wanted error %s for expr: %s", |
| 75 | out.Value(), tc.err, tc.expr) |
| 76 | } |
| 77 | if !strings.Contains(err.Error(), tc.err) { |
| 78 | t.Errorf("got error %v, wanted error %s for expr: %s", err, tc.err, tc.expr) |
| 79 | } |
| 80 | } else if err != nil { |
| 81 | t.Fatal(err) |
| 82 | } else if out.Value() != true { |
| 83 | t.Errorf("got %v, wanted true for expr: %s", out.Value(), tc.expr) |
| 84 | } |
| 85 | } |
| 86 | }) |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | func TestEncodersVersion(t *testing.T) { |
| 91 | _, err := cel.NewEnv(Encoders(EncodersVersion(0))) |
| 92 | if err != nil { |
| 93 | t.Fatalf("EncodersVersion(0) failed: %v", err) |
| 94 | } |
| 95 | } |