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

Option: fix should.js .eql() behaviour #1297

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
20 changes: 9 additions & 11 deletions lib/util/option.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,28 +38,26 @@ class Option {
}

class Some extends Option {
#value;

/* istanbul ignore next */
constructor(value) {
super();
this.#value = value;
this._value = value;
}

get() { return this.#value; }
get() { return this._value; }

map(fn) { return Option.of(fn(this.#value)); }
filter(predicate) { return predicate(this.#value) === true ? this : none; } // eslint-disable-line no-use-before-define
map(fn) { return Option.of(fn(this._value)); }
filter(predicate) { return predicate(this._value) === true ? this : none; } // eslint-disable-line no-use-before-define

orNull() { return this.#value; }
orElse() { return this.#value; }
orElseGet() { return this.#value; }
orThrow() { return this.#value; }
orNull() { return this._value; }
orElse() { return this._value; }
orElseGet() { return this._value; }
orThrow() { return this._value; }

isDefined() { return true; }
isEmpty() { return false; }

ifDefined(consumer) { consumer(this.#value); }
ifDefined(consumer) { consumer(this._value); }

// Used by ramda for comparing objects.
equals(that) {
Expand Down
57 changes: 57 additions & 0 deletions test/unit/util/option.js
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like these tests!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added more tests - to use the same test cases against chai & node's built-in assert module. I think these will be helpful to avoid breaking anything when/if doing incremental migration away from should.js.

Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,61 @@ describe('(libs/FP) Option type', () => {
});
});
});

describe('assertion library interactions', () => {
// N.B. should.equal() is different from should.eql():
//
// * .equal(): check equality using ===
// * .eql(): check equality using "should-equal" module
//
// See: https://www.npmjs.com/package/should-equal

// TODO re-introduce this line when chai is added to the project
//const chaiAssert = require('chai').assert;
const nodeAssert = require('node:assert');

describe('equality', () => {
[
true,
false,
0,
1,
'',
'non-empty string',
].forEach(val => {
it(`should.js should recognise two Options of '${val}' to be equal`, () => {
Option.of(val).should.eql(Option.of(val));
});

// TODO enable this test when chai is introduced to the project
//it(`chai should recognise two Options of '${val}' to be equal`, () => {
// chaiAssert.deepEqual(Option.of(val), Option.of(val));
//});

it(`node:assert should recognise two Options of '${val}' to be equal`, () => {
nodeAssert.deepStrictEqual(Option.of(val), Option.of(val));
});
});

[
[ 0, 1 ],
[ 0, false ],
[ 0, '' ],
[ false, '' ],
].forEach((a, b) => {
it(`should.js should not recognise Options of '${a}' and '${b}' as equal`, () => {
Option.of(a).should.not.eql(Option.of(b));
});

// TODO enable this test when chai is introduced to the project
//it(`chai should not recognise Options of '${a}' and '${b}' as equal`, () => {
// chaiAssert.notDeepEqual(Option.of(a), Option.of(b));
//});

it(`node:assert should not recognise Options of '${a}' and '${b}' as equal`, () => {
nodeAssert.notDeepStrictEqual(Option.of(a), Option.of(b));
});
});
});
});
});