Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding 'deep' option in order to compare while ignoring whitespace in text nodes #2

Merged
merged 2 commits into from
Dec 30, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ npm install chai-xml --save-dev
## Example

```javascript
var chai = require('chai');
var chai = require('chai');
var expect = require('chai').expect;
var chaiXml = require('chai-xml');

Expand All @@ -36,6 +36,12 @@ describe('assert some xml', function(){
it("should be the same XML as otherXml ", function(){
expect(someXml).xml.to.equal(otherXml);
});

it("should be the same XML ignoring the whitespace at the begining and end of the text nodes", function () {
var formattedXml = "<tag>\n\tContent\n</tag>";
var unformattedXml = "<tag>Content</tag>";
expect(formattedXml).xml.to.deep.equal(unformattedXml);
});
});
```

Expand All @@ -50,7 +56,7 @@ describe('assert some xml', function(){
## Contributing

Any contribution is welcome! Please check the [issues](https://github.com/krampstudio/chai-xml/issues). Do some unit tests as far as possible.

## Release History
* _0.1.0_ initial release. Support `xml` property, `valid` method and overwrite the `equal/eq/equals` methods

Expand Down
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