Skip to content

Commit 910805a

Browse files
committed
Merge pull request #25 from astorije/astorije/issue-24
Precisions around deep equality
2 parents 1168ca7 + e442ffc commit 910805a

File tree

3 files changed

+84
-4
lines changed

3 files changed

+84
-4
lines changed

README.md

+14-2
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ var b = List.of(1, 2, 3);
6262
expect(a).to.equal(b);
6363
```
6464

65+
Immutable data structures should only contain other immutable data
66+
structures (unlike `Array`s and `Object`s) to be considered immutable and
67+
properly work against `.equal()`. See
68+
[this issue](https://github.com/astorije/chai-immutable/issues/24) for
69+
more information.
70+
6571
### .include(value)
6672

6773
- **@param** *{ Mixed }* val
@@ -149,15 +155,21 @@ expect(List.of(1, 2, 3)).to.have.sizeOf(3);
149155
- **@param** *{ Collection }* expected
150156

151157
Asserts that the values of the target are equvalent to the values of
152-
`collection`. Note that `.strictEqual` and `.deepEqual` assert exactly like
153-
`.equal` in the context of Immutable data structures.
158+
`collection`. Note that `.strictEqual()` and `.deepEqual()` assert
159+
exactly like `.equal()` in the context of Immutable data structures.
154160

155161
```js
156162
var a = List.of(1, 2, 3);
157163
var b = List.of(1, 2, 3);
158164
assert.equal(a, b);
159165
```
160166

167+
Immutable data structures should only contain other immutable data
168+
structures (unlike `Array`s and `Object`s) to be considered immutable and
169+
properly work against `.equal()`, `.strictEqual()` or `.deepEqual()`. See
170+
[this issue](https://github.com/astorije/chai-immutable/issues/24) for
171+
more information.
172+
161173
### .sizeOf(collection, length)
162174

163175
- **@param** *{ Collection }* collection

chai-immutable.js

+16-2
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,16 @@
7474
* expect(a).to.equal(b);
7575
* ```
7676
*
77+
* Immutable data structures should only contain other immutable data
78+
* structures (unlike `Array`s and `Object`s) to be considered immutable and
79+
* properly work against `.equal()`. See
80+
* [this issue](https://github.com/astorije/chai-immutable/issues/24) for
81+
* more information.
82+
*
7783
* @name equal
7884
* @alias equals
7985
* @alias eq
86+
* @alias eql
8087
* @alias deep.equal
8188
* @param {Collection} value
8289
* @api public
@@ -101,6 +108,7 @@
101108
Assertion.overwriteMethod('equal', assertCollectionEqual);
102109
Assertion.overwriteMethod('equals', assertCollectionEqual);
103110
Assertion.overwriteMethod('eq', assertCollectionEqual);
111+
Assertion.overwriteMethod('eql', assertCollectionEqual);
104112

105113
/**
106114
* ### .include(value)
@@ -438,15 +446,21 @@
438446
* ### .equal(actual, expected)
439447
*
440448
* Asserts that the values of the target are equvalent to the values of
441-
* `collection`. Note that `.strictEqual` and `.deepEqual` assert exactly like
442-
* `.equal` in the context of Immutable data structures.
449+
* `collection`. Note that `.strictEqual()` and `.deepEqual()` assert
450+
* exactly like `.equal()` in the context of Immutable data structures.
443451
*
444452
* ```js
445453
* var a = List.of(1, 2, 3);
446454
* var b = List.of(1, 2, 3);
447455
* assert.equal(a, b);
448456
* ```
449457
*
458+
* Immutable data structures should only contain other immutable data
459+
* structures (unlike `Array`s and `Object`s) to be considered immutable and
460+
* properly work against `.equal()`, `.strictEqual()` or `.deepEqual()`. See
461+
* [this issue](https://github.com/astorije/chai-immutable/issues/24) for
462+
* more information.
463+
*
450464
* @name equal
451465
* @param {Collection} actual
452466
* @param {Collection} expected

test/test.js

+54
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ var Stack = Immutable.Stack;
2020

2121
describe('chai-immutable (' + typeEnv + ')', function () {
2222
var list3 = List.of(1, 2, 3);
23+
var deepMap = new Map({
24+
foo: 'bar',
25+
list: List.of(1, 2, 3)
26+
});
2327

2428
describe('BDD interface', function () {
2529
describe('empty property', function () {
@@ -62,6 +66,8 @@ describe('chai-immutable (' + typeEnv + ')', function () {
6266
expect(list3).to.not.equals(new List());
6367
expect(list3).to.eq(List.of(1, 2, 3));
6468
expect(list3).to.not.eq(new List());
69+
expect(list3).to.eql(List.of(1, 2, 3));
70+
expect(list3).to.not.eql(new List());
6571
expect(list3).to.deep.equal(List.of(1, 2, 3));
6672
expect(list3).to.not.deep.equal(new List());
6773
});
@@ -73,6 +79,32 @@ describe('chai-immutable (' + typeEnv + ')', function () {
7379
expect({ foo: 'bar' }).to.not.equal({ foo: 'bar' });
7480
expect({ foo: 'bar' }).to.deep.equal({ foo: 'bar' });
7581
});
82+
83+
it('should work on deep structures that are equal', function () {
84+
var sameDeepMap = new Map({
85+
foo: 'bar',
86+
list: List.of(1, 2, 3)
87+
});
88+
89+
expect(deepMap).to.equal(sameDeepMap);
90+
expect(deepMap).to.equals(sameDeepMap);
91+
expect(deepMap).to.eq(sameDeepMap);
92+
expect(deepMap).to.eql(sameDeepMap);
93+
expect(deepMap).to.deep.equal(sameDeepMap);
94+
});
95+
96+
it('should work on deep structures that are not equal', function () {
97+
var differentDeepMap = new Map({
98+
foo: 'bar',
99+
list: List.of(42)
100+
});
101+
102+
expect(deepMap).to.not.equal(differentDeepMap);
103+
expect(deepMap).to.not.equals(differentDeepMap);
104+
expect(deepMap).to.not.eq(differentDeepMap);
105+
expect(deepMap).to.not.eql(differentDeepMap);
106+
expect(deepMap).to.not.deep.equal(differentDeepMap);
107+
});
76108
});
77109

78110
describe('include method', function () {
@@ -347,6 +379,28 @@ describe('chai-immutable (' + typeEnv + ')', function () {
347379
});
348380
});
349381

382+
it('should work on deep structures that are equal', function () {
383+
var sameDeepMap = new Map({
384+
foo: 'bar',
385+
list: List.of(1, 2, 3)
386+
});
387+
388+
assert.equal(deepMap, sameDeepMap);
389+
assert.strictEqual(deepMap, sameDeepMap);
390+
assert.deepEqual(deepMap, sameDeepMap);
391+
});
392+
393+
it('should work on deep structures that are not equal', function () {
394+
var differentDeepMap = new Map({
395+
foo: 'bar',
396+
list: List.of(42)
397+
});
398+
399+
assert.notEqual(deepMap, differentDeepMap);
400+
assert.notStrictEqual(deepMap, differentDeepMap);
401+
assert.notDeepEqual(deepMap, differentDeepMap);
402+
});
403+
350404
describe('sizeOf assertion', function () {
351405
it('should be true when given the right size', function () {
352406
assert.sizeOf(List.of(1, 2, 3), 3);

0 commit comments

Comments
 (0)