@pezkuwi/types-codec 16.5.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -0
- package/build/abstract/Array.d.ts +89 -0
- package/build/abstract/Base.d.ts +75 -0
- package/build/abstract/Int.d.ts +80 -0
- package/build/abstract/Object.d.ts +67 -0
- package/build/abstract/index.d.ts +3 -0
- package/build/base/Compact.d.ts +88 -0
- package/build/base/DoNotConstruct.d.ts +63 -0
- package/build/base/Enum.d.ts +118 -0
- package/build/base/Int.d.ts +16 -0
- package/build/base/Null.d.ts +56 -0
- package/build/base/Option.d.ts +94 -0
- package/build/base/Result.d.ts +38 -0
- package/build/base/Tuple.d.ts +42 -0
- package/build/base/UInt.d.ts +15 -0
- package/build/base/Vec.d.ts +28 -0
- package/build/base/VecAny.d.ts +15 -0
- package/build/base/VecFixed.d.ts +30 -0
- package/build/base/index.d.ts +12 -0
- package/build/bundle.d.ts +7 -0
- package/build/extended/BTreeMap.d.ts +5 -0
- package/build/extended/BTreeSet.d.ts +64 -0
- package/build/extended/BitVec.d.ts +37 -0
- package/build/extended/Bytes.d.ts +29 -0
- package/build/extended/HashMap.d.ts +5 -0
- package/build/extended/Linkage.d.ts +37 -0
- package/build/extended/Map.d.ts +59 -0
- package/build/extended/OptionBool.d.ts +36 -0
- package/build/extended/Range.d.ts +29 -0
- package/build/extended/RangeInclusive.d.ts +6 -0
- package/build/extended/Type.d.ts +16 -0
- package/build/extended/U8aFixed.d.ts +16 -0
- package/build/extended/WrapperKeepOpaque.d.ts +40 -0
- package/build/extended/WrapperOpaque.d.ts +10 -0
- package/build/extended/index.d.ts +14 -0
- package/build/index.d.ts +2 -0
- package/build/native/Bool.d.ts +71 -0
- package/build/native/Date.d.ts +84 -0
- package/build/native/Float.d.ts +68 -0
- package/build/native/Json.d.ts +69 -0
- package/build/native/Raw.d.ts +87 -0
- package/build/native/Set.d.ts +84 -0
- package/build/native/Struct.d.ts +106 -0
- package/build/native/Text.d.ts +77 -0
- package/build/native/index.d.ts +8 -0
- package/build/packageDetect.d.ts +1 -0
- package/build/packageInfo.d.ts +6 -0
- package/build/primitive/F32.d.ts +11 -0
- package/build/primitive/F64.d.ts +11 -0
- package/build/primitive/I128.d.ts +11 -0
- package/build/primitive/I16.d.ts +11 -0
- package/build/primitive/I256.d.ts +11 -0
- package/build/primitive/I32.d.ts +11 -0
- package/build/primitive/I64.d.ts +11 -0
- package/build/primitive/I8.d.ts +11 -0
- package/build/primitive/ISize.d.ts +12 -0
- package/build/primitive/U128.d.ts +11 -0
- package/build/primitive/U16.d.ts +11 -0
- package/build/primitive/U256.d.ts +11 -0
- package/build/primitive/U32.d.ts +11 -0
- package/build/primitive/U64.d.ts +11 -0
- package/build/primitive/U8.d.ts +11 -0
- package/build/primitive/USize.d.ts +12 -0
- package/build/primitive/index.d.ts +16 -0
- package/build/types/codec.d.ts +113 -0
- package/build/types/helpers.d.ts +27 -0
- package/build/types/index.d.ts +4 -0
- package/build/types/interfaces.d.ts +74 -0
- package/build/types/registry.d.ts +67 -0
- package/build/utils/compareArray.d.ts +1 -0
- package/build/utils/compareMap.d.ts +1 -0
- package/build/utils/compareSet.d.ts +1 -0
- package/build/utils/decodeU8a.d.ts +26 -0
- package/build/utils/index.d.ts +8 -0
- package/build/utils/sanitize.d.ts +15 -0
- package/build/utils/sortValues.d.ts +12 -0
- package/build/utils/toConstructors.d.ts +16 -0
- package/build/utils/typesToMap.d.ts +2 -0
- package/build/utils/util.d.ts +3 -0
- package/package.json +34 -0
- package/src/abstract/Array.ts +213 -0
- package/src/abstract/Base.ts +129 -0
- package/src/abstract/Int.ts +271 -0
- package/src/abstract/Object.ts +99 -0
- package/src/abstract/index.ts +6 -0
- package/src/base/Compact.spec.ts +99 -0
- package/src/base/Compact.ts +198 -0
- package/src/base/DoNotConstruct.spec.ts +23 -0
- package/src/base/DoNotConstruct.ts +118 -0
- package/src/base/Enum.spec.ts +487 -0
- package/src/base/Enum.ts +460 -0
- package/src/base/Int.spec.ts +225 -0
- package/src/base/Int.ts +34 -0
- package/src/base/Null.spec.ts +41 -0
- package/src/base/Null.ts +96 -0
- package/src/base/Option.spec.ts +216 -0
- package/src/base/Option.ts +275 -0
- package/src/base/Result.spec.ts +64 -0
- package/src/base/Result.ts +79 -0
- package/src/base/Tuple.spec.ts +161 -0
- package/src/base/Tuple.ts +149 -0
- package/src/base/UInt.spec.ts +192 -0
- package/src/base/UInt.ts +30 -0
- package/src/base/Vec.spec.ts +224 -0
- package/src/base/Vec.ts +133 -0
- package/src/base/VecAny.ts +23 -0
- package/src/base/VecFixed.spec.ts +78 -0
- package/src/base/VecFixed.ts +92 -0
- package/src/base/index.ts +15 -0
- package/src/bundle.ts +13 -0
- package/src/checkTypes.manual.ts +12 -0
- package/src/extended/BTreeMap.spec.ts +245 -0
- package/src/extended/BTreeMap.ts +16 -0
- package/src/extended/BTreeSet.spec.ts +260 -0
- package/src/extended/BTreeSet.ts +233 -0
- package/src/extended/BitVec.spec.ts +97 -0
- package/src/extended/BitVec.ts +137 -0
- package/src/extended/Bytes.spec.ts +75 -0
- package/src/extended/Bytes.ts +88 -0
- package/src/extended/HashMap.spec.ts +36 -0
- package/src/extended/HashMap.ts +16 -0
- package/src/extended/Linkage.spec.ts +43 -0
- package/src/extended/Linkage.ts +81 -0
- package/src/extended/Map.spec.ts +123 -0
- package/src/extended/Map.ts +255 -0
- package/src/extended/OptionBool.spec.ts +49 -0
- package/src/extended/OptionBool.ts +93 -0
- package/src/extended/Range.spec.ts +37 -0
- package/src/extended/Range.ts +56 -0
- package/src/extended/RangeInclusive.ts +20 -0
- package/src/extended/Type.spec.ts +118 -0
- package/src/extended/Type.ts +29 -0
- package/src/extended/U8aFixed.spec.ts +117 -0
- package/src/extended/U8aFixed.ts +57 -0
- package/src/extended/WrapperKeepOpaque.spec.ts +101 -0
- package/src/extended/WrapperKeepOpaque.ts +128 -0
- package/src/extended/WrapperOpaque.spec.ts +58 -0
- package/src/extended/WrapperOpaque.ts +27 -0
- package/src/extended/index.ts +17 -0
- package/src/index.ts +6 -0
- package/src/mod.ts +4 -0
- package/src/native/Bool.spec.ts +74 -0
- package/src/native/Bool.ts +137 -0
- package/src/native/Date.spec.ts +85 -0
- package/src/native/Date.ts +169 -0
- package/src/native/Float.spec.ts +51 -0
- package/src/native/Float.ts +136 -0
- package/src/native/Json.ts +147 -0
- package/src/native/Raw.spec.ts +113 -0
- package/src/native/Raw.ts +171 -0
- package/src/native/Set.spec.ts +116 -0
- package/src/native/Set.ts +269 -0
- package/src/native/Struct.data.ts +4 -0
- package/src/native/Struct.spec.ts +411 -0
- package/src/native/Struct.ts +338 -0
- package/src/native/Text.spec.ts +85 -0
- package/src/native/Text.ts +184 -0
- package/src/native/index.ts +11 -0
- package/src/packageDetect.ts +11 -0
- package/src/packageInfo.ts +6 -0
- package/src/primitive/F32.ts +14 -0
- package/src/primitive/F64.ts +14 -0
- package/src/primitive/I128.ts +14 -0
- package/src/primitive/I16.ts +14 -0
- package/src/primitive/I256.ts +14 -0
- package/src/primitive/I32.ts +14 -0
- package/src/primitive/I64.ts +14 -0
- package/src/primitive/I8.ts +14 -0
- package/src/primitive/ISize.ts +21 -0
- package/src/primitive/U128.ts +14 -0
- package/src/primitive/U16.ts +14 -0
- package/src/primitive/U256.ts +14 -0
- package/src/primitive/U32.ts +14 -0
- package/src/primitive/U64.ts +14 -0
- package/src/primitive/U8.ts +14 -0
- package/src/primitive/USize.ts +21 -0
- package/src/primitive/index.ts +19 -0
- package/src/test/performance.ts +61 -0
- package/src/types/codec.ts +140 -0
- package/src/types/helpers.ts +50 -0
- package/src/types/index.ts +7 -0
- package/src/types/interfaces.ts +98 -0
- package/src/types/registry.ts +86 -0
- package/src/utils/compareArray.ts +22 -0
- package/src/utils/compareMap.ts +40 -0
- package/src/utils/compareSet.ts +23 -0
- package/src/utils/decodeU8a.ts +123 -0
- package/src/utils/index.ts +11 -0
- package/src/utils/sanitize.spec.ts +89 -0
- package/src/utils/sanitize.ts +290 -0
- package/src/utils/sortValues.ts +103 -0
- package/src/utils/toConstructors.ts +46 -0
- package/src/utils/typesToMap.ts +14 -0
- package/src/utils/util.ts +8 -0
- package/tsconfig.build.json +16 -0
- package/tsconfig.build.tsbuildinfo +1 -0
- package/tsconfig.spec.json +21 -0
|
@@ -0,0 +1,487 @@
|
|
|
1
|
+
// Copyright 2017-2025 @polkadot/types-codec authors & contributors
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
/// <reference types="@pezkuwi/dev-test/globals.d.ts" />
|
|
5
|
+
|
|
6
|
+
import type { Registry } from '@pezkuwi/types-codec/types';
|
|
7
|
+
|
|
8
|
+
import { TypeRegistry } from '@pezkuwi/types';
|
|
9
|
+
import { Enum, Null, Text, U32 } from '@pezkuwi/types-codec';
|
|
10
|
+
import { stringify, u8aToHex } from '@pezkuwi/util';
|
|
11
|
+
|
|
12
|
+
import { perf } from '../test/performance.js';
|
|
13
|
+
|
|
14
|
+
const PEnum = Enum.with({ a: U32, b: U32 });
|
|
15
|
+
|
|
16
|
+
describe('Enum', (): void => {
|
|
17
|
+
const registry = new TypeRegistry();
|
|
18
|
+
|
|
19
|
+
describe('typed enum (previously EnumType)', (): void => {
|
|
20
|
+
it('provides a clean toString() (value)', (): void => {
|
|
21
|
+
expect(
|
|
22
|
+
new Enum(
|
|
23
|
+
registry,
|
|
24
|
+
{ Text, U32 },
|
|
25
|
+
new Uint8Array([0, 2 << 2, 49, 50])
|
|
26
|
+
).value.toString()
|
|
27
|
+
).toEqual('12');
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it('provides a clean toString() (enum)', (): void => {
|
|
31
|
+
expect(
|
|
32
|
+
new Enum(
|
|
33
|
+
registry,
|
|
34
|
+
{ Text, U32 },
|
|
35
|
+
new Uint8Array([1, 2 << 2, 49, 50])
|
|
36
|
+
).toString()
|
|
37
|
+
).toEqual('{"u32":3289352}');
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it('decodes from a JSON input (lowercase)', (): void => {
|
|
41
|
+
expect(
|
|
42
|
+
new Enum(
|
|
43
|
+
registry,
|
|
44
|
+
{ Text, U32 },
|
|
45
|
+
{ text: 'some text value' }
|
|
46
|
+
).value.toString()
|
|
47
|
+
).toEqual('some text value');
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it('decodes reusing instantiated inputs', (): void => {
|
|
51
|
+
const foo = new Text(registry, 'bar');
|
|
52
|
+
|
|
53
|
+
expect(
|
|
54
|
+
new Enum(
|
|
55
|
+
registry,
|
|
56
|
+
{ foo: Text },
|
|
57
|
+
{ foo }
|
|
58
|
+
).value
|
|
59
|
+
).toBe(foo);
|
|
60
|
+
|
|
61
|
+
expect(
|
|
62
|
+
new Enum(
|
|
63
|
+
registry,
|
|
64
|
+
{ foo: Text },
|
|
65
|
+
foo,
|
|
66
|
+
0
|
|
67
|
+
).value
|
|
68
|
+
).toBe(foo);
|
|
69
|
+
|
|
70
|
+
expect(
|
|
71
|
+
new Enum(
|
|
72
|
+
registry,
|
|
73
|
+
{ foo: Text },
|
|
74
|
+
new Enum(registry, { foo: Text }, { foo })
|
|
75
|
+
).value
|
|
76
|
+
).toBe(foo);
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it('decodes from hex', (): void => {
|
|
80
|
+
expect(
|
|
81
|
+
new Enum(
|
|
82
|
+
registry,
|
|
83
|
+
{ Text, U32 },
|
|
84
|
+
'0x0134120000'
|
|
85
|
+
).value.toString()
|
|
86
|
+
).toEqual('4660'); // 0x1234 in decimal
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it('decodes from hex (string types)', (): void => {
|
|
90
|
+
expect(
|
|
91
|
+
new Enum(
|
|
92
|
+
registry,
|
|
93
|
+
// eslint-disable-next-line sort-keys
|
|
94
|
+
{ foo: 'Text', bar: 'u32' },
|
|
95
|
+
'0x0134120000'
|
|
96
|
+
).value.toString()
|
|
97
|
+
).toEqual('4660'); // 0x1234 in decimal
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it('decodes from a JSON input (mixed case)', (): void => {
|
|
101
|
+
expect(
|
|
102
|
+
new Enum(
|
|
103
|
+
registry,
|
|
104
|
+
{ Text, U32 },
|
|
105
|
+
{ U32: 42 }
|
|
106
|
+
).value.toString()
|
|
107
|
+
).toEqual('42');
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
it('decodes from JSON string', (): void => {
|
|
111
|
+
expect(
|
|
112
|
+
new Enum(
|
|
113
|
+
registry,
|
|
114
|
+
{ Null, U32 },
|
|
115
|
+
'null'
|
|
116
|
+
).type
|
|
117
|
+
).toEqual('Null');
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
it('has correct isXyz/asXyz (Enum.with)', (): void => {
|
|
121
|
+
const test = new (Enum.with({ First: Text, Second: U32, Third: U32 }))(registry, { Second: 42 }) as any as { isFirst: boolean; isSecond: boolean; asSecond: U32; isThird: boolean; asThird: never };
|
|
122
|
+
|
|
123
|
+
// const asKeys = Object.keys(test).filter((k) => k.startsWith('as'));
|
|
124
|
+
// const isKeys = Object.keys(test).filter((k) => k.startsWith('is'));
|
|
125
|
+
|
|
126
|
+
// expect([isKeys, asKeys]).toEqual([
|
|
127
|
+
// ['isFirst', 'isSecond', 'isThird'],
|
|
128
|
+
// ['asFirst', 'asSecond', 'asThird']
|
|
129
|
+
// ]);
|
|
130
|
+
|
|
131
|
+
expect([test.isFirst, test.isSecond, test.isThird]).toEqual([false, true, false]);
|
|
132
|
+
expect(test.asSecond.toNumber()).toEqual(42);
|
|
133
|
+
expect((): never => test.asThird).toThrow(/Cannot convert 'Second' via asThird/);
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
it('stringifies with custom types', (): void => {
|
|
137
|
+
class A extends Null { }
|
|
138
|
+
class B extends Null { }
|
|
139
|
+
class C extends Null { }
|
|
140
|
+
class Test extends Enum {
|
|
141
|
+
constructor (registry: Registry, value?: string, index?: number) {
|
|
142
|
+
super(registry, {
|
|
143
|
+
a: A,
|
|
144
|
+
b: B,
|
|
145
|
+
c: C
|
|
146
|
+
}, value, index);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
expect(new Test(registry).toJSON()).toEqual({ a: null });
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
it('creates via with', (): void => {
|
|
154
|
+
class A extends Null { }
|
|
155
|
+
class B extends U32 { }
|
|
156
|
+
class C extends Null { }
|
|
157
|
+
const Test = Enum.with({ A, B, C });
|
|
158
|
+
|
|
159
|
+
expect(new Test(registry).toJSON()).toEqual({ a: null });
|
|
160
|
+
expect(new Test(registry, 1234, 1).toJSON()).toEqual({ b: 1234 });
|
|
161
|
+
expect(new Test(registry, 0x1234, 1).toU8a()).toEqual(new Uint8Array([1, 0x34, 0x12, 0x00, 0x00]));
|
|
162
|
+
expect(new Test(registry, 0x1234, 1).toU8a(true)).toEqual(new Uint8Array([0x34, 0x12, 0x00, 0x00]));
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
it('allows accessing the type and value', (): void => {
|
|
166
|
+
const text = new Text(registry, 'foo');
|
|
167
|
+
const enumType = new Enum(
|
|
168
|
+
registry,
|
|
169
|
+
{ Text, U32 },
|
|
170
|
+
{ Text: text }
|
|
171
|
+
);
|
|
172
|
+
|
|
173
|
+
expect(enumType.type).toBe('Text');
|
|
174
|
+
expect(enumType.value).toEqual(text);
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
describe('utils', (): void => {
|
|
178
|
+
const DEF = { num: U32, str: Text };
|
|
179
|
+
const u8a = new Uint8Array([1, 3 << 2, 88, 89, 90]);
|
|
180
|
+
const test = new Enum(registry, DEF, u8a);
|
|
181
|
+
|
|
182
|
+
it('compares against index', (): void => {
|
|
183
|
+
expect(test.eq(1)).toBe(true);
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
it('compares against u8a', (): void => {
|
|
187
|
+
expect(test.eq(u8a)).toBe(true);
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
it('compares against hex', (): void => {
|
|
191
|
+
expect(test.eq(u8aToHex(u8a))).toBe(true);
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
it('compares against another enum', (): void => {
|
|
195
|
+
expect(test.eq(new Enum(registry, DEF, u8a))).toBe(true);
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
it('compares against another object', (): void => {
|
|
199
|
+
expect(test.eq({ str: 'XYZ' })).toBe(true);
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
it('compares against values', (): void => {
|
|
203
|
+
expect(test.eq('XYZ')).toBe(true);
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
it('compares basic enum on string', (): void => {
|
|
207
|
+
expect(new Enum(registry, ['A', 'B', 'C'], 1).eq('B')).toBe(true);
|
|
208
|
+
});
|
|
209
|
+
});
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
describe('string-only construction (old Enum)', (): void => {
|
|
213
|
+
const testDecode = (type: string, input: any, expected: any): void =>
|
|
214
|
+
it(`can decode from ${type}`, (): void => {
|
|
215
|
+
const e = new Enum(registry, ['foo', 'bar'], input);
|
|
216
|
+
|
|
217
|
+
expect(e.toString()).toBe(expected);
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
const testEncode = (to: 'toJSON' | 'toNumber' | 'toString' | 'toU8a', expected: any): void =>
|
|
221
|
+
it(`can encode ${to}`, (): void => {
|
|
222
|
+
const e = new Enum(registry, ['Foo', 'Bar'], 1);
|
|
223
|
+
|
|
224
|
+
expect(e[to]()).toEqual(expected);
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
testDecode('Enum', undefined, 'foo');
|
|
228
|
+
testDecode('Enum', new Enum(registry, ['foo', 'bar'], 1), 'bar');
|
|
229
|
+
testDecode('number', 0, 'foo');
|
|
230
|
+
testDecode('number', 1, 'bar');
|
|
231
|
+
testDecode('string', 'bar', 'bar');
|
|
232
|
+
testDecode('Uint8Array', Uint8Array.from([0]), 'foo');
|
|
233
|
+
testDecode('Uint8Array', Uint8Array.from([1]), 'bar');
|
|
234
|
+
|
|
235
|
+
testEncode('toJSON', 'Bar');
|
|
236
|
+
testEncode('toNumber', 1);
|
|
237
|
+
testEncode('toString', 'Bar');
|
|
238
|
+
testEncode('toU8a', Uint8Array.from([1]));
|
|
239
|
+
|
|
240
|
+
it('provides a clean toString()', (): void => {
|
|
241
|
+
expect(
|
|
242
|
+
new Enum(registry, ['foo', 'bar']).toString()
|
|
243
|
+
).toEqual('foo');
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
it('provides a clean toString() (enum)', (): void => {
|
|
247
|
+
expect(
|
|
248
|
+
new Enum(registry, ['foo', 'bar'], new Enum(registry, ['foo', 'bar'], 1)).toNumber()
|
|
249
|
+
).toEqual(1);
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
it('converts to and from Uint8Array', (): void => {
|
|
253
|
+
expect(
|
|
254
|
+
new Enum(registry, ['foo', 'bar'], new Uint8Array([1])).toU8a()
|
|
255
|
+
).toEqual(new Uint8Array([1]));
|
|
256
|
+
});
|
|
257
|
+
|
|
258
|
+
it('converts from JSON', (): void => {
|
|
259
|
+
expect(
|
|
260
|
+
new Enum(registry, ['foo', 'bar', 'baz', 'gaz', 'jaz'], 4).toNumber()
|
|
261
|
+
).toEqual(4);
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
it('has correct isXyz getters (Enum.with)', (): void => {
|
|
265
|
+
const test = new (Enum.with(['First', 'Second', 'Third']))(registry, 'Second') as any as { isFirst: boolean; isSecond: boolean; isThird: boolean };
|
|
266
|
+
|
|
267
|
+
expect([test.isFirst, test.isSecond, test.isThird]).toEqual([false, true, false]);
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
describe('utils', (): void => {
|
|
271
|
+
it('compares against the index value', (): void => {
|
|
272
|
+
expect(
|
|
273
|
+
new Enum(registry, ['foo', 'bar'], 1).eq(1)
|
|
274
|
+
).toBe(true);
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
it('compares against the index value (false)', (): void => {
|
|
278
|
+
expect(
|
|
279
|
+
new Enum(registry, ['foo', 'bar'], 1).eq(0)
|
|
280
|
+
).toBe(false);
|
|
281
|
+
});
|
|
282
|
+
|
|
283
|
+
it('compares against the string value', (): void => {
|
|
284
|
+
expect(
|
|
285
|
+
new Enum(registry, ['foo', 'bar'], 1).eq('bar')
|
|
286
|
+
).toBe(true);
|
|
287
|
+
});
|
|
288
|
+
|
|
289
|
+
it('compares against the string value (false)', (): void => {
|
|
290
|
+
expect(
|
|
291
|
+
new Enum(registry, ['foo', 'bar'], 1).eq('foo')
|
|
292
|
+
).toBe(false);
|
|
293
|
+
});
|
|
294
|
+
|
|
295
|
+
it('has isNone set, with correct index (i.e. no values are used)', (): void => {
|
|
296
|
+
const test = new Enum(registry, ['foo', 'bar'], 1);
|
|
297
|
+
|
|
298
|
+
expect(test.isNone).toBe(true);
|
|
299
|
+
expect(test.index).toEqual(1);
|
|
300
|
+
});
|
|
301
|
+
|
|
302
|
+
it('has a sane inspect', (): void => {
|
|
303
|
+
expect(
|
|
304
|
+
new Enum(
|
|
305
|
+
registry,
|
|
306
|
+
{ Text, U32 },
|
|
307
|
+
'0x0134120000'
|
|
308
|
+
).inspect()
|
|
309
|
+
).toEqual({
|
|
310
|
+
inner: undefined,
|
|
311
|
+
outer: [new Uint8Array([0x01]), new Uint8Array([0x34, 0x12, 0x00, 0x00])]
|
|
312
|
+
});
|
|
313
|
+
});
|
|
314
|
+
});
|
|
315
|
+
});
|
|
316
|
+
|
|
317
|
+
describe('index construction', (): void => {
|
|
318
|
+
it('creates enum where index is specified', (): void => {
|
|
319
|
+
const Test = Enum.with({
|
|
320
|
+
A: U32,
|
|
321
|
+
B: U32
|
|
322
|
+
});
|
|
323
|
+
const test = new Test(registry, new U32(registry, 123), 1);
|
|
324
|
+
|
|
325
|
+
expect(test.type).toEqual('B');
|
|
326
|
+
expect((test.value as U32).toNumber()).toEqual(123);
|
|
327
|
+
});
|
|
328
|
+
|
|
329
|
+
it('creates enum when value is an enum', (): void => {
|
|
330
|
+
const Test = Enum.with({
|
|
331
|
+
A: U32,
|
|
332
|
+
B: U32
|
|
333
|
+
});
|
|
334
|
+
const test = new Test(registry, new Test(registry, 123, 1));
|
|
335
|
+
|
|
336
|
+
expect(test.type).toEqual('B');
|
|
337
|
+
expect((test.value as U32).toNumber()).toEqual(123);
|
|
338
|
+
});
|
|
339
|
+
|
|
340
|
+
it('creates via enum with nested enums as the value', (): void => {
|
|
341
|
+
const Nest = Enum.with({
|
|
342
|
+
C: U32,
|
|
343
|
+
D: U32
|
|
344
|
+
});
|
|
345
|
+
const Test = Enum.with({
|
|
346
|
+
A: U32,
|
|
347
|
+
B: Nest
|
|
348
|
+
});
|
|
349
|
+
const test = new Test(registry, new Nest(registry, 123, 1), 1);
|
|
350
|
+
|
|
351
|
+
expect(test.type).toEqual('B');
|
|
352
|
+
expect((test.value as Enum).type).toEqual('D');
|
|
353
|
+
expect(((test.value as Enum).value as U32).toNumber()).toEqual(123);
|
|
354
|
+
});
|
|
355
|
+
});
|
|
356
|
+
|
|
357
|
+
describe('toRawType', (): void => {
|
|
358
|
+
it('has a sane output for basic enums', (): void => {
|
|
359
|
+
expect(
|
|
360
|
+
new Enum(registry, ['foo', 'bar']).toRawType()
|
|
361
|
+
).toEqual(stringify({ _enum: ['foo', 'bar'] }));
|
|
362
|
+
});
|
|
363
|
+
|
|
364
|
+
it('has a sane output for typed enums', (): void => {
|
|
365
|
+
expect(
|
|
366
|
+
// eslint-disable-next-line sort-keys
|
|
367
|
+
new Enum(registry, { foo: Text, bar: U32 }).toRawType()
|
|
368
|
+
// eslint-disable-next-line sort-keys
|
|
369
|
+
).toEqual(stringify({ _enum: { foo: 'Text', bar: 'u32' } }));
|
|
370
|
+
});
|
|
371
|
+
|
|
372
|
+
it('re-creates via rawType (c-like)', (): void => {
|
|
373
|
+
const type = new Enum(registry, ['foo', 'bar']).toRawType() as 'Raw';
|
|
374
|
+
|
|
375
|
+
expect(registry.createType(type, 1).toString()).toEqual('bar');
|
|
376
|
+
});
|
|
377
|
+
|
|
378
|
+
it('re-creates via rawType (types)', (): void => {
|
|
379
|
+
const type = new Enum(registry, { A: Text, B: U32, C: U32 }).toRawType();
|
|
380
|
+
const value = registry.createType(type, { B: 123 });
|
|
381
|
+
|
|
382
|
+
expect((value as unknown as { isB: boolean }).isB).toEqual(true);
|
|
383
|
+
expect((value as unknown as { asB: U32 }).asB.toNumber()).toEqual(123);
|
|
384
|
+
});
|
|
385
|
+
});
|
|
386
|
+
|
|
387
|
+
describe('indexed enum', (): void => {
|
|
388
|
+
const Test = Enum.with({
|
|
389
|
+
A: 5,
|
|
390
|
+
B: 42,
|
|
391
|
+
C: 69,
|
|
392
|
+
D: 255
|
|
393
|
+
});
|
|
394
|
+
|
|
395
|
+
it('handles an indexed C-like enum', (): void => {
|
|
396
|
+
expect(new Test(registry, 'A').toNumber()).toEqual(5);
|
|
397
|
+
expect(new Test(registry, 'B').toNumber()).toEqual(42);
|
|
398
|
+
expect(new Test(registry, 'C').toNumber()).toEqual(69);
|
|
399
|
+
expect(new Test(registry, 69).toNumber()).toEqual(69);
|
|
400
|
+
expect(new Test(registry, 'D').toNumber()).toEqual(255);
|
|
401
|
+
});
|
|
402
|
+
|
|
403
|
+
it('creates proper raw structure', (): void => {
|
|
404
|
+
expect(new Test(registry).toRawType()).toEqual(stringify({
|
|
405
|
+
_enum: {
|
|
406
|
+
A: 5,
|
|
407
|
+
B: 42,
|
|
408
|
+
C: 69,
|
|
409
|
+
D: 255
|
|
410
|
+
}
|
|
411
|
+
}));
|
|
412
|
+
});
|
|
413
|
+
|
|
414
|
+
it('has the indexes for the enum', (): void => {
|
|
415
|
+
expect(new Test(registry).defIndexes).toEqual([5, 42, 69, 255]);
|
|
416
|
+
});
|
|
417
|
+
|
|
418
|
+
it('has the correct outputs', (): void => {
|
|
419
|
+
const test = new Test(registry, 5);
|
|
420
|
+
|
|
421
|
+
expect(test.toU8a()).toEqual(new Uint8Array([5]));
|
|
422
|
+
expect(test.toHex()).toEqual('0x05');
|
|
423
|
+
expect(test.toJSON()).toEqual('A');
|
|
424
|
+
});
|
|
425
|
+
});
|
|
426
|
+
|
|
427
|
+
describe('toHex', (): void => {
|
|
428
|
+
it('has a proper hex representation & length', (): void => {
|
|
429
|
+
const Test = Enum.with({
|
|
430
|
+
A: Text,
|
|
431
|
+
B: U32
|
|
432
|
+
});
|
|
433
|
+
const test = new Test(registry, 123, 1);
|
|
434
|
+
|
|
435
|
+
expect(test.toHex()).toEqual('0x017b000000');
|
|
436
|
+
expect(test.encodedLength).toEqual(1 + 4);
|
|
437
|
+
});
|
|
438
|
+
|
|
439
|
+
it('encodes a single entry correctly', (): void => {
|
|
440
|
+
const Test = Enum.with({ A: 'u32' });
|
|
441
|
+
const test = new Test(registry, 0x44332211, 0);
|
|
442
|
+
|
|
443
|
+
expect(test.toHex()).toEqual(
|
|
444
|
+
'0x' +
|
|
445
|
+
'00' + // index
|
|
446
|
+
'11223344' // u32 LE encoded
|
|
447
|
+
);
|
|
448
|
+
});
|
|
449
|
+
|
|
450
|
+
it('encodes a single entry correctly (with embedded encoding)', (): void => {
|
|
451
|
+
const Test = Enum.with({ A: 'MultiAddress' });
|
|
452
|
+
const test = new Test(registry, registry.createType('AccountId', '0x0001020304050607080910111213141516171819202122232425262728293031'), 0);
|
|
453
|
+
|
|
454
|
+
expect(test.toHex()).toEqual(
|
|
455
|
+
'0x' +
|
|
456
|
+
'00' + // index
|
|
457
|
+
'00' + // MultiAddress indicating an embedded AccountId
|
|
458
|
+
'0001020304050607080910111213141516171819202122232425262728293031' // AccountId
|
|
459
|
+
);
|
|
460
|
+
});
|
|
461
|
+
});
|
|
462
|
+
|
|
463
|
+
describe('toU8a', (): void => {
|
|
464
|
+
const Test = Enum.with({
|
|
465
|
+
A: Text,
|
|
466
|
+
B: U32
|
|
467
|
+
});
|
|
468
|
+
|
|
469
|
+
it('has a correct toU8a() output', (): void => {
|
|
470
|
+
expect(
|
|
471
|
+
new Test(registry, { B: 69 }).toU8a()
|
|
472
|
+
).toEqual(
|
|
473
|
+
new Uint8Array([1, 69, 0, 0, 0])
|
|
474
|
+
);
|
|
475
|
+
});
|
|
476
|
+
|
|
477
|
+
it('has a correct toU8a(true) output', (): void => {
|
|
478
|
+
expect(
|
|
479
|
+
new Test(registry, { B: 69 }).toU8a(true)
|
|
480
|
+
).toEqual(
|
|
481
|
+
new Uint8Array([69, 0, 0, 0])
|
|
482
|
+
);
|
|
483
|
+
});
|
|
484
|
+
});
|
|
485
|
+
|
|
486
|
+
perf('Enum', 20_000, [[new Uint8Array([0, 31, 32, 33, 34])]], (v: Uint8Array) => new PEnum(registry, v));
|
|
487
|
+
});
|