-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils_test.ts
189 lines (156 loc) · 5.13 KB
/
utils_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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import { describe, it } from 'https://deno.land/std@0.224.0/testing/bdd.ts'
import { expect } from 'https://deno.land/std@0.224.0/expect/mod.ts'
import {
findDeepestParent,
findExactCommand,
hasOptions,
helpMessageForCommand,
} from './utils.ts'
import { CLI } from './spektr.ts'
import { Command } from './types.ts'
describe('hasOptions', () => {
it('detects regular args', () => {
expect(hasOptions(['--test'])).toEqual(true)
})
it('detects short', () => {
expect(hasOptions(['-t'])).toEqual(true)
})
it('detects arg with value', () => {
expect(hasOptions(['--test=123'])).toEqual(true)
})
it('detects multiple args', () => {
expect(hasOptions(['--test', '--test2'])).toEqual(true)
})
it('returns false if no args are detected', () => {
expect(hasOptions(['test'])).toEqual(false)
})
it('returns false on "--" string', () => {
expect(hasOptions(['--'])).toEqual(false)
})
})
describe('findExactCommand', () => {
it('returns the command with the matching option if there are multiple commands and args', () => {
const commands: Command[] = [
{
name: 'test',
path: ['test'],
action: () => {},
options: [{ name: 'opt1', short: 'o', type: 'boolean' }],
},
{
name: 'test2',
path: ['test'],
action: () => {},
options: [{ name: 'opt2', short: 'e', type: 'boolean' }],
},
]
const command = findExactCommand(commands, ['test', '--opt2'])
expect(command?.name).toEqual('test2')
})
it('returns the command with the matching short if there are multiple commands and args', () => {
const commands: Command[] = [
{
name: 'test1',
path: ['test'],
action: () => {},
options: [{ name: 'opt1', short: 'o', type: 'boolean' }],
},
{
name: 'test2',
path: ['test'],
action: () => {},
options: [{ name: 'opt2', short: 'e', type: 'boolean' }],
},
]
const command = findExactCommand(commands, ['test', '-o'])
expect(command?.name).toEqual('test1')
})
it('returns the first command with the same options if there are multiple commands and args', () => {
const commands: Command[] = [
{
name: 'test1',
path: ['test'],
action: () => {},
options: [{ name: 'opt1', short: 'o', type: 'boolean' }],
},
{
name: 'test2',
path: ['test'],
action: () => {},
options: [{ name: 'opt1', short: 'o', type: 'boolean' }],
},
]
const command = findExactCommand(commands, ['test', '--opt1'])
expect(command?.name).toEqual('test1')
})
it('returns undefined if there are no commands', () => {
const commands: Command[] = []
const args = ['--opt1']
const command = findExactCommand(commands, args)
expect(command).toBeUndefined()
})
})
describe('helpMessageForCommand', () => {
it('outputs help message for a command', () => {
const cli = new CLI({ name: 'cli' })
cli.command('test', () => {}, {
options: [{
name: 'test',
short: 't',
type: 'boolean',
description: 'testing',
}],
})
const message = helpMessageForCommand(cli.commands[0])
expect(message).toEqual(
`Usage: test [args]\n --test, -t testing \n --help, -h shows this message \n`,
)
})
it('outputs help message for a command with no options', () => {
const cli = new CLI({ name: 'cli' })
cli.command('test', () => {})
const message = helpMessageForCommand(cli.commands[0])
expect(message).toEqual(
'Usage: test [args]\n --help, -h shows this message \n',
)
})
it('outputs help message for a command with no description', () => {
const cli = new CLI({ name: 'cli' })
cli.command('test', () => {}, {
options: [{ name: 'test', type: 'boolean', short: 't' }],
})
const message = helpMessageForCommand(cli.commands[0])
expect(message).toEqual(
'Usage: test [args]\n' + ' --test, -t \n' +
' --help, -h shows this message \n',
)
})
it('allows not specifying short name for an option', () => {
const cli = new CLI({ name: 'cli' })
cli.command('test', () => {}, {
options: [{ name: 'test', type: 'boolean' }],
})
const message = helpMessageForCommand(cli.commands[0])
expect(message).toEqual(
'Usage: test [args]\n' + ' --test \n' +
' --help, -h shows this message \n',
)
})
})
describe('findDeepestParent', () => {
it('returns a cli if it has no parent', () => {
const cli = new CLI({ name: 'cli' })
expect(findDeepestParent(cli)).toEqual(cli)
})
it('returns a parent cli if it has a parent', () => {
const cli = new CLI({ name: 'cli' })
const child = cli.program('child')
expect(findDeepestParent(child)).toEqual(cli)
})
it('returns the deepest cli if it has multiple parents', () => {
const cli = new CLI({ name: 'cli' })
const child = cli.program('child')
const grandchild = child.program('grandchild')
expect(findDeepestParent(grandchild)).toEqual(cli)
})
})