From 7738fefabc6f717c1d5e2459dabadea0fa588def Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 4 May 2013 10:41:23 +0100 Subject: Add document matchers for lists --- lib/document-matchers.js | 27 +++++++++++++++++++++++---- tests/document-matchers.tests.js | 25 ++++++++++++++++++++----- 2 files changed, 43 insertions(+), 9 deletions(-) diff --git a/lib/document-matchers.js b/lib/document-matchers.js index a40bde7..820d6c2 100644 --- a/lib/document-matchers.js +++ b/lib/document-matchers.js @@ -11,11 +11,30 @@ function run(styleName) { } function Matcher(elementType, styleName) { - this.elementType = elementType; - this.styleName = styleName; + this._elementType = elementType; + this._styleName = styleName; } Matcher.prototype.matches = function(element) { - return element.type === this.elementType && - (!this.styleName || element.styleName === this.styleName); + return element.type === this._elementType && + (!this._styleName || element.styleName === this._styleName) && + (!this._listIndex || isList(element, this._listIndex, this._listIsOrdered)); +} + +Matcher.prototype.orderedList = function(levelIndex) { + this._listIndex = levelIndex; + this._listIsOrdered = true; + return this; +} + +Matcher.prototype.unorderedList = function(levelIndex) { + this._listIndex = levelIndex; + this._listIsOrdered = false; + return this; +} + +function isList(element, levelIndex, isOrdered) { + return element.numbering && + element.numbering.level == levelIndex && + element.numbering.isOrdered == isOrdered; } diff --git a/tests/document-matchers.tests.js b/tests/document-matchers.tests.js index 55fd447..eef463d 100644 --- a/tests/document-matchers.tests.js +++ b/tests/document-matchers.tests.js @@ -3,23 +3,38 @@ var assert = require("assert"); var test = require("./testing").test; var documentMatchers = require("../lib/document-matchers"); var documents = require("../lib/documents"); - +var Paragraph = documents.Paragraph; describe("document-matchers", function() { test("paragraph with no style name matches any paragraph", function() { var matcher = documentMatchers.paragraph(); - assert.ok(matcher.matches(new documents.Paragraph())); - assert.ok(matcher.matches(new paragraphWithStyle("Heading1"))); + assert.ok(matcher.matches(new Paragraph())); + assert.ok(matcher.matches(paragraphWithStyle("Heading1"))); }); test("paragraph style name only matches paragraphs with that style", function() { var matcher = documentMatchers.paragraph("Heading1"); - assert.ok(!matcher.matches(new documents.Paragraph())); + assert.ok(!matcher.matches(new Paragraph())); assert.ok(matcher.matches(paragraphWithStyle("Heading1"))); assert.ok(!matcher.matches(paragraphWithStyle("Heading2"))); }); + + test("ordered-list(index) matches an ordered list with specified level index", function() { + var matcher = documentMatchers.paragraph().orderedList(1); + assert.ok(!matcher.matches(new Paragraph())); + assert.ok(matcher.matches(new Paragraph([], {numbering: {level: 1, isOrdered: true}}))); + assert.ok(!matcher.matches(new Paragraph([], {numbering: {level: 0, isOrdered: true}}))); + assert.ok(!matcher.matches(new Paragraph([], {numbering: {level: 1, isOrdered: false}}))); + }); + + test("unordered-list(index) matches an unordered list with specified level index", function() { + var matcher = documentMatchers.paragraph().unorderedList(1); + assert.ok(!matcher.matches(new Paragraph())); + assert.ok(matcher.matches(new Paragraph([], {numbering: {level: 1, isOrdered: false}}))); + assert.ok(!matcher.matches(new Paragraph([], {numbering: {level: 1, isOrdered: true}}))); + }); }); function paragraphWithStyle(styleName) { - return new documents.Paragraph([], {styleName: styleName}); + return new Paragraph([], {styleName: styleName}); } -- cgit v1.2.3