aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2016-07-03 19:57:37 +0100
committerMichael Williamson <mike@zwobble.org>2016-07-03 19:57:37 +0100
commitc225bc3f3dbe5c001e8fec119d8f8dadf96ec95b (patch)
tree22e6da4fb93dae0cdbfa99cb6a6a508116a1b5ee /test
parentba731d3c8a9408b9d33deb63e0492b54298169c5 (diff)
Return root element directly when reading XML
Diffstat (limited to 'test')
-rw-r--r--test/document-to-html.tests.js3
-rw-r--r--test/docx/comments-reader.tests.js52
-rw-r--r--test/docx/content-types-reader.tests.js24
-rw-r--r--test/docx/notes-reader.tests.js12
-rw-r--r--test/docx/numbering-xml.tests.js6
-rw-r--r--test/docx/office-xml-reader.tests.js2
-rw-r--r--test/docx/styles-reader.tests.js42
-rw-r--r--test/xml/reader.tests.js21
8 files changed, 75 insertions, 87 deletions
diff --git a/test/document-to-html.tests.js b/test/document-to-html.tests.js
index 10ea265..0b4f905 100644
--- a/test/document-to-html.tests.js
+++ b/test/document-to-html.tests.js
@@ -513,8 +513,7 @@ describe('DocumentConverter', function() {
.then(function(result) {
return xml.readString(result.value);
})
- .then(function(htmlImageElementDocument) {
- var htmlImageElement = htmlImageElementDocument.root;
+ .then(function(htmlImageElement) {
assert.equal(htmlImageElement.attributes.alt, "It's a hat");
});
});
diff --git a/test/docx/comments-reader.tests.js b/test/docx/comments-reader.tests.js
index 2902f29..4bb0e9b 100644
--- a/test/docx/comments-reader.tests.js
+++ b/test/docx/comments-reader.tests.js
@@ -7,30 +7,29 @@ var xml = require("../../lib/xml");
var test = require("../test")(module);
-var bodyReader = new BodyReader({});
+function readComment(element) {
+ var bodyReader = new BodyReader({});
+ var commentsReader = createCommentsReader(bodyReader);
+ var comments = commentsReader(element);
+ assert.equal(comments.value.length, 1);
+ return comments.value[0];
+}
test('ID and body of comment are read', function() {
var body = [xml.element("w:p")];
- var comments = createCommentsReader(bodyReader)({
- root: xml.element("w:comments", {}, [
- xml.element("w:comment", {"w:id": "1"}, body)
- ])
- });
- assert.equal(comments.value.length, 1);
- assert.deepEqual(comments.value[0].body, [new documents.Paragraph([])]);
- assert.deepEqual(comments.value[0].commentId, "1");
+ var comment = readComment(xml.element("w:comments", {}, [
+ xml.element("w:comment", {"w:id": "1"}, body)
+ ]));
+ assert.deepEqual(comment.body, [new documents.Paragraph([])]);
+ assert.deepEqual(comment.commentId, "1");
});
test('when optional attributes of comment are missing then they are read as null', function() {
var body = [xml.element("w:p")];
- var comments = createCommentsReader(bodyReader)({
- root: xml.element("w:comments", {}, [
- xml.element("w:comment", {"w:id": "1"}, body)
- ])
- });
- assert.equal(comments.value.length, 1);
- var comment = comments.value[0];
+ var comment = readComment(xml.element("w:comments", {}, [
+ xml.element("w:comment", {"w:id": "1"})
+ ]));
assert.strictEqual(comment.authorName, null);
assert.strictEqual(comment.authorInitials, null);
});
@@ -38,27 +37,18 @@ test('when optional attributes of comment are missing then they are read as null
test('when optional attributes of comment are blank then they are read as null', function() {
var body = [xml.element("w:p")];
- var comments = createCommentsReader(bodyReader)({
- root: xml.element("w:comments", {}, [
- xml.element("w:comment", {"w:id": "1", "w:author": " ", "w:initials": " "}, body)
- ])
- });
- assert.equal(comments.value.length, 1);
- var comment = comments.value[0];
+ var comment = readComment(xml.element("w:comments", {}, [
+ xml.element("w:comment", {"w:id": "1", "w:author": " ", "w:initials": " "})
+ ]));
assert.strictEqual(comment.authorName, null);
assert.strictEqual(comment.authorInitials, null);
});
test('when optional attributes of comment are not blank then they are read', function() {
- var body = [xml.element("w:p")];
- var comments = createCommentsReader(bodyReader)({
- root: xml.element("w:comments", {}, [
- xml.element("w:comment", {"w:id": "1", "w:author": "The Piemaker", "w:initials": "TP"}, body)
- ])
- });
- assert.equal(comments.value.length, 1);
- var comment = comments.value[0];
+ var comment = readComment(xml.element("w:comments", {}, [
+ xml.element("w:comment", {"w:id": "1", "w:author": "The Piemaker", "w:initials": "TP"})
+ ]));
assert.strictEqual(comment.authorName, "The Piemaker");
assert.strictEqual(comment.authorInitials, "TP");
});
diff --git a/test/docx/content-types-reader.tests.js b/test/docx/content-types-reader.tests.js
index a223d09..5ea93ec 100644
--- a/test/docx/content-types-reader.tests.js
+++ b/test/docx/content-types-reader.tests.js
@@ -7,28 +7,28 @@ var test = require("../testing").test;
describe('readContentTypesFromXml', function() {
test('reads default-per-extension from XML', function() {
- var contentTypes = readContentTypesFromXml({
- root: new XmlElement("content-types:Types", {}, [
+ var contentTypes = readContentTypesFromXml(
+ new XmlElement("content-types:Types", {}, [
new XmlElement("content-types:Default", {Extension: "png", ContentType: "image/png"})
])
- });
+ );
assert.equal(contentTypes.findContentType("word/media/hat.png"), "image/png");
});
test('reads overrides in preference to defaults', function() {
- var contentTypes = readContentTypesFromXml({
- root: new XmlElement("content-types:Types", {}, [
+ var contentTypes = readContentTypesFromXml(
+ new XmlElement("content-types:Types", {}, [
new XmlElement("content-types:Default", {Extension: "png", ContentType: "image/png"}),
new XmlElement("content-types:Override", {PartName: "/word/media/hat.png", ContentType: "image/hat"})
])
- });
+ );
assert.equal(contentTypes.findContentType("word/media/hat.png"), "image/hat");
});
test('fallback content types have common image types', function() {
- var contentTypes = readContentTypesFromXml({
- root: new XmlElement("content-types:Types", {}, [])
- });
+ var contentTypes = readContentTypesFromXml(
+ new XmlElement("content-types:Types", {}, [])
+ );
assert.equal(contentTypes.findContentType("word/media/hat.png"), "image/png");
assert.equal(contentTypes.findContentType("word/media/hat.gif"), "image/gif");
assert.equal(contentTypes.findContentType("word/media/hat.jpg"), "image/jpeg");
@@ -39,9 +39,9 @@ describe('readContentTypesFromXml', function() {
});
test('fallback content types are case insensitive on extension', function() {
- var contentTypes = readContentTypesFromXml({
- root: new XmlElement("content-types:Types", {}, [])
- });
+ var contentTypes = readContentTypesFromXml(
+ new XmlElement("content-types:Types", {}, [])
+ );
assert.equal(contentTypes.findContentType("word/media/hat.PnG"), "image/png");
});
});
diff --git a/test/docx/notes-reader.tests.js b/test/docx/notes-reader.tests.js
index 79ffed3..85a49f5 100644
--- a/test/docx/notes-reader.tests.js
+++ b/test/docx/notes-reader.tests.js
@@ -11,11 +11,11 @@ describe('readFootnotesXml', function() {
test('ID and body of footnote are read', function() {
var bodyReader = new BodyReader({});
var footnoteBody = [new XmlElement("w:p", {}, [])];
- var footnotes = createFootnotesReader(bodyReader)({
- root: new XmlElement("w:footnotes", {}, [
+ var footnotes = createFootnotesReader(bodyReader)(
+ new XmlElement("w:footnotes", {}, [
new XmlElement("w:footnote", {"w:id": "1"}, footnoteBody)
])
- });
+ );
assert.equal(footnotes.value.length, 1);
assert.deepEqual(footnotes.value[0].body, [new documents.Paragraph([])]);
assert.deepEqual(footnotes.value[0].noteId, "1");
@@ -28,11 +28,11 @@ footnoteTypeIsIgnored('separator');
function footnoteTypeIsIgnored(type) {
describe('footnotes of type ' + type + ' are ignored', function() {
test('ID and body of footnote are read', function() {
- var footnotes = createFootnotesReader()({
- root: new XmlElement("w:footnotes", {}, [
+ var footnotes = createFootnotesReader()(
+ new XmlElement("w:footnotes", {}, [
new XmlElement("w:footnote", {"w:id": "1", "w:type": type}, [])
])
- });
+ );
assert.equal(footnotes.value.length, 0);
});
});
diff --git a/test/docx/numbering-xml.tests.js b/test/docx/numbering-xml.tests.js
index 2fea5fb..fb28cb8 100644
--- a/test/docx/numbering-xml.tests.js
+++ b/test/docx/numbering-xml.tests.js
@@ -7,8 +7,8 @@ var test = require("../testing").test;
describe('readNumberingXml', function() {
test('w:num element inherits levels from w:abstractNum', function() {
- var numbering = readNumberingXml({
- root: new XmlElement("w:numbering", {}, [
+ var numbering = readNumberingXml(
+ new XmlElement("w:numbering", {}, [
new XmlElement("w:abstractNum", {"w:abstractNumId": "42"}, [
new XmlElement("w:lvl", {"w:ilvl": "0"}, [
new XmlElement("w:numFmt", {"w:val": "bullet"})
@@ -21,7 +21,7 @@ describe('readNumberingXml', function() {
new XmlElement("w:abstractNumId", {"w:val": "42"})
])
])
- });
+ );
duck.assertThat(numbering.findLevel("47", "0"), duck.hasProperties({
isOrdered: false
}));
diff --git a/test/docx/office-xml-reader.tests.js b/test/docx/office-xml-reader.tests.js
index 9d202ba..f28c4f3 100644
--- a/test/docx/office-xml-reader.tests.js
+++ b/test/docx/office-xml-reader.tests.js
@@ -20,7 +20,7 @@ describe("office-xml-reader", function() {
'</mc:AlternateContent>' +
'</numbering>';
return officeXmlReader.read(xmlString).then(function(element) {
- assert.deepEqual(element.root.children, [xml.element("fallback")]);
+ assert.deepEqual(element.children, [xml.element("fallback")]);
});
});
});
diff --git a/test/docx/styles-reader.tests.js b/test/docx/styles-reader.tests.js
index 4ff19c7..8de53ef 100644
--- a/test/docx/styles-reader.tests.js
+++ b/test/docx/styles-reader.tests.js
@@ -7,66 +7,66 @@ var test = require("../testing").test;
describe('readStylesXml', function() {
test('paragraph style is null if no style with that ID exists', function() {
- var styles = readStylesXml({
- root: new XmlElement("w:styles", {}, [])
- });
+ var styles = readStylesXml(
+ new XmlElement("w:styles", {}, [])
+ );
assert.equal(styles.findParagraphStyleById("Heading1"), null);
});
test('paragraph style can be found by ID', function() {
- var styles = readStylesXml({
- root: new XmlElement("w:styles", {}, [
+ var styles = readStylesXml(
+ new XmlElement("w:styles", {}, [
paragraphStyleElement("Heading1", "Heading 1")
])
- });
+ );
assert.equal(styles.findParagraphStyleById("Heading1").styleId, "Heading1");
});
test('character style can be found by ID', function() {
- var styles = readStylesXml({
- root: new XmlElement("w:styles", {}, [
+ var styles = readStylesXml(
+ new XmlElement("w:styles", {}, [
characterStyleElement("Heading1Char", "Heading 1 Char")
])
- });
+ );
assert.equal(styles.findCharacterStyleById("Heading1Char").styleId, "Heading1Char");
});
test('paragraph and character styles are distinct', function() {
- var styles = readStylesXml({
- root: new XmlElement("w:styles", {}, [
+ var styles = readStylesXml(
+ new XmlElement("w:styles", {}, [
paragraphStyleElement("Heading1", "Heading 1"),
characterStyleElement("Heading1Char", "Heading 1 Char")
])
- });
+ );
assert.equal(styles.findCharacterStyleById("Heading1"), null);
assert.equal(styles.findParagraphStyleById("Heading1Char"), null);
});
test('character and table styles are distinct', function() {
- var styles = readStylesXml({
- root: new XmlElement("w:styles", {}, [
+ var styles = readStylesXml(
+ new XmlElement("w:styles", {}, [
styleElement("table", "Heading1", "Heading 1")
])
- });
+ );
assert.equal(styles.findCharacterStyleById("Heading1"), null);
});
test('styles include names', function() {
- var styles = readStylesXml({
- root: new XmlElement("w:styles", {}, [
+ var styles = readStylesXml(
+ new XmlElement("w:styles", {}, [
paragraphStyleElement("Heading1", "Heading 1")
])
- });
+ );
assert.equal(styles.findParagraphStyleById("Heading1").name, "Heading 1");
});
test('style name is null if w:name element does not exist', function() {
- var styles = readStylesXml({
- root: new XmlElement("w:styles", {}, [
+ var styles = readStylesXml(
+ new XmlElement("w:styles", {}, [
styleWithoutWNameElement("paragraph", "Heading1"),
styleWithoutWNameElement("character", "Heading1Char")
])
- });
+ );
assert.equal(styles.findParagraphStyleById("Heading1").name, null);
assert.equal(styles.findCharacterStyleById("Heading1Char").name, null);
});
diff --git a/test/xml/reader.tests.js b/test/xml/reader.tests.js
index afc98de..005753c 100644
--- a/test/xml/reader.tests.js
+++ b/test/xml/reader.tests.js
@@ -7,31 +7,30 @@ var test = require("../testing").test;
describe('xmlreader.readString', function() {
test('should read self-closing element', function() {
return xmlreader.readString("<body/>").then(function(result) {
- assert.deepEqual({type: "element", name: "body", attributes: {}, children: []}, result.root);
+ assert.deepEqual({type: "element", name: "body", attributes: {}, children: []}, result);
});
});
test('should read empty element with separate closing tag', function() {
return xmlreader.readString("<body></body>").then(function(result) {
- assert.deepEqual({type: "element", name: "body", attributes: {}, children: []}, result.root);
+ assert.deepEqual({type: "element", name: "body", attributes: {}, children: []}, result);
});
});
test('should read attributes of tags', function() {
return xmlreader.readString('<body name="bob"/>').then(function(result) {
- assert.deepEqual({name: "bob"}, result.root.attributes);
+ assert.deepEqual({name: "bob"}, result.attributes);
});
});
test('can read text element', function() {
return xmlreader.readString('<body>Hello!</body>').then(function(result) {
- assert.deepEqual({type: "text", value: "Hello!"}, result.root.children[0]);
+ assert.deepEqual({type: "text", value: "Hello!"}, result.children[0]);
});
});
test('should read element with children', function() {
- return xmlreader.readString("<body><a/><b/></body>").then(function(result) {
- var root = result.root;
+ return xmlreader.readString("<body><a/><b/></body>").then(function(root) {
assert.equal(2, root.children.length);
assert.equal("a", root.children[0].name);
assert.equal("b", root.children[1].name);
@@ -40,7 +39,7 @@ describe('xmlreader.readString', function() {
test('unmapped namespaces URIs are included in braces as prefix', function() {
return xmlreader.readString('<w:body xmlns:w="word"/>').then(function(result) {
- assert.deepEqual(result.root.name, "{word}body");
+ assert.deepEqual(result.name, "{word}body");
});
});
@@ -50,7 +49,7 @@ describe('xmlreader.readString', function() {
};
return xmlreader.readString('<w:body xmlns:w="word"/>', namespaceMap).then(function(result) {
- assert.deepEqual(result.root.name, "x:body");
+ assert.deepEqual(result.name, "x:body");
});
});
@@ -60,20 +59,20 @@ describe('xmlreader.readString', function() {
};
var xmlString = '<w:body xmlns:w="word" w:val="Hello!"/>';
return xmlreader.readString(xmlString, namespaceMap).then(function(result) {
- assert.deepEqual(result.root.attributes["x:val"], "Hello!");
+ assert.deepEqual(result.attributes["x:val"], "Hello!");
});
});
test('can find first element with name', function() {
return xmlreader.readString('<body><a/><b index="1"/><b index="2"/></body>').then(function(result) {
- var first = result.root.first("b");
+ var first = result.first("b");
assert.equal("1", first.attributes.index);
});
});
test('whitespace between xml declaration and root tag is ignored', function() {
return xmlreader.readString('<?xml version="1.0" ?>\n<body/>').then(function(result) {
- assert.deepEqual("body", result.root.name);
+ assert.deepEqual("body", result.name);
});
});