-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter_test.go
125 lines (119 loc) · 2.8 KB
/
filter_test.go
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
package thuder
import (
"fmt"
"testing"
"time"
)
func TestFilter(t *testing.T) {
n := &fakeNodes("test", "abc")[0]
f := Filter{}
now := time.Now()
t.Run(fmt.Sprintf("Direction"), func(t *testing.T) {
f.Direction = "foobar"
expErr(t, f.Prepare())
f.Direction = "push"
noErr(t, f.Prepare())
m, a := f.Match(n, true, now)
expect(t, m, a, true, false)
f.Allow = true
m, a = f.Match(n, true, now)
expect(t, m, a, true, true)
m, a = f.Match(n, false, now)
expect(t, m, a, false, false)
f.Direction = "pull"
m, a = f.Match(n, false, now)
expect(t, m, a, true, true)
m, a = f.Match(n, true, now)
expect(t, m, a, false, false)
f.Direction = ""
m, a = f.Match(n, true, now)
expect(t, m, a, true, true)
m, a = f.Match(n, false, now)
expect(t, m, a, true, true)
})
matches := func(t *testing.T, em bool) {
m, a := f.Match(n, false, now)
expect(t, m, a, em, em)
}
t.Run(fmt.Sprintf("FolderOnly"), func(t *testing.T) {
matches(t, true)
f.FolderOnly = true
matches(t, false)
n.info.(*testFileInfo).dir = true
matches(t, true)
})
t.Run(fmt.Sprintf("FileOnly"), func(t *testing.T) {
f.FileOnly = true
expErr(t, f.Prepare())
f.FolderOnly = false
noErr(t, f.Prepare())
matches(t, false)
n.info.(*testFileInfo).dir = false
matches(t, true)
})
t.Run(fmt.Sprintf("PathRx"), func(t *testing.T) {
f.PathRx = "\\"
expErr(t, f.Prepare())
f.PathRx = "testtest"
noErr(t, f.Prepare())
matches(t, false)
f.PathRx = "test"
noErr(t, f.Prepare())
matches(t, true)
})
t.Run(fmt.Sprintf("NameRx"), func(t *testing.T) {
f.NameRx = "\\"
expErr(t, f.Prepare())
f.NameRx = "x"
noErr(t, f.Prepare())
matches(t, false)
f.NameRx = "a"
noErr(t, f.Prepare())
matches(t, true)
})
t.Run(fmt.Sprintf("LastModifiedDays"), func(t *testing.T) {
n.info.(*testFileInfo).modtime = now.Add(-30 * time.Hour)
f.LastModifiedDays = -1
expErr(t, f.Prepare())
f.LastModifiedDays = 1
noErr(t, f.Prepare())
matches(t, false)
f.LastModifiedDays = 2
noErr(t, f.Prepare())
matches(t, true)
})
t.Run(fmt.Sprintf("SubFilters"), func(t *testing.T) {
f2 := f
f.SubFilters = []Filter{f2}
matches(t, true)
f.Allow = false
matches(t, true)
f.SubFilters[0].LastModifiedDays = -1
expErr(t, f.Prepare())
f.SubFilters[0].LastModifiedDays = 1
noErr(t, f.Prepare())
m, a := f.Match(n, false, now)
expect(t, m, a, true, false)
f.SubFilters = append(f.SubFilters, f2)
matches(t, true)
f.LastModifiedDays = 1
matches(t, false)
})
}
func expect(t *testing.T, m, a, em, ea bool) {
if m != em || a != ea {
t.Errorf("got (%v,%v) expected (%v,%v)", m, a, em, ea)
}
}
func expErr(t *testing.T, err error) {
if err == nil {
t.Error("error expected")
} else {
t.Log("expected error: ", err)
}
}
func noErr(t *testing.T, err error) {
if err != nil {
t.Error(err)
}
}