Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
it('skips the subtree for outermost() with tail', () => {
jsonMlMapper.parse(['root', ['foo', ['foo']]], documentNode);
const descendant = documentNode.documentElement.firstChild.firstChild;
// It is successful when the dom facade does not throw
const nodes = evaluateXPathToNodes(
'outermost(tail(descendant::node()))',
documentNode,
buildDomFacade(descendant),
null,
{ language: evaluateXPath.XQUERY_3_1_LANGUAGE }
);
chai.assert.deepEqual(nodes, [documentNode.documentElement.firstChild]);
});
it('skips the subtree for outermost() in debug mode', () => {
jsonMlMapper.parse(['root', ['foo', ['foo']]], documentNode);
const descendant = documentNode.documentElement.firstChild.firstChild;
// It is successful when the dom facade does not throw
const nodes = evaluateXPathToNodes(
'outermost(descendant::foo)',
documentNode,
buildDomFacade(descendant),
null,
{
debug: true
}
);
chai.assert.deepEqual(nodes, [documentNode.documentElement.firstChild]);
});
});
function createEnvironment(cwd, environmentNode) {
const fileName = evaluateXPathToString('source[@role="."]/@file', environmentNode);
const variables = evaluateXPathToNodes('source[@role!="."]', environmentNode).reduce(
(varsByName, variable) =>
Object.assign(varsByName, {
[evaluateXPathToString('@role', variable).substr(1)]: getFile(
(cwd ? cwd + '/' : '') + evaluateXPathToString('@file', variable)
)
}),
{}
);
// Params area also variables. But different
evaluateXPathToNodes('param', environmentNode).forEach(paramNode => {
variables[evaluateXPathToString('@name', paramNode)] = evaluateXPath(
evaluateXPathToString('@select', paramNode)
);
console.log(variables);
});
const namespaces = evaluateXPathToMap(
'(namespace!map:entry(@prefix/string(), @uri/string())) => map:merge()',
environmentNode
);
return {
contextNode: fileName ? getFile((cwd ? cwd + '/' : '') + fileName) : null,
variables,
namespaceResolver: Object.keys(namespaces).length ? prefix => namespaces[prefix] : null
};
createProcessingInstruction: assertNode.ownerDocument.createProcessingInstruction.bind(
assertNode.ownerDocument
),
createTextNode: assertNode.ownerDocument.createTextNode.bind(assertNode.ownerDocument)
};
switch (assertNode.localName) {
case 'all-of': {
const asserts = evaluateXPathToNodes('*', assertNode).map(innerAssertNode =>
createAsserter(baseUrl, innerAssertNode, language)
);
return (xpath, contextNode, variablesInScope, namespaceResolver) =>
asserts.forEach(a => a(xpath, contextNode, variablesInScope, namespaceResolver));
}
case 'any-of': {
const asserts = evaluateXPathToNodes('*', assertNode).map(innerAssertNode =>
createAsserter(baseUrl, innerAssertNode, language)
);
return (xpath, contextNode, variablesInScope, namespaceResolver) => {
const errors = [];
chai.assert(
asserts.some(a => {
try {
a(xpath, contextNode, variablesInScope, namespaceResolver);
} catch (error) {
if (error instanceof TypeError) {
// TypeErrors are always errors
throw error;
}
errors.push(error);
return false;
}
it('works over one empty sequence and a filled one', () => {
chai.assert.deepEqual(evaluateXPathToNodes('() eq (true())'), []);
});
it('supports relative paths', () => {
jsonMlMapper.parse(['someNode', ['someChildNode']], documentNode);
chai.assert.deepEqual(evaluateXPathToNodes('someChildNode', documentNode.documentElement), [
documentNode.documentElement.firstChild
]);
});
function buildTestCases (testGroup) {
evaluateXPathToNodes('test-group | test-case', testGroup).forEach(test => {
switch (test.localName) {
case 'test-group': {
const groupName = evaluateXPathToString('(@name, string(GroupInfo/title), string(GroupInfo/description))[. != ""][1]', test);
describe(groupName, () => buildTestCases(test));
break;
}
case 'test-case': {
const testName = evaluateXPathToString('(@name, description)[. != ""][1]', test);
if (unrunnableTestCasesByName.includes(testName)) {
it.skip(testName);
return;
}
it(testName, async () => await runTestCase(testName, test));
break;
it('Keeps nodes nodes', () =>
chai.assert.deepEqual(evaluateXPathToNodes('.', documentNode, domFacade), [
documentNode
]));
}
});
}
switch (outputFile.compare) {
case 'XML': {
const actual = xdmValue ? xdmValue[0].value : evaluateXPathToFirstNode(...args);
const expected = actual.nodeType === actual.DOCUMENT_NODE ?
parser.parseFromString(expectedString) :
parser.parseFromString(expectedString).documentElement;
catchAssertion(() => assertXml(actual, expected));
break;
}
case 'Fragment': {
const actualNodes = xdmValue ? xdmValue.map(nodeValue => nodeValue.value) : evaluateXPathToNodes(...args);
catchAssertion(() => assertFragment(actualNodes, expectedString));
break;
}
case 'Text': {
if (xdmValue) {
throw new Error('Not yet supported: Updating query with text assertion.');
}
const actual = evaluateXPathToString(...args);
const actualNodes = [new slimdom.Document().createTextNode(actual)];
catchAssertion(() => assertFragment(actualNodes, expectedString));
break;
}
default:
throw new Error('Compare ' + outputFile.compare + ' is not supported.');
chai.assert.throws(() => evaluateXPathToNodes('preceding::*', null), 'XPDY0002');
});