Skip to content

Commit

Permalink
test: test the handling of end of line characters
Browse files Browse the repository at this point in the history
  • Loading branch information
lddubeau committed Sep 12, 2019
1 parent d3f196c commit bc212a9
Showing 1 changed file with 54 additions and 14 deletions.
68 changes: 54 additions & 14 deletions test/parser-position.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,24 @@ const { expect } = require("chai");
const saxes = require("../lib/saxes");
const { test } = require(".");

function testPosition(name, chunks, expectedEvents) {
function testPosition(name, chunks, expectedEvents, options) {
it(name, () => {
const parser = new saxes.SaxesParser();
const parser = new saxes.SaxesParser(options);
let expectedIx = 0;
for (const ev of saxes.EVENTS) {
// eslint-disable-next-line no-loop-func
parser[`on${ev}`] = () => {
const expectation = expectedEvents[expectedIx++];
expect(expectation[0]).to.equal(ev);
parser[`on${ev}`] = (thing) => {
const [expectedEvent, expectedPosition, expectedText] =
expectedEvents[expectedIx++];
expect(expectedEvent).to.equal(ev);
// eslint-disable-next-line guard-for-in
for (const prop in expectation[1]) {
expect(parser[prop]).to.deep.equal(expectation[1][prop]);
for (const prop in expectedPosition) {
expect(parser[prop], `bad ${prop}`)
.to.deep.equal(expectedPosition[prop]);
}

if (expectedEvent === "text" && expectedText !== undefined) {
expect(thing).to.equal(expectedText);
}
};
}
Expand Down Expand Up @@ -45,18 +51,52 @@ describe("parser position", () => {
["closetag", { position: 19 }],
]);

testPosition(
"with various newlines",
["<div>abcde\r\n<foo/>f\rgh</div>"], [
const newlines = "<div>abcde\r\n<foo/>f\rgh\r\ri\u0085j\u2028k</div>";
const trailingCr = [];
for (const x of newlines.split("\r")) {
trailingCr.push(x, "\r");
}
const oneByOne = newlines.split("");

describe("XML 1.0", () => {
const expected = [
["opentagstart", { position: 5, line: 1, column: 5 }],
["opentag", { position: 5, line: 1, column: 5 }],
["text", { position: 17, line: 2, column: 5 }],
["text", { position: 17, line: 2, column: 5 }, "abcde\n"],
["opentagstart", { position: 17, line: 2, column: 5 }],
["opentag", { position: 18, line: 2, column: 6 }],
["closetag", { position: 18, line: 2, column: 6 }],
["text", { position: 28, line: 3, column: 8 }],
["closetag", { position: 28, line: 3, column: 8 }],
]);
["text", { position: 35, line: 5, column: 11 }, "f\ngh\n\ni\u0085j\u2028k"],
["closetag", { position: 35, line: 5, column: 11 }],
];

testPosition("with various newlines", [newlines], expected);
testPosition("with various newlines (one-by-one)", oneByOne, expected);
testPosition("trailing CR", trailingCr, expected);
});

describe("XML 1.1", () => {
const expected = [
["opentagstart", { position: 5, line: 1, column: 5 }],
["opentag", { position: 5, line: 1, column: 5 }],
["text", { position: 17, line: 2, column: 5 }, "abcde\n"],
["opentagstart", { position: 17, line: 2, column: 5 }],
["opentag", { position: 18, line: 2, column: 6 }],
["closetag", { position: 18, line: 2, column: 6 }],
["text", { position: 35, line: 7, column: 7 }, "f\ngh\n\ni\nj\nk"],
["closetag", { position: 35, line: 7, column: 7 }],
];

testPosition("with various newlines", [newlines], expected, {
defaultXMLVersion: "1.1",
});
testPosition("with various newlines (one-by-one)", oneByOne, expected, {
defaultXMLVersion: "1.1",
});
testPosition("trailing CR", trailingCr, expected, {
defaultXMLVersion: "1.1",
});
});

test({
name: "pi before root",
Expand Down

0 comments on commit bc212a9

Please sign in to comment.