feat(config-editor): add AST parser (speed/enumRef/raw) with tests
This commit is contained in:
47
extensions/pixelhero-config-editor/__tests__/parser.test.ts
Normal file
47
extensions/pixelhero-config-editor/__tests__/parser.test.ts
Normal file
@@ -0,0 +1,47 @@
|
||||
import { test } from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import * as ts from 'typescript';
|
||||
import { parseExpression, findExportObjectLiteral } from '../src/io/parser';
|
||||
|
||||
function parse(text: string): ts.Expression {
|
||||
const src = ts.createSourceFile('x.ts', `const _ = ${text};`, ts.ScriptTarget.Latest, true);
|
||||
const decl = src.statements[0] as ts.VariableStatement;
|
||||
return decl.declarationList.declarations[0].initializer!;
|
||||
}
|
||||
|
||||
test('num / str / bool', () => {
|
||||
assert.deepEqual(parseExpression(parse('400')), { kind: 'num', value: 400 });
|
||||
assert.deepEqual(parseExpression(parse('"小铁卫"')), { kind: 'str', value: '小铁卫' });
|
||||
assert.deepEqual(parseExpression(parse('true')), { kind: 'bool', value: true });
|
||||
});
|
||||
test('enumRef: FacSet.HERO', () => {
|
||||
assert.deepEqual(parseExpression(parse('FacSet.HERO')), { kind: 'enumRef', qualifier: 'FacSet', member: 'HERO' });
|
||||
});
|
||||
test('speed: AtkSpeedSet[AtkSpeedLv.Slow3].cd', () => {
|
||||
assert.deepEqual(parseExpression(parse('AtkSpeedSet[AtkSpeedLv.Slow3].cd')), { kind: 'speed', level: 'Slow3' });
|
||||
});
|
||||
test('non-Speed two-segment access falls back to enumRef', () => {
|
||||
assert.deepEqual(parseExpression(parse('Foo.bar')), { kind: 'enumRef', qualifier: 'Foo', member: 'bar' });
|
||||
});
|
||||
test('arr', () => {
|
||||
assert.deepEqual(parseExpression(parse('[1,2,3]')), { kind: 'arr', items: [
|
||||
{ kind: 'num', value: 1 }, { kind: 'num', value: 2 }, { kind: 'num', value: 3 }] });
|
||||
});
|
||||
test('obj with numeric + identifier keys', () => {
|
||||
const v = parseExpression(parse('{6001:{uuid:6001},name:"x"}'));
|
||||
assert.equal(v.kind, 'obj');
|
||||
assert.deepEqual(v.props['6001'], { kind: 'obj', props: { uuid: { kind: 'num', value: 6001 } } });
|
||||
assert.deepEqual(v.props['name'], { kind: 'str', value: 'x' });
|
||||
});
|
||||
test('raw: unsupported expression preserved verbatim', () => {
|
||||
const v = parseExpression(parse('a + b'));
|
||||
assert.equal(v.kind, 'raw');
|
||||
assert.equal((v as any).text, 'a + b');
|
||||
});
|
||||
test('findExportObjectLiteral locates HeroInfo', () => {
|
||||
const src = ts.createSourceFile('x.ts',
|
||||
`export const HeroInfo: Record<number, any> = { 5011:{uuid:5011} };`, ts.ScriptTarget.Latest, true);
|
||||
const node = findExportObjectLiteral(src, 'HeroInfo');
|
||||
assert.ok(node);
|
||||
assert.equal(node!.properties.length, 1);
|
||||
});
|
||||
Reference in New Issue
Block a user