UNPKG

2.92 kBJavaScriptView Raw
1'use strict'
2const TestRunner = require('test-runner')
3const commandLineArgs = require('../')
4const a = require('assert')
5
6const runner = new TestRunner()
7
8runner.test('defaultOption: string', function () {
9 const optionDefinitions = [
10 { name: 'files', defaultOption: true }
11 ]
12 const argv = [ 'file1', 'file2' ]
13 a.deepStrictEqual(commandLineArgs(optionDefinitions, { argv }), {
14 files: 'file1'
15 })
16})
17
18runner.test('defaultOption: multiple string', function () {
19 const optionDefinitions = [
20 { name: 'files', defaultOption: true, multiple: true }
21 ]
22 const argv = [ 'file1', 'file2' ]
23 a.deepStrictEqual(commandLineArgs(optionDefinitions, { argv }), {
24 files: [ 'file1', 'file2' ]
25 })
26})
27
28runner.test('defaultOption: after a boolean', function () {
29 const definitions = [
30 { name: 'one', type: Boolean },
31 { name: 'two', defaultOption: true }
32 ]
33 a.deepStrictEqual(
34 commandLineArgs(definitions, { argv: [ '--one', 'sfsgf' ] }),
35 { one: true, two: 'sfsgf' }
36 )
37})
38
39runner.test('defaultOption: multiple defaultOption values between other arg/value pairs', function () {
40 const optionDefinitions = [
41 { name: 'one' },
42 { name: 'two' },
43 { name: 'files', defaultOption: true, multiple: true }
44 ]
45 const argv = [ '--one', '1', 'file1', 'file2', '--two', '2' ]
46 a.deepStrictEqual(commandLineArgs(optionDefinitions, { argv }), {
47 one: '1',
48 two: '2',
49 files: [ 'file1', 'file2' ]
50 })
51})
52
53runner.test('defaultOption: multiple defaultOption values between other arg/value pairs 2', function () {
54 const optionDefinitions = [
55 { name: 'one', type: Boolean },
56 { name: 'two' },
57 { name: 'files', defaultOption: true, multiple: true }
58 ]
59 const argv = [ 'file0', '--one', 'file1', '--files', 'file2', '--two', '2', 'file3' ]
60 a.deepStrictEqual(commandLineArgs(optionDefinitions, { argv }), {
61 one: true,
62 two: '2',
63 files: [ 'file0', 'file1', 'file2', 'file3' ]
64 })
65})
66
67runner.test('defaultOption: floating args present but no defaultOption', function () {
68 const definitions = [
69 { name: 'one', type: Boolean }
70 ]
71 a.deepStrictEqual(
72 commandLineArgs(definitions, { argv: [ 'aaa', '--one', 'aaa', 'aaa' ] }),
73 { one: true }
74 )
75})
76
77runner.test('defaultOption: non-multiple should take first value', function () {
78 const optionDefinitions = [
79 { name: 'file', defaultOption: true }
80 ]
81 const argv = [ 'file1', 'file2' ]
82 a.deepStrictEqual(commandLineArgs(optionDefinitions, { argv }), {
83 file: 'file1'
84 })
85})
86
87runner.test('defaultOption: non-multiple should take first value 2', function () {
88 const optionDefinitions = [
89 { name: 'file', defaultOption: true },
90 { name: 'one', type: Boolean },
91 { name: 'two', type: Boolean }
92 ]
93 const argv = [ '--two', 'file1', '--one', 'file2' ]
94 a.deepStrictEqual(commandLineArgs(optionDefinitions, { argv }), {
95 file: 'file1',
96 two: true,
97 one: true
98 })
99})