Skip to content

Commit a381002

Browse files
committed
Add tests to match.
1 parent 5f14a2f commit a381002

File tree

2 files changed

+55
-4
lines changed

2 files changed

+55
-4
lines changed

core/tools/parseArgs.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,16 @@ export function getStringArg(
4242
}
4343
}
4444

45-
if (!allowEmpty && !value.trim()) {
46-
throw new Error(`Argument ${argName} must not be empty or whitespace-only`);
47-
}
48-
4945
if (typeof value !== "string") {
5046
throw new Error(
5147
`\`${argName}\` argument is required${allowEmpty ? "" : " and must not be empty or whitespace-only"}. (type string)`,
5248
);
5349
}
5450

51+
if (!allowEmpty && !value.trim()) {
52+
throw new Error(`Argument ${argName} must not be empty or whitespace-only`);
53+
}
54+
5555
return value;
5656
}
5757

core/tools/parseArgs.vitest.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,57 @@ describe("getStringArg", () => {
144144
"`name` argument is required and must not be empty or whitespace-only. (type string)",
145145
);
146146
});
147+
148+
it("should convert parsed JSON object to string for contents parameter", () => {
149+
// This simulates the case where JSON.parse() has converted a JSON string into an object
150+
const args = { contents: { key: "value", number: 123 } };
151+
const result = getStringArg(args, "contents");
152+
expect(result).toBe('{"key":"value","number":123}');
153+
});
154+
155+
it("should convert nested JSON object to string for contents parameter", () => {
156+
const args = {
157+
contents: {
158+
user: {
159+
name: "John",
160+
details: {
161+
age: 30,
162+
preferences: ["coding", "reading"],
163+
},
164+
},
165+
},
166+
};
167+
const result = getStringArg(args, "contents");
168+
const expected =
169+
'{"user":{"name":"John","details":{"age":30,"preferences":["coding","reading"]}}}';
170+
expect(result).toBe(expected);
171+
});
172+
173+
it("should convert JSON array to string for contents parameter", () => {
174+
const args = { contents: ["item1", "item2", { key: "value" }] };
175+
const result = getStringArg(args, "contents");
176+
expect(result).toBe('["item1","item2",{"key":"value"}]');
177+
});
178+
179+
it("should throw error when non-contents parameter is an object", () => {
180+
const args = { filename: { invalid: "object" } };
181+
expect(() => getStringArg(args, "filename")).toThrowError(
182+
"Argument `filename` must be a string, not an object",
183+
);
184+
});
185+
186+
it("should handle contents parameter that is already a string", () => {
187+
const args = { contents: "already a string" };
188+
const result = getStringArg(args, "contents");
189+
expect(result).toBe("already a string");
190+
});
191+
192+
it("should handle contents parameter that is null", () => {
193+
const args = { contents: null };
194+
expect(() => getStringArg(args, "contents")).toThrowError(
195+
"`contents` argument is required and must not be empty or whitespace-only. (type string)",
196+
);
197+
});
147198
});
148199

149200
describe("getOptionalStringArg", () => {

0 commit comments

Comments
 (0)