diff options
| author | Michael Williamson <mike@zwobble.org> | 2016-07-03 19:19:21 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2016-07-03 19:20:00 +0100 |
| commit | 0b56dfce42c3944cce47e284608e6806f4c839c3 (patch) | |
| tree | 5f96f9c2908f1c5b00a7192e71d4c192abd7cf47 /tests/xml | |
| parent | 627c4a755395e313234bdb1914d168392006c483 (diff) | |
Move tests to test for easier Mocha usage
Diffstat (limited to 'tests/xml')
| -rw-r--r-- | tests/xml/reader.tests.js | 88 | ||||
| -rw-r--r-- | tests/xml/writer.tests.js | 83 |
2 files changed, 0 insertions, 171 deletions
diff --git a/tests/xml/reader.tests.js b/tests/xml/reader.tests.js deleted file mode 100644 index afc98de..0000000 --- a/tests/xml/reader.tests.js +++ /dev/null @@ -1,88 +0,0 @@ -var assert = require("assert"); - -var xmlreader = require("../../lib/xml/reader"); -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); - }); - }); - - 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); - }); - }); - - test('should read attributes of tags', function() { - return xmlreader.readString('<body name="bob"/>').then(function(result) { - assert.deepEqual({name: "bob"}, result.root.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]); - }); - }); - - test('should read element with children', function() { - return xmlreader.readString("<body><a/><b/></body>").then(function(result) { - var root = result.root; - assert.equal(2, root.children.length); - assert.equal("a", root.children[0].name); - assert.equal("b", root.children[1].name); - }); - }); - - 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"); - }); - }); - - test('mapped namespaces URIs are translated using map', function() { - var namespaceMap = { - "word": "x" - }; - - return xmlreader.readString('<w:body xmlns:w="word"/>', namespaceMap).then(function(result) { - assert.deepEqual(result.root.name, "x:body"); - }); - }); - - test('namespace of attributes is mapped to prefix', function() { - var namespaceMap = { - "word": "x" - }; - 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!"); - }); - }); - - 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"); - 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); - }); - }); - - test('error if XML is badly formed', function() { - return xmlreader.readString("<bo").then(function(result) { - throw new Error("Expected failure"); - }, function(error) { - assert.ok(error); - return 1; - }); - }); -}); diff --git a/tests/xml/writer.tests.js b/tests/xml/writer.tests.js deleted file mode 100644 index d41ddc1..0000000 --- a/tests/xml/writer.tests.js +++ /dev/null @@ -1,83 +0,0 @@ -var assert = require("assert"); - -var xml = require("../../lib/xml"); -var writer = require("../../lib/xml/writer"); -var test = require("../testing").test; - - -describe('xml.writer', function() { - test('writing empty root element writes out xml declaration and empty root element', function() { - assertXmlString(xml.element("root"), {}, '<root/>'); - }); - - test('can write empty child elements', function() { - assertXmlString(xml.element("root", {}, [xml.element("album"), xml.element("single")]), {}, - '<root><album/><single/></root>'); - }); - - test('can write empty descendant elements', function() { - var element = xml.element("root", {}, [ - xml.element("album", {}, [ - xml.element("year"), - xml.element("song") - ]) - ]); - assertXmlString(element, {}, - '<root><album><year/><song/></album></root>'); - }); - - test('can write element attributes', function() { - var element = xml.element("root", {}, [ - xml.element("album", {"title": "Everything in Transit"}) - ]); - assertXmlString(element, {}, - '<root><album title="Everything in Transit"/></root>'); - }); - - test('can write text nodes', function() { - var element = xml.element("root", {}, [ - xml.element("album", {}, [ - xml.text("Everything in Transit") - ]) - ]); - assertXmlString(element, {}, - '<root><album>Everything in Transit</album></root>'); - }); - - test('can write root element with long-form prefix when URI is namespace', function() { - var element = xml.element("{music}root"); - assertXmlString(element, {"m": "music"}, - '<m:root xmlns:m="music"/>'); - }); - - test('can write child elements with long-form prefix when URI is namespace', function() { - var element = xml.element("root", {}, [ - xml.element("{music}album") - ]); - assertXmlString(element, {"m": "music"}, - '<root xmlns:m="music"><m:album/></root>'); - }); - - test('can write child elements with short-form prefix when URI is namespace', function() { - var element = xml.element("root", {}, [ - xml.element("m:album") - ]); - assertXmlString(element, {"m": "music"}, - '<root xmlns:m="music"><m:album/></root>'); - }); - - test('default namespace has key of empty string', function() { - var element = xml.element("root", {}, [ - xml.element("{music}album") - ]); - assertXmlString(element, {"": "music"}, - '<root xmlns="music"><album/></root>'); - }); -}); - - -function assertXmlString(element, namespaces, expectedString) { - assert.equal(writer.writeString(element, namespaces), - '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' + - expectedString); -} |
