From 504f59e38e57b543ac87f73df53fe3a6ba34e26d Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Fri, 29 Mar 2013 18:57:02 +0000 Subject: Very basic conversion from docx to HTML --- tests/mammoth.tests.js | 38 ++++++++++++++++++++ tests/test-data/simple/word/document.xml | 18 ++++++++++ tests/testing.js | 10 ++++++ tests/xmlreader.tests.js | 59 ++++++++++++++++++++++++++++++++ 4 files changed, 125 insertions(+) create mode 100644 tests/mammoth.tests.js create mode 100644 tests/test-data/simple/word/document.xml create mode 100644 tests/testing.js create mode 100644 tests/xmlreader.tests.js (limited to 'tests') diff --git a/tests/mammoth.tests.js b/tests/mammoth.tests.js new file mode 100644 index 0000000..05ef218 --- /dev/null +++ b/tests/mammoth.tests.js @@ -0,0 +1,38 @@ +var assert = require("assert"); +var path = require("path"); +var fs = require("fs"); +var mammoth = require("../") +var q = require("q"); +var test = require("./testing").test; + + +describe('mammoth', function() { + test('should convert docx containing one one paragraph to single p element', function() { + var docxFile = createFakeDocxFile({ + "word/document.xml": testData("simple/word/document.xml") + }); + return convertToHtml(docxFile).then(function(result) { + assert.equal("

Hello.

", result.html); + }); + }) +}) + +function createFakeDocxFile(files) { + function read(path) { + return files[path]; + } + + return { + read: read + }; +} + +function testData(testDataPath) { + var fullPath = path.join(__dirname, "test-data", testDataPath); + return q.nfcall(fs.readFile, fullPath, "utf-8"); +} + +function convertToHtml(docxFile) { + var converter = new mammoth.Converter(); + return converter.convertToHtml(docxFile); +} diff --git a/tests/test-data/simple/word/document.xml b/tests/test-data/simple/word/document.xml new file mode 100644 index 0000000..1723398 --- /dev/null +++ b/tests/test-data/simple/word/document.xml @@ -0,0 +1,18 @@ + + + + + + Hello. + + + + + + + + + + + + diff --git a/tests/testing.js b/tests/testing.js new file mode 100644 index 0000000..ac413c8 --- /dev/null +++ b/tests/testing.js @@ -0,0 +1,10 @@ +exports.test = test; + +function test(name, func) { + it(name, function(done) { + var result = func(); + result.then(function() { + done() + }).done(); + }); +} diff --git a/tests/xmlreader.tests.js b/tests/xmlreader.tests.js new file mode 100644 index 0000000..ad11e62 --- /dev/null +++ b/tests/xmlreader.tests.js @@ -0,0 +1,59 @@ +var assert = require("assert"); + +var xmlreader = require("../lib/xmlreader"); +var test = require("./testing").test; + + +describe('xmlreader.read', function() { + test('should read self-closing element', function() { + return xmlreader.read("").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.read("").then(function(result) { + assert.deepEqual({type: "element", name: "body", attributes: {}, children: []}, result.root); + }); + }) + + test('should read attributes of tags', function() { + return xmlreader.read('').then(function(result) { + assert.deepEqual({name: "bob"}, result.root.attributes); + }); + }) + + test('can read text element', function() { + return xmlreader.read('Hello!').then(function(result) { + assert.deepEqual({type: "text", value: "Hello!"}, result.root.children[0]); + }); + }) + + test('should read element with children', function() { + return xmlreader.read("").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('should read element with namespace declaration', function() { + return xmlreader.read("").then(function(result) { + assert.deepEqual("w:body", result.root.name); + }); + }) + + test('can find first element with name', function() { + return xmlreader.read('').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.read('\n').then(function(result) { + assert.deepEqual("body", result.root.name); + }); + }) +}); -- cgit v1.2.3