refactor: change CommandSchema's options/flags to be Records
This commit is contained in:
@@ -21,11 +21,11 @@ describe('parseCommandSchema with inline-schema', () => {
|
||||
it('should parse schema with typed options', () => {
|
||||
const schema = parseCommandSchema('move <from> <to> [--all: boolean] [--count: number]');
|
||||
expect(schema.name).toBe('move');
|
||||
expect(schema.flags).toHaveLength(1);
|
||||
expect(schema.options).toHaveLength(1);
|
||||
expect(schema.flags[0].name).toBe('all');
|
||||
expect(schema.options[0].name).toBe('count');
|
||||
expect(schema.options[0].schema).toBeDefined();
|
||||
expect(Object.keys(schema.flags)).toHaveLength(1);
|
||||
expect(Object.keys(schema.options)).toHaveLength(1);
|
||||
expect(schema.flags.all.name).toBe('all');
|
||||
expect(schema.options.count.name).toBe('count');
|
||||
expect(schema.options.count.schema).toBeDefined();
|
||||
});
|
||||
|
||||
it('should parse schema with tuple type', () => {
|
||||
@@ -58,7 +58,7 @@ describe('parseCommandSchema with inline-schema', () => {
|
||||
);
|
||||
expect(schema.name).toBe('move');
|
||||
expect(schema.params).toHaveLength(2);
|
||||
expect(schema.options).toHaveLength(1);
|
||||
expect(Object.keys(schema.options)).toHaveLength(1);
|
||||
});
|
||||
|
||||
it('should parse schema with optional typed param', () => {
|
||||
@@ -73,9 +73,9 @@ describe('parseCommandSchema with inline-schema', () => {
|
||||
it('should parse schema with optional typed option', () => {
|
||||
const schema = parseCommandSchema('move <from> [--speed: number]');
|
||||
expect(schema.name).toBe('move');
|
||||
expect(schema.options).toHaveLength(1);
|
||||
expect(schema.options[0].required).toBe(false);
|
||||
expect(schema.options[0].schema).toBeDefined();
|
||||
expect(Object.keys(schema.options)).toHaveLength(1);
|
||||
expect(schema.options.speed.required).toBe(false);
|
||||
expect(schema.options.speed.schema).toBeDefined();
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -7,18 +7,8 @@ describe('parseCommandSchema', () => {
|
||||
expect(schema).toEqual({
|
||||
name: '',
|
||||
params: [],
|
||||
options: [],
|
||||
flags: [],
|
||||
});
|
||||
});
|
||||
|
||||
it('should parse command name only', () => {
|
||||
const schema = parseCommandSchema('move');
|
||||
expect(schema).toEqual({
|
||||
name: 'move',
|
||||
params: [],
|
||||
options: [],
|
||||
flags: [],
|
||||
options: {},
|
||||
flags: {},
|
||||
});
|
||||
});
|
||||
|
||||
@@ -55,34 +45,36 @@ describe('parseCommandSchema', () => {
|
||||
|
||||
it('should parse long flags', () => {
|
||||
const schema = parseCommandSchema('move [--force] [--quiet]');
|
||||
expect(schema.flags).toEqual([
|
||||
{ name: 'force' },
|
||||
{ name: 'quiet' },
|
||||
]);
|
||||
expect(schema.flags).toEqual({
|
||||
force: { name: 'force', short: undefined },
|
||||
quiet: { name: 'quiet', short: undefined },
|
||||
});
|
||||
});
|
||||
|
||||
it('should parse short flags', () => {
|
||||
const schema = parseCommandSchema('move [-f] [-q]');
|
||||
expect(schema.flags).toEqual([
|
||||
{ name: 'f', short: 'f' },
|
||||
{ name: 'q', short: 'q' },
|
||||
]);
|
||||
expect(schema.flags).toEqual({
|
||||
f: { name: 'f', short: 'f' },
|
||||
q: { name: 'q', short: 'q' },
|
||||
});
|
||||
});
|
||||
|
||||
it('should parse long options', () => {
|
||||
const schema = parseCommandSchema('move --x: string [--y: string]');
|
||||
expect(schema.options).toEqual([
|
||||
{ name: 'x', required: true, schema: expect.any(Object) },
|
||||
{ name: 'y', required: false, schema: expect.any(Object) },
|
||||
]);
|
||||
expect(Object.keys(schema.options)).toEqual(['x', 'y']);
|
||||
expect(schema.options.x).toMatchObject({ name: 'x', required: true });
|
||||
expect(schema.options.x.schema).toBeDefined();
|
||||
expect(schema.options.y).toMatchObject({ name: 'y', required: false });
|
||||
expect(schema.options.y.schema).toBeDefined();
|
||||
});
|
||||
|
||||
it('should parse short options', () => {
|
||||
const schema = parseCommandSchema('move -x: string [-y: string]');
|
||||
expect(schema.options).toEqual([
|
||||
{ name: 'x', short: 'x', required: true, schema: expect.any(Object) },
|
||||
{ name: 'y', short: 'y', required: false, schema: expect.any(Object) },
|
||||
]);
|
||||
expect(Object.keys(schema.options)).toEqual(['x', 'y']);
|
||||
expect(schema.options.x).toMatchObject({ name: 'x', short: 'x', required: true });
|
||||
expect(schema.options.x.schema).toBeDefined();
|
||||
expect(schema.options.y).toMatchObject({ name: 'y', short: 'y', required: false });
|
||||
expect(schema.options.y.schema).toBeDefined();
|
||||
});
|
||||
|
||||
it('should parse mixed schema', () => {
|
||||
@@ -93,13 +85,13 @@ describe('parseCommandSchema', () => {
|
||||
{ name: 'from', required: true, variadic: false, schema: undefined },
|
||||
{ name: 'to', required: true, variadic: false, schema: undefined },
|
||||
],
|
||||
flags: [
|
||||
{ name: 'force' },
|
||||
{ name: 'f', short: 'f' },
|
||||
],
|
||||
options: [
|
||||
{ name: 'speed', short: 's', required: false, schema: expect.any(Object), defaultValue: undefined },
|
||||
],
|
||||
flags: {
|
||||
force: { name: 'force', short: undefined },
|
||||
f: { name: 'f', short: 'f' },
|
||||
},
|
||||
options: {
|
||||
speed: { name: 'speed', short: 's', required: false, schema: expect.any(Object), defaultValue: undefined },
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
@@ -107,8 +99,8 @@ describe('parseCommandSchema', () => {
|
||||
const schema = parseCommandSchema('place <piece> <region> [x...] [--rotate: number] [--force] [-f]');
|
||||
expect(schema.name).toBe('place');
|
||||
expect(schema.params).toHaveLength(3);
|
||||
expect(schema.flags).toHaveLength(2);
|
||||
expect(schema.options).toHaveLength(1);
|
||||
expect(Object.keys(schema.flags)).toHaveLength(2);
|
||||
expect(Object.keys(schema.options)).toHaveLength(1);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -237,39 +229,8 @@ describe('integration', () => {
|
||||
|
||||
it('should parse short alias syntax', () => {
|
||||
const schema = parseCommandSchema('move <from> [--verbose: boolean -v]');
|
||||
expect(schema.flags).toHaveLength(1);
|
||||
expect(schema.flags[0]).toEqual({ name: 'verbose', short: 'v' });
|
||||
});
|
||||
|
||||
it('should parse short alias for options', () => {
|
||||
const schema = parseCommandSchema('move <from> [--speed: number -s]');
|
||||
expect(schema.options).toHaveLength(1);
|
||||
expect(schema.options[0]).toEqual({
|
||||
name: 'speed',
|
||||
short: 's',
|
||||
required: false,
|
||||
schema: expect.any(Object),
|
||||
defaultValue: undefined,
|
||||
});
|
||||
});
|
||||
|
||||
it('should parse default value syntax', () => {
|
||||
const schema = parseCommandSchema('move <from> [--speed: number = 10]');
|
||||
expect(schema.options).toHaveLength(1);
|
||||
expect(schema.options[0].defaultValue).toBe(10);
|
||||
});
|
||||
|
||||
it('should parse default string value', () => {
|
||||
const schema = parseCommandSchema('move <from> [--name: string = "default"]');
|
||||
expect(schema.options).toHaveLength(1);
|
||||
expect(schema.options[0].defaultValue).toBe('default');
|
||||
});
|
||||
|
||||
it('should parse short alias with default value', () => {
|
||||
const schema = parseCommandSchema('move <from> [--speed: number -s = 5]');
|
||||
expect(schema.options).toHaveLength(1);
|
||||
expect(schema.options[0].short).toBe('s');
|
||||
expect(schema.options[0].defaultValue).toBe(5);
|
||||
expect(Object.keys(schema.flags)).toHaveLength(1);
|
||||
expect(schema.flags.verbose).toEqual({ name: 'verbose', short: 'v' });
|
||||
});
|
||||
|
||||
it('should parse command with short alias', () => {
|
||||
@@ -288,3 +249,5 @@ describe('integration', () => {
|
||||
expect(command.options.s).toBe('100');
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user