/[sudobot]/trunk/tests/framework/arguments/ArgumentParser.test.ts
ViewVC logotype

Annotation of /trunk/tests/framework/arguments/ArgumentParser.test.ts

Parent Directory Parent Directory | Revision Log Revision Log


Revision 575 - (hide annotations)
Mon Jul 29 17:59:26 2024 UTC (8 months ago) by rakinar2
File MIME type: application/typescript
File size: 3595 byte(s)
chore: add trunk
1 rakinar2 575 import "reflect-metadata";
2    
3     import Application from "@framework/app/Application";
4     import ArgumentParser from "@framework/arguments/ArgumentParser";
5     import { ArgumentSchema } from "@framework/arguments/ArgumentTypes";
6     import { InvalidArgumentError } from "@framework/arguments/InvalidArgumentError";
7     import NumberArgument from "@framework/arguments/NumberArgument";
8     import StringArgument from "@framework/arguments/StringArgument";
9     import { Command } from "@framework/commands/Command";
10     import LegacyContext from "@framework/commands/LegacyContext";
11     import { Message } from "discord.js";
12     import { beforeEach, describe, expect, it } from "vitest";
13     import { createApplication } from "../../mocks/application.mock";
14    
15     @ArgumentSchema.Definition({
16     names: ["string"],
17     types: [StringArgument],
18     optional: false
19     })
20     @ArgumentSchema.Definition({
21     names: ["number"],
22     types: [NumberArgument],
23     optional: false
24     })
25     @ArgumentSchema.Options([
26     {
27     id: "option",
28     longNames: ["option"],
29     shortNames: ["o"],
30     requiresValue: false,
31     required: false
32     },
33     {
34     id: "flag",
35     longNames: ["flag"],
36     shortNames: ["f"],
37     requiresValue: true,
38     required: false
39     }
40     ])
41     class TestCommand {
42     public readonly name = "test";
43     public readonly description = "Test command";
44    
45     public async execute() {
46     return;
47     }
48     }
49    
50     describe("ArgumentParser", () => {
51     let application: Application;
52     let parser: ArgumentParser;
53    
54     beforeEach(() => {
55     application = createApplication();
56     parser = new ArgumentParser();
57     });
58    
59     it("should parse basic arguments", async () => {
60     const commandContent = "test hello 12";
61     const argv = commandContent.split(/\s+/);
62     const args = argv.slice(1);
63    
64     const result = await parser.parse({
65     command: new TestCommand() as unknown as Command,
66     context: new LegacyContext("test", commandContent, {} as Message<true>, args, argv)
67     });
68    
69     expect(result).toEqual({
70     value: {
71     parsedArgs: {
72     string: "hello",
73     number: 12
74     },
75     parsedOptions: {}
76     },
77     errors: {}
78     });
79     });
80    
81     it("should parse basic arguments with options", async () => {
82     const commandContent = "test -o hello -f 486 12";
83     const argv = commandContent.split(/\s+/);
84     const args = argv.slice(1);
85    
86     const result = await parser.parse({
87     command: new TestCommand() as unknown as Command,
88     context: new LegacyContext("test", commandContent, {} as Message<true>, args, argv)
89     });
90    
91     expect(result).toEqual({
92     value: {
93     parsedArgs: {
94     string: "hello",
95     number: 12
96     },
97     parsedOptions: {
98     option: true,
99     flag: "486"
100     }
101     },
102     errors: {}
103     });
104     });
105    
106     it("should throw an error if arguments are passed incorrectly", () => {
107     const commandContent = "test hello";
108     const argv = commandContent.split(/\s+/);
109     const args = argv.slice(1);
110    
111     return expect(
112     parser.parse({
113     command: new TestCommand() as unknown as Command,
114     context: new LegacyContext("test", commandContent, {} as Message<true>, args, argv),
115     throwOnError: true
116     })
117     ).rejects.toThrowError(InvalidArgumentError);
118     });
119     });

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26