-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestAssertion.js
73 lines (67 loc) · 2.26 KB
/
testAssertion.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
// jshint node:true
// jshint shadow:true
module.exports = testAssertion;
function testAssertion(test, selected, select, xmlDoc, resourceDir, xmlSnippetMaxLength) {
var results = [];
for (var i = 0; i < selected.length; i++) {
try {
var result = select('boolean(' + test + ')', selected[i]);
var lineNumber = null;
var xmlSnippet = null;
if (selected[i].lineNumber) {
lineNumber = selected[i].lineNumber;
xmlSnippet = selected[i].toString();
}
var maxLength = (xmlSnippetMaxLength || 1e308);
if (xmlSnippet && xmlSnippet.length > maxLength) {
xmlSnippet = xmlSnippet.slice(0, maxLength) + '...';
}
results.push({ result: result, line: lineNumber, path: getXPath(selected[i]), xml: xmlSnippet });
}
catch (err) {
return { ignored: true, errorMessage: err.message };
}
}
for (var i = 0; i < results.length; i++) {
if (results[i].result !== true && results[i].result !== false) {
return { ignored: true, errorMessage: 'Test returned non-boolean result' };
}
}
return results;
}
function getXPath(node, path) {
var top = !path ? true : false;
path = path || [];
if (node.parentNode) {
path = getXPath(node.parentNode, path);
}
var count = 1;
if (node.previousSibling) {
var sibling = node.previousSibling;
do {
if (sibling.nodeType === 1 && sibling.nodeName === node.nodeName) {
count++;
}
sibling = sibling.previousSibling;
} while (sibling);
if (count === 1) {
count = null;
}
}
else if (node.nextSibling) {
var sibling = node.nextSibling;
do {
if (sibling.nodeType === 1 && sibling.nodeName === node.nodeName) {
var count = 1;
sibling = null;
} else {
var count = null;
sibling = sibling.previousSibling;
}
} while (sibling);
}
if (node.nodeType === 1) {
path.push(node.nodeName + ('[' + (count || 1) + ']'));
}
return top ? '/' + path.join('/') : path;
}