-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtest-model.js
More file actions
333 lines (295 loc) · 10.3 KB
/
test-model.js
File metadata and controls
333 lines (295 loc) · 10.3 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
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
/*global Backbone, $, $rdf */
// Test model, ID is combination of version, hostLanguage and num
window.Test = Backbone.Model.extend({
// Calls back with retrieved source
source: function (cb) {
var entries = [];
var hostLanguages = this.get('hostLanguages');
var versions = this.get('versions');
var num = this.get('num');
versions.forEach(function (version) {
hostLanguages.forEach(function (lang) {
var suffix = {
xhtml1: "xhtml",
xhtml5: "xhtml",
html4: "html",
html5: "html",
svg: "svg",
xml: "xml"
}[lang];
if (version === 'rdfa1.0' && ['html5', 'xhtml5'].includes(lang)) {
// RDFa 1.0 not defined for HTML5
} else {
entries.push({
num: num,
doc_uri: "test-cases/" + version + "/" + lang + "/" + num + "." + suffix,
version_lang: version + '+' + lang});
}
});
});
cb(entries);
},
// A URL to the test server
url: function (path) {
var u = location.protocol + "//" + location.hostname;
if (!["", "80", "443"].includes(location.port)) { u += ":" + location.port;}
return (u + path);
},
// Get the URL for the test file
testUrl: function () {
var suffix = {
xhtml1: "xhtml",
xhtml5: "xhtml",
html4: "html",
html5: "html",
svg: "svg",
xml: "xml"
}[this.get('hostLanguage')];
// FIXME: Use our base URL.
return '/test-suite/test-cases' +
'/' + this.get('version') +
'/' + this.get('hostLanguage') +
'/' + this.get('num') +
'.' + suffix;
},
// Get the URL for the extracted RDF from the test file
extractedUrl: function () {
return this.processorURL() + this.url(this.testUrl());
},
// Get the URL for the SPARQL file
sparqlUrl: function () {
return '/test-suite/test-cases' +
'/' + this.get('version') +
'/' + this.get('hostLanguage') +
'/' + this.get('num') +
'.sparql';
},
// Get the URL for the reference file
referenceUrl: function () {
return '/test-suite/test-cases' +
'/' + this.get('version') +
'/' + this.get('hostLanguage') +
'/' + this.get('num') +
'.ttl';
},
// Get the details for a given test
details: function (cb) {
// Retrieve results from processor and canonical representation
var testUrl = this.testUrl();
var referenceUrl = this.referenceUrl();
var extractedUrl = this.extractedUrl();
var sparqlUrl = this.sparqlUrl();
var res = {
num: this.get('num'),
purpose: this.get('purpose'),
docUrl: testUrl,
referenceText:"unloaded",
docText: "unloaded",
extractedUrl: extractedUrl,
extractedText:"unloaded",
sparqlUrl: sparqlUrl,
sparqlText: "unloaded"
};
$.ajax({
url: testUrl,
dataType: 'text'
}).then(function(docText) {
res.docText = docText;
return $.ajax({url: referenceUrl, dataType: 'text'});
}).then(function(referenceText) {
res.referenceText = referenceText;
return $.ajax({url: sparqlUrl, dataType: 'text'});
}).then(function(sparqlText) {
res.sparqlText = sparqlText;
return $.ajax({url: extractedUrl, dataType: 'text'});
}).then(function(extractedText) {
res.extractedText = extractedText;
cb(res);
}).fail(function (xhr) {
// Indicate fail and style
res.extractedText = "Failed to load: " + xhr.status + ' ' + xhr.statusText;
cb(res);
});
},
// Run the test, causes this.result to be set
run: function () {
var that = this,
kb = $rdf.graph(),
sparqlUrl = this.sparqlUrl(),
extractedUrl = this.extractedUrl(),
base = this.testURI(),
expectedResults = this.get("expectedResults");
this.set("result", "running");
// Fetch SPARQL
$.ajax({url: extractedUrl, dataType: 'text'})
.always(function (extractedText, status, xhr) {
if (status === "error") {
window.console && window.console.log("Error fetching " + extractedUrl);
that.set("result", "error");
return;
}
var ct = xhr.getResponseHeader("content-type").split(";")[0];
if (xhr.status !== 200) {extractedText = "";}
try {
$rdf.parse(extractedText, kb, base, ct);
} catch (parseErr) {
// Indicate fail and style
window.console && window.console.log("Parse error: " + parseErr);
that.set("result", "error");
return;
}
$.ajax({url: sparqlUrl, dataType: 'text'})
.then(function(sparqlText) {
// Parse SPARQL
var query = $rdf.SPARQLToQuery(sparqlText, true, kb);
var queryResult = "error";
if (query) {
// rdflib.js does not always call the final onDone callback (bug?)
var timeoutFallback = setTimeout(function() {
window.console && window.console.log("timeout waiting for query to call done callback");
that.set('result', queryResult);
}, 8000);
queryResult = expectedResults ? "FAIL" : "PASS";
kb.fetcher = null; // disables resource fetching
kb.query(query, function(result) {
// result is an empty array since ASK queries have no bindings
queryResult = Array.isArray(result) === expectedResults ? "PASS" : "FAIL";
}, null, function () {
clearTimeout(timeoutFallback);
that.set('result', queryResult);
});
} else {
// Indicate fail and style
that.set('result', queryResult);
}
}).fail(function (result) {
// Indicate fail and style
window.console && window.console.log("Error fetching " + sparqlUrl + ": " + result);
that.set("result", "error");
});
});
},
// Return the selected processor URI
// This is set when the hostLanguage is selected on all selected test divs
// The processorURL is updated with query parameters from the test-case object.
// Reset test to original condition
processorURL: function() {
var url = this.get('processorURL');
var queryParam = this.get('queryParam');
if (queryParam) {
// Add any parameter to the processorURL
url = url.replace(/([\?&])([^\?&]*)$/, "$1" + queryParam + "&$2");
}
return url;
},
// Test URI
testURI: function() {
return "http://rdfa.info/test-suite/test-cases" +
'/' + this.get('version') +
'/' + this.get('hostLanguage') +
'/manifest#' + this.get('num');
},
reset: function () {
this.result = this.source = this.details = null;
}
});
// Collection of defined tests, ordered by test number.
// There is a different collection of tests for each combination of version and host language
window.TestCollection = Backbone.Collection.extend({
model: Test,
url: 'manifest.jsonld',
initialize: function(models, options) {
if (options) {
this.version = options.version;
}
// Bind do our own change event to be notified when tests complete to allow them to
// chain to the next
this.bind('change', this.run_next, this);
},
// Update models based on test options
filter: function(version) {
var version = this.version.get('version');
var hostLanguage = this.version.get('hostLanguage');
var processorURL = this.version.get('processorURL');
this.running = null;
this.passed = this.failed = 0;
var filteredTests = _.filter(this.loadedData, function(data) {
return _.include(data.versions, version) &&
_.include(data.hostLanguages, hostLanguage);
});
// Reset the collection with filtered tests
var tests = _.map(filteredTests, function(data) {
// Add selected version, hostLanguage and processorURL to each test
return _.extend({
version: version,
hostLanguage: hostLanguage,
processorURL: processorURL
}, data);
});
this.reset(tests);
return tests;
},
// Run the tests, sequence through each test trigging it to run and signal an update
// event for each test completion.
run: function() {
console.log("start running tests");
this.running = "running";
this.passed = this.failed = 0;
// Reset test results
_.each(this.models, function(test) { test.set('result', null); });
// Run first test
this.models[0].run();
},
// Triggered when a model changes, which causes chaining if we're running tests
run_next: function(test) {
var result = test.get('result');
if (this.running && _.include(["PASS", "FAIL", "error"], result)) {
console.log("test " + test.get('num') + ' completed with ' + result);
this.passed = _.reduce(this.models, function(memo, test) {
return memo + (test.get('result') == "PASS" ? 1 : 0);
}, 0);
this.failed = _.reduce(this.models, function(memo, test) {
return memo + (test.get('result') == "FAIL" ? 1 : 0);
}, 0);
this.errored = _.reduce(this.models, function(memo, test) {
return memo + (test.get('result') == "error" ? 1 : 0);
}, 0);
var total = this.passed + this.failed + this.errored;
var next = this.at(total);
if (next) {
console.log("next test: " + next.get('num'));
next.run();
} else {
console.log('tests completed');
this.running = "completed";
}
}
},
// Override parse() to deal with JSON-LD array semantics
// Return all tests
parse: function(response) {
this.loadedData = response['@graph'];
// Initialize Host Language Version mappings by inspecting each test
var map = [];
_.each(this.loadedData, function(test) {
_.each(test.versions, function(vers) {
if (map[vers] == null) { map[vers] = []; }
_.each(test.hostLanguages, function(hl) {
var hlUpper = hl.toUpperCase();
if (_.indexOf(map[vers], hlUpper) === -1) {
map[vers].push(hlUpper);
}
});
});
});
// Hard-code host languages for RDFa 1.0
map['rdfa1.0'] = ["XHTML1", "XML"];
this.version.set('versionHostLanguageMap', map);
// Don't return anything on parse, that is done through filtering
return this.filter();
},
// Order tests by test number
comparator: function(test) {
return test.get('num');
}
});