-
-
Notifications
You must be signed in to change notification settings - Fork 9.5k
/
Copy pathoptions.test.ts
165 lines (147 loc) · 4.28 KB
/
options.test.ts
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import { createCommand } from 'commander';
import { describe, it, expect } from '@jest/globals';
import {
getOptions,
areOptionsSatisfied,
getCommand,
OptionValues,
MaybeOptionValues,
} from './options';
const allOptions = {
first: {
description: 'first',
},
second: {
description: 'second',
inverse: true,
},
third: {
description: 'third',
values: ['one', 'two', 'three'],
required: true as const,
},
fourth: {
description: 'fourth',
values: ['a', 'b', 'c'],
multiple: true as const,
},
};
// TS "tests"
// deepscan-disable-next-line
function test(mv: MaybeOptionValues<typeof allOptions>, v: OptionValues<typeof allOptions>) {
console.log(mv.first, mv.second, mv.third, mv.fourth);
// @ts-expect-error as it's not allowed
console.log(mv.fifth);
console.log(v.first, v.second, v.third, v.fourth);
// @ts-expect-error as it's not allowed
console.log(v.fifth);
}
describe('getOptions', () => {
it('deals with boolean options', () => {
expect(getOptions(createCommand(), allOptions, ['command', 'name', '--first'])).toMatchObject({
first: true,
second: true,
});
});
it('deals with inverse boolean options', () => {
expect(
getOptions(createCommand(), allOptions, ['command', 'name', '--no-second'])
).toMatchObject({
first: false,
second: false,
});
});
it('deals with short options', () => {
expect(getOptions(createCommand(), allOptions, ['command', 'name', '-f', '-S'])).toMatchObject({
first: true,
second: false,
});
});
it('deals with string options', () => {
const r = getOptions(createCommand(), allOptions, ['command', 'name', '--third', 'one']);
expect(
getOptions(createCommand(), allOptions, ['command', 'name', '--third', 'one'])
).toMatchObject({
third: 'one',
});
});
it('disallows invalid string options', () => {
expect(() =>
getOptions(createCommand(), allOptions, ['command', 'name', '--third', 'random'])
).toThrow(/Unexpected value/);
});
it('deals with multiple string options', () => {
expect(
getOptions(createCommand(), allOptions, ['command', 'name', '--fourth', 'a'])
).toMatchObject({
fourth: ['a'],
});
expect(
getOptions(createCommand(), allOptions, ['command', 'name', '--fourth', 'a', '--fourth', 'b'])
).toMatchObject({
fourth: ['a', 'b'],
});
});
it('disallows invalid multiple string options', () => {
expect(() =>
getOptions(createCommand(), allOptions, ['command', 'name', '--fourth', 'random'])
).toThrow(/Unexpected value/);
});
});
describe('areOptionsSatisfied', () => {
it('checks each required string option has a value', () => {
expect(
areOptionsSatisfied(allOptions, {
first: true,
second: true,
third: undefined,
fourth: ['a', 'c'],
})
).toBe(false);
expect(
areOptionsSatisfied(allOptions, {
first: true,
second: true,
third: 'one',
fourth: [],
})
).toBe(true);
});
});
describe('getCommand', () => {
const { first, second, third, fourth } = allOptions;
it('works with boolean options', () => {
expect(getCommand('node foo', { first, second }, { first: true, second: true })).toBe(
'node foo --first'
);
});
it('works with inverse boolean options', () => {
expect(getCommand('node foo', { first, second }, { first: false, second: false })).toBe(
'node foo --no-second'
);
});
it('works with string options', () => {
expect(getCommand('node foo', { third }, { third: 'one' })).toBe('node foo --third one');
});
it('works with multiple string options', () => {
expect(getCommand('node foo', { fourth }, { fourth: ['a', 'b'] })).toBe(
'node foo --fourth a --fourth b'
);
});
// This is for convenience
it('works with partial options', () => {
expect(getCommand('node foo', allOptions, { third: 'one' })).toBe(
'node foo --no-second --third one'
);
});
it('works with combinations string options', () => {
expect(
getCommand('node foo', allOptions, {
first: true,
second: false,
third: 'one',
fourth: ['a', 'b'],
})
).toBe('node foo --first --no-second --third one --fourth a --fourth b');
});
});