Skip to content

Commit 6e7ae77

Browse files
committed
separate file for single tests
1 parent 2045543 commit 6e7ae77

2 files changed

Lines changed: 130 additions & 120 deletions

File tree

test/single.js

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
import { strict as a } from 'assert'
2+
import CommandLineArgs from 'command-line-args'
3+
import util from 'node:util'
4+
util.inspect.defaultOptions.depth = 6
5+
util.inspect.defaultOptions.breakLength = process?.stdout?.columns || 80
6+
util.inspect.defaultOptions.maxArrayLength = Infinity
7+
8+
const [test, only, skip] = [new Map(), new Map(), new Map()]
9+
10+
test.set('single --option flag, value set', async function () {
11+
const argv = ['one', 'two', '--one', '--two']
12+
const optionDefinitions = [
13+
{
14+
name: 'one',
15+
extractor: 'single',
16+
single: '--one',
17+
output: extraction => extraction.length === 1 ? true : undefined
18+
}
19+
]
20+
const cla = new CommandLineArgs(argv)
21+
const result = await cla.parse(optionDefinitions)
22+
a.deepEqual(result, { one: true })
23+
a.deepEqual(cla.argv, ['one', 'two', '--two'])
24+
})
25+
26+
test.set('single --option flag, value set not, output returns false', async function () {
27+
const argv = ['one', 'two', '--two']
28+
const optionDefinitions = [
29+
{
30+
name: 'one',
31+
extractor: 'single',
32+
single: '--one',
33+
output: extraction => extraction.length === 1
34+
}
35+
]
36+
const cla = new CommandLineArgs(argv)
37+
const result = await cla.parse(optionDefinitions)
38+
a.deepEqual(result, { one: false })
39+
a.deepEqual(cla.argv, ['one', 'two', '--two'])
40+
})
41+
42+
test.set('single --option flag, not set. Returning undefined omits option from result.', async function () {
43+
const argv = ['one', 'two', '--two']
44+
const optionDefinitions = [
45+
{
46+
name: 'one',
47+
extractor: 'single',
48+
single: '--one',
49+
output: extraction => extraction.length === 1 ? true : undefined
50+
}
51+
]
52+
const cla = new CommandLineArgs(argv)
53+
const result = await cla.parse(optionDefinitions)
54+
a.deepEqual(result, {})
55+
a.deepEqual(cla.argv, ['one', 'two', '--two'])
56+
})
57+
58+
test.set('single regex', async function () {
59+
const argv = ['one', 'two', '--one', '--two']
60+
const optionDefinitions = [
61+
{
62+
name: 'one',
63+
extractor: 'single',
64+
single: /--o[n]+e/,
65+
output: extraction => true
66+
}
67+
]
68+
const cla = new CommandLineArgs(argv)
69+
const result = await cla.parse(optionDefinitions)
70+
a.deepEqual(result, { one: true })
71+
a.deepEqual(cla.argv, ['one', 'two', '--two'])
72+
73+
const argv2 = ['one', 'two', '--onnnnne', '--two']
74+
const cla2 = new CommandLineArgs(argv2)
75+
const result2 = cla2.parse(optionDefinitions)
76+
a.deepEqual(result, { one: true })
77+
a.deepEqual(cla.argv, ['one', 'two', '--two'])
78+
})
79+
80+
test.set('--option flag not present', async function () {
81+
const argv = ['one', 'two', '--not-one', '--two']
82+
const optionDefinitions = [
83+
{
84+
name: 'one',
85+
extractor: 'single',
86+
single: '--one',
87+
output: extraction => extraction.length === 1
88+
}
89+
]
90+
const cla = new CommandLineArgs(argv)
91+
const result = await cla.parse(optionDefinitions)
92+
a.deepEqual(result, { one: false })
93+
a.deepEqual(cla.argv, ['one', 'two', '--not-one', '--two'])
94+
})
95+
96+
test.set('Repeatable singles, raw output', async function () {
97+
const argv = ['--var', 'one', 'something', '--var', 'two', 'another', '--var']
98+
const cla = new CommandLineArgs(argv)
99+
const result = await cla.parse([
100+
{
101+
name: 'var',
102+
extractor: 'single',
103+
single: '--var',
104+
repeatable: true,
105+
output: e => e
106+
}
107+
])
108+
109+
// this.data = result
110+
a.deepEqual(result, { var: [['--var'], ['--var'], ['--var']] })
111+
})
112+
113+
test.set('Repeatable singles, boolean output', async function () {
114+
const argv = ['--var', 'one', 'something', '--var', 'two', 'another', '--var']
115+
const cla = new CommandLineArgs(argv)
116+
const result = await cla.parse([
117+
{
118+
name: 'var',
119+
extractor: 'single',
120+
single: '--var',
121+
repeatable: true,
122+
output: e => e.map(i => i.length === 1)
123+
}
124+
])
125+
126+
// this.data = result
127+
a.deepEqual(result, { var: [true, true, true] })
128+
})
129+
130+
export { test, only, skip }

