-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpost_parser_test.go
93 lines (69 loc) · 1.55 KB
/
post_parser_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
package hype
import (
"fmt"
"testing"
"github.com/stretchr/testify/require"
)
type postParserNode struct {
*Element
PostParseFn
}
func newPostParserNode(t testing.TB, fn PostParseFn) *postParserNode {
t.Helper()
return &postParserNode{
PostParseFn: fn,
Element: &Element{},
}
}
func Test_PostParsers_PostParse(t *testing.T) {
t.Parallel()
r := require.New(t)
nodes := Nodes{
newPostParserNode(t, func(p *Parser, d *Document, err error) error {
d.Title = "Hello"
return nil
}),
}
d := &Document{}
err := nodes.PostParse(nil, d, nil)
r.NoError(err)
act := d.Title
exp := "Hello"
r.Equal(exp, act)
}
func Test_PostParsers_PostParse_Errors(t *testing.T) {
t.Parallel()
efn := newPostParserNode(t, func(p *Parser, d *Document, err error) error {
d.Title = "Hello"
return fmt.Errorf("boom")
})
nofn := newPostParserNode(t, func(p *Parser, d *Document, err error) error {
d.Title = "Hello"
return nil
})
table := []struct {
name string
fn Node
exp string
}{
{name: "no error", fn: nofn, exp: "original"},
{name: "nodes list", fn: Nodes{nofn}, exp: "original"},
{name: "extra error", fn: efn, exp: "boom; original"},
}
for _, tc := range table {
t.Run(tc.name, func(t *testing.T) {
r := require.New(t)
nodes := Nodes{
tc.fn,
}
d := &Document{}
p := testParser(t, "testdata/whole/simple")
p.Filename = "hype.md"
err := nodes.PostParse(p, d, fmt.Errorf("original"))
r.Error(err)
var pperr PostParseError
r.ErrorAs(err, &pperr)
r.Equal("Hello", d.Title)
})
}
}