boardgame-core/tests/utils/command.test.ts

175 lines
5.1 KiB
TypeScript

import { describe, it, expect } from 'vitest';
import { parseCommand, type Command } from '../../src/utils/command';
describe('parseCommand', () => {
it('should parse empty string', () => {
const result = parseCommand('');
expect(result).toEqual({
name: '',
flags: {},
options: {},
params: []
});
});
it('should parse command name only', () => {
const result = parseCommand('move');
expect(result).toEqual({
name: 'move',
flags: {},
options: {},
params: []
});
});
it('should parse command with params', () => {
const result = parseCommand('move meeple1 region1');
expect(result).toEqual({
name: 'move',
flags: {},
options: {},
params: ['meeple1', 'region1']
});
});
it('should parse command with long flags', () => {
const result = parseCommand('move meeple1 --force --quiet');
expect(result).toEqual({
name: 'move',
flags: { force: true, quiet: true },
options: {},
params: ['meeple1']
});
});
it('should parse command with short flags', () => {
const result = parseCommand('move meeple1 -f -q');
expect(result).toEqual({
name: 'move',
flags: { f: true, q: true },
options: {},
params: ['meeple1']
});
});
it('should parse command with long options', () => {
const result = parseCommand('move meeple1 --x 10 --y 20');
expect(result).toEqual({
name: 'move',
flags: {},
options: { x: '10', y: '20' },
params: ['meeple1']
});
});
it('should parse command with short options', () => {
const result = parseCommand('move meeple1 -x 10 -y 20');
expect(result).toEqual({
name: 'move',
flags: {},
options: { x: '10', y: '20' },
params: ['meeple1']
});
});
it('should parse command with mixed flags和选项', () => {
const result = parseCommand('move meeple1 region1 --force -x 10 -q');
expect(result).toEqual({
name: 'move',
flags: { force: true, q: true },
options: { x: '10' },
params: ['meeple1', 'region1']
});
});
it('should handle extra whitespace', () => {
const result = parseCommand(' move meeple1 --force ');
expect(result).toEqual({
name: 'move',
flags: { force: true },
options: {},
params: ['meeple1']
});
});
it('should parse complex command', () => {
const result = parseCommand('place meeple1 board --x 5 --y 3 --rotate 90 --force');
expect(result).toEqual({
name: 'place',
flags: { force: true },
options: { x: '5', y: '3', rotate: '90' },
params: ['meeple1', 'board']
});
});
it('should treat negative number as option value', () => {
const result = parseCommand('set --value -10');
expect(result).toEqual({
name: 'set',
flags: {},
options: { value: '-10' },
params: []
});
});
it('should parse quoted string with double quotes', () => {
const result = parseCommand('place tile "large castle" --x 5');
expect(result).toEqual({
name: 'place',
flags: {},
options: { x: '5' },
params: ['tile', 'large castle']
});
});
it('should parse quoted string with single quotes', () => {
const result = parseCommand("place tile 'large castle' --x 5");
expect(result).toEqual({
name: 'place',
flags: {},
options: { x: '5' },
params: ['tile', 'large castle']
});
});
it('should handle escaped quotes', () => {
const result = parseCommand('say "hello \\"world\\""');
expect(result).toEqual({
name: 'say',
flags: {},
options: {},
params: ['hello "world"']
});
});
it('should handle escaped backslash', () => {
const result = parseCommand('set path "C:\\\\Users"');
expect(result).toEqual({
name: 'set',
flags: {},
options: {},
params: ['path', 'C:\\Users']
});
});
it('should handle mixed quotes', () => {
const result = parseCommand('cmd "hello world" \'foo bar\' --flag');
expect(result).toEqual({
name: 'cmd',
flags: { flag: true },
options: {},
params: ['hello world', 'foo bar']
});
});
it('should handle quote in middle of argument', () => {
const result = parseCommand('cmd "hello\'s world"');
expect(result).toEqual({
name: 'cmd',
flags: {},
options: {},
params: ["hello's world"]
});
});
});