/
junction_test.go
198 lines (153 loc) · 3.3 KB
/
junction_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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package junction_test
import (
"github.com/toshaf/junction"
"testing"
)
type Person struct {
Name string
Age int
}
func (p *Person) SetName(name string) {
p.Name = name
}
func Test_Junction_rejects_non_pointer_model(t *testing.T) {
var out chan Person
err := junction.Validate(&out, []junction.Source{{
Input: make(chan string),
Model: Person{}, // this won't fly
Update: (*Person).SetName,
}})
if err == nil {
t.Error("Expected error, got nil")
}
}
func Test_Run_rejects_non_pointer_channel(t *testing.T) {
var out chan Person
err := junction.Validate(out, nil)
if err == nil {
t.Error("Expected error, got nil")
}
}
func Test_Run_rejects_pointer_to_non_chan(t *testing.T) {
var out string
err := junction.Validate(&out, nil)
if err == nil {
t.Error("Expected error, got nil")
}
}
func Test_Run_creates_channel(t *testing.T) {
var out chan Person
junction.New(&out, nil)
if out == nil {
t.Error("Channel is nil")
}
}
func Test_update_via_member_func_yields_updated_value(t *testing.T) {
s := make(chan string)
var out chan Person
junction.New(&out, []junction.Source{{
Input: s,
Update: (*Person).SetName,
Model: &Person{Name: "Jeff", Age: 56},
}})
s <- "Fishy Bob"
person := <-out
fb := Person{Age: 56, Name: "Fishy Bob"}
if person != fb {
t.Errorf("Where's Fishy Bob?")
}
}
func UpdatePersonAge(p *Person, age int) {
p.Age = age
}
func Test_update_via_non_member_func_yields_updated_value(t *testing.T) {
i := make(chan int)
var out chan Person
junction.New(&out, []junction.Source{{
Input: i,
Update: UpdatePersonAge,
Model: &Person{Name: "Jeff", Age: 56},
}})
i <- 57
person := <-out
older := Person{Age: 57, Name: "Jeff"}
if person != older {
t.Errorf("Expected %v but got %v", older, person)
}
}
type People map[int]*Person
func (people People) ById(ider Ider) (*Person, bool) {
p, ok := people[ider.Id()]
return p, ok
}
type Ider interface {
Id() int
}
type NameUpdate struct {
id int
name string
}
func (u NameUpdate) Id() int {
return u.id
}
func ApplyNameUpdate(p *Person, u NameUpdate) {
p.Name = u.name
}
type AgeUpdate struct {
id int
}
func (u AgeUpdate) Id() int {
return u.id
}
func ApplyAgeUpdate(p *Person, u AgeUpdate) {
p.Age++
}
func Test_Model_lookup_function(t *testing.T) {
people := People{
123: &Person{Name: "Ann", Age: 23},
456: &Person{Name: "Bob", Age: 21},
}
names := make(chan NameUpdate)
ages := make(chan AgeUpdate)
var out chan Person
junction.New(&out, []junction.Source{
{
Input: names,
Update: ApplyNameUpdate,
Model: people.ById,
},
{
Input: ages,
Update: ApplyAgeUpdate,
Model: people.ById,
},
})
names <- NameUpdate{
id: 123,
name: "Anne",
}
anne := <-out
if anne != (Person{Name: "Anne", Age: 23}) {
t.Error("Name update failed")
}
ages <- AgeUpdate{id: 123}
anne = <-out
if anne.Age != 24 {
t.Error("Age update failed")
}
if anne.Name != "Anne" {
t.Error("Name update was lost")
}
ages <- AgeUpdate{id: 456}
bob := <-out
if bob != (Person{Name: "Bob", Age: 22}) {
t.Error("Age update failed")
}
// check state
if *people[123] != (Person{Name: "Anne", Age: 24}) {
t.Errorf("Wrong value for Anne: %v", people[123])
}
if *people[456] != (Person{Name: "Bob", Age: 22}) {
t.Errorf("Wrong value for Bob: %v", people[456])
}
}