Skip to content

Commit

Permalink
Adds the possiblity of using the 'deep' option in order to compare ig…
Browse files Browse the repository at this point in the history
…noring whitespace at the begining and end of the text nodes
  • Loading branch information
Jesús Martínez committed Dec 20, 2014
1 parent 2acbc84 commit 4194ec4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 23 deletions.
20 changes: 10 additions & 10 deletions src/chai-xml.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
);
});
Expand All @@ -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){
Expand All @@ -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;
32 changes: 19 additions & 13 deletions test/chai-xml-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @license MIT <https://raw.githubusercontent.com/krampstudio/chai-xml/master/LICENSE>
*/

var chai = require('chai');
var chai = require('chai');
var expect = require('chai').expect;
var chaiXml = require('../src/chai-xml');

Expand All @@ -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(){
[ '<root><child/>',
'<root><child>value</root></child>',
Expand All @@ -52,28 +52,34 @@ 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);
expect('foo').to.equal('foo');
expect('foo').to.not.equal('bar');
expect(true).to.equal(true);
});

it("should compare XML", function(){
expect('<root><child></child></root>').xml.to.equal('<root><child></child></root>');
expect('<root><child></child></root>').xml.to.equal('<root><child /></root>');
expect('<root><child></child></root>').xml.to.not.equal('<root><children /></root>');
expect('<root>\n\t<child name="foo" value="bar"></child>\n</root>').xml.to.equal('<root><child value="bar" name="foo" /></root>');
});

it("when used with the deep option should trim the whitespace (including line breaks) at the beginning and end of text nodes", function () {
expect('<root>\n\t<child>\n\t\tText\n\t</child>\n</root>').xml.to.deep.equal('<root><child>Text</child></root>');
expect('<root>\n\t<child>\n\t\tLine1\n\t\tLine2\n\t</child>\n</root>').xml.to.not.deep.equal('<root><child>Line1Line2</child></root>');
expect('<root>\n\t<child>\n\t\tText\n\t</child>\n</root>').xml.to.not.equal('<root><child>Text</child></root>');
});
});

describe('the equal aliases', function(){

it("should also compare XML", function(){
Expand Down

0 comments on commit 4194ec4

Please sign in to comment.