refactor: component test

This commit is contained in:
cha0s 2024-08-14 17:46:59 -05:00
parent 77dcb7c5a1
commit ac9e0b4134
3 changed files with 31 additions and 35 deletions

View File

@ -3,25 +3,21 @@ import {expect, test} from 'vitest';
import Component from './component.js'; import Component from './component.js';
import Ecs from './ecs.js'; import Ecs from './ecs.js';
import System from './system.js'; import System from './system.js';
import {wrapComponents} from './test-helper.js';
function wrapProperties(name, properties) { const Components = wrapComponents([
return class WrappedComponent extends Component { ['Empty', {}],
static componentName = name; ['Momentum', {x: {type: 'int32'}, y: {type: 'int32'}, z: {type: 'int32'}}],
static properties = properties; ['Name', {name: {type: 'string'}}],
}; ['Position', {x: {type: 'int32', defaultValue: 32}, y: {type: 'int32'}, z: {type: 'int32'}}],
} ]);
const Empty = wrapProperties('Empty', {}); const {
Empty,
const Name = wrapProperties('Name', { Momentum,
name: {type: 'string'}, Position,
}); Name,
} = Components;
const Position = wrapProperties('Position', {
x: {type: 'int32', defaultValue: 32},
y: {type: 'int32'},
z: {type: 'int32'},
});
function asyncTimesTwo(x) { function asyncTimesTwo(x) {
return new Promise((resolve) => { return new Promise((resolve) => {
@ -131,11 +127,6 @@ test('inserts components into entities', async () => {
}); });
test('ticks systems', async () => { test('ticks systems', async () => {
const Momentum = wrapProperties('Momentum', {
x: {type: 'int32'},
y: {type: 'int32'},
z: {type: 'int32'},
});
const ecs = new Ecs({ const ecs = new Ecs({
Components: {Momentum, Position}, Components: {Momentum, Position},
Systems: { Systems: {

View File

@ -1,23 +1,14 @@
import {expect, test} from 'vitest'; import {expect, test} from 'vitest';
import Ecs from './ecs'; import Ecs from './ecs.js';
import Component from './component.js'; import {wrapComponents} from './test-helper.js';
import Query from './query.js'; import Query from './query.js';
const Components = [ const Components = wrapComponents([
['A', {a: {type: 'int32', defaultValue: 64}}], ['A', {a: {type: 'int32', defaultValue: 64}}],
['B', {b: {type: 'int32', defaultValue: 32}}], ['B', {b: {type: 'int32', defaultValue: 32}}],
['C', {c: {type: 'int32'}}], ['C', {c: {type: 'int32'}}],
] ]);
.reduce((Components, [componentName, properties]) => {
return {
...Components,
[componentName]: class extends Component {
static componentName = componentName;
static properties = properties;
},
};
}, {})
const ecsTest = test.extend({ const ecsTest = test.extend({
ecs: async ({}, use) => { ecs: async ({}, use) => {

14
app/ecs/test-helper.js Normal file
View File

@ -0,0 +1,14 @@
import Component from './component.js';
export function wrapComponents(Components) {
return Components
.reduce((Components, [componentName, properties]) => {
return {
...Components,
[componentName]: class extends Component {
static componentName = componentName;
static properties = properties;
},
};
}, {})
}