command-parser/tests/Parser.test.js
Navy.gif 4fb0bed436
All checks were successful
continuous-integration/drone/push Build is passing
more tests & enforce structure
2023-02-12 18:08:52 +02:00

70 lines
2.1 KiB
JavaScript

const { inspect } = require('node:util');
const { Parser, Command, OptionType } = require('../build');
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();
})
test('Command Parser', async () => {
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
}]
}]
});
const parser = new Parser({ commands: [command], prefix: '' });
await expect(parser.parseMessage('create')).rejects.toThrow();
// 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);
});