-
Notifications
You must be signed in to change notification settings - Fork 4
/
sampleSpec.js
78 lines (63 loc) · 2.71 KB
/
sampleSpec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
(function (Random) {
describe("sample", function () {
[-Infinity, Infinity, NaN, -1, 5].forEach(function (sampleSize) {
it("throws a RangeError if sampleSize is " + sampleSize, function () {
expect(function () {
Random.sample(function () {}, [], sampleSize);
}).toThrow(new RangeError("Expected sampleSize to be within 0 and the length of the population"));
});
});
describe("when sampleSize is the same as length", function () {
it("calls shuffle on a clone of the array", function () {
var dummy = [];
spyOn(Random, "shuffle").andReturn(dummy);
var engine = function () {};
var array = ["a", "b", "c"];
var actual = Random.sample(engine, array, array.length);
expect(actual).toBe(dummy);
expect(Random.shuffle).toHaveBeenCalledWith(engine, array, 0);
expect(Random.shuffle.mostRecentCall.args[1]).not.toBe(array);
});
});
describe("when sampleSize is less than the length", function () {
it("calls shuffle on a clone of the array", function () {
var dummy = ["e", "d", "c", "b", "a"];
spyOn(Random, "shuffle").andReturn(dummy);
var engine = function () {};
var array = ["a", "b", "c", "d", "e"];
var sampleSize = 3;
var expected = dummy.slice(array.length - sampleSize);
var actual = Random.sample(engine, array, sampleSize);
expect(actual).toEqual(expected);
expect(Random.shuffle).toHaveBeenCalledWith(engine, array, array.length - sampleSize);
expect(Random.shuffle.mostRecentCall.args[1]).not.toBe(array);
});
});
describe("when sampleSize is 0", function () {
it("returns an empty array", function () {
var array = ["a", "b", "c"];
var expected = [];
var engine = function () {};
var sampleSize = 0;
var actual = Random.sample(engine, array, sampleSize);
expect(actual).toEqual(expected);
});
it("does not call shuffle", function () {
var array = ["a", "b", "c"];
spyOn(Random, "shuffle");
var engine = function () {};
var sampleSize = 0;
Random.sample(engine, array, sampleSize);
expect(Random.shuffle).not.toHaveBeenCalled();
});
it("does not call the engine", function () {
var array = ["a", "b", "c"];
spyOn(Random, "shuffle");
var engine = jasmine.createSpy();
var sampleSize = 0;
Random.sample(engine, array, sampleSize);
expect(engine).not.toHaveBeenCalled();
});
});
});
}(typeof module !== "undefined" ? require("../lib/random") : Random));