-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhtml-parser-test.js
More file actions
151 lines (119 loc) · 4.67 KB
/
html-parser-test.js
File metadata and controls
151 lines (119 loc) · 4.67 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
console.log("html-parser-test.js");
(function() {
var testsPassed = 0;
var testsFailed = 0;
function testEqual(title, executor, expectedValue) {
if (!title || !title.length) {
title = "Untitled Test";
}
var result;
try { result = executor(); } catch(e) { result = e; }
if (result !== expectedValue) {
console.warn("Failed Test:", title, "Expected: ", expectedValue, "but got:", result);
testsFailed++;
} else {
//console.log("Passed:", title);
testsPassed++;
}
};
function testThrows(title, executor) {
if (!title || !title.length) {
title = "Untitled Test";
}
var result = null;
try { executor(); } catch(e) { result = e; }
if (result === null) {
console.warn("Failed Test:", title, "Expected a thrown error, but executed without error.");
testsFailed++;
} else {
//console.log("Passed:", title);
testsPassed++;
}
};
// htmlSelfClosingTagName
testEqual(
"htmlSelfClosingTagName doesn't parse non-existent tagname", function() {
return htmlSelfClosingTagName.parse("kumquat"); }, null);
htmlSelfClosingTagNames.forEach(function(tagName) {
testEqual(
"htmlSelfClosingTagName parses " + tagName, function() {
return htmlSelfClosingTagName.parse(tagName); }, tagName);
});
// htmlPairedTagName
testEqual(
"htmlPairedTagName doesn't parse non-existent tagname", function() {
return htmlPairedTagName.parse("kumquat"); }, null);
htmlPairedTagNames.forEach(function(tagName) {
testEqual(
"htmlPairedTagName parses " + tagName, function() {
return htmlPairedTagName.parse(tagName); }, tagName);
});
// htmlAttributeName
testEqual(
"htmlAttributeName parses 'class' attribute correctly", function() {
return htmlAttributeName.parse("class"); }, "class");
testEqual(
"htmlAttributeName doesn't overstep", function() {
return htmlAttributeName.parse("class=\"fancy\""); }, "class");
// htmlAttributeValue
testEqual(
"htmlAttributeValue parses correctly", function() {
return htmlAttributeValue.parse("\"checked\""); }, "\"checked\"");
// htmlAttribute
testEqual(
"htmlAttribute parses correctly", function() {
return htmlAttribute.parse("type=\"radio\""); }, "type=\"radio\"");
// htmlAttributes
testEqual(
"htmlAttributes parses single attribute", function() {
return htmlAttributes.parse(" class=\"disabled\""); }, " class=\"disabled\"");
testEqual(
"htmlAttributes parses multiple attributes", function() {
return htmlAttributes.parse(" type=\"radio\" class=\"disabled\""); }, " type=\"radio\" class=\"disabled\"");
// htmlSelfClosingTag
testEqual(
"htmlSelfClosingTag doesn't parse non-existent tagname", function() {
return htmlSelfClosingTag.parse("<kumquat />"); }, null);
htmlSelfClosingTagNames.forEach(function(tagName) {
testEqual(
"htmlSelfClosingTag parses " + tagName, function() {
return htmlSelfClosingTag.parse("<" + tagName + " />"); }, "<" + tagName + " />");
});
htmlSelfClosingTagNames.forEach(function(tagName) {
testEqual(
"htmlSelfClosingTag parses " + tagName + " without trailing space.", function() {
return htmlSelfClosingTag.parse("<" + tagName + "/>"); }, "<" + tagName + "/>");
});
testEqual(
"htmlSelfClosingTag parses a self-closing tag with an attribute", function() {
return htmlSelfClosingTag.parse("<input class=\"disabled\" />"); }, "<input class=\"disabled\" />");
testEqual(
"htmlSelfClosingTag parses a self-closing start tag with attributes", function() {
return htmlSelfClosingTag.parse("<input class=\"disabled\" type=\"radio\" />"); }, "<input class=\"disabled\" type=\"radio\" />");
// htmlPairedTagStart
testEqual(
"htmlPairedTagStart parses a paired start tag", function() {
return htmlPairedTagStart.parse("<b>"); }, "<b>");
testEqual(
"htmlPairedTagStart parses a paired start tag with an attribute", function() {
return htmlPairedTagStart.parse("<div class=\"disabled\">"); }, "<div class=\"disabled\">");
testEqual(
"htmlPairedTagStart parses a paired start tag with attributes", function() {
return htmlPairedTagStart.parse("<div class=\"disabled\" type=\"radio\">"); }, "<div class=\"disabled\" type=\"radio\">");
testEqual(
"htmlPairedTagStart refuses a self closing tag", function() {
return htmlPairedTagStart.parse("<hr>"); }, null);
// htmlPairedTagEnd
testEqual(
"htmlPairedTagEnd parses a paired end tag", function() {
return htmlPairedTagEnd.parse("</b>"); }, "</b>");
testEqual(
"htmlPairedTagEnd refuses a self closing tag", function() {
return htmlPairedTagEnd.parse("</hr>"); }, null);
console.log("Tests Passed:", testsPassed);
if (testsFailed == 0) {
console.log("Tests Failed:", testsFailed);
} else {
console.warn("Tests Failed:", testsFailed);
}
})();