-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
post_execute_test.go
86 lines (66 loc) · 1.53 KB
/
post_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
package hype
import (
"context"
"fmt"
"testing"
"github.com/stretchr/testify/require"
)
type postExecuterNode struct {
*Element
PostExecuteFn
}
func newPostExecuteNode(t testing.TB, fn PostExecuteFn) *postExecuterNode {
t.Helper()
return &postExecuterNode{
PostExecuteFn: fn,
Element: &Element{},
}
}
func Test_Nodes_PostExecute(t *testing.T) {
t.Parallel()
r := require.New(t)
nodes := Nodes{
newPostExecuteNode(t, func(ctx context.Context, d *Document, err error) error {
d.Title = "Hello"
return nil
}),
newPostExecuteNode(t, func(ctx context.Context, d *Document, err error) error {
d.Title += " World"
return nil
}),
}
doc := &Document{}
err := nodes.PostExecute(context.Background(), doc, nil)
r.NoError(err)
act := doc.Title
exp := "Hello World"
r.Equal(exp, act)
}
func Test_Nodes_PostExecute_Errors(t *testing.T) {
t.Parallel()
en := newPostExecuteNode(t, func(ctx context.Context, d *Document, err error) error {
return fmt.Errorf("boom")
})
nen := newPostExecuteNode(t, func(ctx context.Context, d *Document, err error) 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.PostExecute(context.Background(), nil, fmt.Errorf("original"))
r.Error(err)
r.Contains(err.Error(), "boom")
r.Contains(err.Error(), "original")
})
}
}