|
4 | 4 | import { expect } from 'bupkis'; |
5 | 5 | import { describe, it } from 'node:test'; |
6 | 6 |
|
| 7 | +import { HelpError } from '../src/errors.js'; |
7 | 8 | import { opt } from '../src/opt.js'; |
8 | 9 | import { parseSimple } from '../src/parser.js'; |
9 | 10 | import { validatePositionalsSchema } from '../src/validate.js'; |
@@ -31,6 +32,120 @@ describe('parseSimple', () => { |
31 | 32 | expect(result.values, 'to deeply equal', { verbose: true }); |
32 | 33 | }); |
33 | 34 |
|
| 35 | + describe('boolean negation (--no-<flag>)', () => { |
| 36 | + it('sets flag to false with --no-<flag>', () => { |
| 37 | + const result = parseSimple({ |
| 38 | + args: ['--no-verbose'], |
| 39 | + options: { |
| 40 | + verbose: opt.boolean(), |
| 41 | + }, |
| 42 | + }); |
| 43 | + |
| 44 | + expect(result.values, 'to deeply equal', { verbose: false }); |
| 45 | + }); |
| 46 | + |
| 47 | + it('--no-<flag> overrides default: true', () => { |
| 48 | + const result = parseSimple({ |
| 49 | + args: ['--no-verbose'], |
| 50 | + options: { |
| 51 | + verbose: opt.boolean({ default: true }), |
| 52 | + }, |
| 53 | + }); |
| 54 | + |
| 55 | + expect(result.values, 'to deeply equal', { verbose: false }); |
| 56 | + }); |
| 57 | + |
| 58 | + it('--<flag> sets flag to true', () => { |
| 59 | + const result = parseSimple({ |
| 60 | + args: ['--verbose'], |
| 61 | + options: { |
| 62 | + verbose: opt.boolean(), |
| 63 | + }, |
| 64 | + }); |
| 65 | + |
| 66 | + expect(result.values, 'to deeply equal', { verbose: true }); |
| 67 | + }); |
| 68 | + |
| 69 | + it('throws HelpError when both --<flag> and --no-<flag> are specified', () => { |
| 70 | + expect( |
| 71 | + () => |
| 72 | + parseSimple({ |
| 73 | + args: ['--verbose', '--no-verbose'], |
| 74 | + options: { |
| 75 | + verbose: opt.boolean(), |
| 76 | + }, |
| 77 | + }), |
| 78 | + 'to throw a', |
| 79 | + HelpError, |
| 80 | + ); |
| 81 | + }); |
| 82 | + |
| 83 | + it('error message mentions conflicting options', () => { |
| 84 | + expect( |
| 85 | + () => |
| 86 | + parseSimple({ |
| 87 | + args: ['--no-verbose', '--verbose'], |
| 88 | + options: { |
| 89 | + verbose: opt.boolean(), |
| 90 | + }, |
| 91 | + }), |
| 92 | + 'to throw', |
| 93 | + /Conflicting options.*--verbose.*--no-verbose/, |
| 94 | + ); |
| 95 | + }); |
| 96 | + |
| 97 | + it('negated keys never appear in result values', () => { |
| 98 | + const result = parseSimple({ |
| 99 | + args: ['--no-verbose'], |
| 100 | + options: { |
| 101 | + verbose: opt.boolean(), |
| 102 | + }, |
| 103 | + }); |
| 104 | + |
| 105 | + expect(Object.keys(result.values), 'not to contain', 'no-verbose'); |
| 106 | + expect(result.values, 'to have keys', ['verbose']); |
| 107 | + }); |
| 108 | + |
| 109 | + it('works with multiple boolean options', () => { |
| 110 | + const result = parseSimple({ |
| 111 | + args: ['--verbose', '--no-quiet', '--no-debug'], |
| 112 | + options: { |
| 113 | + debug: opt.boolean({ default: true }), |
| 114 | + quiet: opt.boolean(), |
| 115 | + verbose: opt.boolean(), |
| 116 | + }, |
| 117 | + }); |
| 118 | + |
| 119 | + expect(result.values, 'to deeply equal', { |
| 120 | + debug: false, |
| 121 | + quiet: false, |
| 122 | + verbose: true, |
| 123 | + }); |
| 124 | + }); |
| 125 | + |
| 126 | + it('applies default when neither flag nor negation provided', () => { |
| 127 | + const result = parseSimple({ |
| 128 | + args: [], |
| 129 | + options: { |
| 130 | + verbose: opt.boolean({ default: true }), |
| 131 | + }, |
| 132 | + }); |
| 133 | + |
| 134 | + expect(result.values, 'to deeply equal', { verbose: true }); |
| 135 | + }); |
| 136 | + |
| 137 | + it('returns undefined when no flag, no negation, and no default', () => { |
| 138 | + const result = parseSimple({ |
| 139 | + args: [], |
| 140 | + options: { |
| 141 | + verbose: opt.boolean(), |
| 142 | + }, |
| 143 | + }); |
| 144 | + |
| 145 | + expect(result.values.verbose, 'to be', undefined); |
| 146 | + }); |
| 147 | + }); |
| 148 | + |
34 | 149 | it('parses number options', () => { |
35 | 150 | const result = parseSimple({ |
36 | 151 | args: ['--count', '5'], |
|
0 commit comments