Skip to content

Commit 9a91e91

Browse files
committed
Update unit test
1 parent 1581be0 commit 9a91e91

File tree

2 files changed

+312
-48
lines changed

2 files changed

+312
-48
lines changed

libraries/ts-command-line/src/test/CommandLineParameter.test.ts

Lines changed: 118 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -28,40 +28,76 @@ function createParser(): DynamicCommandLineParser {
2828
});
2929
commandLineParser.addAction(action);
3030

31-
action.defineChoiceParameter({
32-
parameterLongName: '--choice-with-default',
33-
description: 'A choice with a default',
34-
alternatives: [ 'one', 'two' ],
35-
defaultValue: 'one'
36-
});
31+
// Choice
3732
action.defineChoiceParameter({
3833
parameterLongName: '--choice',
3934
parameterShortName: '-c',
40-
description: 'A choice without a default',
41-
alternatives: [ 'one', 'two' ]
35+
description: 'A choice',
36+
alternatives: [ 'one', 'two', 'three', 'default' ],
37+
environmentVariable: 'ENV_CHOICE'
4238
});
39+
action.defineChoiceParameter({
40+
parameterLongName: '--choice-with-default',
41+
description: 'A choice with a default',
42+
alternatives: [ 'one', 'two', 'three', 'default' ],
43+
environmentVariable: 'ENV_CHOICE',
44+
defaultValue: 'default'
45+
});
46+
47+
// Flag
4348
action.defineFlagParameter({
4449
parameterLongName: '--flag',
4550
parameterShortName: '-f',
46-
description: 'A flag'
51+
description: 'A flag',
52+
environmentVariable: 'ENV_FLAG'
4753
});
54+
55+
// Integer
4856
action.defineIntegerParameter({
4957
parameterLongName: '--integer',
5058
parameterShortName: '-i',
5159
description: 'An integer',
52-
argumentName: 'NUMBER'
60+
argumentName: 'NUMBER',
61+
environmentVariable: 'ENV_INTEGER'
62+
});
63+
action.defineIntegerParameter({
64+
parameterLongName: '--integer-with-default',
65+
description: 'An integer with a default',
66+
argumentName: 'NUMBER',
67+
environmentVariable: 'ENV_INTEGER',
68+
defaultValue: 123
5369
});
70+
action.defineIntegerParameter({
71+
parameterLongName: '--integer-required',
72+
description: 'An integer',
73+
argumentName: 'NUMBER',
74+
environmentVariable: 'ENV_INTEGER',
75+
required: true
76+
});
77+
78+
// String
5479
action.defineStringParameter({
5580
parameterLongName: '--string',
5681
parameterShortName: '-s',
5782
description: 'A string',
58-
argumentName: 'TEXT'
83+
argumentName: 'TEXT',
84+
environmentVariable: 'ENV_INTEGER'
85+
});
86+
action.defineStringParameter({
87+
parameterLongName: '--string-with-default',
88+
description: 'A string with a default',
89+
argumentName: 'TEXT',
90+
environmentVariable: 'ENV_INTEGER',
91+
defaultValue: '123'
5992
});
93+
94+
// String List
6095
action.defineStringListParameter({
6196
parameterLongName: '--string-list',
6297
parameterShortName: '-l',
6398
description: 'A string list',
64-
argumentName: 'LIST'
99+
argumentName: 'LIST',
100+
environmentVariable: 'ENV_INTEGER'
65101
});
66102
return commandLineParser;
67103
}
@@ -75,6 +111,20 @@ function expectPropertiesToMatchSnapshot(object: {}, propertyNames: string[]): v
75111
expect(snapshotObject).toMatchSnapshot();
76112
}
77113

