|
| 1 | +import { |
| 2 | + assertEquals, |
| 3 | + assertRejects, |
| 4 | + assertSpyCalls, |
| 5 | + spy, |
| 6 | +} from "../../../dev_deps.ts"; |
| 7 | +import { Command } from "../../command.ts"; |
| 8 | + |
| 9 | +Deno.test("[command] should execute standalone option action", async () => { |
| 10 | + const actionSpy = spy(); |
| 11 | + const optionActionSpy = spy(); |
| 12 | + |
| 13 | + const cmd = new Command() |
| 14 | + .throwErrors() |
| 15 | + .option("--standalone", "description ...", { |
| 16 | + action: optionActionSpy, |
| 17 | + standalone: true, |
| 18 | + }) |
| 19 | + .action(actionSpy); |
| 20 | + |
| 21 | + const { options } = await cmd.parse(["--standalone"]); |
| 22 | + |
| 23 | + assertSpyCalls(optionActionSpy, 1); |
| 24 | + assertSpyCalls(actionSpy, 0); |
| 25 | + assertEquals(options, { standalone: true }); |
| 26 | +}); |
| 27 | + |
| 28 | +Deno.test("[command] should execute main action with standalone option", async () => { |
| 29 | + const actionSpy = spy(); |
| 30 | + |
| 31 | + const cmd = new Command() |
| 32 | + .throwErrors() |
| 33 | + .option("--standalone", "description ...", { |
| 34 | + standalone: true, |
| 35 | + }) |
| 36 | + .action(actionSpy); |
| 37 | + |
| 38 | + const { options } = await cmd.parse(["--standalone"]); |
| 39 | + |
| 40 | + assertSpyCalls(actionSpy, 1); |
| 41 | + assertEquals(options, { standalone: true }); |
| 42 | +}); |
| 43 | + |
| 44 | +Deno.test("[command] should throw an error if standalone option is combined with other options", async () => { |
| 45 | + const actionSpy = spy(); |
| 46 | + |
| 47 | + const cmd = new Command() |
| 48 | + .throwErrors() |
| 49 | + .option("--standalone", "description ...", { |
| 50 | + standalone: true, |
| 51 | + }) |
| 52 | + .option("--foo", "description ...") |
| 53 | + .action(actionSpy); |
| 54 | + |
| 55 | + await assertRejects( |
| 56 | + () => |
| 57 | + cmd.parse( |
| 58 | + ["--standalone", "--foo"], |
| 59 | + ), |
| 60 | + Error, |
| 61 | + 'Option "--standalone" cannot be combined with other options.', |
| 62 | + ); |
| 63 | +}); |
0 commit comments