-
Notifications
You must be signed in to change notification settings - Fork 2
/
extract_test.go
98 lines (76 loc) · 1.62 KB
/
extract_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
package simpleflow
import (
"testing"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
)
type ExtractSuite struct {
suite.Suite
}
func TestExtract(t *testing.T) {
s := new(ExtractSuite)
suite.Run(t, s)
}
func (s *ExtractSuite) TestExtractToSlice() {
type Object struct {
Name string
}
in := []Object{
{Name: "John"},
{Name: "Paul"},
{Name: "George"},
{Name: "Ringo"},
{Name: "Bob"},
}
var names []string
fn := func(t Object) (string, bool) {
if t.Name == "Bob" {
return "", false
}
return t.Name, true
}
names = ExtractToSlice(in, fn, names)
expected := []string{"John", "Paul", "George", "Ringo"}
require.Equal(s.T(), expected, names)
}
func (s *ExtractSuite) TestExtractToChannel() {
type Object struct {
Name string
}
in := []Object{
{Name: "John"},
{Name: "Paul"},
{Name: "George"},
{Name: "Ringo"},
{Name: "Bob"},
}
names := make(chan string, len(in))
fn := func(t Object) (string, bool) {
if t.Name == "Bob" {
return "", false
}
return t.Name, true
}
ExtractToChannel(in, fn, names)
close(names)
require.Len(s.T(), names, 4)
expected := []string{"John", "Paul", "George", "Ringo"}
require.Equal(s.T(), expected, ChannelToSlice(names))
}
func (s *ExtractSuite) TestExtractFirst() {
values := []int{4, 1, 5, 7}
fn := func(v int) bool {
return v > 4
}
v, found := ExtractFirst(values, fn)
expected := 5
require.True(s.T(), found)
require.Equal(s.T(), expected, v)
fn = func(v int) bool {
return v > 10
}
v, found = ExtractFirst(values, fn)
expected = 0
require.False(s.T(), found)
require.Equal(s.T(), expected, v)
}