Define properties on Struct.with automatically

This commit is contained in:
Jaco Greeff
2018-11-29 16:10:50 +01:00
parent 99c7052102
commit d348f50dcf
2 changed files with 53 additions and 31 deletions
+46 -31
View File
@@ -8,40 +8,43 @@ import Text from '../Text';
import U32 from '../U32';
import Vector from './Vector';
const testDecode = (type: string, input: any) =>
it(`can decode from ${type}`, () => {
const s = new Struct({
foo: Text,
bar: U32
}, input);
expect([...s.keys()]).toEqual(['foo', 'bar']);
expect(
[...s.values()].map((v) =>
v.toString()
)
).toEqual(['bazzing', '69']);
});
const testEncode = (to: CodecTo, expected: any) =>
it(`can encode ${to}`, () => {
const s = new Struct({
foo: Text,
bar: U32
}, { foo: 'bazzing', bar: 69 });
expect(s[to]()).toEqual(expected);
});
describe('Struct', () => {
describe('decoding', () => {
const testDecode = (type: string, input: any) =>
it(`can decode from ${type}`, () => {
const s = new Struct({
foo: Text,
bar: U32
}, input);
expect([...s.keys()]).toEqual(['foo', 'bar']);
expect(
[...s.values()].map((v) =>
v.toString()
)
).toEqual(['bazzing', '69']);
});
testDecode('array', ['bazzing', 69]);
testDecode('hex', '0x1c62617a7a696e6745000000');
testDecode('object', { foo: 'bazzing', bar: 69 });
testDecode('Uint8Array', Uint8Array.from([28, 98, 97, 122, 122, 105, 110, 103, 69, 0, 0, 0]));
testDecode('array', ['bazzing', 69]);
testDecode('hex', '0x1c62617a7a696e6745000000');
testDecode('object', { foo: 'bazzing', bar: 69 });
testDecode('Uint8Array', Uint8Array.from([28, 98, 97, 122, 122, 105, 110, 103, 69, 0, 0, 0]));
});
testEncode('toHex', '0x1c62617a7a696e6745000000');
testEncode('toJSON', { foo: 'bazzing', bar: 69 });
testEncode('toU8a', Uint8Array.from([28, 98, 97, 122, 122, 105, 110, 103, 69, 0, 0, 0]));
testEncode('toString', '{"foo":"bazzing","bar":69}');
describe('encoding', () => {
const testEncode = (to: CodecTo, expected: any) =>
it(`can encode ${to}`, () => {
const s = new Struct({
foo: Text,
bar: U32
}, { foo: 'bazzing', bar: 69 });
expect(s[to]()).toEqual(expected);
});
testEncode('toHex', '0x1c62617a7a696e6745000000');
testEncode('toJSON', { foo: 'bazzing', bar: 69 });
testEncode('toU8a', Uint8Array.from([28, 98, 97, 122, 122, 105, 110, 103, 69, 0, 0, 0]));
testEncode('toString', '{"foo":"bazzing","bar":69}');
});
it('decodes null', () => {
expect(
@@ -85,6 +88,18 @@ describe('Struct', () => {
).toEqual('{"txt":"foo","u32":1193046}');
});
it('exposes the properties on the object', () => {
const struct = new (
Struct.with({
txt: Text,
u32: U32
})
)({ txt: 'foo', u32: 0x123456 });
expect((struct as any).txt.toString()).toEqual('foo');
expect((struct as any).u32.toNumber()).toEqual(0x123456);
});
it('correctly encodes length', () => {
expect(
new (
+7
View File
@@ -111,6 +111,13 @@ export default class Struct<
return class extends Struct<S> {
constructor (value?: any, jsonMap?: Map<keyof S, string>) {
super(Types, value, jsonMap);
(Object.keys(Types) as Array<keyof S>).forEach((key, index) => {
Object.defineProperty(this, key, {
enumerable: true,
get: () => this.getAtIndex(index)
});
});
}
};
}