From 3a875e79c03731cb761e040e0647348a8df0a67f Mon Sep 17 00:00:00 2001 From: Craig Leinoff Date: Tue, 7 Jan 2014 11:51:59 -0500 Subject: Adding test for style applicator. --- tests/style-applicator.tests.js | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 tests/style-applicator.tests.js (limited to 'tests') diff --git a/tests/style-applicator.tests.js b/tests/style-applicator.tests.js new file mode 100644 index 0000000..26e00e0 --- /dev/null +++ b/tests/style-applicator.tests.js @@ -0,0 +1,39 @@ +var assert = require("assert"); + +var StyleApplicator = require("../lib/style-applicator").StyleApplicator; +var documents = require("../lib/documents"); + +var testing = require("./testing"); +var test = testing.test; +var testData = testing.testData; + + +describe("style-applicator", function() { + test("can process styleless document with single, center-aligned paragraph", function() { + var testOptions = { + styleMap: [], + styleApplicator: { + alignment: true + } + }; + var testDocument = documents.Document([ + documents.Paragraph([ + documents.Run([ + documents.Text("Test document with centered paragraph.") + ]) + ], {alignment: 'center'}) + ]); + var expectedDocument = documents.Document([ + documents.Paragraph([ + documents.Run([ + documents.Text("Test document with centered paragraph.") + ]) + ], {styleName: 'centerAligned', alignment: 'center'}) + ]); + + var styleApplicator = new StyleApplicator(testOptions); + return styleApplicator.processDocumentStyles(testDocument).then(function(result) { + assert.deepEqual(expectedDocument, result); + }); + }); +}); \ No newline at end of file -- cgit v1.2.3 From 51c625fb8474bcf790a0341ae5bbabb1316a2583 Mon Sep 17 00:00:00 2001 From: Craig Leinoff Date: Tue, 7 Jan 2014 14:31:41 -0500 Subject: Fixing unit tests for style application. --- tests/style-applicator.tests.js | 46 ++++++++++++++++++++++++++++------------- 1 file changed, 32 insertions(+), 14 deletions(-) (limited to 'tests') diff --git a/tests/style-applicator.tests.js b/tests/style-applicator.tests.js index 26e00e0..3164fa2 100644 --- a/tests/style-applicator.tests.js +++ b/tests/style-applicator.tests.js @@ -1,39 +1,57 @@ var assert = require("assert"); -var StyleApplicator = require("../lib/style-applicator").StyleApplicator; var documents = require("../lib/documents"); var testing = require("./testing"); var test = testing.test; -var testData = testing.testData; +var style = require("../lib/style-reader").readStyle; describe("style-applicator", function() { - test("can process styleless document with single, center-aligned paragraph", function() { + test("can use custom documentTransform to process styleless center-aligned paragraph into h2.centerAlign", function() { var testOptions = { - styleMap: [], - styleApplicator: { - alignment: true - } + styleMap: [ + style("p.centerAligned => h2.sectionHeader:fresh") + ], + transformDocument: transformDocument }; var testDocument = documents.Document([ documents.Paragraph([ documents.Run([ - documents.Text("Test document with centered paragraph.") + documents.Text("Test.") ]) ], {alignment: 'center'}) ]); var expectedDocument = documents.Document([ documents.Paragraph([ documents.Run([ - documents.Text("Test document with centered paragraph.") + documents.Text("Test.") ]) ], {styleName: 'centerAligned', alignment: 'center'}) ]); - var styleApplicator = new StyleApplicator(testOptions); - return styleApplicator.processDocumentStyles(testDocument).then(function(result) { - assert.deepEqual(expectedDocument, result); - }); + assert.deepEqual(expectedDocument, transformDocument(testDocument)); }); -}); \ No newline at end of file +}); + +function transformDocument(document) { + if (document.children) { + for (var i = 0; i < document.children.length; i++) { + var element = document.children[i]; + switch (element.type) { + case 'paragraph': + document.children[i] = processParagraphStyles(document.children[i]); + break; + } + } + } + return document; +} +function processParagraphStyles(element) { + if (!element.styleName) { + if (element.alignment) { + element.styleName = element.alignment+"Aligned"; + } + } + return element; +} \ No newline at end of file -- cgit v1.2.3