UNPKG

3.96 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('default value', function () {
9 let defs = [
10 { name: 'one' },
11 { name: 'two', defaultValue: 'two' }
12 ]
13 let argv = [ '--one', '1' ]
14 a.deepStrictEqual(commandLineArgs(defs, { argv }), {
15 one: '1',
16 two: 'two'
17 })
18
19 defs = [ { name: 'two', defaultValue: 'two' } ]
20 argv = []
21 a.deepStrictEqual(commandLineArgs(defs, { argv }), { two: 'two' })
22
23 defs = [ { name: 'two', defaultValue: 'two' } ]
24 argv = [ '--two', 'zwei' ]
25 a.deepStrictEqual(commandLineArgs(defs, { argv }), { two: 'zwei' })
26
27 defs = [ { name: 'two', multiple: true, defaultValue: [ 'two', 'zwei' ] } ]
28 argv = [ '--two', 'duo' ]
29 a.deepStrictEqual(commandLineArgs(defs, { argv }), { two: [ 'duo' ] })
30})
31
32runner.test('default value 2', function () {
33 const defs = [{ name: 'two', multiple: true, defaultValue: ['two', 'zwei'] }]
34 const result = commandLineArgs(defs, [])
35 a.deepStrictEqual(result, { two: [ 'two', 'zwei' ] })
36})
37
38runner.test('default value: array as defaultOption', function () {
39 const defs = [
40 { name: 'two', multiple: true, defaultValue: ['two', 'zwei'], defaultOption: true }
41 ]
42 const argv = [ 'duo' ]
43 a.deepStrictEqual(commandLineArgs(defs, { argv }), { two: [ 'duo' ] })
44})
45
46runner.test('default value: falsy default values', function () {
47 const defs = [
48 { name: 'one', defaultValue: 0 },
49 { name: 'two', defaultValue: false }
50 ]
51
52 const argv = []
53 a.deepStrictEqual(commandLineArgs(defs, { argv }), {
54 one: 0,
55 two: false
56 })
57})
58
59runner.test('default value: is arrayifed if multiple set', function () {
60 const defs = [
61 { name: 'one', defaultValue: 0, multiple: true }
62 ]
63
64 let argv = []
65 a.deepStrictEqual(commandLineArgs(defs, { argv }), {
66 one: [ 0 ]
67 })
68 argv = [ '--one', '2' ]
69 a.deepStrictEqual(commandLineArgs(defs, { argv }), {
70 one: [ '2' ]
71 })
72})
73
74runner.test('default value: combined with defaultOption', function () {
75 const defs = [
76 { name: 'path', defaultOption: true, defaultValue: './' }
77 ]
78
79 let argv = [ '--path', 'test' ]
80 a.deepStrictEqual(commandLineArgs(defs, { argv }), {
81 path: 'test'
82 })
83 argv = [ 'test' ]
84 a.deepStrictEqual(commandLineArgs(defs, { argv }), {
85 path: 'test'
86 })
87 argv = [ ]
88 a.deepStrictEqual(commandLineArgs(defs, { argv }), {
89 path: './'
90 })
91})
92
93runner.test('default value: combined with multiple and defaultOption', function () {
94 const defs = [
95 { name: 'path', multiple: true, defaultOption: true, defaultValue: './' }
96 ]
97
98 let argv = [ '--path', 'test1', 'test2' ]
99 a.deepStrictEqual(commandLineArgs(defs, { argv }), {
100 path: [ 'test1', 'test2' ]
101 })
102 argv = [ '--path', 'test' ]
103 a.deepStrictEqual(commandLineArgs(defs, { argv }), {
104 path: [ 'test' ]
105 })
106 argv = [ 'test1', 'test2' ]
107 a.deepStrictEqual(commandLineArgs(defs, { argv }), {
108 path: [ 'test1', 'test2' ]
109 })
110 argv = [ 'test' ]
111 a.deepStrictEqual(commandLineArgs(defs, { argv }), {
112 path: [ 'test' ]
113 })
114 argv = [ ]
115 a.deepStrictEqual(commandLineArgs(defs, { argv }), {
116 path: [ './' ]
117 })
118})
119
120runner.test('default value: array default combined with multiple and defaultOption', function () {
121 const defs = [
122 { name: 'path', multiple: true, defaultOption: true, defaultValue: [ './' ] }
123 ]
124
125 let argv = [ '--path', 'test1', 'test2' ]
126 a.deepStrictEqual(commandLineArgs(defs, { argv }), {
127 path: [ 'test1', 'test2' ]
128 })
129 argv = [ '--path', 'test' ]
130 a.deepStrictEqual(commandLineArgs(defs, { argv }), {
131 path: [ 'test' ]
132 })
133 argv = [ 'test1', 'test2' ]
134 a.deepStrictEqual(commandLineArgs(defs, { argv }), {
135 path: [ 'test1', 'test2' ]
136 })
137 argv = [ 'test' ]
138 a.deepStrictEqual(commandLineArgs(defs, { argv }), {
139 path: [ 'test' ]
140 })
141 argv = [ ]
142 a.deepStrictEqual(commandLineArgs(defs, { argv }), {
143 path: [ './' ]
144 })
145})