-
Notifications
You must be signed in to change notification settings - Fork 1
/
xml_test.go
103 lines (92 loc) · 2.68 KB
/
xml_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
package reactor
import (
"fmt"
"testing"
"github.com/stretchr/testify/require"
)
func TestParser(t *testing.T) {
m := MustParseDisplayModel(`<test id="abc" htmlID="def" bool:testt="true" bool:testf="false" reportEvents="click input"> test </test>`)
require.NotNil(t, m)
require.Equal(t, "abc", m.ID)
require.Equal(t, true, m.Attributes["testt"])
require.Equal(t, false, m.Attributes["testf"])
require.Equal(t, false, m.Attributes["testf"])
require.Equal(t, "def", m.Attributes["id"])
require.Equal(t, []ReportEvent{ReportEvent{Name: "click", ExtraValues: []string{}}, ReportEvent{Name: "input", ExtraValues: []string{}}}, m.ReportEvents)
require.Equal(t, 1, len(m.Children))
require.Equal(t, " test ", m.Children[0].Text)
}
func TestParserWithIntermittentTags(t *testing.T) {
m := MustParseDisplayModel(`<test id="abc" htmlID="def" bool:testt="true" bool:testf="false" reportEvents="click input"> test <span>1</span> 2 </test>`)
require.NotNil(t, m)
require.Equal(t, "abc", m.ID)
require.Equal(t, true, m.Attributes["testt"])
require.Equal(t, false, m.Attributes["testf"])
require.Equal(t, false, m.Attributes["testf"])
require.Equal(t, "def", m.Attributes["id"])
require.Equal(t, []ReportEvent{
ReportEvent{
Name: "click",
ExtraValues: []string{},
},
ReportEvent{
Name: "input",
ExtraValues: []string{},
},
}, m.ReportEvents)
require.Equal(t, 3, len(m.Children))
require.Equal(t, " 2 ", m.Children[2].Text)
require.Equal(t, " test ", m.Children[0].Text)
}
func TestParseWhitespace(t *testing.T) {
cases := []struct {
Source string
ExpectedStruct *DisplayModel
}{
{"<x/>",
&DisplayModel{
Element: "x",
Attributes: map[string]interface{}{},
},
},
{"<x></x>",
&DisplayModel{
Element: "x",
Attributes: map[string]interface{}{},
},
},
{"<x> <y/> </x>",
&DisplayModel{
Element: "x",
Attributes: map[string]interface{}{},
Children: []*DisplayModel{
{
Element: "y",
Attributes: map[string]interface{}{},
},
},
},
},
}
for i, c := range cases {
t.Run(fmt.Sprintf("Case %d", i+1), func(t *testing.T) {
require.EqualValues(t, c.ExpectedStruct, MustParseDisplayModel(c.Source))
})
}
}
func TestParserWithReportEvent(t *testing.T) {
m := MustParseDisplayModel(`<test id="abc" htmlID="def" bool:testt="true" bool:testf="false" reportEvents="click:PD input:SP:X-screenX"/>`)
require.NotNil(t, m)
require.Equal(t, []ReportEvent{
ReportEvent{
PreventDefault: true,
Name: "click",
ExtraValues: []string{},
},
ReportEvent{
StopPropagation: true,
Name: "input",
ExtraValues: []string{"screenX"},
},
}, m.ReportEvents)
}