|
2 | 2 | assertEquals, |
3 | 3 | assertSpyCall, |
4 | 4 | assertSpyCalls, |
| 5 | + assertType, |
| 6 | + IsExact, |
5 | 7 | spy, |
6 | 8 | } from "../../../dev_deps.ts"; |
7 | 9 | import { Command } from "../../command.ts"; |
@@ -79,25 +81,71 @@ Deno.test("[flags] should call global action handler", async () => { |
79 | 81 | const barGlobalSpy = spy(); |
80 | 82 | const barSpy = spy(); |
81 | 83 |
|
82 | | - const barCmd = new Command() |
| 84 | + const barCmd = new Command<{ main?: true; foo?: true }>() |
83 | 85 | .globalOption("--bar", "...") |
84 | 86 | .option("--baz", "...") |
85 | 87 | .arguments("<foo> <bar>") |
86 | | - .globalAction(barGlobalSpy) |
| 88 | + .globalAction((opts, ...args) => { |
| 89 | + assertType< |
| 90 | + IsExact<typeof opts, { |
| 91 | + main?: true; |
| 92 | + foo?: true; |
| 93 | + bar?: true; |
| 94 | + baz?: true; |
| 95 | + }> |
| 96 | + >(true); |
| 97 | + assertType<IsExact<typeof args, Array<unknown>>>(true); |
| 98 | + barGlobalSpy(opts, ...args); |
| 99 | + }) |
87 | 100 | .action(barSpy); |
88 | 101 |
|
89 | | - const fooCmd = new Command() |
| 102 | + const fooCmd = new Command<{ main?: true }>() |
90 | 103 | .globalOption("--foo", "...") |
91 | | - .globalAction(fooGlobalSpy) |
| 104 | + .globalAction((opts, ...args) => { |
| 105 | + assertType< |
| 106 | + IsExact<typeof opts, { |
| 107 | + main?: true; |
| 108 | + foo?: true; |
| 109 | + }> |
| 110 | + >(true); |
| 111 | + assertType<IsExact<typeof args, Array<unknown>>>(true); |
| 112 | + fooGlobalSpy(opts, ...args); |
| 113 | + }) |
92 | 114 | .action(fooSpy) |
93 | 115 | .command("bar", barCmd); |
94 | 116 |
|
95 | 117 | const cmd = new Command() |
96 | 118 | .throwErrors() |
97 | 119 | .globalOption("--main", "...") |
98 | | - .globalAction(mainGlobalSpy) |
99 | | - .action(mainSpy) |
100 | | - .command("foo", fooCmd); |
| 120 | + .option("--test", "...") |
| 121 | + .globalAction((opts, ...args) => { |
| 122 | + assertType< |
| 123 | + IsExact<typeof opts, { |
| 124 | + main?: true; |
| 125 | + test?: true; |
| 126 | + }> |
| 127 | + >(true); |
| 128 | + assertType<IsExact<typeof args, Array<unknown>>>(true); |
| 129 | + mainGlobalSpy(opts, ...args); |
| 130 | + }) |
| 131 | + .action((opts, ...args) => { |
| 132 | + mainSpy(opts, ...args); |
| 133 | + assertType< |
| 134 | + IsExact<typeof opts, { |
| 135 | + main?: true; |
| 136 | + test?: true; |
| 137 | + }> |
| 138 | + >(true); |
| 139 | + }) |
| 140 | + .command("foo", fooCmd) |
| 141 | + .command("other", "desc...") |
| 142 | + .action((opts) => { |
| 143 | + assertType< |
| 144 | + IsExact<typeof opts, { |
| 145 | + main?: true; |
| 146 | + }> |
| 147 | + >(true); |
| 148 | + }); |
101 | 149 |
|
102 | 150 | await cmd.parse([ |
103 | 151 | "--main", |
|
0 commit comments