refactor and unit test filters

This commit is contained in:
bel
2020-01-20 16:24:43 -07:00
parent b47327dfe6
commit cb73169eeb
2 changed files with 68 additions and 21 deletions

View File

@@ -2,6 +2,7 @@ package ajax
import (
"encoding/json"
"local/todo-server/server/ajax/form"
"local/todo-server/server/ajax/task"
"net/http"
"net/http/httptest"
@@ -207,3 +208,48 @@ func TestAjaxChangeOrder(t *testing.T) {
t.Error(tasks[2])
}
}
func TestFilterComplete(t *testing.T) {
task := &task.Task{Complete: false}
cases := []struct {
query string
out bool
}{
{query: "", out: true},
{query: "0", out: true},
{query: "1", out: false},
}
for name, c := range cases {
out := filterComplete(c.query)
if out(task) != c.out {
t.Errorf("[%d] want %v, got %v", name, c.out, out(task))
}
}
}
func TestFilterTags(t *testing.T) {
cases := map[string]struct {
query string
tags []string
out bool
}{
"no filter": {query: "", out: true},
"single matching filter": {query: "a", out: true, tags: []string{"a"}},
"single non-matching filter": {query: "a", out: false, tags: []string{"b"}},
"duo matching filter": {query: "a, b", out: true, tags: []string{"b", "a"}},
"duo partial-matching filter": {query: "a, c", out: false, tags: []string{"b", "a"}},
"duo non-matching filter": {query: "a, c", out: false, tags: []string{"d", "e"}},
"trio matching filter": {query: "a, b, c", out: true, tags: []string{"b", "a", "c", "d", "e"}},
"trio partial-matching filter": {query: "a, c, d", out: false, tags: []string{"b", "a", "d"}},
"trio non-matching filter": {query: "a, b, c", out: false, tags: []string{"x", "y", "z"}},
}
for name, c := range cases {
task := &task.Task{Tags: c.tags}
out := filterTags(form.ToStrArr(c.query))
if v := out(task); v != c.out {
t.Errorf("[%s] want %v, got %v", name, c.out, v)
}
}
}