|
1 | 1 | package expr_test |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "encoding/json" |
4 | 5 | "fmt" |
5 | 6 | "github.com/antonmedv/expr/ast" |
| 7 | + "github.com/antonmedv/expr/file" |
6 | 8 | "reflect" |
7 | 9 | "strings" |
8 | 10 | "testing" |
@@ -1017,6 +1019,41 @@ func TestPatch_length(t *testing.T) { |
1017 | 1019 | require.Equal(t, true, output) |
1018 | 1020 | } |
1019 | 1021 |
|
| 1022 | +func TestCompile_exposed_error(t *testing.T) { |
| 1023 | + _, err := expr.Compile(`1 == true`) |
| 1024 | + require.Error(t, err) |
| 1025 | + |
| 1026 | + fileError, ok := err.(*file.Error) |
| 1027 | + require.True(t, ok, "error should be of type *file.Error") |
| 1028 | + require.Equal(t, "invalid operation: == (mismatched types int and bool) (1:3)\n | 1 == true\n | ..^", fileError.Error()) |
| 1029 | + require.Equal(t, 2, fileError.Column) |
| 1030 | + require.Equal(t, 1, fileError.Line) |
| 1031 | + |
| 1032 | + b, err := json.Marshal(err) |
| 1033 | + require.NoError(t, err) |
| 1034 | + require.Equal(t, `{"Line":1,"Column":2,"Message":"invalid operation: == (mismatched types int and bool)","Snippet":"\n | 1 == true\n | ..^"}`, string(b)) |
| 1035 | +} |
| 1036 | + |
| 1037 | +func TestAsBool_exposed_error_(t *testing.T) { |
| 1038 | + _, err := expr.Compile(`42`, expr.AsBool()) |
| 1039 | + require.Error(t, err) |
| 1040 | + |
| 1041 | + _, ok := err.(*file.Error) |
| 1042 | + require.False(t, ok, "error must not be of type *file.Error") |
| 1043 | + require.Equal(t, "expected bool, but got int", err.Error()) |
| 1044 | +} |
| 1045 | + |
| 1046 | +func TestEval_exposed_error(t *testing.T) { |
| 1047 | + _, err := expr.Eval(`1/0`, nil) |
| 1048 | + require.Error(t, err) |
| 1049 | + |
| 1050 | + fileError, ok := err.(*file.Error) |
| 1051 | + require.True(t, ok, "error should be of type *file.Error") |
| 1052 | + require.Equal(t, "runtime error: integer divide by zero (1:2)\n | 1/0\n | .^", fileError.Error()) |
| 1053 | + require.Equal(t, 1, fileError.Column) |
| 1054 | + require.Equal(t, 1, fileError.Line) |
| 1055 | +} |
| 1056 | + |
1020 | 1057 | // |
1021 | 1058 | // Mock types |
1022 | 1059 | // |
|
0 commit comments