Skip to content

Commit

Permalink
add test case for serde default handlers in world-builder
Browse files Browse the repository at this point in the history
  • Loading branch information
minecrawler committed Apr 16, 2024
1 parent 2b2230b commit 7cb9f57
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions src/world/world-builder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import {expect} from 'chai';
import {dataStructDeserializer, dataStructSerializer} from "./world-builder.util";
import {WorldBuilder} from "./world-builder";
import {SerDe} from "../serde/serde";
import {Entity, IEntity} from "../entity/entity";
import {SerialFormat} from "../serde/serial-format";

describe('Test WorldBuilder', () => {
const worldName = 'world1' as const;
Expand All @@ -27,6 +29,59 @@ describe('Test WorldBuilder', () => {
expect(deserializedObj instanceof Component).eq(true);
});

it('Default De-/Serialize non-empty object component with methods', () => {
const entity1 = new Entity();

class AComponent {
constructor(
private _foo = 1,
public bar = 'baz',
) {}

get foo() {
return this._foo / 100;
}

set foo(val: number) {
this._foo = val * 100;
}

public getInternalFoo() {
return this._foo;
}
}

entity1.addComponent(new AComponent());

{
const serde = new SerDe();
const serdeOptions = {
useDefaultHandler: false,
useRegisteredHandlers: true,
};

serde.registerTypeHandler(
AComponent,
dataStructDeserializer.bind(undefined, AComponent),
dataStructSerializer
);

const serial = serde.serialize({
entities: [entity1].values(),
resources: {},
}, serdeOptions).toJSON();
const deserialized = serde.deserialize(SerialFormat.fromJSON(serial), serdeOptions);
const deserializedEntity1 = deserialized.entities.next().value as IEntity | null;
const deserializedAComponent = deserializedEntity1?.getComponent(AComponent);

expect(deserializedAComponent instanceof AComponent).eq(true);
// The setter isn't invoked when deserializing the object
expect(deserializedAComponent!.getInternalFoo()).eq(1);
// However the getter is, here!
expect(deserializedAComponent!.foo).eq(0.01);
}
});

it('Aliases', () => {
const worldBuilder = new WorldBuilder(new SerDe());
const world = worldBuilder.name(worldName).build();
Expand Down

0 comments on commit 7cb9f57

Please sign in to comment.