|
1 | 1 | import { describe, expect, test } from '@jest/globals'; |
2 | 2 | import { randomShipRegistryNumber, randomStardate, randomPlanetClass } from './captains-log'; |
3 | | -describe('randomShipRegistryNumber',() => { |
4 | | - test('registry numbers are valid',() => { |
5 | | - for (let i=0;i<4; i++){ |
6 | | - expect(randomShipRegistryNumber()).toMatch(/NCC-[1-9][0-9]{3}/) |
| 3 | + |
| 4 | +describe('randomShipRegistryNumber', () => { |
| 5 | + test('registry numbers are valid', () => { |
| 6 | + for (let i = 0; i < 4; i++) { |
| 7 | + expect(randomShipRegistryNumber()).toMatch(/NCC-[1-9][0-9]{3}/); |
7 | 8 | } |
8 | 9 | }); |
9 | | - test('returns a random registry number',() => { |
10 | | - expect(randomShipRegistryNumber()).not.toEqual(randomShipRegistryNumber()) |
| 10 | + |
| 11 | + test('returns a random registry number', () => { |
| 12 | + expect(randomShipRegistryNumber()).not.toEqual(randomShipRegistryNumber()); |
11 | 13 | }); |
12 | 14 | }); |
| 15 | + |
13 | 16 | function loadDie(...values) { |
14 | | - const originalRandom = Math.random() |
| 17 | + const originalRandom = Math.random(); |
15 | 18 |
|
16 | 19 | Math.random = function loadedDie() { |
17 | 20 | if (values.length === 0) { |
18 | 21 | return originalRandom(); |
19 | 22 | } |
20 | 23 | return values.shift(); |
21 | | - } |
| 24 | + }; |
| 25 | + |
22 | 26 | return () => { |
23 | 27 | Math.random = originalRandom; |
24 | | - } |
| 28 | + }; |
25 | 29 | } |
26 | | -describe('randomStardate',() => { |
27 | | - test('stardate is between 41000 and 42000',() => { |
| 30 | + |
| 31 | +describe('randomStardate', () => { |
| 32 | + test('stardate is between 41000 and 42000', () => { |
28 | 33 | const restore = loadDie( |
29 | 34 | 0, 0, 0, 0, 0, 0, |
30 | 35 | 1, 1, 1, 1, 1, 1, |
31 | 36 | 0.5, 0.5, 0.5, 0.5, 0.5, 0.5 |
32 | 37 | ); |
| 38 | + |
33 | 39 | for (let i = 0; i < 10_000; i++) { |
34 | | - const starDate = randomStardate() |
| 40 | + const starDate = randomStardate(); |
35 | 41 | expect(starDate).toBeGreaterThanOrEqual(41_000); |
36 | 42 | expect(starDate).toBeLessThan(42_000); |
37 | 43 | } |
| 44 | + |
38 | 45 | restore(); |
39 | | - } |
40 | | -} |
| 46 | + }); |
| 47 | +}); |
| 48 | + |
41 | 49 | describe('randomPlanetClass', () => { |
42 | | - test('planet classes are valid', () => { |
43 | | - const expected = 'DHJKLMNRTY' |
44 | | - const seen = {} |
45 | | - for (let i=0; i < 1_000; i++){ |
46 | | - const actual = randomPlanetClass() |
47 | | - expect(expected).toContain(actual) |
| 50 | + test('planet classes are valid', () => { |
| 51 | + const expected = 'DHJKLMNRTY'; |
| 52 | + for (let i = 0; i < 1_000; i++) { |
| 53 | + const actual = randomPlanetClass(); |
| 54 | + expect(expected).toContain(actual); |
48 | 55 | } |
49 | | - }) |
50 | | - test('all planet classes can be returned', () => { |
51 | | - const expected = 'DHJKLMNRTY' |
52 | | - const seen = {} |
53 | | - for (let i=0; i < 1_000; i++){ |
54 | | - const actual = randomPlanetClass() |
55 | | - seen[actual] = true |
| 56 | + }); |
| 57 | + |
| 58 | + test('all planet classes can be returned', () => { |
| 59 | + const expected = 'DHJKLMNRTY'; |
| 60 | + const seen = {}; |
| 61 | + |
| 62 | + for (let i = 0; i < 1_000; i++) { |
| 63 | + const actual = randomPlanetClass(); |
| 64 | + seen[actual] = true; |
56 | 65 | } |
57 | | - expected(Object.keys(seen).length).toBe(expected.length); |
58 | | - }) |
59 | | -}) |
| 66 | + |
| 67 | + expect(Object.keys(seen).length).toBe(expected.length); |
| 68 | + }); |
| 69 | +}); |
0 commit comments