Skip to content

Commit

Permalink
Use Iterable.isIterable for Immutable check
Browse files Browse the repository at this point in the history
  • Loading branch information
jakelazaroff committed Mar 13, 2016
1 parent 6a9c9ed commit 7c8697f
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 16 deletions.
38 changes: 22 additions & 16 deletions chai-immutable.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@

var Assertion = chai.Assertion;

function assertIsIterable(obj) {
new Assertion(obj).assert(
Immutable.Iterable.isIterable(obj),
'expected #{this} to be an Iterable'
);
}

/**
* ## BDD API Reference
*/
Expand All @@ -46,7 +53,7 @@
return function () {
var obj = this._obj;

if (obj && obj instanceof Collection) {
if (Immutable.Iterable.isIterable(obj)) {
var size = obj.size;
new Assertion(size).a('number');

Expand Down Expand Up @@ -96,7 +103,7 @@
return function (collection) {
var obj = this._obj;

if (obj && obj instanceof Collection) {
if (Immutable.Iterable.isIterable(obj)) {
this.assert(
Immutable.is(obj, collection),
'expected #{act} to equal #{exp}',
Expand Down Expand Up @@ -141,7 +148,7 @@
return function (val) {
var obj = this._obj;

if (obj && obj instanceof Collection) {
if (Immutable.Iterable.isIterable(obj)) {
this.assert(
obj.includes(val),
'expected #{act} to include #{exp}',
Expand Down Expand Up @@ -217,14 +224,13 @@

var obj = this._obj;

if (obj && obj instanceof KeyedCollection) {
if (Immutable.Iterable.isKeyed(obj)) {
switch (utils.type(keys)) {
case 'object':
if (keys instanceof IndexedCollection ||
keys instanceof SetCollection) {
if (Immutable.Iterable.isIndexed(keys))
keys = keys.toJS();
}
else if (keys instanceof KeyedCollection) keys = keys.keySeq().toJS();
else if (Immutable.Iterable.isIterable(keys))
keys = keys.keySeq().toJS();
else keys = Object.keys(keys);
case 'array':
if (arguments.length > 1) throw new Error(
Expand Down Expand Up @@ -309,7 +315,7 @@
*/

function assertCollectionSize(n) {
new Assertion(this._obj).instanceof(Collection);
assertIsIterable(this._obj);

var size = this._obj.size;
new Assertion(size).a('number');
Expand All @@ -335,7 +341,7 @@
function assertCollectionSizeLeast(_super) {
return function (n) {
if (utils.flag(this, 'immutable.collection.size')) {
new Assertion(this._obj).instanceof(Collection);
assertIsIterable(this._obj);

var size = this._obj.size;
new Assertion(size).a('number');
Expand All @@ -355,7 +361,7 @@
function assertCollectionSizeMost(_super) {
return function (n) {
if (utils.flag(this, 'immutable.collection.size')) {
new Assertion(this._obj).instanceof(Collection);
assertIsIterable(this._obj);

var size = this._obj.size;
new Assertion(size).a('number');
Expand All @@ -375,7 +381,7 @@
function assertCollectionSizeAbove(_super) {
return function (n) {
if (utils.flag(this, 'immutable.collection.size')) {
new Assertion(this._obj).instanceof(Collection);
assertIsIterable(this._obj);

var size = this._obj.size;
new Assertion(size).a('number');
Expand All @@ -395,7 +401,7 @@
function assertCollectionSizeBelow(_super) {
return function (n) {
if (utils.flag(this, 'immutable.collection.size')) {
new Assertion(this._obj).instanceof(Collection);
assertIsIterable(this._obj);

var size = this._obj.size;
new Assertion(size).a('number');
Expand Down Expand Up @@ -429,7 +435,7 @@
Assertion.overwriteMethod('within', function (_super) {
return function (min, max) {
if (utils.flag(this, 'immutable.collection.size')) {
new Assertion(this._obj).instanceof(Collection);
assertIsIterable(this._obj);

var size = this._obj.size;
new Assertion(size).a('number');
Expand Down Expand Up @@ -483,7 +489,7 @@
// It seems like we shouldn't actually need this check, however,
// `assert.equal` actually behaves differently than its BDD counterpart!
// Namely, the BDD version is strict while the "assert" one isn't.
if (actual instanceof Collection) {
if (Immutable.Iterable.isIterable(actual)) {
return new Assertion(actual).equal(expected);
}
else return originalEqual(actual, expected);
Expand All @@ -509,7 +515,7 @@
*/

assert.notEqual = function (actual, expected) {
if (actual instanceof Collection) {
if (Immutable.Iterable.isIterable(actual)) {
return new Assertion(actual).not.equal(expected);
}
else return originalNotEqual(actual, expected);
Expand Down
56 changes: 56 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
'use strict';

// From http://stackoverflow.com/a/728694
function clone(obj) {
if (null === obj || 'object' !== typeof obj) return obj;
var copy = obj.constructor();
for (var attr in obj) {
if (obj.hasOwnProperty(attr)) copy[attr] = obj[attr];
}
return copy;
}

var typeEnv;
if (!chai) {
var chai = require('chai');
Expand All @@ -11,6 +21,8 @@ if (!chai) {
}
else typeEnv = 'PhantomJS';

var clonedImmutable = clone(Immutable);

var assert = chai.assert;
var expect = chai.expect;
var List = Immutable.List;
Expand Down Expand Up @@ -45,6 +57,8 @@ describe('chai-immutable (' + typeEnv + ')', function () {
list: List.of(42)
});

var clonedImmutableList = clonedImmutable.List.of(1, 2, 3);

describe('BDD interface', function () {
describe('empty property', function () {
it('should pass given an empty collection', function () {
Expand All @@ -68,6 +82,10 @@ describe('chai-immutable (' + typeEnv + ')', function () {
it('should fail using `not` given an empty collection', function () {
fail(function () { expect(new List()).to.not.be.empty; });
});

it('should work if using different copies of Immutable', function () {
expect(clonedImmutable.List()).to.be.empty;
});
});

describe('equal method', function () {
Expand Down Expand Up @@ -162,6 +180,10 @@ describe('chai-immutable (' + typeEnv + ')', function () {
fail(function () { expect(deepMap).to.not.eqls(sameDeepMap); });
fail(function () { expect(deepMap).to.not.deep.equal(sameDeepMap); });
});

it('should work if using different copies of Immutable', function () {
expect(clonedImmutableList).to.equal(List.of(1, 2, 3));
});
});

describe('include method', function () {
Expand Down Expand Up @@ -376,6 +398,10 @@ describe('chai-immutable (' + typeEnv + ')', function () {
fail(function () { expect(map).to.contain.key('z'); });
fail(function () { expect(obj).to.contain.key('z'); });
});

it('should work if using different copies of Immutable', function () {
expect(clonedImmutable.Map({ x: 1 })).to.have.key('x');
});
});

describe('size method', function () {
Expand All @@ -399,6 +425,10 @@ describe('chai-immutable (' + typeEnv + ')', function () {
it('should fail using `not` given the right size', function () {
fail(function () { expect(list3).to.not.have.size(3); });
});

it('should work if using different copies of Immutable', function () {
expect(clonedImmutableList).to.have.size(3);
});
});

describe('size property', function () {
Expand Down Expand Up @@ -530,6 +560,10 @@ describe('chai-immutable (' + typeEnv + ')', function () {
it('most should fail using `not` given a bad max size', function () {
fail(function () { expect(list3).to.not.have.size.of.at.most(42); });
});

it('should work if using different copies of Immutable', function () {
expect(clonedImmutableList).to.have.size.above(2);
});
});
});

Expand Down Expand Up @@ -568,6 +602,10 @@ describe('chai-immutable (' + typeEnv + ')', function () {
it('should fail given deeply different values', function () {
fail(function () { assert.equal(deepMap, differentDeepMap); });
});

it('should work if using different copies of Immutable', function () {
assert.equal(clonedImmutableList, List.of(1, 2, 3));
});
});

describe('notEqual assertion', function () {
Expand Down Expand Up @@ -595,6 +633,10 @@ describe('chai-immutable (' + typeEnv + ')', function () {
it('should fail given deeply equal values', function () {
fail(function () { assert.notEqual(deepMap, sameDeepMap); });
});

it('should work if using different copies of Immutable', function () {
assert.notEqual(clonedImmutableList, List.of());
});
});

describe('unoverridden strictEqual and deepEqual assertions', function () {
Expand All @@ -617,6 +659,11 @@ describe('chai-immutable (' + typeEnv + ')', function () {
fail(function () { assert.strictEqual(deepMap, differentDeepMap); });
fail(function () { assert.deepEqual(deepMap, differentDeepMap); });
});

it('should work if using different copies of Immutable', function () {
assert.strictEqual(clonedImmutableList, List.of(1, 2, 3));
assert.deepEqual(clonedImmutableList, List.of(1, 2, 3));
});
});

describe('unoverridden notStrictEqual and notDeepEqual assertions', function () {
Expand All @@ -639,6 +686,11 @@ describe('chai-immutable (' + typeEnv + ')', function () {
fail(function () { assert.notStrictEqual(deepMap, sameDeepMap); });
fail(function () { assert.notDeepEqual(deepMap, sameDeepMap); });
});

it('should work if using different copies of Immutable', function () {
assert.notStrictEqual(clonedImmutableList, List());
assert.notDeepEqual(clonedImmutableList, List());
});
});

describe('sizeOf assertion', function () {
Expand All @@ -653,6 +705,10 @@ describe('chai-immutable (' + typeEnv + ')', function () {
it('should fail given the wrong size', function () {
fail(function () { assert.sizeOf(list3, 42); });
});

it('should work if using different copies of Immutable', function () {
assert.sizeOf(clonedImmutableList, 3);
});
});
});
});

0 comments on commit 7c8697f

Please sign in to comment.