114+
const snapshotPropertyNames: string[] = [
115+
'description',
116+
'kind',
117+
'longName',
118+
'shortName',
119+
'value',
120+
'kind',
121+
'argumentName',
122+
'environmentVariable',
123+
'required',
124+
'defaultValue',
125+
'values'
126+
];
127+
78128
describe('CommandLineParameter', () => {
79129
it('prints the global help', () => {
80130
const commandLineParser: CommandLineParser = createParser();
@@ -92,72 +142,113 @@ describe('CommandLineParameter', () => {
92142
const commandLineParser: CommandLineParser = createParser();
93143
const action: CommandLineAction = commandLineParser.getAction('do-job');
94144

95-
const args: string[] = [ '-g',
96-
'do-job', '-c', 'two', '-f', '-i', '123', '-s', 'hello', '-l', 'first', '-l', 'second'];
145+
const args: string[] = [
146+
'--global-flag',
147+
'do-job',
148+
'--choice', 'two',
149+
'--flag',
150+
'--integer', '123',
151+
'--integer-required', '321',
152+
'--string', 'hello',
153+
'--string-list', 'first',
154+
'--string-list', 'second'
155+
];
97156

98157
return commandLineParser.execute(args).then(() => {
99158
expect(commandLineParser.selectedAction).toBe(action);
100159

101160
expectPropertiesToMatchSnapshot(
102161
commandLineParser.getFlagParameter('--global-flag'),
103-
['description', 'kind', 'longName', 'shortName', 'value']
162+
snapshotPropertyNames
104163
);
105164

106165
expectPropertiesToMatchSnapshot(
107166
action.getChoiceParameter('--choice'),
108-
['alternatives', 'defaultValue', 'description', 'kind', 'longName', 'shortName', 'value']
167+
snapshotPropertyNames
168+
);
169+
expectPropertiesToMatchSnapshot(
170+
action.getChoiceParameter('--choice-with-default'),
171+
snapshotPropertyNames
109172
);
110173
expectPropertiesToMatchSnapshot(
111174
action.getFlagParameter('--flag'),
112-
['description', 'kind', 'longName', 'shortName', 'value']
175+
snapshotPropertyNames
113176
);
114177
expectPropertiesToMatchSnapshot(
115178
action.getIntegerParameter('--integer'),
116-
['argumentName', 'description', 'kind', 'longName', 'shortName', 'value']
179+
snapshotPropertyNames
180+
);
181+
expectPropertiesToMatchSnapshot(
182+
action.getIntegerParameter('--integer-with-default'),
183+
snapshotPropertyNames
184+
);
185+
expectPropertiesToMatchSnapshot(
186+
action.getIntegerParameter('--integer-required'),
187+
snapshotPropertyNames
117188
);
118189
expectPropertiesToMatchSnapshot(
119190
action.getStringParameter('--string'),
120-
['argumentName', 'description', 'kind', 'longName', 'shortName', 'value']
191+
snapshotPropertyNames
192+
);
193+
expectPropertiesToMatchSnapshot(
194+
action.getStringParameter('--string-with-default'),
195+
snapshotPropertyNames
121196
);
122197
expectPropertiesToMatchSnapshot(
123198
action.getStringListParameter('--string-list'),
124-
['argumentName', 'description', 'kind', 'longName', 'shortName', 'values']
199+
snapshotPropertyNames
125200
);
126201
});
127202
});
128203

129204
it('parses an input with NO parameters', () => {
130205
const commandLineParser: CommandLineParser = createParser();
131206
const action: CommandLineAction = commandLineParser.getAction('do-job');
132-
const args: string[] = [ 'do-job'];
207+
const args: string[] = [ 'do-job', '--integer-required', '123'];
133208

134209
return commandLineParser.execute(args).then(() => {
135210
expect(commandLineParser.selectedAction).toBe(action);
136211

137212
expectPropertiesToMatchSnapshot(
138213
commandLineParser.getFlagParameter('--global-flag'),
139-
['description', 'kind', 'longName', 'shortName', 'value']
214+
snapshotPropertyNames
140215
);
141216

142217
expectPropertiesToMatchSnapshot(
143218
action.getChoiceParameter('--choice'),
144-
['alternatives', 'defaultValue', 'description', 'kind', 'longName', 'shortName', 'value']
219+
snapshotPropertyNames
220+
);
221+
expectPropertiesToMatchSnapshot(
222+
action.getChoiceParameter('--choice-with-default'),
223+
snapshotPropertyNames
145224
);
146225
expectPropertiesToMatchSnapshot(
147226
action.getFlagParameter('--flag'),
148-
['description', 'kind', 'longName', 'shortName', 'value']
227+
snapshotPropertyNames
149228
);
150229
expectPropertiesToMatchSnapshot(
151230
action.getIntegerParameter('--integer'),
152-
['argumentName', 'description', 'kind', 'longName', 'shortName', 'value']
231+
snapshotPropertyNames
232+
);
233+
expectPropertiesToMatchSnapshot(
234+
action.getIntegerParameter('--integer-with-default'),
235+
snapshotPropertyNames
236+
);
237+
expectPropertiesToMatchSnapshot(
238+
action.getIntegerParameter('--integer-required'),
239+
snapshotPropertyNames
153240
);
154241
expectPropertiesToMatchSnapshot(
155242
action.getStringParameter('--string'),
156-
['argumentName', 'description', 'kind', 'longName', 'shortName', 'value']
243+
snapshotPropertyNames
244+
);
245+
expectPropertiesToMatchSnapshot(
246+
action.getStringParameter('--string-with-default'),
247+
snapshotPropertyNames
157248
);
158249
expectPropertiesToMatchSnapshot(
159250
action.getStringListParameter('--string-list'),
160-
['argumentName', 'description', 'kind', 'longName', 'shortName', 'values']
251+
snapshotPropertyNames
161252
);
162253
});
163254
});

0 commit comments

Comments
 (0)