| 1 |
/* |
| 2 |
Copyright 2024 The Kubernetes Authors. |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package cel |
| 18 |
|
| 19 |
import ( |
| 20 |
"errors" |
| 21 |
"testing" |
| 22 |
|
| 23 |
"github.com/google/cel-go/cel" |
| 24 |
) |
| 25 |
|
| 26 |
func TestOutOfBudgetError(t *testing.T) { |
| 27 |
err := &Error{ |
| 28 |
Type: ErrorTypeInvalid, |
| 29 |
Detail: "expression out of budget", |
| 30 |
Cause: ErrOutOfBudget, |
| 31 |
} |
| 32 |
if !errors.Is(err, ErrOutOfBudget) { |
| 33 |
t.Errorf("unexpected %v is not %v", err, ErrOutOfBudget) |
| 34 |
} |
| 35 |
if !errors.Is(err, ErrInvalid) { |
| 36 |
t.Errorf("unexpected %v is not %v", err, ErrInvalid) |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
func TestCompilationError(t *testing.T) { |
| 41 |
if !errors.Is(ErrCompilation, ErrInvalid) { |
| 42 |
t.Errorf("unexpected %v is not %v", ErrCompilation, ErrInvalid) |
| 43 |
} |
| 44 |
issues := &cel.Issues{} |
| 45 |
err := &Error{ |
| 46 |
Type: ErrorTypeInvalid, |
| 47 |
Detail: "fake compilation failed", |
| 48 |
Cause: NewCompilationError(issues), |
| 49 |
} |
| 50 |
if !errors.Is(err, ErrCompilation) { |
| 51 |
t.Errorf("unexpected %v is not %v", err, ErrCompilation) |
| 52 |
} |
| 53 |
if !errors.Is(err, ErrInvalid) { |
| 54 |
t.Errorf("unexpected %v is not %v", err, ErrInvalid) |
| 55 |
} |
| 56 |
var compilationErr *CompilationError |
| 57 |
if errors.As(err, &compilationErr); compilationErr == nil { |
| 58 |
t.Errorf("unexpected %v cannot be fitted into CompilationError", err) |
| 59 |
} |
| 60 |
if compilationErr.Issues != issues { |
| 61 |
t.Errorf("retrieved issues is not the original") |
| 62 |
} |
| 63 |
} |