forked from w3c/testharness.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testharnessreport.js
380 lines (347 loc) · 13.3 KB
/
testharnessreport.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/*
* This file is intended for vendors to implement
* code needed to integrate testharness.js tests with their own test systems.
*
* The default implementation extracts metadata from the tests and validates
* it against the cached version that should be present in the test source
* file. If the cache is not found or is out of sync, source code suitable for
* caching the metadata is optionally generated.
*
* The cached metadata is present for extraction by test processing tools that
* are unable to execute javascript.
*
* Metadata is attached to tests via the properties parameter in the test
* constructor. See testharness.js for details.
*
* Typically test system integration will attach callbacks when each test has
* run, using add_result_callback(callback(test)), or when the whole test file
* has completed, using
* add_completion_callback(callback(tests, harness_status)).
*
* For more documentation about the callback functions and the
* parameters they are called with see testharness.js
*/
var metadata_generator = {
currentMetadata: {},
cachedMetadata: false,
metadataProperties: ['help', 'assert', 'author'],
error: function(message) {
var messageElement = document.createElement('p');
messageElement.setAttribute('class', 'error');
this.appendText(messageElement, message);
var summary = document.getElementById('summary');
if (summary) {
summary.parentNode.insertBefore(messageElement, summary);
}
else {
document.body.appendChild(messageElement);
}
},
/**
* Ensure property value has contact information
*/
validateContact: function(test, propertyName) {
var result = true;
var value = test.properties[propertyName];
var values = Array.isArray(value) ? value : [value];
for (var index = 0; index < values.length; index++) {
value = values[index];
var re = /(\S+)(\s*)<(.*)>(.*)/;
if (! re.test(value)) {
re = /(\S+)(\s+)(http[s]?:\/\/)(.*)/
if (! re.test(value)) {
this.error('Metadata property "' + propertyName +
'" for test: "' + test.name +
'" must have name and contact information ' +
'("name <email>" or "name http(s)://")');
result = false;
}
}
}
return result;
},
/**
* Extract metadata from test object
*/
extractFromTest: function(test) {
var testMetadata = {};
// filter out metadata from other properties in test
for (var metaIndex = 0; metaIndex < this.metadataProperties.length;
metaIndex++) {
var meta = this.metadataProperties[metaIndex];
if (test.properties.hasOwnProperty(meta)) {
if ('author' == meta) {
this.validateContact(test, meta);
}
testMetadata[meta] = test.properties[meta];
}
}
return testMetadata;
},
/**
* Compare cached metadata to extracted metadata
*/
validateCache: function() {
for (var testName in this.currentMetadata) {
if (! this.cachedMetadata.hasOwnProperty(testName)) {
return false;
}
var testMetadata = this.currentMetadata[testName];
var cachedTestMetadata = this.cachedMetadata[testName];
delete this.cachedMetadata[testName];
for (var metaIndex = 0; metaIndex < this.metadataProperties.length;
metaIndex++) {
var meta = this.metadataProperties[metaIndex];
if (cachedTestMetadata.hasOwnProperty(meta) &&
testMetadata.hasOwnProperty(meta)) {
if (Array.isArray(cachedTestMetadata[meta])) {
if (! Array.isArray(testMetadata[meta])) {
return false;
}
if (cachedTestMetadata[meta].length ==
testMetadata[meta].length) {
for (var index = 0;
index < cachedTestMetadata[meta].length;
index++) {
if (cachedTestMetadata[meta][index] !=
testMetadata[meta][index]) {
return false;
}
}
}
else {
return false;
}
}
else {
if (Array.isArray(testMetadata[meta])) {
return false;
}
if (cachedTestMetadata[meta] != testMetadata[meta]) {
return false;
}
}
}
else if (cachedTestMetadata.hasOwnProperty(meta) ||
testMetadata.hasOwnProperty(meta)) {
return false;
}
}
}
for (var testName in this.cachedMetadata) {
return false;
}
return true;
},
appendText: function(elemement, text) {
elemement.appendChild(document.createTextNode(text));
},
jsonifyArray: function(arrayValue, indent) {
var output = '[';
if (1 == arrayValue.length) {
output += JSON.stringify(arrayValue[0]);
}
else {
for (var index = 0; index < arrayValue.length; index++) {
if (0 < index) {
output += ',\n ' + indent;
}
output += JSON.stringify(arrayValue[index]);
}
}
output += ']';
return output;
},
jsonifyObject: function(objectValue, indent) {
var output = '{';
var count = 0;
for (var property in objectValue) {
++count;
if (Array.isArray(objectValue[property]) ||
('object' == typeof(value))) {
++count;
}
}
if (1 == count) {
for (var property in objectValue) {
output += ' "' + property + '": '
+ JSON.stringify(objectValue[property])
+ ' ';
}
}
else {
var first = true;
for (var property in objectValue) {
if (! first) {
output += ',';
}
first = false;
output += '\n ' + indent + '"' + property + '": ';
var value = objectValue[property];
if (Array.isArray(value)) {
output += this.jsonifyArray(value, indent +
' '.substr(0, 5 + property.length));
}
else if ('object' == typeof(value)) {
output += this.jsonifyObject(value, indent + ' ');
}
else {
output += JSON.stringify(value);
}
}
if (1 < output.length) {
output += '\n' + indent;
}
}
output += '}';
return output;
},
/**
* Generate javascript source code for captured metadata
* Metadata is in pretty-printed JSON format
*/
generateSource: function() {
var source =
'<script id="metadata_cache">/*\n' +
this.jsonifyObject(this.currentMetadata, '') + '\n' +
'*/</script>\n';
return source;
},
/**
* Add element containing metadata source code
*/
addSourceElement: function(event) {
var sourceWrapper = document.createElement('div');
sourceWrapper.setAttribute('id', 'metadata_source');
var instructions = document.createElement('p');
if (this.cachedMetadata) {
this.appendText(instructions,
'Replace the existing <script id="metadata_cache"> element ' +
'in the test\'s <head> with the following:');
}
else {
this.appendText(instructions,
'Copy the following into the <head> element of the test ' +
'or the test\'s metadata sidecar file:');
}
sourceWrapper.appendChild(instructions);
var sourceElement = document.createElement('pre');
this.appendText(sourceElement, this.generateSource());
sourceWrapper.appendChild(sourceElement);
var messageElement = document.getElementById('metadata_issue');
messageElement.parentNode.insertBefore(sourceWrapper,
messageElement.nextSibling);
messageElement.parentNode.removeChild(messageElement);
(event.preventDefault) ? event.preventDefault() :
event.returnValue = false;
},
/**
* Extract the metadata cache from the cache element if present
*/
getCachedMetadata: function() {
var cacheElement = document.getElementById('metadata_cache');
if (cacheElement) {
var cacheText = cacheElement.firstChild.nodeValue;
var openBrace = cacheText.indexOf('{');
var closeBrace = cacheText.lastIndexOf('}');
if ((-1 < openBrace) && (-1 < closeBrace)) {
cacheText = cacheText.slice(openBrace, closeBrace + 1);
try {
this.cachedMetadata = JSON.parse(cacheText);
}
catch (exc) {
this.cachedMetadata = 'Invalid JSON in Cached metadata. ';
}
}
else {
this.cachedMetadata = 'Metadata not found in cache element. ';
}
}
},
/**
* Main entry point, extract metadata from tests, compare to cached version
* if present.
* If cache not present or differs from extrated metadata, generate an error
*/
process: function(tests, harness_status) {
for (var index = 0; index < tests.length; index++) {
var test = tests[index];
if (this.currentMetadata.hasOwnProperty(test.name)) {
this.error('Duplicate test name: ' + test.name);
}
else {
this.currentMetadata[test.name] = this.extractFromTest(test);
}
}
this.getCachedMetadata();
var message = null;
var messageClass = 'warning';
var showSource = false;
if (0 == tests.length) {
if (this.cachedMetadata) {
message = 'Cached metadata present but no tests. ';
}
}
else if (1 == tests.length) {
if (this.cachedMetadata) {
message = 'Single test files should not have cached metadata. ';
}
else {
var testMetadata = this.currentMetadata[tests[0].name];
var hasMetadata = false;
for (var meta in testMetadata) {
hasMetadata |= testMetadata.hasOwnProperty(meta);
}
if (hasMetadata) {
message = 'Single tests should not have metadata. ' +
'Move metadata to <head>. ';
}
}
}
else {
if (this.cachedMetadata) {
messageClass = 'error';
if ('string' == typeof(this.cachedMetadata)) {
message = this.cachedMetadata;
showSource = true;
}
else if (! this.validateCache()) {
message = 'Cached metadata out of sync. ';
showSource = true;
}
}
}
if (message) {
var messageElement = document.createElement('p');
messageElement.setAttribute('id', 'metadata_issue');
messageElement.setAttribute('class', messageClass);
this.appendText(messageElement, message);
if (showSource) {
var link = document.createElement('a');
this.appendText(link, 'Click for source code.');
link.setAttribute('href', '#');
link.setAttribute('onclick',
'metadata_generator.addSourceElement(event)');
messageElement.appendChild(link);
}
var summary = document.getElementById('summary');
if (summary) {
summary.parentNode.insertBefore(messageElement, summary);
}
else {
var log = document.getElementById('log');
if (log) {
log.appendChild(messageElement);
}
}
}
},
setup: function() {
add_completion_callback(
function (tests, harness_status) {
metadata_generator.process(tests, harness_status)
});
}
}
metadata_generator.setup();
// vim: set expandtab shiftwidth=4 tabstop=4: