Skip to content

Commit cee5d42

Browse files
committed
Add tests for the new CommandLineParameter values
1 parent 538a43c commit cee5d42

File tree

2 files changed

+245
-7
lines changed

2 files changed

+245
-7
lines changed

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

Lines changed: 100 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33

44
import * as colors from 'colors';
55

6+
import { CommandLineAction } from '../CommandLineAction';
7+
import { CommandLineParser } from '../CommandLineParser';
68
import { DynamicCommandLineParser } from '../DynamicCommandLineParser';
79
import { DynamicCommandLineAction } from '../DynamicCommandLineAction';
810

9-
describe('CommandLineParameter', () => {
11+
function createParser(): DynamicCommandLineParser {
1012
const commandLineParser: DynamicCommandLineParser = new DynamicCommandLineParser(
1113
{
1214
toolFilename: 'example',
@@ -27,12 +29,17 @@ describe('CommandLineParameter', () => {
2729
commandLineParser.addAction(action);
2830

2931
action.defineChoiceParameter({
30-
parameterLongName: '--choice',
31-
parameterShortName: '-c',
32-
description: 'A choice',
32+
parameterLongName: '--choice-with-default',
33+
description: 'A choice with a default',
3334
alternatives: [ 'one', 'two' ],
3435
defaultValue: 'one'
3536
});
37+
action.defineChoiceParameter({
38+
parameterLongName: '--choice',
39+
parameterShortName: '-c',
40+
description: 'A choice without a default',
41+
alternatives: [ 'one', 'two' ]
42+
});
3643
action.defineFlagParameter({
3744
parameterLongName: '--flag',
3845
parameterShortName: '-f',
@@ -56,14 +63,102 @@ describe('CommandLineParameter', () => {
5663
description: 'A string list',
5764
argumentName: 'LIST'
5865
});
66+
return commandLineParser;
67+
}
68+
69+
function expectPropertiesToMatchSnapshot(object: {}, propertyNames: string[]): void {
70+
const snapshotObject: {} = {};
71+
72+
for (const propertyName of propertyNames) {
73+
snapshotObject[propertyName] = object[propertyName];
74+
}
75+
expect(snapshotObject).toMatchSnapshot();
76+
}
5977

78+
describe('CommandLineParameter', () => {
6079
it('prints the global help', () => {
80+
const commandLineParser: CommandLineParser = createParser();
6181
const helpText: string = colors.stripColors(commandLineParser.renderHelpText());
6282
expect(helpText).toMatchSnapshot();
6383
});
6484

6585
it('prints the action help', () => {
66-
const helpText: string = colors.stripColors(action.renderHelpText());
86+
const commandLineParser: CommandLineParser = createParser();
87+
const helpText: string = colors.stripColors(commandLineParser.getAction('do-job').renderHelpText());
6788
expect(helpText).toMatchSnapshot();
6889
});
90+
91+
it('parses an input with ALL parameters', () => {
92+
const commandLineParser: CommandLineParser = createParser();
93+
const action: CommandLineAction = commandLineParser.getAction('do-job');
94+
95+
const args: string[] = [ '-g',
96+
'do-job', '-c', 'two', '-f', '-i', '123', '-s', 'hello', '-l', 'first', '-l', 'second'];
97+
98+
return commandLineParser.execute(args).then(() => {
99+
expect(commandLineParser.selectedAction).toBe(action);
100+
101+
expectPropertiesToMatchSnapshot(
102+
commandLineParser.getFlagParameter('--global-flag'),
103+
['description', 'kind', 'longName', 'shortName', 'value']
104+
);
105+
106+
expectPropertiesToMatchSnapshot(
107+
action.getChoiceParameter('--choice'),
108+
['alternatives', 'defaultValue', 'description', 'kind', 'longName', 'shortName', 'value']
109+
);
110+
expectPropertiesToMatchSnapshot(
111+
action.getFlagParameter('--flag'),
112+
['description', 'kind', 'longName', 'shortName', 'value']
113+
);
114+
expectPropertiesToMatchSnapshot(
115+
action.getIntegerParameter('--integer'),
116+
['argumentName', 'description', 'kind', 'longName', 'shortName', 'value']
117+
);
118+
expectPropertiesToMatchSnapshot(
119+
action.getStringParameter('--string'),
120+
['argumentName', 'description', 'kind', 'longName', 'shortName', 'value']
121+
);
122+
expectPropertiesToMatchSnapshot(
123+
action.getStringListParameter('--string-list'),
124+
['argumentName', 'description', 'kind', 'longName', 'shortName', 'values']
125+
);
126+
});
127+
});
128+
129+
it('parses an input with NO parameters', () => {
130+
const commandLineParser: CommandLineParser = createParser();
131+
const action: CommandLineAction = commandLineParser.getAction('do-job');
132+
const args: string[] = [ 'do-job'];
133+
134+
return commandLineParser.execute(args).then(() => {
135+
expect(commandLineParser.selectedAction).toBe(action);
136+
137+
expectPropertiesToMatchSnapshot(
138+
commandLineParser.getFlagParameter('--global-flag'),
139+
['description', 'kind', 'longName', 'shortName', 'value']
140+
);
141+
142+
expectPropertiesToMatchSnapshot(
143+
action.getChoiceParameter('--choice'),
144+
['alternatives', 'defaultValue', 'description', 'kind', 'longName', 'shortName', 'value']
145+
);
146+
expectPropertiesToMatchSnapshot(
147+
action.getFlagParameter('--flag'),
148+
['description', 'kind', 'longName', 'shortName', 'value']
149+
);
150+
expectPropertiesToMatchSnapshot(
151+
action.getIntegerParameter('--integer'),
152+
['argumentName', 'description', 'kind', 'longName', 'shortName', 'value']
153+
);
154+
expectPropertiesToMatchSnapshot(
155+
action.getStringParameter('--string'),
156+
['argumentName', 'description', 'kind', 'longName', 'shortName', 'value']
157+
);
158+
expectPropertiesToMatchSnapshot(
159+
action.getStringListParameter('--string-list'),
160+
['argumentName', 'description', 'kind', 'longName', 'shortName', 'values']
161+
);
162+
});
163+
});
69164
});

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

Lines changed: 145 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,157 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`CommandLineParameter parses an input with ALL parameters 1`] = `
4+
Object {
5+
"description": "A flag that affects all actions",
6+
"kind": 1,
7+
"longName": "--global-flag",
8+
"shortName": "-g",
9+
"value": true,
10+
}
11+
`;
12+
13+
exports[`CommandLineParameter parses an input with ALL parameters 2`] = `
14+
Object {
15+
"alternatives": Array [
16+
"one",
17+
"two",
18+
],
19+
"defaultValue": undefined,
20+
"description": "A choice without a default",
21+
"kind": 0,
22+
"longName": "--choice",
23+
"shortName": "-c",
24+
"value": "two",
25+
}
26+
`;
27+
28+
exports[`CommandLineParameter parses an input with ALL parameters 3`] = `
29+
Object {
30+
"description": "A flag",
31+
"kind": 1,
32+
"longName": "--flag",
33+
"shortName": "-f",
34+
"value": true,
35+
}
36+
`;
37+
38+
exports[`CommandLineParameter parses an input with ALL parameters 4`] = `
39+
Object {
40+
"argumentName": "NUMBER",
41+
"description": "An integer",
42+
"kind": 2,
43+
"longName": "--integer",
44+
"shortName": "-i",
45+
"value": 123,
46+
}
47+
`;
48+
49+
exports[`CommandLineParameter parses an input with ALL parameters 5`] = `
50+
Object {
51+
"argumentName": "TEXT",
52+
"description": "A string",
53+
"kind": 3,
54+
"longName": "--string",
55+
"shortName": "-s",
56+
"value": "hello",
57+
}
58+
`;
59+
60+
exports[`CommandLineParameter parses an input with ALL parameters 6`] = `
61+
Object {
62+
"argumentName": "LIST",
63+
"description": "A string list",
64+
"kind": 4,
65+
"longName": "--string-list",
66+
"shortName": "-l",
67+
"values": Array [
68+
"first",
69+
"second",
70+
],
71+
}
72+
`;
73+
74+
exports[`CommandLineParameter parses an input with NO parameters 1`] = `
75+
Object {
76+
"description": "A flag that affects all actions",
77+
"kind": 1,
78+
"longName": "--global-flag",
79+
"shortName": "-g",
80+
"value": false,
81+
}
82+
`;
83+
84+
exports[`CommandLineParameter parses an input with NO parameters 2`] = `
85+
Object {
86+
"alternatives": Array [
87+
"one",
88+
"two",
89+
],
90+
"defaultValue": undefined,
91+
"description": "A choice without a default",
92+
"kind": 0,
93+
"longName": "--choice",
94+
"shortName": "-c",
95+
"value": undefined,
96+
}
97+
`;
98+
99+
exports[`CommandLineParameter parses an input with NO parameters 3`] = `
100+
Object {
101+
"description": "A flag",
102+
"kind": 1,
103+
"longName": "--flag",
104+
"shortName": "-f",
105+
"value": false,
106+
}
107+
`;
108+
109+
exports[`CommandLineParameter parses an input with NO parameters 4`] = `
110+
Object {
111+
"argumentName": "NUMBER",
112+
"description": "An integer",
113+
"kind": 2,
114+
"longName": "--integer",
115+
"shortName": "-i",
116+
"value": undefined,
117+
}
118+
`;
119+
120+
exports[`CommandLineParameter parses an input with NO parameters 5`] = `
121+
Object {
122+
"argumentName": "TEXT",
123+
"description": "A string",
124+
"kind": 3,
125+
"longName": "--string",
126+
"shortName": "-s",
127+
"value": undefined,
128+
}
129+
`;
130+
131+
exports[`CommandLineParameter parses an input with NO parameters 6`] = `
132+
Object {
133+
"argumentName": "LIST",
134+
"description": "A string list",
135+
"kind": 4,
136+
"longName": "--string-list",
137+
"shortName": "-l",
138+
"values": Array [],
139+
}
140+
`;
141+
3142
exports[`CommandLineParameter prints the action help 1`] = `
4-
"usage: example do-job [-h] [-c {one,two}] [-f] [-i NUMBER] [-s TEXT] [-l LIST]
143+
"usage: example do-job [-h] [--choice-with-default {one,two}] [-c {one,two}]
144+
[-f] [-i NUMBER] [-s TEXT] [-l LIST]
145+
5146
6147
a longer description
7148
8149
Optional arguments:
9150
-h, --help Show this help message and exit.
151+
--choice-with-default {one,two}
152+
A choice with a default
10153
-c {one,two}, --choice {one,two}
11-
A choice
154+
A choice without a default
12155
-f, --flag A flag
13156
-i NUMBER, --integer NUMBER
14157
An integer

0 commit comments

Comments
 (0)