silphius/app/ecs/schema.test.js

137 lines
3.6 KiB
JavaScript
Raw Normal View History

2024-06-10 22:42:30 -05:00
import {expect, test} from 'vitest';
import Schema from './schema.js';
2024-06-12 01:38:05 -05:00
test('defaults values', () => {
[
'uint8',
'int8',
'uint16',
'int16',
'uint32',
'int32',
'uint64',
'int64',
'float32',
'float64',
].forEach((type) => {
expect(Schema.defaultValue({type}))
.to.equal(0);
expect(new Schema({type}).specification.defaultValue)
.to.equal(0);
});
expect(Schema.defaultValue({type: 'string'}))
.to.equal('');
expect(new Schema({type: 'string'}).specification.defaultValue)
.to.equal('');
expect(Schema.defaultValue({type: 'array', subtype: {type: 'string'}}))
.to.deep.equal([]);
expect(new Schema({type: 'array', subtype: {type: 'string'}}).specification.defaultValue)
.to.deep.equal([]);
expect(Schema.defaultValue({
type: 'object',
properties: {
foo: {type: 'uint8'},
bar: {type: 'string'},
baz: {type: 'object', properties: {blah: {type: 'array', subtype: {type: 'string'}}}},
},
}))
.to.deep.equal({foo: 0, bar: '', baz: {blah: []}});
expect(new Schema({
type: 'object',
properties: {
foo: {type: 'uint8'},
bar: {type: 'string'},
baz: {type: 'object', properties: {blah: {type: 'array', subtype: {type: 'string'}}}},
},
}).specification.defaultValue)
.to.deep.equal({foo: 0, bar: '', baz: {blah: []}});
});
2024-06-10 22:42:30 -05:00
2024-06-12 01:38:05 -05:00
test('validates a schema', () => {
[
'uint8',
'int8',
'uint16',
'int16',
'uint32',
'int32',
'uint64',
'int64',
'float32',
'float64',
'string',
].forEach((type) => {
expect(() => {
new Schema({type});
new Schema({type: 'array', subtype: {type}});
new Schema({type: 'object', properties: {foo: {type}}});
})
.to.not.throw();
});
2024-06-10 22:42:30 -05:00
});
2024-06-12 01:38:05 -05:00
test('calculates the size of concrete instances', () => {
expect(
(new Schema({type: 'string'}))
.sizeOf('hi')
)
.to.equal(4 + (new TextEncoder().encode('hi')).length);
2024-06-11 21:00:03 -05:00
expect(
2024-06-12 01:38:05 -05:00
(new Schema(
{type: 'object', properties: {
foo: {type: 'uint8'},
bar: {type: 'uint32'},
baz: {type: 'string'},
}}
))
.sizeOf({foo: 69, bar: 420, baz: 'aα'})
2024-06-11 21:00:03 -05:00
)
2024-06-12 01:38:05 -05:00
.to.equal(
1
+ 4
+ 4 + (new TextEncoder().encode('aα')).length
);
2024-06-11 21:00:03 -05:00
expect(
2024-06-12 01:38:05 -05:00
(new Schema({type: 'array', subtype: {type: 'string'}}))
.sizeOf(['hallo', 'hαllo'])
2024-06-11 21:00:03 -05:00
)
2024-06-12 01:38:05 -05:00
.to.equal(
4
+ 4 + (new TextEncoder().encode('hallo')).length
+ 4 + (new TextEncoder().encode('hαllo')).length
);
2024-06-10 22:42:30 -05:00
});
2024-06-11 21:00:03 -05:00
2024-06-12 01:38:05 -05:00
test('encodes and decodes', () => {
2024-06-11 21:00:03 -05:00
const entries = [
2024-06-12 01:38:05 -05:00
[{type: 'uint8'}, 255],
[{type: 'int8'}, -128],
[{type: 'int8'}, 127],
[{type: 'uint16'}, 65535],
[{type: 'int16'}, -32768],
[{type: 'int16'}, 32767],
[{type: 'uint32'}, 4294967295],
[{type: 'int32'}, -2147483648],
[{type: 'int32'}, 2147483647],
[{type: 'uint64'}, 18446744073709551615n],
[{type: 'int64'}, -9223372036854775808n],
[{type: 'int64'}, 9223372036854775807n],
[{type: 'float32'}, 0.5],
[{type: 'float64'}, 1.234],
[{type: 'string'}, 'hello world'],
[{type: 'string'}, 'α'],
[{type: 'array', subtype: {type: 'uint8'}}, [1, 2, 3, 4]],
[{type: 'array', subtype: {type: 'string'}}, ['one', 'two', 'three', 'four']],
[{type: 'object', properties: {foo: {type: 'uint8'}, bar: {type: 'string'}}}, {foo: 64, bar: 'baz'}],
2024-06-11 21:00:03 -05:00
];
2024-06-12 01:38:05 -05:00
entries.forEach(([specification, concrete]) => {
const schema = new Schema(specification);
const view = new DataView(new ArrayBuffer(schema.sizeOf(concrete)));
schema.serialize(concrete, view);
expect(concrete)
.to.deep.equal(schema.deserialize(view));
});
2024-06-11 21:00:03 -05:00
});