Press n or j to go to the next uncovered block, b, p or k for the previous block.
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 | const {convertCommands} = require('./convertCommands') const {parser} = require('../parser') const parsers = {__: parser(), _: parser()} test('convertCommands works as expected', () => { const opts = [ {key: 'name', types: ['string'], args: ['--name']}, {key: 'variadic', args: ['--var']}, { key: 'ask', args: ['ask'], opts: [ {key: 'question', types: ['string'], args: ['-q']}, {key: 'jokingly', types: [], args: ['-j'], defaultValue: [1]} ], values: ['-q', "What's your lastname?", '--name', 'Logan', '--var', 'foo', 'bar'] } ] const {errs, args} = convertCommands(parsers)({opts}) const expArgs = { _: [], name: 'Logan', ask: { _: ['--name', 'Logan', '--var', 'foo', 'bar'], question: "What's your lastname?" }, variadic: ['foo', 'bar'] } const expErrs = [] expect(args).toStrictEqual(expArgs) expect(errs).toStrictEqual(expErrs) }) test('convertCommands works as expected if parent parser is not set', () => { const opts = [ {key: 'name', types: ['string'], args: ['--name']}, { key: 'ask', args: ['ask'], opts: [ {key: 'question', types: ['string'], args: ['-q']}, {key: 'jokingly', types: [], args: ['-j'], defaultValue: [1]} ], values: ['-q', "What's your lastname?", '--name', 'Logan'] } ] const parsers = {_: parser()} // @ts-ignore const {errs, args} = convertCommands(parsers)({opts}) const expArgs = { _: [], ask: { _: ['--name', 'Logan'], question: "What's your lastname?" } } const expErrs = [] expect(args).toStrictEqual(expArgs) expect(errs).toStrictEqual(expErrs) }) test('convertCommands works with command-specific parsers', () => { const opts = [ {key: 'name', types: ['string'], args: ['--name']}, {key: 'other', args: ['other'], opts: [], values: []}, { key: 'ask', args: ['ask'], opts: [ {key: 'question', types: ['string'], args: ['-q']}, {key: 'jokingly', types: [], args: ['-j'], defaultValue: [1]} ], values: ['-q', "What's your lastname?", '--name', 'Logan'] } ] const parsers = { __: parser(), ask: () => () => ({errs: [], args: {_: [], test: 'ask parser!'}}), _: () => () => ({errs: [], args: {_: [], test: 'default parser!'}}) } const {errs, args} = convertCommands(parsers)({opts}) const expArgs = { _: [], ask: { _: [], test: 'ask parser!' }, other: { _: [], test: 'default parser!' } } const expErrs = [] expect(args).toStrictEqual(expArgs) expect(errs).toStrictEqual(expErrs) }) test('convertCommands only sets a command once and does not set it again if the same command is specified several times', () => { const opts = [ {key: 'other', args: ['other'], opts: [], values: ['foo']}, {key: 'other', args: ['other'], opts: [], values: ['bar']} ] const parsers = { __: parser(), _: parser() } const {errs, args} = convertCommands(parsers)({opts}) const expArgs = { _: ['foo'], other: { _: ['foo'] } } const expErrs = [] expect(args).toStrictEqual(expArgs) expect(errs).toStrictEqual(expErrs) }) test('convertCommands works if opts is undefined', () => { const obj = {} const {args} = convertCommands(parsers)(obj) expect(args).toStrictEqual({_: []}) }) test('convertCommands works if input is undefined', () => { const {args} = convertCommands(parsers)() expect(args).toStrictEqual({_: []}) }) test('convertCommands passes on errors', () => { const ERRS = [{code: 'foo', msg: 'bar', info: {}}] const {errs} = convertCommands(parsers)({errs: ERRS}) expect(errs).toStrictEqual(ERRS) }) |