-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
parse: test all filtering scenarios, with and without context
- Loading branch information
Showing
8 changed files
with
675 additions
and
157 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,221 @@ | ||
package parse | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"github.com/google/go-cmp/cmp/cmpopts" | ||
) | ||
|
||
func TestJQ(t *testing.T) { | ||
referenceLine := func() *line { return &line{msg: "foo", fields: map[string]interface{}{"foo": 42, "bar": "hi"}} } | ||
testData := []struct { | ||
jq string | ||
l *line | ||
wantLine *line | ||
wantFiltered bool | ||
wantErr error | ||
}{ | ||
{ | ||
jq: ".", | ||
l: referenceLine(), | ||
wantLine: referenceLine(), | ||
wantFiltered: false, | ||
wantErr: nil, | ||
}, | ||
{ | ||
jq: "", | ||
l: referenceLine(), | ||
wantLine: referenceLine(), | ||
wantFiltered: false, | ||
wantErr: nil, | ||
}, | ||
{ | ||
jq: `error("goodbye")`, | ||
l: referenceLine(), | ||
wantLine: referenceLine(), | ||
wantFiltered: false, | ||
wantErr: Match("goodbye"), | ||
}, | ||
{ | ||
jq: "null", | ||
l: referenceLine(), | ||
wantLine: referenceLine(), | ||
wantFiltered: false, | ||
wantErr: Match("unexpected nil result"), | ||
}, | ||
{ | ||
jq: "3.141592", | ||
l: referenceLine(), | ||
wantLine: referenceLine(), | ||
wantFiltered: false, | ||
wantErr: Match("unexpected result type float64\\(3.1"), | ||
}, | ||
{ | ||
jq: "1 > 2", | ||
l: referenceLine(), | ||
wantLine: referenceLine(), | ||
wantFiltered: false, | ||
wantErr: Match("unexpected boolean output"), | ||
}, | ||
{ | ||
jq: "{}", | ||
l: referenceLine(), | ||
wantLine: &line{msg: "foo"}, | ||
wantFiltered: false, | ||
wantErr: nil, | ||
}, | ||
{ | ||
jq: "{}, {}", | ||
l: referenceLine(), | ||
wantLine: &line{msg: "foo"}, | ||
wantFiltered: false, | ||
wantErr: Match("unexpectedly produced more than 1 output"), | ||
}, | ||
{ | ||
jq: "empty", | ||
l: referenceLine(), | ||
wantLine: referenceLine(), | ||
wantFiltered: true, | ||
wantErr: nil, | ||
}, | ||
} | ||
for _, test := range testData { | ||
fs := new(FilterScheme) | ||
if err := fs.AddJQ(test.jq); err != nil { | ||
t.Fatal(err) | ||
} | ||
gotFiltered, gotErr := fs.runJQ(test.l) | ||
if diff := cmp.Diff(test.l, test.wantLine, cmp.AllowUnexported(line{}), cmpopts.EquateEmpty()); diff != "" { | ||
t.Errorf("line: %s", diff) | ||
} | ||
if got, want := gotFiltered, test.wantFiltered; got != want { | ||
t.Errorf("filtered:\n got: %v\n want: %v", got, want) | ||
} | ||
if got, want := gotErr, test.wantErr; !comperror(got, want) { | ||
t.Errorf("error:\n got: %v\n want: %v", got, want) | ||
} | ||
} | ||
} | ||
|
||
func TestAdds(t *testing.T) { | ||
testData := []struct { | ||
name string | ||
match, matchagain, nomatch, jq []string | ||
want []error | ||
}{ | ||
{ | ||
name: "empty", | ||
}, | ||
{ | ||
name: "valid match", | ||
match: []string{"foo"}, | ||
}, | ||
{ | ||
name: "valid nomatch", | ||
nomatch: []string{"foo"}, | ||
}, | ||
{ | ||
name: "valid jq", | ||
jq: []string{"{}"}, | ||
}, | ||
{ | ||
name: "valid match and jq", | ||
match: []string{"foo"}, | ||
jq: []string{"{}"}, | ||
}, | ||
{ | ||
name: "valid nomatch and jq", | ||
nomatch: []string{"foo"}, | ||
jq: []string{"{}"}, | ||
}, | ||
{ | ||
name: "unparseable jq", | ||
jq: []string{"{"}, | ||
want: []error{Match("EOF")}, | ||
}, | ||
{ | ||
name: "uncompilable jq", | ||
jq: []string{"$INVALID"}, | ||
want: []error{Match("variable not defined")}, | ||
}, | ||
{ | ||
name: "double jq", | ||
jq: []string{".", "."}, | ||
want: []error{Match("already added")}, | ||
}, | ||
{ | ||
name: "invalid match", | ||
match: []string{"["}, | ||
want: []error{Match("missing closing ]")}, | ||
}, | ||
{ | ||
name: "invalid nomatch", | ||
nomatch: []string{"["}, | ||
want: []error{Match("missing closing ]")}, | ||
}, | ||
{ | ||
name: "invalid regexes", | ||
match: []string{"["}, | ||
nomatch: []string{"["}, | ||
want: []error{Match("missing closing ]"), Match("missing closing ]")}, | ||
}, | ||
{ | ||
name: "match and nomatch", | ||
match: []string{".*"}, | ||
nomatch: []string{".*"}, | ||
want: []error{ErrConflict}, | ||
}, | ||
{ | ||
name: "nomatch and match", | ||
nomatch: []string{".*"}, | ||
matchagain: []string{".*"}, | ||
want: []error{ErrConflict}, | ||
}, | ||
{ | ||
name: "double match", | ||
match: []string{"a", "b"}, | ||
want: []error{ErrAlreadyAdded}, | ||
}, | ||
{ | ||
name: "double nomatch", | ||
nomatch: []string{"a", "b"}, | ||
want: []error{ErrAlreadyAdded}, | ||
}, | ||
} | ||
|
||
for _, test := range testData { | ||
t.Run(test.name, func(t *testing.T) { | ||
f := new(FilterScheme) | ||
var errs []error | ||
for _, jq := range test.jq { | ||
if err := f.AddJQ(jq); err != nil { | ||
errs = append(errs, err) | ||
} | ||
} | ||
for _, rx := range test.match { | ||
if err := f.AddMatchRegex(rx); err != nil { | ||
errs = append(errs, err) | ||
} | ||
} | ||
for _, rx := range test.nomatch { | ||
if err := f.AddNoMatchRegex(rx); err != nil { | ||
errs = append(errs, err) | ||
} | ||
} | ||
for _, rx := range test.matchagain { | ||
if err := f.AddMatchRegex(rx); err != nil { | ||
errs = append(errs, err) | ||
} | ||
} | ||
if diff := cmp.Diff(errs, test.want, cmp.Comparer(comperror)); diff != "" { | ||
t.Errorf("error:\n%s", diff) | ||
} | ||
if len(errs) > 0 { | ||
if _, err := f.Run(&line{}); err != nil { | ||
t.Errorf("run: %v", err) | ||
} | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.