-
Notifications
You must be signed in to change notification settings - Fork 10
/
filter_test.go
87 lines (69 loc) · 1.35 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
//
// Copyright (c) 2018 Dean Jackson <[email protected]>
//
// MIT Licence. See http://opensource.org/licenses/MIT
//
// Created on 2018-01-27
//
package main
import (
"path/filepath"
"testing"
)
func TestFilter(t *testing.T) {
data := []struct {
in, out []string
}{
{[]string{""}, []string{}},
{[]string{"file", "file.txt"}, []string{"file.txt"}},
{[]string{"file.txt", "file.pdf"}, []string{"file.txt", "file.pdf"}},
{[]string{"file.mp4", "file.pdf"}, []string{"file.pdf"}},
}
for _, td := range data {
var in = make(chan string)
// Generate input
go func(c chan string, data []string) {
for _, s := range data {
if s == "" {
continue
}
x := filepath.Ext(s)
if x == ".mp4" || x == "" {
continue
}
c <- s
}
close(in)
}(in, td.in)
f := Filter{}
f.Use(func(in <-chan string) <-chan string {
var out = make(chan string)
go func() {
defer close(out)
for s := range in {
out <- s
}
}()
return out
})
out := f.Apply(in)
res := []string{}
for s := range out {
res = append(res, s)
}
if !strSlicesEqual(res, td.out) {
t.Errorf("Bad Filter. Expected=%#v, Got=%#v", td.out, res)
}
}
}
func strSlicesEqual(s1, s2 []string) bool {
if len(s1) != len(s2) {
return false
}
for i, s := range s1 {
if s != s2[i] {
return false
}
}
return true
}