options.implies.test.js
This file
Path
tests/options.implies.test.js
214 lines · 7.7 KiB · JavaScript
Last changed by
Assume a string parameter to implies is name of boolean option. (#1854)John Geebefore adoptionf4c7349
History of this fileTake it
Add Option.implies() (#1724)John Geebefore adoptiond660967
lines 1–172why, one step back1const { Command, Option } = require('../');23describe('check priorities', () => {4 test('when source undefined and implied undefined then implied is undefined', () => {5 const program = new Command();6 program7 .addOption(new Option('--foo').implies({ bar: 'implied' }))8 .option('--bar');9 program.parse([], { from: 'user' });10 expect(program.opts()).toEqual({});11 });1213 test('when source default and implied undefined then implied is undefined', () => {14 const program = new Command();15 program16 .addOption(new Option('--foo').implies({ bar: 'implied' }).default('default'))17 .option('--bar');18 program.parse([], { from: 'user' });19 expect(program.opts()).toEqual({ foo: 'default' });20 });2122 test('when source from env and implied undefined then implied is implied', () => {23 const program = new Command();24 const envName = 'COMMANDER_TEST_DELETE_ME';25 process.env[envName] = 'env';26 program27 .addOption(new Option('--foo').implies({ bar: 'implied' }).env(envName))28 .option('--bar');29 program.parse([], { from: 'user' });30 expect(program.opts()).toEqual({ foo: true, bar: 'implied' });31 delete process.env[envName];32 });3334 test('when source from cli and implied undefined then implied is implied', () => {35 const program = new Command();36 program37 .addOption(new Option('--foo').implies({ bar: 'implied' }))38 .option('--bar');39 program.parse(['--foo'], { from: 'user' });40 expect(program.opts()).toEqual({ foo: true, bar: 'implied' });41 });4243 test('when source cli and implied default then implied is implied', () => {44 const program = new Command();45 program46 .addOption(new Option('--foo').implies({ bar: 'implied' }))47 .option('--bar', '', 'default');48 program.parse(['--foo'], { from: 'user' });49 expect(program.opts()).toEqual({ foo: true, bar: 'implied' });50 });5152 test('when source cli and env default then implied is env', () => {53 const program = new Command();54 const envName = 'COMMANDER_TEST_DELETE_ME';55 process.env[envName] = 'env';56 program57 .addOption(new Option('--foo').implies({ bar: 'implied' }))58 .addOption(new Option('--bar <value>').env(envName));59 program.parse(['--foo'], { from: 'user' });60 expect(program.opts()).toEqual({ foo: true, bar: 'env' });61 delete process.env[envName];62 });63});6465test('when imply non-option then ok and stored', () => {66 const program = new Command();67 program68 .addOption(new Option('--foo').implies({ bar: 'implied' }));69 program.parse(['--foo'], { from: 'user' });70 expect(program.opts()).toEqual({ foo: true, bar: 'implied' });71});7273test('when imply multiple values then store multiple values', () => {74 const program = new Command();75 program76 .addOption(new Option('--foo').implies({ one: 'ONE', two: 'TWO' }))77 .option('--one')78 .option('--two');79 program.parse(['--foo'], { from: 'user' });80 expect(program.opts()).toEqual({ foo: true, one: 'ONE', two: 'TWO' });81});8283test('when imply multiple times then store multiple values', () => {84 const program = new Command();85 program86 .addOption(new Option('--foo').implies({ one: 'ONE' }).implies({ two: 'TWO' }))87 .option('--one')88 .option('--two');89 program.parse(['--foo'], { from: 'user' });90 expect(program.opts()).toEqual({ foo: true, one: 'ONE', two: 'TWO' });91});9293test('when imply from positive option then positive implied', () => {94 const program = new Command();95 program96 .addOption(new Option('--foo').implies({ implied: 'POSITIVE' }))97 .addOption(new Option('--no-foo').implies({ implied: 'NEGATIVE' }));98 program.parse(['--foo'], { from: 'user' });99 expect(program.opts()).toEqual({ foo: true, implied: 'POSITIVE' });100});101102test('when imply from negative option then negative implied', () => {103 const program = new Command();104 program105 .addOption(new Option('--foo').implies({ implied: 'POSITIVE' }))106 .addOption(new Option('--no-foo').implies({ implied: 'NEGATIVE' }));107 program.parse(['--no-foo'], { from: 'user' });108 expect(program.opts()).toEqual({ foo: false, implied: 'NEGATIVE' });109});110111test('when imply from lone negative option then negative implied', () => {112 const program = new Command();113 program114 .addOption(new Option('--no-foo').implies({ implied: 'NEGATIVE' }));115 program.parse(['--no-foo'], { from: 'user' });116 expect(program.opts()).toEqual({ foo: false, implied: 'NEGATIVE' });117});118119test('when imply from negative option with preset then negative implied', () => {120 const program = new Command();121 program122 .addOption(new Option('--foo').implies({ implied: 'POSITIVE' }))123 .addOption(new Option('--no-foo').implies({ implied: 'NEGATIVE' }).preset('FALSE'));124 program.parse(['--no-foo'], { from: 'user' });125 expect(program.opts()).toEqual({ foo: 'FALSE', implied: 'NEGATIVE' });126});127128test('when chained implies then only explicitly trigger', () => {129 const program = new Command();130 program131 .addOption(new Option('--one').implies({ two: true }))132 .addOption(new Option('--two').implies({ three: true }))133 .addOption(new Option('--three'));134 program.parse(['--one'], { from: 'user' });135 expect(program.opts()).toEqual({ one: true, two: true });136});137138test('when looped implies then no infinite loop', () => {139 const program = new Command();140 program141 .addOption(new Option('--ying').implies({ yang: true }))142 .addOption(new Option('--yang').implies({ ying: true }));143 program.parse(['--ying'], { from: 'user' });144 expect(program.opts()).toEqual({ ying: true, yang: true });145});146147test('when conflict with implied value then throw', () => {148 const program = new Command();149 program150 .exitOverride()151 .configureOutput({152 writeErr: () => {}153 })154 .addOption(new Option('--unary'))155 .addOption(new Option('--binary').conflicts('unary'))156 .addOption(new Option('--one').implies({ unary: true }));157158 expect(() => {159 program.parse(['--binary', '--one'], { from: 'user' });160 }).toThrow();161});162163test('when requiredOption with implied value then not throw', () => {164 const program = new Command();165 program166 .requiredOption('--target <target-file>')167 .addOption(new Option('--default-target').implies({ target: 'default-file' }));168169 expect(() => {170 program.parse(['--default-target'], { from: 'user' });171 }).not.toThrow();172});Add more tests for implies (#1730)John Geebefore adoption3103c6b
lines 173–203why, one step back173174test('when implies on program and use subcommand then program updated', () => {175 const program = new Command();176 program177 .addOption(new Option('--foo').implies({ bar: 'implied' }));178 program179 .command('sub')180 .action(() => {});181 program.parse(['--foo', 'sub'], { from: 'user' });182 expect(program.opts().bar).toEqual('implied');183});184185test('when option with implies used multiple times then implied gets single value', () => {186 const program = new Command();187 program188 .addOption(new Option('--foo').implies({ bar: 'implied' }))189 .option('-b, --bar <value...>');190 program.parse(['--foo', '--foo'], { from: 'user' });191 expect(program.opts().bar).toEqual('implied');192});193194test('when implied option has custom processing then custom processing not called', () => {195 let called = false;196 const program = new Command();197 program198 .addOption(new Option('--foo').implies({ bar: true }))199 .option('-b, --bar', 'description', () => { called = true; });200 program.parse(['--foo'], { from: 'user' });201 expect(program.opts().bar).toEqual(true);202 expect(called).toEqual(false);203});Assume a string parameter to implies is name of boolean option. (#1854)John Geebefore adoptionf4c7349
lines 204–214why, one step back204205test('when passed string then treat as boolean', () => {206 // Do something sensible instead of something silly if user passes just name of option.207 // https://github.com/tj/commander.js/issues/1852208 const program = new Command();209 program210 .addOption(new Option('--foo').implies('bar'))211 .option('-b, --bar', 'description');212 program.parse(['--foo'], { from: 'user' });213 expect(program.opts().bar).toEqual(true);214});