fix: query tests

This commit is contained in:
cha0s 2024-08-03 11:20:08 -05:00
parent 2f5032762a
commit 4cb29e56cd

View File

@ -1,37 +1,39 @@
import {expect, test} from 'vitest';
import Ecs from './ecs';
import Component from './component.js';
import Query from './query.js';
function wrapProperties(name, properties) {
return class WrappedComponent extends Component {
static name = name;
const Components = [
['A', {a: {type: 'int32', defaultValue: 64}}],
['B', {b: {type: 'int32', defaultValue: 32}}],
['C', {c: {type: 'int32'}}],
]
.reduce((Components, [componentName, properties]) => {
return {
...Components,
[componentName]: class extends Component {
static componentName = componentName;
static properties = properties;
},
};
}
}, {})
const A = new (wrapProperties('A', {a: {type: 'int32', defaultValue: 420}}));
const B = new (wrapProperties('B', {b: {type: 'int32', defaultValue: 69}}));
const C = new (wrapProperties('C', {c: {type: 'int32'}}));
const Components = {A, B, C};
Components.A.createMany([[2], [3]]);
Components.B.createMany([[1], [2]]);
Components.C.createMany([[2], [4]]);
const fakeEcs = (Components) => ({
Components,
get(id) {
return Object.fromEntries(
Object.entries(Components)
.map(([componentName, Component]) => [componentName, Component.get(id)])
.concat([['id', id]])
);
const ecsTest = test.extend({
ecs: async ({}, use) => {
const ecs = new Ecs({Components});
await ecs.createManySpecific([
[1, {B: {}}],
[2, {A: {}, B: {}, C: {}}],
[3, {A: {}}],
[4, {C: {}}],
]);
await use(ecs);
},
});
function testQuery(parameters, expected) {
const query = new Query(parameters, fakeEcs(Components));
async function testQuery(ecs, parameters, expected) {
const query = new Query(parameters, ecs);
query.reindex([1, 2, 3]);
expect(query.count)
.to.equal(expected.length);
@ -41,22 +43,22 @@ function testQuery(parameters, expected) {
}
}
test('can query all', () => {
testQuery([], [1, 2, 3]);
ecsTest('can query all', async ({ecs}) => {
await testQuery(ecs, [], [1, 2, 3]);
});
test('can query some', () => {
testQuery(['A'], [2, 3]);
testQuery(['A', 'B'], [2]);
ecsTest('can query some', async ({ecs}) => {
await testQuery(ecs, ['A'], [2, 3]);
await testQuery(ecs, ['A', 'B'], [2]);
});
test('can query excluding', () => {
testQuery(['!A'], [1]);
testQuery(['A', '!B'], [3]);
ecsTest('can query excluding', async ({ecs}) => {
await testQuery(ecs, ['!A'], [1]);
await testQuery(ecs, ['A', '!B'], [3]);
});
test('can deindex', () => {
const query = new Query(['A'], fakeEcs(Components));
ecsTest('can deindex', async ({ecs}) => {
const query = new Query(['A'], ecs);
query.reindex([1, 2, 3]);
expect(query.count)
.to.equal(2);
@ -65,24 +67,22 @@ test('can deindex', () => {
.to.equal(1);
});
test('can reindex', () => {
const Test = new (wrapProperties('Test', {a: {type: 'int32', defaultValue: 420}}));
Test.createMany([[2], [3]]);
const query = new Query(['Test'], fakeEcs({Test}));
query.reindex([2, 3]);
ecsTest('can reindex', async ({ecs}) => {
const query = new Query(['B'], ecs);
query.reindex([1, 2]);
expect(query.count)
.to.equal(2);
Test.destroy(2);
query.reindex([2, 3]);
ecs.destroyMany(new Set([2]));
query.reindex([1, 2]);
expect(query.count)
.to.equal(1);
});
test('can select', () => {
const query = new Query(['A'], fakeEcs(Components));
ecsTest('can select', async ({ecs}) => {
const query = new Query(['A'], ecs);
query.reindex([1, 2, 3]);
const it = query.select();
const {value: {A}} = it.next();
expect(A.a)
.to.equal(420);
.to.equal(64);
});