-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfilter_errors_test.go
More file actions
66 lines (61 loc) · 1.92 KB
/
Copy pathfilter_errors_test.go
File metadata and controls
66 lines (61 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package qfv
import (
"errors"
"testing"
)
func TestQFVFilterError_Error(t *testing.T) {
withField := &QFVFilterError{Field: "name", Message: "bad"}
if got, want := withField.Error(), "error on field 'name': bad"; got != want {
t.Errorf("Error() = %q, want %q", got, want)
}
noField := &QFVFilterError{Message: "bad"}
if got, want := noField.Error(), "error: bad"; got != want {
t.Errorf("Error() = %q, want %q", got, want)
}
}
// TestParse_JoinedErrorsAreInspectable verifies the aggregated error can be
// unwrapped into *QFVFilterError values via errors.As.
func TestParse_JoinedErrorsAreInspectable(t *testing.T) {
p := NewFilterParser([]string{"name"})
_, err := p.Parse("unknown = 'x'")
if err == nil {
t.Fatal("expected error for disallowed field")
}
var fe *QFVFilterError
if !errors.As(err, &fe) {
t.Fatalf("errors.As failed to extract *QFVFilterError from %T", err)
}
if fe.Field != "unknown" {
t.Errorf("Field = %q, want unknown", fe.Field)
}
}
func TestNode_TypeMethods(t *testing.T) {
if got := (FieldsNode{}).Type(); got != NodeTypeFieldList {
t.Errorf("FieldsNode.Type() = %s", got)
}
if got := (SortNode{}).Type(); got != NodeTypeSort {
t.Errorf("SortNode.Type() = %s", got)
}
if got := (SortFieldNode{}).Type(); got != NodeTypeSortField {
t.Errorf("SortFieldNode.Type() = %s", got)
}
}
// TestFilterParser_ErrorBranches drives the IN/BETWEEN error paths.
func TestFilterParser_ErrorBranches(t *testing.T) {
p := NewFilterParser([]string{"name", "age"})
cases := []string{
"name IN ()", // empty list
"name IN ('x',)", // trailing comma
"name IN 'x'", // missing opening paren
"name IN ('x'", // missing closing paren
"age BETWEEN 10", // missing AND
"age BETWEEN 10 20", // missing AND keyword
}
for _, in := range cases {
t.Run(in, func(t *testing.T) {
if _, err := p.Parse(in); err == nil {
t.Errorf("Parse(%q) = nil error, want error", in)
}
})
}
}