-
Notifications
You must be signed in to change notification settings - Fork 4
/
stringSpec.js
54 lines (43 loc) · 1.6 KB
/
stringSpec.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
(function (Random) {
describe("string", function () {
describe("with pool = 'abcde'", function () {
it("calls Random.integer", function () {
spyOn(Random, "integer");
Random.string("abcde");
expect(Random.integer).toHaveBeenCalledWith(0, 4);
});
it("calls the integer distribution N times based on the length", function () {
var index = 0;
var expected = "";
spyOn(Random, "integer").andReturn(function () {
index = (index + 3) % 5;
expected += "abcde".charAt(index);
return index;
});
var engine = function () {};
var generate = Random.string("abcde");
var actual = generate(engine, 64);
expect(actual).toBe(expected);
expect(actual.length).toBe(64);
});
});
describe("with default pool", function () {
it("calls Random.integer", function () {
spyOn(Random, "integer");
Random.string();
expect(Random.integer).toHaveBeenCalledWith(0, 63);
});
it("uses a pool of letters, numbers, '_', and '-'", function () {
var index = 0;
spyOn(Random, "integer").andReturn(function () {
return index++;
});
var engine = function () {};
var expected = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-";
var generate = Random.string();
var actual = generate(engine, 64);
expect(actual).toBe(expected);
});
});
});
}(typeof module !== "undefined" ? require("../lib/random") : Random));