Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: args #160

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export type BooleanArgDef = Omit<_ArgDef<"boolean", boolean>, "options"> & {
negativeDescription?: string;
};
export type StringArgDef = Omit<_ArgDef<"string", string>, "options">;
export type NumberArgDef = Omit<_ArgDef<"number", boolean>, "options">;
export type NumberArgDef = Omit<_ArgDef<"number", number>, "options">;
export type EnumArgDef = _ArgDef<"enum", string>;
export type PositionalArgDef = Omit<
_ArgDef<"positional", string>,
Expand Down
139 changes: 139 additions & 0 deletions test/args.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
import { describe, it, expect } from "vitest";
import { parseArgs } from "../src/args";
import { ArgsDef, ParsedArgs } from "../src";

describe("args", () => {
it.each<[string[], ArgsDef, ParsedArgs]>([
[[], {}, { _: [] }],
/**
* String
*/
[["--name", "John"], { name: { type: "string" } }, { name: "John", _: [] }],
[
[],
{ name: { type: "string", default: "John" } },
{ name: "John", _: [] },
],
[
["--name", "Jane"],
{ name: { type: "string", default: "John" } },
{ name: "Jane", _: [] },
],
[
["-n", "Jane"],
{ name: { type: "string", alias: "n" } },
{ name: "Jane", n: "Jane", _: [] },
],
/**
* Number
*/
[["--age", "25"], { age: { type: "number" } }, { age: 25, _: [] }],
[[], { age: { type: "number", default: 25 } }, { age: 25, _: [] }],
[
["--age", "30"],
{ age: { type: "number", default: 25 } },
{ age: 30, _: [] },
],
[
["-a", "30"],
{ age: { type: "number", alias: "a" } },
{ age: 30, a: "30", _: [] },
],
/**
* Boolean
*/
[["--force"], { force: { type: "boolean" } }, { force: true, _: [] }],
[
["-f"],
{ force: { alias: "f", type: "boolean" } },
{ force: true, f: true, _: [] },
],
[[], { force: { type: "boolean", default: true } }, { force: true, _: [] }],
[
["--no-force"],
{ force: { type: "boolean", negativeDescription: "force" } },
{ force: false, _: [] },
],
/**
* Positional
*/
[
["subCommand"],
{ command: { type: "positional" } },
{ _: ["subCommand"], command: "subCommand" },
],
[
[],
{ command: { type: "positional", default: "subCommand" } },
{ _: [], command: "subCommand" },
],
[[], { command: { type: "positional", required: false } }, { _: [] }],
/**
* Enum
*/
[
["--value", "one"],
{ value: { type: "enum", options: ["one", "two"] } },
{ value: "one", _: [] },
],
])("should parsed correctly %o (%o)", (rawArgs, definition, result) => {
const parsed = parseArgs(rawArgs, definition);

expect(parsed).toEqual(result);
});

it.each<[string[], ArgsDef, string]>([
[
[],
{ name: { type: "string", required: true } },
"Missing required argument: --name",
],
[
["--age", "twenty-five"],
{ age: { type: "number" } },
"Invalid value for argument: `--age` (`twenty-five`). Expected a number.",
],
[
[],
{
name: { type: "positional" },
},
"Missing required positional argument: NAME",
],
[
["--value", "three"],
{ value: { type: "enum", options: ["one", "two"] } },
"Invalid value for argument: `--value` (`three`). Expected one of: `one`, `two`.",
],
])("should throw error with %o (%o)", (rawArgs, definition, result) => {
// TODO: should check for exact match
// https://github.com/vitest-dev/vitest/discussions/6048
expect(() => {
parseArgs(rawArgs, definition);
}).toThrowError(result);
});

it("should resolve camelCase argument", () => {
const definition: ArgsDef = {
"user-name": { type: "string" },
};
const rawArgs = ["--userName", "Jane"];

const parsed = parseArgs(rawArgs, definition);

expect(parsed["user-name"]).toBe("Jane");
expect(parsed._).toEqual([]);
});

it("should resolve kebab-case argument", () => {
const definition: ArgsDef = {
userName: { type: "string" },
};
const rawArgs = ["--user-name", "Jane"];

const parsed = parseArgs(rawArgs, definition);

expect(parsed.userName).toBe("Jane");
expect(parsed._).toEqual([]);
});
});