Skip to content

Commit

Permalink
test: args
Browse files Browse the repository at this point in the history
  • Loading branch information
Barbapapazes committed Jul 8, 2024
1 parent 53cd585 commit f4e7de3
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 1 deletion.
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([]);
});
});

0 comments on commit f4e7de3

Please sign in to comment.