Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(command): add more tests (#166)
- Loading branch information
Showing
12 changed files
with
1,065 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
// deno-fmt-ignore-file | ||
|
||
import { assertEquals } from "../../../dev_deps.ts"; | ||
import { Command } from "../../command.ts"; | ||
|
||
function command(): Command { | ||
return new Command() | ||
.throwErrors() | ||
.command("global", new Command().command("one")) | ||
.global() | ||
.command("global-hidden", new Command().command("two")) | ||
.global().hidden() | ||
.command("foo", new Command().command("three")) | ||
.command("foo-hidden", new Command().command("four")) | ||
.hidden(); | ||
} | ||
|
||
Deno.test("command - command - has commands", () => { | ||
const cmd: Command = command(); | ||
assertEquals(cmd.hasCommands(), true); | ||
assertEquals(cmd.hasCommands(true), true); | ||
assertEquals(new Command().hasCommands(), false); | ||
assertEquals(new Command().hasCommands(true), false); | ||
}); | ||
|
||
Deno.test("command - command - get commands", () => { | ||
const cmd: Command = command(); | ||
assertEquals(cmd.getCommands().length, 2); | ||
assertEquals(cmd.getCommands(true).length, 4); | ||
assertEquals(!!cmd.getCommands().find((cmd) => cmd.getName() === "global"), true); | ||
assertEquals(!!cmd.getCommands(true).find((cmd) => cmd.getName() === "global"), true); | ||
assertEquals(!!cmd.getCommands().find((cmd) => cmd.getName() === "global-hidden"), false); | ||
assertEquals(!!cmd.getCommands(true).find((cmd) => cmd.getName() === "global-hidden"), true); | ||
assertEquals(!!cmd.getCommands().find((cmd) => cmd.getName() === "foo"), true); | ||
assertEquals(!!cmd.getCommands(true).find((cmd) => cmd.getName() === "foo"), true); | ||
assertEquals(!!cmd.getCommands().find((cmd) => cmd.getName() === "foo-hidden"), false); | ||
assertEquals(!!cmd.getCommands(true).find((cmd) => cmd.getName() === "foo-hidden"), true); | ||
}); | ||
|
||
Deno.test("command - command - get base commands", () => { | ||
const cmd: Command = command(); | ||
assertEquals(cmd.getBaseCommands().length, 2); | ||
assertEquals(cmd.getBaseCommands(true).length, 4); | ||
assertEquals(!!cmd.getBaseCommands().find((cmd) => cmd.getName() === "global"), true); | ||
assertEquals(!!cmd.getBaseCommands(true).find((cmd) => cmd.getName() === "global"), true); | ||
assertEquals(!!cmd.getBaseCommands().find((cmd) => cmd.getName() === "global-hidden"), false); | ||
assertEquals(!!cmd.getBaseCommands(true).find((cmd) => cmd.getName() === "global-hidden"), true); | ||
assertEquals(!!cmd.getBaseCommands().find((cmd) => cmd.getName() === "foo"), true); | ||
assertEquals(!!cmd.getBaseCommands(true).find((cmd) => cmd.getName() === "foo"), true); | ||
assertEquals(!!cmd.getBaseCommands().find((cmd) => cmd.getName() === "foo-hidden"), false); | ||
assertEquals(!!cmd.getBaseCommands(true).find((cmd) => cmd.getName() === "foo-hidden"), true); | ||
}); | ||
|
||
Deno.test("command - command - get global commands", () => { | ||
const cmd: Command = command(); | ||
assertEquals(cmd.getCommand("foo")?.getGlobalCommands().length, 1); | ||
assertEquals(cmd.getCommand("foo")?.getGlobalCommands(true).length, 2); | ||
assertEquals(!!cmd.getCommand("foo")?.getGlobalCommands().find((cmd) => cmd.getName() === "global"), true); | ||
assertEquals(!!cmd.getCommand("foo")?.getGlobalCommands(true).find((cmd) => cmd.getName() === "global"), true); | ||
assertEquals(!!cmd.getCommand("foo")?.getGlobalCommands().find((cmd) => cmd.getName() === "global-hidden"), false); | ||
assertEquals(!!cmd.getCommand("foo")?.getGlobalCommands(true).find((cmd) => cmd.getName() === "global-hidden"), true); | ||
assertEquals(!!cmd.getCommand("foo")?.getGlobalCommands().find((cmd) => cmd.getName() === "foo"), false); | ||
assertEquals(!!cmd.getCommand("foo")?.getGlobalCommands(true).find((cmd) => cmd.getName() === "foo"), false); | ||
assertEquals(!!cmd.getCommand("foo")?.getGlobalCommands().find((cmd) => cmd.getName() === "foo-hidden"), false); | ||
assertEquals(!!cmd.getCommand("foo")?.getGlobalCommands(true).find((cmd) => cmd.getName() === "foo-hidden"), false); | ||
}); | ||
|
||
Deno.test("command - command - has command", () => { | ||
const cmd: Command = command(); | ||
assertEquals(cmd.hasCommand("global"), true); | ||
assertEquals(cmd.hasCommand("global-hidden"), false); | ||
assertEquals(cmd.hasCommand("global-hidden", true), true); | ||
assertEquals(cmd.hasCommand("foo"), true); | ||
assertEquals(cmd.hasCommand("foo-hidden"), false); | ||
assertEquals(cmd.hasCommand("foo-hidden", true), true); | ||
assertEquals(cmd.hasCommand("unknown"), false); | ||
assertEquals(cmd.getCommand("foo")?.hasCommand("global"), true); | ||
assertEquals(cmd.getCommand("foo")?.hasCommand("global-hidden"), false); | ||
assertEquals(cmd.getCommand("foo")?.hasCommand("global-hidden", true), true); | ||
assertEquals(cmd.getCommand("foo")?.hasCommand("unknown"), false); | ||
assertEquals(cmd.getCommand("foo")?.getCommand("three")?.hasCommand("global"), true); | ||
assertEquals(cmd.getCommand("foo")?.getCommand("three")?.hasCommand("global-hidden"), false); | ||
assertEquals(cmd.getCommand("foo")?.getCommand("three")?.hasCommand("global-hidden", true), true); | ||
assertEquals(cmd.getCommand("foo")?.getCommand("three")?.hasCommand("unknown"), false); | ||
}); | ||
|
||
Deno.test("command - command - get command", () => { | ||
const cmd: Command = command(); | ||
assertEquals(cmd.getCommand("global")?.getName(), "global"); | ||
assertEquals(cmd.getCommand("global-hidden")?.getName(), undefined); | ||
assertEquals(cmd.getCommand("global-hidden", true)?.getName(), "global-hidden"); | ||
assertEquals(cmd.getCommand("foo")?.getName(), "foo"); | ||
assertEquals(cmd.getCommand("foo-hidden")?.getName(), undefined); | ||
assertEquals(cmd.getCommand("foo-hidden", true)?.getName(), "foo-hidden"); | ||
assertEquals(cmd.getCommand("unknown")?.getName(), undefined); | ||
assertEquals(cmd.getCommand("foo")?.getCommand("global")?.getName(), "global"); | ||
assertEquals(cmd.getCommand("foo")?.getCommand("global-hidden")?.getName(), undefined); | ||
assertEquals(cmd.getCommand("foo")?.getCommand("global-hidden", true)?.getName(), "global-hidden"); | ||
assertEquals(cmd.getCommand("foo")?.getCommand("unknown")?.getName(), undefined); | ||
}); | ||
|
||
Deno.test("command - command - get base command", () => { | ||
const cmd: Command = command(); | ||
assertEquals(cmd.getBaseCommand("global")?.getName(), "global"); | ||
assertEquals(cmd.getBaseCommand("global-hidden")?.getName(), undefined); | ||
assertEquals(cmd.getBaseCommand("global-hidden", true)?.getName(), "global-hidden"); | ||
assertEquals(cmd.getBaseCommand("foo")?.getName(), "foo"); | ||
assertEquals(cmd.getBaseCommand("foo-hidden")?.getName(), undefined); | ||
assertEquals(cmd.getBaseCommand("foo-hidden", true)?.getName(), "foo-hidden"); | ||
assertEquals(cmd.getBaseCommand("unknown")?.getName(), undefined); | ||
assertEquals(cmd.getCommand("foo")?.getBaseCommand("global")?.getName(), undefined); | ||
assertEquals(cmd.getCommand("foo")?.getBaseCommand("global-hidden")?.getName(), undefined); | ||
assertEquals(cmd.getCommand("foo")?.getBaseCommand("global-hidden", true)?.getName(), undefined); | ||
assertEquals(cmd.getCommand("foo")?.getBaseCommand("unknown")?.getName(), undefined); | ||
}); | ||
|
||
Deno.test("command - command - get global command", () => { | ||
const cmd: Command = command(); | ||
assertEquals(cmd.getGlobalCommand("global")?.getName(), undefined); | ||
assertEquals(cmd.getGlobalCommand("global-hidden")?.getName(), undefined); | ||
assertEquals(cmd.getGlobalCommand("global-hidden", true)?.getName(), undefined); | ||
assertEquals(cmd.getGlobalCommand("foo")?.getName(), undefined); | ||
assertEquals(cmd.getGlobalCommand("foo-hidden")?.getName(), undefined); | ||
assertEquals(cmd.getGlobalCommand("foo-hidden", true)?.getName(), undefined); | ||
assertEquals(cmd.getGlobalCommand("unknown")?.getName(), undefined); | ||
assertEquals(cmd.getCommand("foo")?.getGlobalCommand("global")?.getName(), "global"); | ||
assertEquals(cmd.getCommand("foo")?.getGlobalCommand("global-hidden")?.getName(), undefined); | ||
assertEquals(cmd.getCommand("foo")?.getGlobalCommand("global-hidden", true)?.getName(), "global-hidden"); | ||
assertEquals(cmd.getCommand("foo")?.getGlobalCommand("foo")?.getName(), undefined); | ||
assertEquals(cmd.getCommand("foo")?.getGlobalCommand("foo-hidden")?.getName(), undefined); | ||
assertEquals(cmd.getCommand("foo")?.getGlobalCommand("foo-hidden", true)?.getName(), undefined); | ||
assertEquals(cmd.getCommand("foo")?.getGlobalCommand("unknown")?.getName(), undefined); | ||
assertEquals(cmd.getCommand("foo")?.getCommand("three")?.getGlobalCommand("global")?.getName(), "global"); | ||
assertEquals(cmd.getCommand("foo")?.getCommand("three")?.getGlobalCommand("global-hidden")?.getName(), undefined); | ||
assertEquals(cmd.getCommand("foo")?.getCommand("three")?.getGlobalCommand("global-hidden", true)?.getName(), "global-hidden"); | ||
assertEquals(cmd.getCommand("foo")?.getCommand("three")?.getGlobalCommand("unknown")?.getName(), undefined); | ||
}); | ||
|
||
Deno.test("command - command - remove command", () => { | ||
const cmd: Command = command(); | ||
assertEquals(cmd.getCommand("foo")?.getName(), "foo"); | ||
assertEquals(cmd.removeCommand("foo")?.getName(), "foo"); | ||
assertEquals(cmd.getCommand("foo"), undefined); | ||
assertEquals(cmd.removeCommand("foo"), undefined); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// deno-fmt-ignore-file | ||
|
||
import { assertEquals } from "../../../dev_deps.ts"; | ||
import { Command } from "../../command.ts"; | ||
import type { ICompletion } from "../../types.ts"; | ||
|
||
function command(): Command { | ||
return new Command() | ||
.throwErrors() | ||
.globalComplete("global", () => ["global1", "global2"]) | ||
.complete("foo", () => ["foo1", "foo2"]) | ||
.command( | ||
"bar", | ||
new Command() | ||
.complete("bar", () => ["bar1", "bar2"]) | ||
.command("baz") | ||
.complete("baz", () => ["baz1", "baz2"]) | ||
); | ||
} | ||
|
||
Deno.test("command - completion - completion properties", () => { | ||
const cmd: Command = new Command() | ||
.throwErrors() | ||
.complete("foo", () => ["foo1", "foo2"]) | ||
.complete("bar", () => ["bar1", "bar2"], { | ||
global: true, | ||
override: true, | ||
}); | ||
const fooCompletion: ICompletion = cmd.getCompletion("foo") as ICompletion; | ||
const barCompletion: ICompletion = cmd.getCompletion("bar") as ICompletion; | ||
assertEquals(fooCompletion.name, "foo"); | ||
assertEquals(fooCompletion.global, undefined); | ||
assertEquals(barCompletion.name, "bar"); | ||
assertEquals(barCompletion.global, true); | ||
}); | ||
|
||
Deno.test("command - completion - get completions", () => { | ||
const cmd: Command = command(); | ||
assertEquals(cmd.getCompletions().length, 2); | ||
assertEquals(!!cmd.getCompletions().find((opt) => opt.name === "global"), true); | ||
assertEquals(!!cmd.getCompletions().find((opt) => opt.name === "foo"), true); | ||
}); | ||
|
||
Deno.test("command - completion - get base completions", () => { | ||
const cmd: Command = command(); | ||
assertEquals(cmd.getBaseCompletions().length, 2); | ||
assertEquals(!!cmd.getBaseCompletions().find((opt) => opt.name === "global"), true); | ||
assertEquals(!!cmd.getBaseCompletions().find((opt) => opt.name === "foo"), true); | ||
}); | ||
|
||
Deno.test("command - completion - get global completions", () => { | ||
const cmd: Command = command(); | ||
assertEquals(cmd.getCommand("bar")?.getGlobalCompletions().length, 1); | ||
assertEquals(!!cmd.getCommand("bar")?.getGlobalCompletions().find((opt) => opt.name === "global"), true); | ||
assertEquals(!!cmd.getCommand("bar")?.getGlobalCompletions().find((opt) => opt.name === "foo"), false); | ||
}); | ||
|
||
Deno.test("command - completion - get completion", () => { | ||
const cmd: Command = command(); | ||
assertEquals(cmd.getCompletion("global")?.name, "global"); | ||
assertEquals(cmd.getCompletion("foo")?.name, "foo"); | ||
assertEquals(cmd.getCompletion("unknown")?.name, undefined); | ||
assertEquals(cmd.getCommand("bar")?.getCompletion("global")?.name, "global"); | ||
assertEquals(cmd.getCommand("bar")?.getCompletion("bar")?.name, "bar"); | ||
assertEquals(cmd.getCommand("bar")?.getCompletion("unknown")?.name, undefined); | ||
assertEquals(cmd.getCommand("bar")?.getCommand("baz")?.getCompletion("global")?.name, "global"); | ||
assertEquals(cmd.getCommand("bar")?.getCommand("baz")?.getCompletion("baz")?.name, "baz"); | ||
assertEquals(cmd.getCommand("bar")?.getCommand("baz")?.getCompletion("unknown")?.name, undefined); | ||
}); | ||
|
||
Deno.test("command - completion - get base completion", () => { | ||
const cmd: Command = command(); | ||
assertEquals(cmd.getBaseCompletion("global")?.name, "global"); | ||
assertEquals(cmd.getBaseCompletion("foo")?.name, "foo"); | ||
assertEquals(cmd.getBaseCompletion("unknown")?.name, undefined); | ||
assertEquals(cmd.getCommand("bar")?.getBaseCompletion("global")?.name, undefined); | ||
assertEquals(cmd.getCommand("bar")?.getBaseCompletion("bar")?.name, "bar"); | ||
assertEquals(cmd.getCommand("bar")?.getBaseCompletion("unknown")?.name, undefined); | ||
assertEquals(cmd.getCommand("bar")?.getCommand("baz")?.getBaseCompletion("global")?.name, undefined); | ||
assertEquals(cmd.getCommand("bar")?.getCommand("baz")?.getBaseCompletion("baz")?.name, "baz"); | ||
assertEquals(cmd.getCommand("bar")?.getCommand("baz")?.getBaseCompletion("unknown")?.name, undefined); | ||
}); | ||
|
||
Deno.test("command - completion - get global completion", () => { | ||
const cmd: Command = command(); | ||
assertEquals(cmd.getGlobalCompletion("global")?.name, undefined); | ||
assertEquals(cmd.getGlobalCompletion("foo")?.name, undefined); | ||
assertEquals(cmd.getGlobalCompletion("unknown")?.name, undefined); | ||
assertEquals(cmd.getCommand("bar")?.getGlobalCompletion("global")?.name, "global"); | ||
assertEquals(cmd.getCommand("bar")?.getGlobalCompletion("bar")?.name, undefined); | ||
assertEquals(cmd.getCommand("bar")?.getGlobalCompletion("unknown")?.name, undefined); | ||
assertEquals(cmd.getCommand("bar")?.getCommand("baz")?.getGlobalCompletion("global")?.name, "global"); | ||
assertEquals(cmd.getCommand("bar")?.getCommand("baz")?.getGlobalCompletion("baz")?.name, undefined); | ||
assertEquals(cmd.getCommand("bar")?.getCommand("baz")?.getGlobalCompletion("unknown")?.name, undefined); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { assertEquals } from "../../../dev_deps.ts"; | ||
import { Command } from "../../command.ts"; | ||
|
||
Deno.test("command - raw args - command with usRawArgs disabled", async () => { | ||
let action = 0; | ||
const { options, args, literal } = await new Command() | ||
.throwErrors() | ||
.default("bar") | ||
.command("foo") | ||
.action(() => { | ||
action = 2; | ||
}) | ||
.command("bar") | ||
.action(() => { | ||
action = 3; | ||
}) | ||
.parse([]); | ||
|
||
assertEquals(action, 3); | ||
assertEquals(options, {}); | ||
assertEquals(args, []); | ||
assertEquals(literal, []); | ||
}); |
Oops, something went wrong.