Define properties on Struct.with automatically
This commit is contained in:
@@ -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 (
|
||||
|
||||
@@ -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)
|
||||
});
|
||||
});
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user