diff options
Diffstat (limited to 'filter_test.go')
| -rw-r--r-- | filter_test.go | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/filter_test.go b/filter_test.go new file mode 100644 index 0000000..736b9fc --- /dev/null +++ b/filter_test.go @@ -0,0 +1,93 @@ +package main + +import ( + "testing" + + "git.sr.ht/~eugenetriguba/checkmate/assert" +) + +func TestFilterFunc_MapsByBoolField(t *testing.T) { + items := []map[string]any{ + {"title": "Post 1", "draft": false}, + {"title": "Post 2", "draft": true}, + {"title": "Post 3", "draft": false}, + } + + got := filterFunc(items, "draft", false) + + assert.Equal(t, len(got), 2) + assert.DeepEqual(t, got[0], map[string]any{"title": "Post 1", "draft": false}) + assert.DeepEqual(t, got[1], map[string]any{"title": "Post 3", "draft": false}) +} + +func TestFilterFunc_MapsByStringField(t *testing.T) { + items := []map[string]any{ + {"title": "Post 1", "category": "tech"}, + {"title": "Post 2", "category": "news"}, + {"title": "Post 3", "category": "tech"}, + } + + got := filterFunc(items, "category", "tech") + + assert.Equal(t, len(got), 2) + assert.DeepEqual(t, got[0], map[string]any{"title": "Post 1", "category": "tech"}) + assert.DeepEqual(t, got[1], map[string]any{"title": "Post 3", "category": "tech"}) +} + +func TestFilterFunc_NoMatches(t *testing.T) { + items := []map[string]any{ + {"title": "Post 1", "category": "tech"}, + } + + got := filterFunc(items, "category", "news") + + assert.Equal(t, len(got), 0) +} + +func TestFilterFunc_MissingField(t *testing.T) { + items := []map[string]any{ + {"title": "Post 1"}, + {"title": "Post 2", "draft": false}, + } + + got := filterFunc(items, "draft", false) + + assert.Equal(t, len(got), 1) + assert.DeepEqual(t, got[0], map[string]any{"title": "Post 2", "draft": false}) +} + +func TestFilterFunc_EmptySlice(t *testing.T) { + items := []map[string]any{} + got := filterFunc(items, "draft", false) + assert.Equal(t, len(got), 0) +} + +func TestFilterFunc_NonSliceInput(t *testing.T) { + got := filterFunc("not a slice", "field", "value") + assert.Equal(t, len(got), 0) +} + +func TestFilterFunc_NilInput(t *testing.T) { + got := filterFunc(nil, "field", "value") + assert.Equal(t, len(got), 0) +} + +func TestFilterFunc_Structs(t *testing.T) { + type Post struct { + Title string + Draft bool + } + items := []Post{ + {Title: "Post 1", Draft: false}, + {Title: "Post 2", Draft: true}, + {Title: "Post 3", Draft: false}, + } + + got := filterFunc(items, "Draft", false) + + assert.Equal(t, len(got), 2) + post1 := got[0].(Post) + post2 := got[1].(Post) + assert.Equal(t, post1.Title, "Post 1") + assert.Equal(t, post2.Title, "Post 3") +} |