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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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")
}
|