Skip to content

Commit 7879773

Browse files
committed
test: fix up shuffle unit tests
1 parent 676f8f3 commit 7879773

File tree

2 files changed

+39
-13
lines changed

2 files changed

+39
-13
lines changed

src/utils.ts

+1-10
Original file line numberDiff line numberDiff line change
@@ -1321,15 +1321,6 @@ export class HostAddress {
13211321
Object.freeze(this);
13221322
}
13231323

1324-
equals(other: HostAddress): boolean {
1325-
return (
1326-
this.host === other.host &&
1327-
this.port === other.port &&
1328-
this.socketPath === other.socketPath &&
1329-
this.isIPv6 === other.isIPv6
1330-
);
1331-
}
1332-
13331324
[Symbol.for('nodejs.util.inspect.custom')](): string {
13341325
return this.inspect();
13351326
}
@@ -1440,7 +1431,7 @@ export function shuffle<T>(sequence: Iterable<T>, limit = 0): Array<T> {
14401431
const items = Array.from(sequence); // shallow copy in order to never shuffle the input
14411432

14421433
if (limit > items.length) {
1443-
throw new MongoInvalidArgumentError('Limit must be less than the number of items');
1434+
throw new MongoRuntimeError('Limit must be less than the number of items');
14441435
}
14451436

14461437
let remainingItemsToShuffle = items.length;

test/unit/utils.test.js

+38-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const {
88
} = require('../../src/utils');
99
const { expect } = require('chai');
1010
const sinon = require('sinon');
11-
const { MongoInvalidArgumentError } = require('../../src/error');
11+
const { MongoRuntimeError } = require('../../src/error');
1212

1313
describe('utils', function () {
1414
context('eachAsync', function () {
@@ -466,13 +466,34 @@ describe('utils', function () {
466466
const output = shuffle(input);
467467
expect(Array.isArray(output)).to.be.true;
468468
});
469+
470+
it('should not mutate the original input', function () {
471+
const input = Object.freeze(['a', 'b', 'c', 'd', 'e']);
472+
const output = shuffle(input); // This will throw if shuffle tries to edit the input
473+
expect(output).to.not.deep.equal(input);
474+
expect(output).to.have.lengthOf(input.length);
475+
});
476+
469477
it('should give a random subset if limit is less than the input length', () => {
470478
const input = ['a', 'b', 'c', 'd', 'e'];
471479
const output = shuffle(input, 3);
472480
expect(output).to.have.lengthOf(3);
473481
});
474482

475-
it('should give a random shuffling of the input', () => {
483+
const input = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'];
484+
for (let n = 1; n <= input.length; n++) {
485+
it(`should give a random subset of length ${n}`, function () {
486+
const output = shuffle(input, n);
487+
if (n > 2) {
488+
// This expectation fails more at n values below 3
489+
// sparing us the flake
490+
expect(output).to.not.deep.equal(input.slice(0, n));
491+
}
492+
expect(output).to.have.lengthOf(n);
493+
});
494+
}
495+
496+
it('should give a random shuffling of the entire input when no limit provided', () => {
476497
const input = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'];
477498
const output = shuffle(input);
478499
// Of course it is possible a shuffle returns exactly the same as the input
@@ -481,8 +502,22 @@ describe('utils', function () {
481502
expect(output).to.have.lengthOf(input.length);
482503
});
483504

505+
it('should handle empty array', function () {
506+
expect(shuffle([])).to.deep.equal([]);
507+
expect(shuffle([], 0)).to.deep.equal([]);
508+
});
509+
510+
it('should handle limit set to 0', function () {
511+
expect(shuffle(['a', 'b'])).to.have.lengthOf(2);
512+
expect(shuffle(['a', 'b'], 0)).to.have.lengthOf(2);
513+
});
514+
515+
it('should throw if limit is greater than zero and empty array', function () {
516+
expect(() => shuffle([], 2)).to.throw(MongoRuntimeError);
517+
});
518+
484519
it('should throw if limit is larger than input size', () => {
485-
expect(() => shuffle([], 100)).to.throw(MongoInvalidArgumentError);
520+
expect(() => shuffle(['a', 'b'], 3)).to.throw(MongoRuntimeError);
486521
});
487522
});
488523
});

0 commit comments

Comments
 (0)