command-parser/tests/Parser.test.js

70 lines
2.1 KiB
JavaScript
Raw Normal View History

const { inspect } = require('node:util');
const { Parser, Command, OptionType } = require('../build');
2023-02-12 17:08:52 +01:00
test('Command definition', () => {
expect(() => new Command()).toThrow();
expect(() => new Command({})).toThrow();
expect(() => new Command(235345)).toThrow();
expect(() => new Command({ name: 'create' })).not.toThrow();
expect(() => new Command({
name: 'create',
options: [{
name: 'test',
type: OptionType.SUB_COMMAND,
options: [{
name:'dingus',
type: OptionType.SUB_COMMAND
}]
}]
})).toThrow();
expect(() => new Command({
name: 'create',
options: [{
name: 'test',
type: OptionType.STRING,
options: [{
name:'dingus',
type: OptionType.SUB_COMMAND
}]
}]
})).toThrow();
expect(() => new Command({
name: 'create',
options: [{
name: 'test',
type: OptionType.SUB_COMMAND_GROUP,
options: [{
name:'dingus',
type: OptionType.SUB_COMMAND
}]
}] })).not.toThrow();
})
2023-02-12 16:20:05 +01:00
test('Command Parser', async () => {
2023-02-12 17:08:52 +01:00
const command = new Command({
name: 'create',
options: [{
name: 'registration-code',
aliases: ['code'],
type: OptionType.SUB_COMMAND,
options: [{
name: 'amount',
type: OptionType.INTEGER,
defaultValue: 1,
valueOptional: true
}]
}]
});
2023-02-12 16:20:05 +01:00
const parser = new Parser({ commands: [command], prefix: '' });
2023-02-12 16:20:05 +01:00
await expect(parser.parseMessage('create')).rejects.toThrow();
2023-02-12 17:08:52 +01:00
// Cannot have an option at the sub-command level
await expect(parser.parseMessage('create test')).rejects.toThrow();
await expect(parser.parseMessage('create code')).resolves.toHaveProperty('command.name', 'create');
await expect(parser.parseMessage('create code 1')).resolves.toHaveProperty('args.amount.value', 1);
2023-02-12 16:20:05 +01:00
});