From 4194ec43346286e8262129964340147d49f2ba61 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Mart=C3=ADnez?= Date: Sat, 20 Dec 2014 22:25:40 +0100 Subject: [PATCH] Adds the possiblity of using the 'deep' option in order to compare ignoring whitespace at the begining and end of the text nodes --- src/chai-xml.js | 20 ++++++++++---------- test/chai-xml-spec.js | 32 +++++++++++++++++++------------- 2 files changed, 29 insertions(+), 23 deletions(-) diff --git a/src/chai-xml.js b/src/chai-xml.js index d40a84d..a3d7ef8 100644 --- a/src/chai-xml.js +++ b/src/chai-xml.js @@ -24,12 +24,12 @@ var chaiXmlPlugin = function chaiXmlPlugin(chai, utils){ //flag it as xml flag(this, 'xml', true); - + }); /** * Add the valid method. - * Check wheter the XML is well-formed (not validated against DTD, XSD, + * Check whether the XML is well-formed (not validated against DTD, XSD, * but it could be implemented in a further version). */ Assertion.addMethod('valid', function (value) { @@ -38,9 +38,9 @@ var chaiXmlPlugin = function chaiXmlPlugin(chai, utils){ new xml2js.Parser().parseString(this._obj, function(err, result){ self.assert( - err === null, - 'expected #{this} to be valid', - 'expected #{this} not be not valid', + err === null, + 'expected #{this} to be valid', + 'expected #{this} not be not valid', err ); }); @@ -51,13 +51,13 @@ var chaiXmlPlugin = function chaiXmlPlugin(chai, utils){ * The strings are mapped to objects using xml2js that are deeply compared) */ var compareXml = function(_super){ - var self = this; + var self = this; return function assertEqual(value){ var negate; var parser; if(flag(this, 'xml')){ negate = flag(this, 'negate'); - parser = new xml2js.Parser(); + parser = new xml2js.Parser({trim: flag(this, 'deep')}); parser.parseString(this._obj, function(err, actual){ new Assertion(err).to.be.null; parser.parseString(value, function(err, expected){ @@ -74,10 +74,10 @@ var chaiXmlPlugin = function chaiXmlPlugin(chai, utils){ } }; }; - Assertion.overwriteMethod('equal', compareXml); - Assertion.overwriteMethod('equals', compareXml); + Assertion.overwriteMethod('equal', compareXml); + Assertion.overwriteMethod('equals', compareXml); Assertion.overwriteMethod('eq', compareXml); - + }; module.exports = chaiXmlPlugin; diff --git a/test/chai-xml-spec.js b/test/chai-xml-spec.js index e602f08..95cee31 100644 --- a/test/chai-xml-spec.js +++ b/test/chai-xml-spec.js @@ -3,7 +3,7 @@ * @license MIT */ -var chai = require('chai'); +var chai = require('chai'); var expect = require('chai').expect; var chaiXml = require('../src/chai-xml'); @@ -18,23 +18,23 @@ describe('chai-xml : ', function(){ }); it("should apply only on strings", function(){ expect('foo').xml.to.be.a('string'); - - expect(function(){ - expect({}).xml; + + expect(function(){ + expect({}).xml; }).to.throw(); - expect(function(){ - expect(1).xml; + expect(function(){ + expect(1).xml; }).to.throw(); }); - }); + }); + - describe('the valid member', function(){ it("should be a chai method", function(){ expect(chai.Assertion.prototype.valid).to.be.a('function'); }); - + it("should fail when object is invalid XML", function(){ [ '', 'value', @@ -52,12 +52,12 @@ describe('chai-xml : ', function(){ }); }); }); - + describe('the equal member', function(){ it("should be a chai method", function(){ expect(chai.Assertion.prototype.equal).to.be.a('function'); }); - it("should not affect overriden beahvior", function(){ + it("should not affect overriden behavior", function(){ expect(true).to.equal(true); expect(true).to.not.equal(false); expect(null).to.equal(null); @@ -65,15 +65,21 @@ describe('chai-xml : ', function(){ expect('foo').to.not.equal('bar'); expect(true).to.equal(true); }); - + it("should compare XML", function(){ expect('').xml.to.equal(''); expect('').xml.to.equal(''); expect('').xml.to.not.equal(''); expect('\n\t\n').xml.to.equal(''); }); + + it("when used with the deep option should trim the whitespace (including line breaks) at the beginning and end of text nodes", function () { + expect('\n\t\n\t\tText\n\t\n').xml.to.deep.equal('Text'); + expect('\n\t\n\t\tLine1\n\t\tLine2\n\t\n').xml.to.not.deep.equal('Line1Line2'); + expect('\n\t\n\t\tText\n\t\n').xml.to.not.equal('Text'); + }); }); - + describe('the equal aliases', function(){ it("should also compare XML", function(){