command-parser/tests/Parser.test.js

120 lines
3.7 KiB
JavaScript

// import { Parser, Command, OptionType } from '../build/esm/index';
const { Parser, Command, OptionType } = require('../build/cjs');
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',
flag: true,
type: OptionType.INTEGER,
defaultValue: 1,
valueOptional: true
}]
}]
});
const parser = new Parser({ commands: [command], prefix: '', debug: true });
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 -a 1')).resolves.toHaveProperty('args.amount.value', 1)
});
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);
});
test('Choice option', async () => {
const command = new Command({
name: 'create',
options: [{
name: 'registration-code',
aliases: ['code'],
type: OptionType.SUB_COMMAND,
options: [{
name: 'amount',
type: OptionType.STRING,
choices: ['one', 'two']
}]
}]
});
const parser = new Parser({ commands: [command], prefix: '' });
await expect(parser.parseMessage('create code 1')).rejects.toThrow();
await expect(parser.parseMessage('create code one')).resolves.toHaveProperty('args.amount.value', 'one');
});