-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunc_test.go
More file actions
138 lines (122 loc) · 3.07 KB
/
func_test.go
File metadata and controls
138 lines (122 loc) · 3.07 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package ff_test
import (
"context"
"net/url"
"testing"
"time"
"github.com/mmcdole/gofeed"
"github.com/nakatanakatana/ff"
"gotest.tools/v3/assert"
is "gotest.tools/v3/assert/cmp"
)
func createTestItem() *gofeed.Item {
testUpdated := time.Date(2021, time.July, 11, 0, 0, 0, 0, time.UTC)
testPublished := time.Date(2021, time.July, 1, 0, 0, 0, 0, time.UTC)
testItem := &gofeed.Item{
Title: "title",
Description: "description",
Link: "https://github.com/nakatanakatana/ff",
Author: &gofeed.Person{Name: "aname", Email: "aname@nakatanakatana.dev"},
UpdatedParsed: &testUpdated,
PublishedParsed: &testPublished,
}
return testItem
}
func TestParseQueries(t *testing.T) {
t.Parallel()
filtersMap := ff.CreateFiltersMap([]string{}, []string{})
modifierMap := ff.CreateModifierMap()
for _, tt := range []struct {
name string
urlString string
expectFilterLen int
expectModifierLen int
}{
{"parameter is empty", "https://t.io/", 0, 0},
{"filterOnly", "https://t.io/?link.contains=t.io", 1, 0},
{"modifierOnly", "https://t.io/?rm.description", 0, 1},
{"filterAndModifier multiple", "https://t.io/?title.equal=title&latest&rm.content", 2, 1},
} {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
u, err := url.Parse(tt.urlString)
assert.NilError(t, err)
f, m := ff.ParseQueries(u.Query(), filtersMap, modifierMap)
assert.Check(t, len(f) == tt.expectFilterLen)
assert.Check(t, len(m) == tt.expectModifierLen)
})
}
}
//nolint:funlen
func TestFilterAndModifier(t *testing.T) {
t.Parallel()
testItem := createTestItem()
expectSameItem := *testItem
expectRemoveDescription := *testItem
expectRemoveDescription.Description = ""
for _, tt := range []struct {
name string
filters []ff.FilterFunc
modifiers []ff.ModifierFunc
expectItemLen int
expectItem *gofeed.Item
}{
{
"empty",
[]ff.FilterFunc{},
[]ff.ModifierFunc{},
1,
&expectSameItem,
},
{
"filterOnly matched",
[]ff.FilterFunc{ff.TitleEqual("title")},
[]ff.ModifierFunc{},
1,
&expectSameItem,
},
{
"filterOnly unmatch",
[]ff.FilterFunc{ff.TitleEqual("ti")},
[]ff.ModifierFunc{},
0,
nil,
},
{
"modifierOnly",
[]ff.FilterFunc{},
[]ff.ModifierFunc{ff.RemoveDescription("")},
1,
&expectRemoveDescription,
},
{
"filterAndModifier matched",
[]ff.FilterFunc{ff.TitleEqual("title")},
[]ff.ModifierFunc{ff.RemoveDescription("")},
1,
&expectRemoveDescription,
},
{
"filterAndModifier unmatch",
[]ff.FilterFunc{ff.TitleEqual("ti")},
[]ff.ModifierFunc{ff.RemoveDescription("")},
0,
nil,
},
} {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
testFeed := &gofeed.Feed{
Items: []*gofeed.Item{testItem},
}
result, err := ff.Apply(context.Background(), testFeed, tt.filters, tt.modifiers)
assert.NilError(t, err)
assert.Check(t, is.Len(result.Items, tt.expectItemLen))
if tt.expectItemLen > 0 {
assert.Check(t, is.DeepEqual(tt.expectItem, result.Items[0]))
}
})
}
}