-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
execute_test.go
96 lines (74 loc) · 1.58 KB
/
execute_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
package hype
import (
"context"
"errors"
"fmt"
"testing"
"github.com/stretchr/testify/require"
"golang.org/x/sync/errgroup"
)
type executeNode struct {
*Element
ExecuteFn
}
func newExecuteNode(t testing.TB, fn ExecuteFn) *executeNode {
t.Helper()
return &executeNode{
ExecuteFn: fn,
Element: &Element{},
}
}
func Test_Nodes_Execute(t *testing.T) {
t.Parallel()
r := require.New(t)
doc := &Document{}
n1 := newExecuteNode(t, func(ctx context.Context, d *Document) error {
d.Lock()
d.Nodes = append(d.Nodes, Text("foo"))
d.Unlock()
return nil
})
n1.Nodes = append(n1.Nodes, newExecuteNode(t, func(ctx context.Context, d *Document) error {
d.Lock()
d.Nodes = append(d.Nodes, Text("bar"))
d.Unlock()
return nil
}))
nodes := Nodes{n1}
wg := &errgroup.Group{}
err := nodes.Execute(wg, nil, doc)
r.NoError(err)
err = wg.Wait()
r.NoError(err)
r.Len(doc.Nodes, 2)
}
func Test_Nodes_Execute_Errors(t *testing.T) {
t.Parallel()
en := newExecuteNode(t, func(ctx context.Context, d *Document) error {
return fmt.Errorf("boom")
})
nen := newExecuteNode(t, func(ctx context.Context, d *Document) error {
return nil
})
nen.Nodes = append(nen.Nodes, en)
table := []struct {
name string
node Node
}{
{"top level", en},
{"nested", nen},
}
for _, tc := range table {
tc := tc
t.Run(tc.name, func(t *testing.T) {
r := require.New(t)
nodes := Nodes{tc.node}
wg := &errgroup.Group{}
err := nodes.Execute(wg, nil, &Document{})
r.NoError(err)
err = wg.Wait()
r.Error(err)
r.True(errors.Is(err, ExecuteError{}), err)
})
}
}