Skip to content

Commit 67b5213

Browse files
authored
test: add test for parser (#192)
1 parent 7de2a6c commit 67b5213

1 file changed

Lines changed: 91 additions & 0 deletions

File tree

test/parser.test.ts

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import { parseRawArgs } from "../src/_parser";
2+
import { describe, it, expect } from "vitest";
3+
4+
describe("parseRawArgs", () => {
5+
it("parses arguments correctly", () => {
6+
const args = ["--name", "John", "--age", "30"];
7+
const opts = { boolean: ["age"] };
8+
const result = parseRawArgs(args, opts);
9+
10+
expect(result).toEqual({
11+
_: [30],
12+
name: "John",
13+
age: true,
14+
});
15+
});
16+
17+
it("handles unknown options", () => {
18+
const args = ["--unknown", "value"];
19+
const opts = { unknown: () => false };
20+
const result = parseRawArgs(args, opts);
21+
22+
expect(result).toEqual(false);
23+
});
24+
25+
it("handles default values", () => {
26+
const args = [];
27+
const opts = { default: { name: "Default" } };
28+
const result = parseRawArgs(args, opts);
29+
30+
expect(result).toEqual({
31+
_: [],
32+
name: "Default",
33+
});
34+
});
35+
36+
it("handles aliases", () => {
37+
const args = ["-n", "John"];
38+
const opts = { alias: { n: "name" } };
39+
const result = parseRawArgs(args, opts);
40+
41+
expect(result).toEqual({
42+
_: [],
43+
n: "John",
44+
name: "John",
45+
});
46+
});
47+
48+
it("handles boolean flags", () => {
49+
const args = ["--flag"];
50+
const opts = { boolean: ["flag"] };
51+
const result = parseRawArgs(args, opts);
52+
53+
expect(result).toEqual({
54+
_: [],
55+
flag: true,
56+
});
57+
});
58+
59+
it("handles string flags", () => {
60+
const args = ["--name", "John"];
61+
const opts = { string: ["name"] };
62+
const result = parseRawArgs(args, opts);
63+
64+
expect(result).toEqual({
65+
_: [],
66+
name: "John",
67+
});
68+
});
69+
70+
it("handles mixed flags", () => {
71+
const args = ["--name", "John", "--age", "30"];
72+
const opts = { string: ["name"], boolean: ["age"] };
73+
const result = parseRawArgs(args, opts);
74+
75+
expect(result).toEqual({
76+
_: [30],
77+
name: "John",
78+
age: true,
79+
});
80+
});
81+
82+
it("handles empty arguments", () => {
83+
const args = [];
84+
const opts = {};
85+
const result = parseRawArgs(args, opts);
86+
87+
expect(result).toEqual({
88+
_: [],
89+
});
90+
});
91+
});

0 commit comments

Comments
 (0)