Skip to content

Commit

Permalink
Merge pull request #218 from jakubzitny/reference-equal
Browse files Browse the repository at this point in the history
Implement reference comparison
  • Loading branch information
astorije authored Apr 21, 2019
2 parents afad864 + f8ad825 commit 9d943d0
Show file tree
Hide file tree
Showing 3 changed files with 204 additions and 8 deletions.
64 changes: 60 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,31 @@ expect(a).to.equal(b);
Immutable data structures should only contain other immutable data
structures (unlike `Array`s and `Object`s) to be considered immutable and
properly work against `.equal()`. See
[this issue](https://github.com/astorije/chai-immutable/issues/24) for
more information.
[issue #24](https://github.com/astorije/chai-immutable/issues/24) for more
information.

Also, note that `deep.equal` and `eql` are synonyms of `equal` when
tested against immutable data structures, therefore they are aliases to
`equal`.

### .referenceEqual(value)

- **@param** _{Collection}_ value

Asserts that the reference of the target is equivalent to the reference of
`collection`. This method preserves the original behavior of Chai's `equal`.

See [issue #210](https://github.com/astorije/chai-immutable/issues/210) for
more details.

```js
const a = List.of(1, 2, 3);
const b = a;
const c = List.of(1, 2, 3);
expect(a).to.referenceEqual(b);
expect(a).to.not.referenceEqual(c);
```

### .include(value)

- **@param** _{ Mixed }_ val
Expand Down Expand Up @@ -385,8 +403,27 @@ assert.equal(a, b);
Immutable data structures should only contain other immutable data
structures (unlike `Array`s and `Object`s) to be considered immutable and
properly work against `.equal()`, `.strictEqual()` or `.deepEqual()`. See
[this issue](https://github.com/astorije/chai-immutable/issues/24) for
more information.
[issue #24](https://github.com/astorije/chai-immutable/issues/24) for more
information.

### .referenceEqual(actual, expected)

- **@param** _{Collection}_ actual
- **@param** _{Collection}_ expected

Asserts that the reference of `actual` is equivalent to the reference of
`expected`. This method preserves the original behavior of Chai's `equal`.

See [issue #210](https://github.com/astorije/chai-immutable/issues/210) for
more details.

```js
const a = List.of(1, 2, 3);
const b = a;
const c = List.of(1, 2, 3);
assert.referenceEqual(a, b);
assert.throws(() => assert.referenceEqual(a, c));
```

### .notEqual(actual, expected)

Expand All @@ -403,6 +440,25 @@ const b = List.of(4, 5, 6);
assert.notEqual(a, b);
```

### .notReferenceEqual(actual, expected)

- **@param** _{Collection}_ actual
- **@param** _{Collection}_ expected

Asserts that the reference of `actual` is not equivalent to the reference of
`expected`. This method preserves the original behavior of Chai's `notEqual`.

See [issue #210](https://github.com/astorije/chai-immutable/issues/210) for
more details.

```js
const a = List.of(1, 2, 3);
const b = a;
const c = List.of(1, 2, 3);
assert.throws(() => assert.notReferenceEqual(a, b));
assert.notReferenceEqual(a, c);
```

### .sizeOf(collection, length)

- **@param** _{ Collection }_ collection
Expand Down
100 changes: 96 additions & 4 deletions chai-immutable.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@
* Immutable data structures should only contain other immutable data
* structures (unlike `Array`s and `Object`s) to be considered immutable and
* properly work against `.equal()`. See
* [this issue](https://github.com/astorije/chai-immutable/issues/24) for
* more information.
* [issue #24](https://github.com/astorije/chai-immutable/issues/24) for more
* information.
*
* Also, note that `deep.equal` and `eql` are synonyms of `equal` when
* tested against immutable data structures, therefore they are aliases to
Expand Down Expand Up @@ -128,6 +128,46 @@
Assertion.overwriteMethod('eql', assertImmutableEqual);
Assertion.overwriteMethod('eqls', assertImmutableEqual);

/**
* ### .referenceEqual(value)
*
* Asserts that the reference of the target is equivalent to the reference of
* `collection`. This method preserves the original behavior of Chai's `equal`.
*
* See [issue #210](https://github.com/astorije/chai-immutable/issues/210) for
* more details.
*
* ```js
* const a = List.of(1, 2, 3);
* const b = a;
* const c = List.of(1, 2, 3);
* expect(a).to.referenceEqual(b);
* expect(a).to.not.referenceEqual(c);
* ```
*
* @name referenceEqual
* @param {Collection} value
* @namespace BDD
* @api public
*/

function assertCollectionReferenceEqual() {
return function(collection) {
const obj = this._obj;

this.assert(
obj === collection,
'expected #{act} reference to equal #{exp}',
'expected #{act} reference to not equal #{exp}',
collection.toJS(),
obj.toJS(),
true
);
};
}

Assertion.addMethod('referenceEqual', assertCollectionReferenceEqual);

/**
* ### .include(value)
*
Expand Down Expand Up @@ -786,8 +826,8 @@
* Immutable data structures should only contain other immutable data
* structures (unlike `Array`s and `Object`s) to be considered immutable and
* properly work against `.equal()`, `.strictEqual()` or `.deepEqual()`. See
* [this issue](https://github.com/astorije/chai-immutable/issues/24) for
* more information.
* [issue #24](https://github.com/astorije/chai-immutable/issues/24) for more
* information.
*
* @name equal
* @param {Collection} actual
Expand All @@ -807,6 +847,32 @@
}
};

/**
* ### .referenceEqual(actual, expected)
*
* Asserts that the reference of `actual` is equivalent to the reference of
* `expected`. This method preserves the original behavior of Chai's `equal`.
*
* See [issue #210](https://github.com/astorije/chai-immutable/issues/210) for
* more details.
*
* ```js
* const a = List.of(1, 2, 3);
* const b = a;
* const c = List.of(1, 2, 3);
* assert.referenceEqual(a, b);
* assert.throws(() => assert.referenceEqual(a, c));
* ```
*
* @name referenceEqual
* @param {Collection} actual
* @param {Collection} expected
* @namespace Assert
* @api public
*/

assert.referenceEqual = originalEqual;

/**
* ### .notEqual(actual, expected)
*
Expand Down Expand Up @@ -835,6 +901,32 @@
}
};

/**
* ### .notReferenceEqual(actual, expected)
*
* Asserts that the reference of `actual` is not equivalent to the reference of
* `expected`. This method preserves the original behavior of Chai's `notEqual`.
*
* See [issue #210](https://github.com/astorije/chai-immutable/issues/210) for
* more details.
*
* ```js
* const a = List.of(1, 2, 3);
* const b = a;
* const c = List.of(1, 2, 3);
* assert.throws(() => assert.notReferenceEqual(a, b));
* assert.notReferenceEqual(a, c);
* ```
*
* @name notReferenceEqual
* @param {Collection} actual
* @param {Collection} expected
* @namespace Assert
* @api public
*/

assert.notReferenceEqual = originalNotEqual;

/**
* ### .sizeOf(collection, length)
*
Expand Down
48 changes: 48 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,22 @@ describe('chai-immutable', function() {
});
});

describe('referenceEqual method', function() {
it('should pass for equal references', function() {
const list1 = List.of(1, 2, 3);
const list2 = list1;

expect(list1).to.referenceEqual(list2);
});

it('should not pass for different immutable collections with equal values', function() {
const list1 = List.of(1, 2, 3);
const list2 = List.of(1, 2, 3);

expect(list1).to.not.referenceEqual(list2);
});
});

describe('include method', function() {
it('should pass given an existing value', function() {
expect(new List([1, 2, 3])).to.include(2);
Expand Down Expand Up @@ -930,6 +946,22 @@ describe('chai-immutable', function() {
});
});

describe('referenceEqual assertion', function() {
it('should pass for equal references', function() {
const list1 = List.of(1, 2, 3);
const list2 = list1;

assert.referenceEqual(list1, list2);
});

it('should not pass for different immutable collections with equal values', function() {
const list1 = List.of(1, 2, 3);
const list2 = List.of(1, 2, 3);

fail(() => assert.referenceEqual(list1, list2));
});
});

describe('notEqual assertion', function() {
it('should pass given different values', function() {
assert.notEqual(list3, new List());
Expand Down Expand Up @@ -961,6 +993,22 @@ describe('chai-immutable', function() {
});
});

describe('notReferenceEqual assertion', function() {
it('should pass for different immutable collections with equal values', function() {
const list1 = List.of(1, 2, 3);
const list2 = List.of(1, 2, 3);

assert.notReferenceEqual(list1, list2);
});

it('should not pass for different immutable collections with equal values', function() {
const list1 = List.of(1, 2, 3);
const list2 = list1;

fail(() => assert.notReferenceEqual(list1, list2));
});
});

describe('unoverridden strictEqual and deepEqual assertions', function() {
it('should pass given equal values', function() {
assert.strictEqual(list3, List.of(1, 2, 3));
Expand Down

0 comments on commit 9d943d0

Please sign in to comment.