added test for contains filter

This commit is contained in:
BroodjeAap 2022-10-03 17:56:55 +00:00
parent 60da3b9f91
commit bc97a0947c
2 changed files with 36 additions and 3 deletions

View file

@ -326,10 +326,10 @@ func getFilterResultContains(filter *Filter) {
for _, parent := range filter.Parents {
for _, result := range parent.Results {
log.Println(result, substring, invert, strings.Contains(result, substring))
if strings.Contains(result, substring) {
contains := strings.Contains(result, substring)
if contains && !invert {
filter.Results = append(filter.Results, result)
} else if invert {
} else if !contains && invert {
filter.Results = append(filter.Results, result)
}
}

View file

@ -258,6 +258,39 @@ func TestFilterSubstring(t *testing.T) {
}
}
func TestFilterContains(t *testing.T) {
var tests = []struct {
Input []string
Query string
Invert string
Want []string
}{
{[]string{"some text", "other text"}, "some", "false", []string{"some text"}},
{[]string{"some text", "other text"}, "some", "true", []string{"other text"}},
{[]string{"some text", "other text"}, "needle", "false", []string{}},
{[]string{"some text", "other text"}, "needle", "true", []string{"some text", "other text"}},
}
for _, test := range tests {
testname := fmt.Sprintf("%s %s %s", test.Input, test.Query, test.Invert)
t.Run(testname, func(t *testing.T) {
filter := Filter{
Parents: []*Filter{
{Results: test.Input},
},
Var1: test.Query,
Var2: &test.Invert,
}
getFilterResultContains(
&filter,
)
if (filter.Results != nil && test.Want != nil) && !reflect.DeepEqual(test.Want, filter.Results) {
t.Errorf("Got %s, want %s", filter.Results, test.Want)
}
})
}
}
func TestFilterSum(t *testing.T) {
var tests = []struct {
Input []string