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 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 | const {foo, getOptsFromPath} = require('./parser3') test('foo must stay in child command', () => { const C = {key: 'C', args: ['c'], types: []} const B = {key: 'B', args: ['b'], types: []} const A = {key: 'A', args: ['a']} const opt = { opts: [ {...A, opts: [B, C]} ] } const obj = { errs: [], argv: ['a', 'b', 'c'] } const {errs, opts} = foo(opt)(obj) const expErrs = [] const expOpts = [ {...A, opts: [B, C], values: [{...B, values: [1]}, {...C, values: [1]}]} ] expect(errs).toStrictEqual(expErrs) expect(opts).toStrictEqual(expOpts) }) test('foo must fall back to parent command', () => { const C = {key: 'C', args: ['c'], types: []} const B = {key: 'B', args: ['b'], types: []} const A = {key: 'A', args: ['a']} const opt = { opts: [ {...A, opts: [B]}, C ] } const obj = { errs: [], argv: ['a', 'b', 'c'] } const {errs, opts} = foo(opt)(obj) const expErrs = [] const expOpts = [ {...A, opts: [B], values: [{...B, values: [1]}]}, {...C, values: [1]} ] expect(errs).toStrictEqual(expErrs) expect(opts).toStrictEqual(expOpts) }) test('foo must stay in child command and fall back at the same time, not duplicating options', () => { const D = {key: 'D', args: ['d'], types: []} const C = {key: 'C', args: ['c'], types: []} const B = {key: 'B', args: ['b'], types: []} const A1 = {key: 'A1', args: ['a']} const A2 = {key: 'A2', args: ['a']} const opt = { opts: [ {...A1, opts: [B]}, {...A2, opts: [B, C]}, C, D ] } const obj = { errs: [], argv: ['a', 'b', 'c', 'd'] } const {errs, opts} = foo(opt)(obj) const expErrs = [] // 'd' is not added two times in the same step, to the same layer, but just once const expOpts = [ {...A1, opts: [B], values: [{...B, values: [1]}]}, {...C, values: [1]}, {...A2, opts: [B, C], values: [{...B, values: [1]}, {...C, values: [1]}]}, {...D, values: [1]} ] expect(errs).toStrictEqual(expErrs) expect(opts).toStrictEqual(expOpts) }) test('foo must cope with variadic options and commands with the same name', () => { const D = {key: 'D', args: ['d'], types: []} const C = {key: 'C', args: ['c'], types: []} const B = {key: 'B', args: ['b'], types: []} const A1 = {key: 'A1', args: ['a']} const A2 = {key: 'A2', args: ['a']} const opt = { opts: [ A1, {...A2, opts: [B, C]}, D ] } const obj = { errs: [], argv: ['a', 'b', 'c', 'd'] } const {errs, opts} = foo(opt)(obj) const expErrs = [] const expOpts = [ {...A1, values: ['b', 'c', 'd']}, {...A2, opts: [B, C], values: [{...B, values: [1]}, {...C, values: [1]}]}, {...D, values: [1]} ] expect(errs).toStrictEqual(expErrs) expect(opts).toStrictEqual(expOpts) }) test('getOptsFromPath selects an opts array from a program or command', () => { const D = {key: 'D', args: ['d'], types: []} const C = {key: 'C', args: ['c']} const B = {key: 'B', args: ['b'], types: []} const A = {key: 'A', args: ['a']} const opt = { opts: [ A, {...A, opts: [{...C, opts: [D]}], B} ] } expect( getOptsFromPath([], opt) ).toStrictEqual( opt.opts ) expect( getOptsFromPath([1], opt) ).toStrictEqual( opt.opts[1].opts ) expect( getOptsFromPath([1, 0], opt) ).toStrictEqual( opt.opts[1].opts[0].opts ) expect( getOptsFromPath([0], opt) ).toStrictEqual( [] ) expect( getOptsFromPath([9, 4], opt) ).toStrictEqual( [] ) }) /* Rules: If two options have the same arg + If exactly one is a command, it wins + If more than one are commands, the first defined command wins + If all are primitive or arrays and have the same length, they all win + If all are primitive or arrays and have the same length, but one that is not first, it is removed, all others win + If all are primitive or arrays and have the same length, but one that is first, all others are removed, the one wins + If all are flags, all win + If some are flags, but one other is not, the other one wins */ |