test/test.js

Lines changed: 0 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -42,92 +42,6 @@ test.set('--option <no value>', async function () {
4242
a.deepEqual(cla.argv, ['one', 'two', '--two', 'three'])
4343
})
4444

45-
test.set('single --option flag, value set', async function () {
46-
const argv = ['one', 'two', '--one', '--two']
47-
const optionDefinitions = [
48-
{
49-
name: 'one',
50-
extractor: 'single',
51-
single: '--one',
52-
output: extraction => extraction.length === 1 ? true : undefined
53-
}
54-
]
55-
const cla = new CommandLineArgs(argv)
56-
const result = await cla.parse(optionDefinitions)
57-
a.deepEqual(result, { one: true })
58-
a.deepEqual(cla.argv, ['one', 'two', '--two'])
59-
})
60-
61-
test.set('single --option flag, value set not, output returns false', async function () {
62-
const argv = ['one', 'two', '--two']
63-
const optionDefinitions = [
64-
{
65-
name: 'one',
66-
extractor: 'single',
67-
single: '--one',
68-
output: extraction => extraction.length === 1
69-
}
70-
]
71-
const cla = new CommandLineArgs(argv)
72-
const result = await cla.parse(optionDefinitions)
73-
a.deepEqual(result, { one: false })
74-
a.deepEqual(cla.argv, ['one', 'two', '--two'])
75-
})
76-
77-
test.set('single --option flag, not set. Returning undefined omits option from result.', async function () {
78-
const argv = ['one', 'two', '--two']
79-
const optionDefinitions = [
80-
{
81-
name: 'one',
82-
extractor: 'single',
83-
single: '--one',
84-
output: extraction => extraction.length === 1 ? true : undefined
85-
}
86-
]
87-
const cla = new CommandLineArgs(argv)
88-
const result = await cla.parse(optionDefinitions)
89-
a.deepEqual(result, {})
90-
a.deepEqual(cla.argv, ['one', 'two', '--two'])
91-
})
92-
93-
test.set('single regex', async function () {
94-
const argv = ['one', 'two', '--one', '--two']
95-
const optionDefinitions = [
96-
{
97-
name: 'one',
98-
extractor: 'single',
99-
single: /--o[n]+e/,
100-
output: extraction => true
101-
}
102-
]
103-
const cla = new CommandLineArgs(argv)
104-
const result = await cla.parse(optionDefinitions)
105-
a.deepEqual(result, { one: true })
106-
a.deepEqual(cla.argv, ['one', 'two', '--two'])
107-
108-
const argv2 = ['one', 'two', '--onnnnne', '--two']
109-
const cla2 = new CommandLineArgs(argv2)
110-
const result2 = cla2.parse(optionDefinitions)
111-
a.deepEqual(result, { one: true })
112-
a.deepEqual(cla.argv, ['one', 'two', '--two'])
113-
})
114-
115-
test.set('--option flag not present', async function () {
116-
const argv = ['one', 'two', '--not-one', '--two']
117-
const optionDefinitions = [
118-
{
119-
name: 'one',
120-
extractor: 'single',
121-
single: '--one',
122-
output: extraction => extraction.length === 1
123-
}
124-
]
125-
const cla = new CommandLineArgs(argv)
126-
const result = await cla.parse(optionDefinitions)
127-
a.deepEqual(result, { one: false })
128-
a.deepEqual(cla.argv, ['one', 'two', '--not-one', '--two'])
129-
})
130-
13145
test.set('Positionals must be args 1 and 2, with options following', async function () {
13246
const argv = ['positional1', '100', '--option', '4', '--flag', 'unwanted']
13347

@@ -517,38 +431,4 @@ test.set('Repeating single option values without repeatable set', async function
517431
a.deepEqual(result, { var: ['--var', 'one'] })
518432
})
519433

520-
test.set('Repeatable singles, raw output', async function () {
521-
const argv = ['--var', 'one', 'something', '--var', 'two', 'another', '--var']
522-
const cla = new CommandLineArgs(argv)
523-
const result = await cla.parse([
524-
{
525-
name: 'var',
526-
extractor: 'single',
527-
single: '--var',
528-
repeatable: true,
529-
output: e => e
530-
}
531-
])
532-
533-
// this.data = result
534-
a.deepEqual(result, { var: [['--var'], ['--var'], ['--var']] })
535-
})
536-
537-
test.set('Repeatable singles, boolean output', async function () {
538-
const argv = ['--var', 'one', 'something', '--var', 'two', 'another', '--var']
539-
const cla = new CommandLineArgs(argv)
540-
const result = await cla.parse([
541-
{
542-
name: 'var',
543-
extractor: 'single',
544-
single: '--var',
545-
repeatable: true,
546-
output: e => e.map(i => i.length === 1)
547-
}
548-
])
549-
550-
// this.data = result
551-
a.deepEqual(result, { var: [true, true, true] })
552-
})
553-
554434
export { test, only, skip }

0 commit comments

Comments
 (0)