Files
esengine/tests/ECS/Utils/Matcher.test.ts

288 lines
10 KiB
TypeScript
Raw Normal View History

/**
* Matcher完整测试套件
2025-07-31 15:37:40 +08:00
* Matcher条件构建功能和QuerySystem集成
*/
import { Scene } from '../../../src/ECS/Scene';
import { Entity } from '../../../src/ECS/Entity';
import { Component } from '../../../src/ECS/Component';
import { Matcher } from '../../../src/ECS/Utils/Matcher';
// 测试组件
class Position extends Component {
2025-07-31 15:37:40 +08:00
public x: number = 0;
public y: number = 0;
constructor(...args: unknown[]) {
super();
2025-07-31 15:37:40 +08:00
const [x = 0, y = 0] = args as [number?, number?];
this.x = x;
this.y = y;
}
}
class Velocity extends Component {
2025-07-31 15:37:40 +08:00
public vx: number = 0;
public vy: number = 0;
constructor(...args: unknown[]) {
super();
2025-07-31 15:37:40 +08:00
const [vx = 0, vy = 0] = args as [number?, number?];
this.vx = vx;
this.vy = vy;
}
}
class Health extends Component {
2025-07-31 15:37:40 +08:00
public hp: number = 100;
constructor(...args: unknown[]) {
super();
2025-07-31 15:37:40 +08:00
const [hp = 100] = args as [number?];
this.hp = hp;
}
}
class Dead extends Component {}
describe('Matcher测试套件', () => {
let scene: Scene;
let entities: Entity[];
beforeEach(() => {
scene = new Scene();
scene.begin();
// 创建测试实体
entities = [];
// 实体1: 移动的活体
const entity1 = scene.createEntity('MovingAlive');
entity1.addComponent(new Position(10, 20));
entity1.addComponent(new Velocity(1, 0));
entity1.addComponent(new Health(100));
entities.push(entity1);
// 实体2: 静止的活体
const entity2 = scene.createEntity('StillAlive');
entity2.addComponent(new Position(30, 40));
entity2.addComponent(new Health(50));
entities.push(entity2);
// 实体3: 移动的死体
const entity3 = scene.createEntity('MovingDead');
entity3.addComponent(new Position(50, 60));
entity3.addComponent(new Velocity(0, 1));
entity3.addComponent(new Dead());
entities.push(entity3);
// 实体4: 静止的死体
const entity4 = scene.createEntity('StillDead');
entity4.addComponent(new Position(70, 80));
entity4.addComponent(new Dead());
entities.push(entity4);
});
afterEach(() => {
scene.end();
});
2025-07-31 15:37:40 +08:00
describe('Matcher条件构建测试', () => {
test('Matcher.all()应该创建正确的查询条件', () => {
const matcher = Matcher.all(Position, Health);
const condition = matcher.getCondition();
2025-07-31 15:37:40 +08:00
expect(condition.all).toContain(Position);
expect(condition.all).toContain(Health);
expect(condition.all.length).toBe(2);
});
2025-07-31 15:37:40 +08:00
test('Matcher.any()应该创建正确的查询条件', () => {
const matcher = Matcher.any(Health, Dead);
const condition = matcher.getCondition();
2025-07-31 15:37:40 +08:00
expect(condition.any).toContain(Health);
expect(condition.any).toContain(Dead);
expect(condition.any.length).toBe(2);
});
2025-07-31 15:37:40 +08:00
test('Matcher.none()应该创建正确的查询条件', () => {
const matcher = Matcher.none(Dead);
const condition = matcher.getCondition();
2025-07-31 15:37:40 +08:00
expect(condition.none).toContain(Dead);
expect(condition.none.length).toBe(1);
});
2025-07-31 15:37:40 +08:00
test('链式调用应该正确工作', () => {
const matcher = Matcher.all(Position)
.any(Health, Velocity)
.none(Dead);
2025-07-31 15:37:40 +08:00
const condition = matcher.getCondition();
expect(condition.all).toContain(Position);
expect(condition.any).toContain(Health);
expect(condition.any).toContain(Velocity);
expect(condition.none).toContain(Dead);
});
2025-07-31 15:37:40 +08:00
test('byComponent()应该创建单组件查询条件', () => {
const matcher = Matcher.byComponent(Position);
const condition = matcher.getCondition();
2025-07-31 15:37:40 +08:00
expect(condition.component).toBe(Position);
});
2025-07-31 15:37:40 +08:00
test('byTag()应该创建标签查询条件', () => {
const matcher = Matcher.byTag(123);
const condition = matcher.getCondition();
2025-07-31 15:37:40 +08:00
expect(condition.tag).toBe(123);
});
2025-07-31 15:37:40 +08:00
test('byName()应该创建名称查询条件', () => {
const matcher = Matcher.byName('TestEntity');
const condition = matcher.getCondition();
2025-07-31 15:37:40 +08:00
expect(condition.name).toBe('TestEntity');
});
});
describe('QuerySystem集成测试', () => {
test('使用QuerySystem的queryAll()查询所有匹配实体', () => {
const result = scene.querySystem.queryAll(Position, Health);
expect(result.entities.map(e => e.name).sort()).toEqual(['MovingAlive', 'StillAlive']);
});
2025-07-31 15:37:40 +08:00
test('使用QuerySystem的queryAny()查询任一匹配实体', () => {
const result = scene.querySystem.queryAny(Health, Dead);
expect(result.entities.length).toBe(4); // 所有实体都有Health或Dead
});
test('使用QuerySystem的queryNone()查询排除实体', () => {
const result = scene.querySystem.queryNone(Dead);
const aliveEntities = result.entities.filter(e => e.hasComponent(Position));
expect(aliveEntities.map(e => e.name).sort()).toEqual(['MovingAlive', 'StillAlive']);
});
test('QuerySystem查询性能统计', () => {
scene.querySystem.queryAll(Position, Velocity);
const stats = scene.querySystem.getStats();
2025-07-31 15:37:40 +08:00
expect(stats.queryStats.totalQueries).toBeGreaterThan(0);
expect(stats.queryStats.cacheHits).toBeGreaterThanOrEqual(0);
});
});
2025-07-31 15:37:40 +08:00
describe('实际使用场景测试', () => {
test('游戏系统中的移动实体查询', () => {
// 查询所有可移动的实体(有位置和速度的)
const movableEntities = scene.querySystem.queryAll(Position, Velocity);
expect(movableEntities.entities.length).toBe(2); // MovingAlive, MovingDead
movableEntities.entities.forEach(entity => {
const pos = entity.getComponent(Position)!;
const vel = entity.getComponent(Velocity)!;
expect(pos).toBeDefined();
expect(vel).toBeDefined();
});
});
2025-07-31 15:37:40 +08:00
test('游戏系统中的活体实体查询', () => {
// 查询所有活体实体(有血量,没有死亡标记的)
const aliveEntitiesAll = scene.querySystem.queryAll(Health);
const deadEntitiesAll = scene.querySystem.queryAll(Dead);
2025-07-31 15:37:40 +08:00
expect(aliveEntitiesAll.entities.length).toBe(2); // MovingAlive, StillAlive
expect(deadEntitiesAll.entities.length).toBe(2); // MovingDead, StillDead
});
2025-07-31 15:37:40 +08:00
test('复杂查询:查找活着的移动实体', () => {
// 首先获取所有有位置和速度的实体
const movableEntities = scene.querySystem.queryAll(Position, Velocity);
2025-07-31 15:37:40 +08:00
// 然后过滤出活着的(有血量的)
const aliveMovableEntities = movableEntities.entities.filter(entity =>
entity.hasComponent(Health)
);
2025-07-31 15:37:40 +08:00
expect(aliveMovableEntities.length).toBe(1); // 只有MovingAlive
expect(aliveMovableEntities[0].name).toBe('MovingAlive');
});
2025-07-31 15:37:40 +08:00
test('复合查询条件应用', () => {
// 使用Matcher建立复杂条件然后用QuerySystem执行
const matcher = Matcher.all(Position).any(Health, Dead);
const condition = matcher.getCondition();
2025-07-31 15:37:40 +08:00
// 这里演示如何用条件实际执行需要QuerySystem支持复合条件
expect(condition.all).toContain(Position);
expect(condition.any).toContain(Health);
expect(condition.any).toContain(Dead);
});
});
2025-07-31 15:37:40 +08:00
describe('性能测试', () => {
test('大量简单查询的性能', () => {
const startTime = performance.now();
2025-07-31 15:37:40 +08:00
for (let i = 0; i < 1000; i++) {
scene.querySystem.queryAll(Position);
}
2025-07-31 15:37:40 +08:00
const executionTime = performance.now() - startTime;
expect(executionTime).toBeLessThan(100); // 应该在100ms内完成
});
2025-07-31 15:37:40 +08:00
test('复杂查询的性能', () => {
const startTime = performance.now();
2025-07-31 15:37:40 +08:00
for (let i = 0; i < 100; i++) {
scene.querySystem.queryAll(Position, Health);
scene.querySystem.queryAny(Health, Dead);
scene.querySystem.queryNone(Dead);
}
2025-07-31 15:37:40 +08:00
const executionTime = performance.now() - startTime;
expect(executionTime).toBeLessThan(50);
});
2025-07-31 15:37:40 +08:00
test('不存在组件的查询性能', () => {
class NonExistentComponent extends Component {
constructor(...args: unknown[]) {
super();
}
}
2025-07-31 15:37:40 +08:00
const result = scene.querySystem.queryAll(NonExistentComponent);
expect(result.entities.length).toBe(0);
});
});
2025-07-31 15:37:40 +08:00
describe('边界情况测试', () => {
2025-07-31 15:37:40 +08:00
test('空查询应该返回所有实体', () => {
const result = scene.querySystem.queryAll();
expect(result.entities.length).toBe(entities.length);
});
2025-07-31 15:37:40 +08:00
test('查询不存在的组件应该返回空结果', () => {
class NonExistentComponent extends Component {
constructor(...args: unknown[]) {
super();
}
}
2025-07-31 15:37:40 +08:00
const result = scene.querySystem.queryAll(NonExistentComponent);
expect(result.entities.length).toBe(0);
});
2025-07-31 15:37:40 +08:00
test('Matcher条件构建的边界情况', () => {
const emptyMatcher = Matcher.complex();
const condition = emptyMatcher.getCondition();
2025-07-31 15:37:40 +08:00
expect(condition.all.length).toBe(0);
expect(condition.any.length).toBe(0);
expect(condition.none.length).toBe(0);
});
});
});