-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
pre_execute_test.go
85 lines (64 loc) · 1.43 KB
/
pre_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
package hype
import (
"context"
"fmt"
"testing"
"github.com/stretchr/testify/require"
)
type preExecuteNode struct {
*Element
PreExecuteFn
}
func newPreExecuteNode(t testing.TB, fn PreExecuteFn) *preExecuteNode {
t.Helper()
return &preExecuteNode{
PreExecuteFn: fn,
Element: &Element{},
}
}
func Test_Nodes_PreExecute(t *testing.T) {
t.Parallel()
r := require.New(t)
doc := &Document{}
n1 := newPreExecuteNode(t, func(ctx context.Context, d *Document) error {
d.Title = "Hello"
return nil
})
n1.Nodes = append(n1.Nodes, newPreExecuteNode(t, func(ctx context.Context, d *Document) error {
d.Title += " World"
return nil
}))
nodes := Nodes{n1}
err := nodes.PreExecute(context.Background(), doc)
r.NoError(err)
act := doc.Title
exp := "Hello World"
r.Equal(exp, act)
}
func Test_Nodes_PreExecute_Errors(t *testing.T) {
t.Parallel()
en := newPreExecuteNode(t, func(ctx context.Context, d *Document) error {
return fmt.Errorf("boom")
})
nen := newPreExecuteNode(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}
err := nodes.PreExecute(context.Background(), &Document{})
r.Error(err)
r.Contains(err.Error(), "boom")
})
}
}