-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathattlist_test.go
122 lines (90 loc) · 2.93 KB
/
attlist_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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package main
import (
"testing"
"github.com/blefort/DTDParser/DTD"
)
// AttrTestResult struct to attributes
// @todo replace by dtd.Attlist
type AttrTestResult struct {
Name string
Type int
Attributes []DTD.Attribute
}
// loadAttlistTests Load attribute tests
func loadAttlistTests(file string) []AttrTestResult {
var tests []AttrTestResult
loadJSON(file, &tests)
return tests
}
// TestParseAttlistBlock Test for the attlist parser
func TestParseAttlistBlock(t *testing.T) {
// - parse the DTD test
// - compare it to data stored in a json file
// - render it in the tmp dir
testAttlistDTD(t, "tests/attlist.dtd", true)
// - load the generated DTD
// - compare it to data stored in a json file
// testAttlistDTD(t, "tmp/attlist.dtd", false)
}
// testAttlistDTD main testing func for attlist
func testAttlistDTD(t *testing.T, path string, recreate bool) {
var tests []AttrTestResult
var dir string
if recreate {
dir = "tmp"
} else {
dir = "tmp2"
}
// New parser
p := newParser(dir)
p.Parse(path)
tests = loadAttlistTests("tests/attlist.json")
if len(p.Collection) != len(tests) {
t.Errorf("Number of elements in the collection (%d) differs from number of tests (%d), please update either your DTD test or the corresponding json file", len(p.Collection), len(tests))
t.SkipNow()
}
for idx, test := range tests {
var ret bool
AttlistBlock := p.Collection[idx].(*DTD.Attlist)
//log.Debugf("Attlist: test: %#v", test)
//log.Debugf("Attlist: parsed: %#v", AttlistBlock)
if len(AttlistBlock.Attributes) == 0 {
t.Errorf("Attlist: Not attribute definition found in '%#v'", AttlistBlock)
}
t.Run("Attlist: Check Attributes Count", checkIntValue(len(AttlistBlock.Attributes), len(test.Attributes), AttlistBlock, test))
for attrID, attr := range AttlistBlock.Attributes {
attrTest := test.Attributes[attrID]
ret = t.Run("Attlist:Attribute:Check name", checkStrValue(attr.Name, attrTest.Name, attr, attrTest))
if !ret {
t.FailNow()
}
ret = ret && t.Run("Attlist:Attribute:Check default value", checkStrValue(attr.Value, attrTest.Value, attr, attrTest))
if !ret {
t.FailNow()
}
ret = ret && t.Run("Attlist:Attribute:Check #REQUIRED", checkBoolValue(attr.Required, attrTest.Required, attr, test))
if !ret {
t.FailNow()
}
ret = ret && t.Run("Attlist:Attribute:Check #IMPLIED", checkBoolValue(attr.Implied, attrTest.Implied, attr, test))
if !ret {
t.FailNow()
}
ret = ret && t.Run("Attlist:Attribute:Check #FIXED", checkBoolValue(attr.Fixed, attrTest.Fixed, attr, test))
if !ret {
t.FailNow()
}
if !ret {
t.FailNow()
}
}
t.Run("Attlist: Check name", checkStrValue(AttlistBlock.GetName(), test.Name, test, AttlistBlock))
}
t.Run("Render DTD", render(p))
}
// AttlistExported() Helper to test DTD.Attlist.GetExported()
func AttlistExported() {
var a DTD.Attlist
ret := a.GetExported()
log.Debugf("AttlistExported( return %t", ret)
}