Skip to content

Commit

Permalink
Add a fast-check test for arg parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
jakebailey committed Jul 27, 2024
1 parent e1b1aca commit 60cb497
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 23 deletions.
85 changes: 85 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"@ava/typescript": "^3.0.1",
"@changesets/cli": "^2.27.7",
"@codspeed/tinybench-plugin": "^3.1.1",
"@fast-check/ava": "^2.0.0",
"@tsconfig/node12": "^12.1.3",
"@types/command-line-usage": "^5.0.4",
"@types/minimist": "^1.2.5",
Expand Down
64 changes: 41 additions & 23 deletions src/__tests__/cli/parseArgs.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { fc, testProp } from "@fast-check/ava";
import test from "ava";

import { getUsage, parseArgs } from "../../cli/parseArgs.js";
Expand All @@ -12,29 +13,46 @@ const macro = test.macro<[string[]]>({
},
});

test(macro, []);
test(macro, ["--help"]);
test(macro, ["-h"]);
test(macro, ["--tasks"]);
test(macro, ["--tasks", "true"]);
test(macro, ["--tasks=true"]);
test(macro, ["--tasks", "TRUE"]);
test(macro, ["--tasks", "yes"]);
test(macro, ["--tasks", "1"]);
test(macro, ["--tasks", "false"]);
test(macro, ["--tasks=false"]);
test(macro, ["--tasks", "FALSE"]);
test(macro, ["--tasks", "0"]);
test(macro, ["-T"]);
test(macro, ["-T", "true"]);
test(macro, ["-T", "false"]);
test(macro, ["--tasks-simple"]);
test(macro, ["--tasks-simple", "true"]);
test(macro, ["build", "test", "--light=false"]);
test(macro, ["build", "test", "--", "--light=false"]);
test(macro, ["build", "test", "--", "not-a-task", "--light=false"]);
test(macro, ["--herebyfile", "path/to/Herebyfile.js", "build", "test", "--light=false"]);
test(macro, ["--herebyfile", "path/to/Herebyfile.js", "build", "test", "--light=false", "not-a-task"]);
const argvTests = [
[],
["--help"],
["-h"],
["--tasks"],
["--tasks", "true"],
["--tasks=true"],
["--tasks", "TRUE"],
["--tasks", "yes"],
["--tasks", "1"],
["--tasks", "false"],
["--tasks=false"],
["--tasks", "FALSE"],
["--tasks", "0"],
["-T"],
["-T", "true"],
["-T", "false"],
["--tasks-simple"],
["--tasks-simple", "true"],
["build", "test", "--light=false"],
["build", "test", "--", "--light=false"],
["build", "test", "--", "not-a-task", "--light=false"],
["--herebyfile", "path/to/Herebyfile.js", "build", "test", "--light=false"],
["--herebyfile", "path/to/Herebyfile.js", "build", "test", "--light=false", "not-a-task"],
];

for (const argv of argvTests) {
test(macro, argv);
}

testProp(
"fast-check",
[fc.array(fc.string())],
(t, argv) => {
t.true(typeof parseArgs(argv) === "object");
},
{
examples: argvTests.map((v) => [v]),
},
);

test.serial("usage", (t) => {
t.snapshot(getUsage().replace(/\r/g, ""));
Expand Down

0 comments on commit 60cb497

Please sign in to comment.