-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparse_test.ts
94 lines (93 loc) · 2.32 KB
/
parse_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
import { describe, it } from 'https://deno.land/[email protected]/testing/bdd.ts'
import { expect } from 'https://deno.land/[email protected]/expect/mod.ts'
import { handleArgParsing } from './parse.ts'
describe('handleArgParsing', () => {
it('parses command line arguments and positionals based on given options', () => {
expect(handleArgParsing({
options: [
{
name: 'test',
type: 'string',
},
],
}, ['pos1', '--test=val', 'pos2'])).toEqual({
parsed: { test: 'val' },
positionals: ['pos1', 'pos2'],
})
})
it('supports aliases and returns them as well', () => {
expect(handleArgParsing({
options: [
{
name: 'test',
type: 'string',
short: 't',
},
],
}, ['-t', 'val'])).toEqual({
parsed: { test: 'val' },
positionals: [],
})
})
it('throws on unknown arg', () => {
try {
handleArgParsing({
options: [],
}, ['-t=val'])
} catch (e) {
expect((e as Error).message).toEqual('Unknown argument: t')
}
})
it('throws on invalid arg type', () => {
try {
handleArgParsing({
options: [{ name: 'test', type: 'boolean' }],
}, ['--test', 'val'])
} catch (e) {
expect((e as Error).message).toEqual(
'Invalid argument type for test: expected boolean, got string',
)
}
})
it('throws if required arg is not present', () => {
try {
handleArgParsing({
options: [{
name: 'test',
type: 'boolean',
required: true,
}],
}, [])
} catch (e) {
expect((e as Error).message).toEqual(
'Argument test is required',
)
}
})
it('does not throw for required if short is passed', () => {
expect(handleArgParsing({
options: [{
name: 'test',
type: 'boolean',
required: true,
short: 't',
}],
}, ['-t'])).toEqual({ parsed: { test: true }, positionals: [] })
})
it('throws if neither arg nor short was passed', () => {
try {
handleArgParsing({
options: [{
name: 'test',
type: 'boolean',
required: true,
short: 't',
}],
}, [])
} catch (e) {
expect((e as Error).message).toEqual(
'Argument test is required',
)
}
})
})