command-parser/tests/Parser.test.js

120 lines
3.7 KiB
JavaScript
Raw Normal View History

// import { Parser, Command, OptionType } from '../build/esm/index';
const { Parser, Command, OptionType } = require('../build/cjs');
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();
})
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)
});
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);
});
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');
2023-02-12 16:20:05 +01:00
});