/
DOMTestCase.js
282 lines (248 loc) · 7.57 KB
/
DOMTestCase.js
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
Copyright (c) 2001-2004 World Wide Web Consortium,
(Massachusetts Institute of Technology, Institut National de
Recherche en Informatique et en Automatique, Keio University). All
Rights Reserved. This program is distributed under the W3C's Software
Intellectual Property License. This program is distributed in the
hope that it will be useful, but WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.
See W3C License http://www.w3.org/Consortium/Legal/ for more details.
*/
exports.assertSize = function(descr, expected, actual) {
var actualSize;
assertNotNull(descr, actual);
actualSize = actual.length;
assertEquals(descr, expected, actualSize);
}
exports.assertEqualsAutoCase = function(context, descr, expected, actual) {
if (builder.contentType == "text/html") {
if(context == "attribute") {
assertEquals(descr, expected.toLowerCase(), actual.toLowerCase());
} else {
assertEquals(descr, expected.toUpperCase(), actual);
}
} else {
assertEquals(descr, expected, actual);
}
}
exports.assertEqualsCollectionAutoCase = function(context, descr, expected, actual) {
//
// if they aren't the same size, they aren't equal
assertEquals(descr, expected.length, actual.length);
//
// if there length is the same, then every entry in the expected list
// must appear once and only once in the actual list
var expectedLen = expected.length;
var expectedValue;
var actualLen = actual.length;
var i;
var j;
var matches;
for(i = 0; i < expectedLen; i++) {
matches = 0;
expectedValue = expected[i];
for(j = 0; j < actualLen; j++) {
if (builder.contentType == "text/html") {
if (context == "attribute") {
if (expectedValue.toLowerCase() == actual[j].toLowerCase()) {
matches++;
}
} else {
if (expectedValue.toUpperCase() == actual[j]) {
matches++;
}
}
} else {
if(expectedValue == actual[j]) {
matches++;
}
}
}
if(matches == 0) {
throw new MjsUnitAssertionError(descr + ": No match found for " + expectedValue);
}
if(matches > 1) {
throw new MjsUnitAssertionError(descr + ": Multiple matches found for " + expectedValue);
}
}
}
exports.assertEqualsCollection = function(descr, expected, actual) {
//
// if they aren't the same size, they aren't equal
assertEquals(descr, expected.length, actual.length);
//
// if there length is the same, then every entry in the expected list
// must appear once and only once in the actual list
var expectedLen = expected.length;
var expectedValue;
var actualLen = actual.length;
var i;
var j;
var matches;
for(i = 0; i < expectedLen; i++) {
matches = 0;
expectedValue = expected[i];
for(j = 0; j < actualLen; j++) {
if(expectedValue == actual[j]) {
matches++;
}
}
if(matches == 0) {
throw new MjsUnitAssertionError(descr + ": No match found for " + expectedValue);
}
if(matches > 1) {
throw new MjsUnitAssertionError(descr + ": Multiple matches found for " + expectedValue);
}
}
}
exports.assertEqualsListAutoCase = function(context, descr, expected, actual) {
var minLength = expected.length;
if (actual.length < minLength) {
minLength = actual.length;
}
//
for(var i = 0; i < minLength; i++) {
assertEqualsAutoCase(context, descr, expected[i], actual[i]);
}
//
// if they aren't the same size, they aren't equal
assertEquals(descr, expected.length, actual.length);
}
exports.assertEqualsList = function(descr, expected, actual) {
var minLength = expected.length;
if (actual.length < minLength) {
minLength = actual.length;
}
//
for(var i = 0; i < minLength; i++) {
if(expected[i] != actual[i]) {
assertEquals(descr, expected[i], actual[i]);
}
}
//
// if they aren't the same size, they aren't equal
assertEquals(descr, expected.length, actual.length);
}
exports.assertInstanceOf = function(descr, type, obj) {
if(type == "Attr") {
assertEquals(descr,2,obj.nodeType);
var specd = obj.specified;
}
}
exports.assertSame = function(descr, expected, actual) {
if(expected != actual) {
assertEquals(descr, expected.nodeType, actual.nodeType);
assertEquals(descr, expected.nodeValue, actual.nodeValue);
}
}
exports.assertURIEquals = function(assertID, scheme, path, host, file, name, query, fragment, isAbsolute, actual) {
//
// URI must be non-null
assertNotNull(assertID, actual);
var uri = actual;
var lastPound = actual.lastIndexOf("#");
var actualFragment = "";
if(lastPound != -1) {
//
// substring before pound
//
uri = actual.substring(0,lastPound);
actualFragment = actual.substring(lastPound+1);
}
if(fragment != null) assertEquals(assertID,fragment, actualFragment);
var lastQuestion = uri.lastIndexOf("?");
var actualQuery = "";
if(lastQuestion != -1) {
//
// substring before pound
//
uri = actual.substring(0,lastQuestion);
actualQuery = actual.substring(lastQuestion+1);
}
if(query != null) assertEquals(assertID, query, actualQuery);
var firstColon = uri.indexOf(":");
var firstSlash = uri.indexOf("/");
var actualPath = uri;
var actualScheme = "";
if(firstColon != -1 && firstColon < firstSlash) {
actualScheme = uri.substring(0,firstColon);
actualPath = uri.substring(firstColon + 1);
}
if(scheme != null) {
assertEquals(assertID, scheme, actualScheme);
}
if(path != null) {
assertEquals(assertID, path, actualPath);
}
if(host != null) {
var actualHost = "";
if(actualPath.substring(0,2) == "//") {
var termSlash = actualPath.substring(2).indexOf("/") + 2;
actualHost = actualPath.substring(0,termSlash);
}
assertEquals(assertID, host, actualHost);
}
if(file != null || name != null) {
var actualFile = actualPath;
var finalSlash = actualPath.lastIndexOf("/");
if(finalSlash != -1) {
actualFile = actualPath.substring(finalSlash+1);
}
if (file != null) {
assertEquals(assertID, file, actualFile);
}
if (name != null) {
var actualName = actualFile;
var finalDot = actualFile.lastIndexOf(".");
if (finalDot != -1) {
actualName = actualName.substring(0, finalDot);
}
assertEquals(assertID, name, actualName);
}
}
if(isAbsolute != null) {
assertEquals(assertID, isAbsolute, actualPath.substring(0,1) == "/");
}
}
// size() used by assertSize element
function size(collection)
{
return collection.length;
}
function same(expected, actual)
{
return expected === actual;
}
function getSuffix(contentType) {
switch(contentType) {
case "text/html":
return ".html";
case "text/xml":
return ".xml";
case "application/xhtml+xml":
return ".xhtml";
case "image/svg+xml":
return ".svg";
case "text/mathml":
return ".mml";
}
return ".html";
}
exports.toLowerArray = function(src) {
var newArray = new Array();
var i;
for (i = 0; i < src.length; i++) {
newArray[i] = src[i].toLowerCase();
}
return newArray;
}
exports.equalsAutoCase = function(context, expected, actual) {
if (builder.contentType == "text/html") {
if (context == "attribute") {
return expected.toLowerCase() == actual;
}
return expected.toUpperCase() == actual;
}
return expected == actual;
}