-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharguments.test.js
130 lines (115 loc) · 3.68 KB
/
arguments.test.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import { describe, jest, test, beforeEach } from '@jest/globals';
import { InvalidArgumentError } from 'commander';
import { mockConfig } from './mocks.js';
// setup mocks
const { getConfigValue } = mockConfig();
// import after mocks set up
const { intParser, decorateName, getDayArg, getLevelArg } = await import(
'../src/arguments.js'
);
describe('arguments', () => {
beforeEach(() => {
jest.resetAllMocks();
});
describe('intParser', () => {
test.each([null, undefined, '', false, true, {}, Promise.resolve(true)])(
'throws if not parsable as int: %s',
(value) => {
expect(() => {
intParser([1, 2, 3])(value);
}).toThrow(InvalidArgumentError);
}
);
test('throws if value is not a valid choice', () => {
expect(() => {
intParser([1, 2, 3])('4');
}).toThrow(InvalidArgumentError);
});
test('returns integer if value is a valid choice', () => {
const result = intParser([1, 2, 3])('1');
expect(result).toBe(1);
});
});
describe('decorateName()', () => {
test('wraps with angle brackets if required is true', () => {
const name = 'COOLNAME';
const result = decorateName(name, true);
expect(result).toBe(`<${name}>`);
});
test('wraps with square brackets if required is false', () => {
const name = 'COOLNAME';
const result = decorateName(name, false);
expect(result).toBe(`[${name}]`);
});
});
describe('getDayArg()', () => {
test('arg is required if required is true', () => {
const arg = getDayArg(true);
expect(arg.required).toBe(true);
});
test('arg is optional if required is false', () => {
const arg = getDayArg(false);
expect(arg.required).toBe(false);
});
test('parseArg() throws if day not valid', () => {
getConfigValue.mockImplementation((key) => {
switch (key) {
case 'aoc.validation.days':
return [1, 2, 3];
default:
throw new Error(`unknown key: ${key}`);
}
});
const arg = getDayArg(false);
expect(() => arg.parseArg(4)).toThrow(InvalidArgumentError);
});
test('parseArg() returns number if day is valid', () => {
getConfigValue.mockImplementation((key) => {
switch (key) {
case 'aoc.validation.days':
return [1, 2, 3];
default:
throw new Error(`unknown key: ${key}`);
}
});
const arg = getDayArg(false);
const result = arg.parseArg('2');
expect(result).toBe(2);
});
});
describe('getLevelArg()', () => {
test('arg is required if required is true', () => {
const result = getLevelArg(true);
expect(result.required).toBe(true);
});
test('arg is optional if required is false', () => {
const result = getLevelArg(false);
expect(result.required).toBe(false);
});
test('parseArg() throws if level not valid', () => {
getConfigValue.mockImplementation((key) => {
switch (key) {
case 'aoc.validation.levels':
return [10, 20];
default:
throw new Error(`unknown key: ${key}`);
}
});
const arg = getLevelArg(false);
expect(() => arg.parseArg(4)).toThrow(InvalidArgumentError);
});
test('parseArg() returns number if day is valid', () => {
getConfigValue.mockImplementation((key) => {
switch (key) {
case 'aoc.validation.levels':
return [10, 20];
default:
throw new Error(`unknown key: ${key}`);
}
});
const arg = getLevelArg(false);
const result = arg.parseArg('20');
expect(result).toBe(20);
});
});
});