@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.
Files changed (197) hide show
  1. package/README.md +3 -0
  2. package/build/abstract/Array.d.ts +89 -0
  3. package/build/abstract/Base.d.ts +75 -0
  4. package/build/abstract/Int.d.ts +80 -0
  5. package/build/abstract/Object.d.ts +67 -0
  6. package/build/abstract/index.d.ts +3 -0
  7. package/build/base/Compact.d.ts +88 -0
  8. package/build/base/DoNotConstruct.d.ts +63 -0
  9. package/build/base/Enum.d.ts +118 -0
  10. package/build/base/Int.d.ts +16 -0
  11. package/build/base/Null.d.ts +56 -0
  12. package/build/base/Option.d.ts +94 -0
  13. package/build/base/Result.d.ts +38 -0
  14. package/build/base/Tuple.d.ts +42 -0
  15. package/build/base/UInt.d.ts +15 -0
  16. package/build/base/Vec.d.ts +28 -0
  17. package/build/base/VecAny.d.ts +15 -0
  18. package/build/base/VecFixed.d.ts +30 -0
  19. package/build/base/index.d.ts +12 -0
  20. package/build/bundle.d.ts +7 -0
  21. package/build/extended/BTreeMap.d.ts +5 -0
  22. package/build/extended/BTreeSet.d.ts +64 -0
  23. package/build/extended/BitVec.d.ts +37 -0
  24. package/build/extended/Bytes.d.ts +29 -0
  25. package/build/extended/HashMap.d.ts +5 -0
  26. package/build/extended/Linkage.d.ts +37 -0
  27. package/build/extended/Map.d.ts +59 -0
  28. package/build/extended/OptionBool.d.ts +36 -0
  29. package/build/extended/Range.d.ts +29 -0
  30. package/build/extended/RangeInclusive.d.ts +6 -0
  31. package/build/extended/Type.d.ts +16 -0
  32. package/build/extended/U8aFixed.d.ts +16 -0
  33. package/build/extended/WrapperKeepOpaque.d.ts +40 -0
  34. package/build/extended/WrapperOpaque.d.ts +10 -0
  35. package/build/extended/index.d.ts +14 -0
  36. package/build/index.d.ts +2 -0
  37. package/build/native/Bool.d.ts +71 -0
  38. package/build/native/Date.d.ts +84 -0
  39. package/build/native/Float.d.ts +68 -0
  40. package/build/native/Json.d.ts +69 -0
  41. package/build/native/Raw.d.ts +87 -0
  42. package/build/native/Set.d.ts +84 -0
  43. package/build/native/Struct.d.ts +106 -0
  44. package/build/native/Text.d.ts +77 -0
  45. package/build/native/index.d.ts +8 -0
  46. package/build/packageDetect.d.ts +1 -0
  47. package/build/packageInfo.d.ts +6 -0
  48. package/build/primitive/F32.d.ts +11 -0
  49. package/build/primitive/F64.d.ts +11 -0
  50. package/build/primitive/I128.d.ts +11 -0
  51. package/build/primitive/I16.d.ts +11 -0
  52. package/build/primitive/I256.d.ts +11 -0
  53. package/build/primitive/I32.d.ts +11 -0
  54. package/build/primitive/I64.d.ts +11 -0
  55. package/build/primitive/I8.d.ts +11 -0
  56. package/build/primitive/ISize.d.ts +12 -0
  57. package/build/primitive/U128.d.ts +11 -0
  58. package/build/primitive/U16.d.ts +11 -0
  59. package/build/primitive/U256.d.ts +11 -0
  60. package/build/primitive/U32.d.ts +11 -0
  61. package/build/primitive/U64.d.ts +11 -0
  62. package/build/primitive/U8.d.ts +11 -0
  63. package/build/primitive/USize.d.ts +12 -0
  64. package/build/primitive/index.d.ts +16 -0
  65. package/build/types/codec.d.ts +113 -0
  66. package/build/types/helpers.d.ts +27 -0
  67. package/build/types/index.d.ts +4 -0
  68. package/build/types/interfaces.d.ts +74 -0
  69. package/build/types/registry.d.ts +67 -0
  70. package/build/utils/compareArray.d.ts +1 -0
  71. package/build/utils/compareMap.d.ts +1 -0
  72. package/build/utils/compareSet.d.ts +1 -0
  73. package/build/utils/decodeU8a.d.ts +26 -0
  74. package/build/utils/index.d.ts +8 -0
  75. package/build/utils/sanitize.d.ts +15 -0
  76. package/build/utils/sortValues.d.ts +12 -0
  77. package/build/utils/toConstructors.d.ts +16 -0
  78. package/build/utils/typesToMap.d.ts +2 -0
  79. package/build/utils/util.d.ts +3 -0
  80. package/package.json +34 -0
  81. package/src/abstract/Array.ts +213 -0
  82. package/src/abstract/Base.ts +129 -0
  83. package/src/abstract/Int.ts +271 -0
  84. package/src/abstract/Object.ts +99 -0
  85. package/src/abstract/index.ts +6 -0
  86. package/src/base/Compact.spec.ts +99 -0
  87. package/src/base/Compact.ts +198 -0
  88. package/src/base/DoNotConstruct.spec.ts +23 -0
  89. package/src/base/DoNotConstruct.ts +118 -0
  90. package/src/base/Enum.spec.ts +487 -0
  91. package/src/base/Enum.ts +460 -0
  92. package/src/base/Int.spec.ts +225 -0
  93. package/src/base/Int.ts +34 -0
  94. package/src/base/Null.spec.ts +41 -0
  95. package/src/base/Null.ts +96 -0
  96. package/src/base/Option.spec.ts +216 -0
  97. package/src/base/Option.ts +275 -0
  98. package/src/base/Result.spec.ts +64 -0
  99. package/src/base/Result.ts +79 -0
  100. package/src/base/Tuple.spec.ts +161 -0
  101. package/src/base/Tuple.ts +149 -0
  102. package/src/base/UInt.spec.ts +192 -0
  103. package/src/base/UInt.ts +30 -0
  104. package/src/base/Vec.spec.ts +224 -0
  105. package/src/base/Vec.ts +133 -0
  106. package/src/base/VecAny.ts +23 -0
  107. package/src/base/VecFixed.spec.ts +78 -0
  108. package/src/base/VecFixed.ts +92 -0
  109. package/src/base/index.ts +15 -0
  110. package/src/bundle.ts +13 -0
  111. package/src/checkTypes.manual.ts +12 -0
  112. package/src/extended/BTreeMap.spec.ts +245 -0
  113. package/src/extended/BTreeMap.ts +16 -0
  114. package/src/extended/BTreeSet.spec.ts +260 -0
  115. package/src/extended/BTreeSet.ts +233 -0
  116. package/src/extended/BitVec.spec.ts +97 -0
  117. package/src/extended/BitVec.ts +137 -0
  118. package/src/extended/Bytes.spec.ts +75 -0
  119. package/src/extended/Bytes.ts +88 -0
  120. package/src/extended/HashMap.spec.ts +36 -0
  121. package/src/extended/HashMap.ts +16 -0
  122. package/src/extended/Linkage.spec.ts +43 -0
  123. package/src/extended/Linkage.ts +81 -0
  124. package/src/extended/Map.spec.ts +123 -0
  125. package/src/extended/Map.ts +255 -0
  126. package/src/extended/OptionBool.spec.ts +49 -0
  127. package/src/extended/OptionBool.ts +93 -0
  128. package/src/extended/Range.spec.ts +37 -0
  129. package/src/extended/Range.ts +56 -0
  130. package/src/extended/RangeInclusive.ts +20 -0
  131. package/src/extended/Type.spec.ts +118 -0
  132. package/src/extended/Type.ts +29 -0
  133. package/src/extended/U8aFixed.spec.ts +117 -0
  134. package/src/extended/U8aFixed.ts +57 -0
  135. package/src/extended/WrapperKeepOpaque.spec.ts +101 -0
  136. package/src/extended/WrapperKeepOpaque.ts +128 -0
  137. package/src/extended/WrapperOpaque.spec.ts +58 -0
  138. package/src/extended/WrapperOpaque.ts +27 -0
  139. package/src/extended/index.ts +17 -0
  140. package/src/index.ts +6 -0
  141. package/src/mod.ts +4 -0
  142. package/src/native/Bool.spec.ts +74 -0
  143. package/src/native/Bool.ts +137 -0
  144. package/src/native/Date.spec.ts +85 -0
  145. package/src/native/Date.ts +169 -0
  146. package/src/native/Float.spec.ts +51 -0
  147. package/src/native/Float.ts +136 -0
  148. package/src/native/Json.ts +147 -0
  149. package/src/native/Raw.spec.ts +113 -0
  150. package/src/native/Raw.ts +171 -0
  151. package/src/native/Set.spec.ts +116 -0
  152. package/src/native/Set.ts +269 -0
  153. package/src/native/Struct.data.ts +4 -0
  154. package/src/native/Struct.spec.ts +411 -0
  155. package/src/native/Struct.ts +338 -0
  156. package/src/native/Text.spec.ts +85 -0
  157. package/src/native/Text.ts +184 -0
  158. package/src/native/index.ts +11 -0
  159. package/src/packageDetect.ts +11 -0
  160. package/src/packageInfo.ts +6 -0
  161. package/src/primitive/F32.ts +14 -0
  162. package/src/primitive/F64.ts +14 -0
  163. package/src/primitive/I128.ts +14 -0
  164. package/src/primitive/I16.ts +14 -0
  165. package/src/primitive/I256.ts +14 -0
  166. package/src/primitive/I32.ts +14 -0
  167. package/src/primitive/I64.ts +14 -0
  168. package/src/primitive/I8.ts +14 -0
  169. package/src/primitive/ISize.ts +21 -0
  170. package/src/primitive/U128.ts +14 -0
  171. package/src/primitive/U16.ts +14 -0
  172. package/src/primitive/U256.ts +14 -0
  173. package/src/primitive/U32.ts +14 -0
  174. package/src/primitive/U64.ts +14 -0
  175. package/src/primitive/U8.ts +14 -0
  176. package/src/primitive/USize.ts +21 -0
  177. package/src/primitive/index.ts +19 -0
  178. package/src/test/performance.ts +61 -0
  179. package/src/types/codec.ts +140 -0
  180. package/src/types/helpers.ts +50 -0
  181. package/src/types/index.ts +7 -0
  182. package/src/types/interfaces.ts +98 -0
  183. package/src/types/registry.ts +86 -0
  184. package/src/utils/compareArray.ts +22 -0
  185. package/src/utils/compareMap.ts +40 -0
  186. package/src/utils/compareSet.ts +23 -0
  187. package/src/utils/decodeU8a.ts +123 -0
  188. package/src/utils/index.ts +11 -0
  189. package/src/utils/sanitize.spec.ts +89 -0
  190. package/src/utils/sanitize.ts +290 -0
  191. package/src/utils/sortValues.ts +103 -0
  192. package/src/utils/toConstructors.ts +46 -0
  193. package/src/utils/typesToMap.ts +14 -0
  194. package/src/utils/util.ts +8 -0
  195. package/tsconfig.build.json +16 -0
  196. package/tsconfig.build.tsbuildinfo +1 -0
  197. package/tsconfig.spec.json +21 -0
@@ -0,0 +1,411 @@
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
+ /* eslint-disable sort-keys */
7
+
8
+ import type { Bool, Option } from '@pezkuwi/types-codec';
9
+ import type { CodecTo } from '@pezkuwi/types-codec/types';
10
+
11
+ import { TypeRegistry } from '@pezkuwi/types';
12
+ import { Enum, Struct, Text, U32, Vec } from '@pezkuwi/types-codec';
13
+ import { stringify } from '@pezkuwi/util';
14
+
15
+ import { TEST_A } from './Struct.data.js';
16
+
17
+ describe('Struct', (): void => {
18
+ const registry = new TypeRegistry();
19
+
20
+ describe('decoding', (): void => {
21
+ const testDecode = (type: string, input: any): void =>
22
+ it(`can decode from ${type}`, (): void => {
23
+ const s = new Struct(registry, {
24
+ foo: Text,
25
+ bar: U32
26
+ }, input);
27
+
28
+ expect([...s.keys()]).toEqual(['foo', 'bar']);
29
+ expect(
30
+ [...s.values()].map((v): string =>
31
+ v.toString()
32
+ )
33
+ ).toEqual(['bazzing', '69']);
34
+ });
35
+
36
+ testDecode('array', ['bazzing', 69]);
37
+ testDecode('hex', '0x1c62617a7a696e6745000000');
38
+ testDecode('object', { foo: 'bazzing', bar: 69 });
39
+ testDecode('Uint8Array', Uint8Array.from([28, 98, 97, 122, 122, 105, 110, 103, 69, 0, 0, 0]));
40
+ testDecode('Struct.with', new (Struct.with({ foo: 'Text', bar: 'U32' }))(registry, { foo: 'bazzing', bar: 69 }));
41
+
42
+ it('decodes null/undefined/empty correctly (& equivalent)', (): void => {
43
+ const Clazz = Struct.with({
44
+ txt: Text,
45
+ u32: U32
46
+ });
47
+ const expected = { txt: '', u32: '0' };
48
+
49
+ expect(new Clazz(registry, {}).toHuman()).toEqual(expected);
50
+ expect(new Clazz(registry, null).toHuman()).toEqual(expected);
51
+ expect(new Clazz(registry, undefined).toHuman()).toEqual(expected);
52
+ });
53
+
54
+ it('decodes with Optionals', (): void => {
55
+ const Clazz = Struct.with({
56
+ a: 'Option<Bool>',
57
+ b: 'Option<Bool>'
58
+ });
59
+
60
+ const c = new Clazz(registry, { a: false }) as unknown as { a: Option<Bool>, b: Option<Bool> };
61
+
62
+ expect(c.a.isSome).toEqual(true);
63
+ expect(c.a.unwrap().isTrue).toEqual(false);
64
+ expect(c.b.isSome).toEqual(false);
65
+ });
66
+
67
+ it('decodes reusing instantiated inputs', (): void => {
68
+ const foo = new Text(registry, 'bar');
69
+
70
+ expect(
71
+ (new Struct(
72
+ registry,
73
+ { foo: Text },
74
+ { foo }
75
+ )).get('foo')
76
+ ).toBe(foo);
77
+ });
78
+
79
+ it('decodes a more complicated type', (): void => {
80
+ const s = new Struct(registry, {
81
+ foo: Vec.with(Struct.with({
82
+ bar: Text
83
+ }))
84
+ }, { foo: [{ bar: 1 }, { bar: 2 }] });
85
+
86
+ expect(s.toString()).toBe('{"foo":[{"bar":"1"},{"bar":"2"}]}');
87
+ });
88
+
89
+ it('decodes a previously problematic input', (): void => {
90
+ let data;
91
+
92
+ try {
93
+ data = new Struct(registry, {
94
+ a: 'u32',
95
+ b: 'H256',
96
+ c: 'H256',
97
+ swap: Enum.with({
98
+ A: 'u256',
99
+ B: 'u256'
100
+ }),
101
+ d: Vec.with('u8'),
102
+ e: 'u8'
103
+ }, TEST_A);
104
+ } catch (error) {
105
+ console.error(error);
106
+
107
+ throw error;
108
+ }
109
+
110
+ expect(data.get('d')).toHaveLength(50000);
111
+ });
112
+
113
+ it('decodes from a Map input', (): void => {
114
+ const s = new Struct(registry, {
115
+ txt: Text,
116
+ foo: U32,
117
+ bar: U32
118
+ }, new Map<string, unknown>([['a', 42], ['txt', 'fubar']]));
119
+
120
+ expect(s.toString()).toEqual('{"txt":"fubar","foo":0,"bar":0}');
121
+ });
122
+
123
+ it('decodes from a snake_case input', (): void => {
124
+ const input = new Struct(registry, {
125
+ snakeCaseA: U32,
126
+ snakeCaseB: Text,
127
+ other: U32
128
+ }, { snake_case_a: 42, snake_case_b: 'fubar', other: 69 } as any);
129
+
130
+ expect(input.toString()).toEqual('{"snakeCaseA":42,"snakeCaseB":"fubar","other":69}');
131
+ });
132
+
133
+ it('throws when it cannot decode', (): void => {
134
+ expect(
135
+ (): Struct<any> => new (
136
+ Struct.with({
137
+ txt: Text,
138
+ u32: U32
139
+ })
140
+ )(registry, 'ABC')
141
+ ).toThrow(/Cannot decode value/);
142
+ });
143
+
144
+ it('throws a sensical error on incorrect array values passed to structs', (): void => {
145
+ expect(
146
+ () => new Struct(registry, {
147
+ _: 'Vec<u32>'
148
+ }, [123, 456])
149
+ ).toThrow(/array to object with known keys/);
150
+ });
151
+ });
152
+
153
+ describe('encoding', (): void => {
154
+ const testEncode = (to: CodecTo, expected: any): void =>
155
+ it(`can encode ${to}`, (): void => {
156
+ const s = new Struct(registry, {
157
+ foo: Text,
158
+ bar: U32
159
+ }, { foo: 'bazzing', bar: 69 });
160
+
161
+ expect(s[to]()).toEqual(expected);
162
+ });
163
+
164
+ testEncode('toHex', '0x1c62617a7a696e6745000000');
165
+ testEncode('toJSON', { foo: 'bazzing', bar: 69 });
166
+ testEncode('toU8a', Uint8Array.from([28, 98, 97, 122, 122, 105, 110, 103, 69, 0, 0, 0]));
167
+ testEncode('toString', '{"foo":"bazzing","bar":69}');
168
+ });
169
+
170
+ it('provides a clean toString()', (): void => {
171
+ expect(
172
+ new (
173
+ Struct.with({
174
+ txt: Text,
175
+ u32: U32
176
+ })
177
+ )(registry, { txt: 'foo', u32: 0x123456 }).toString()
178
+ ).toEqual('{"txt":"foo","u32":1193046}');
179
+ });
180
+
181
+ it('provides a clean toString() (string types)', (): void => {
182
+ expect(
183
+ new (
184
+ Struct.with({
185
+ txt: 'Text',
186
+ num: 'u32',
187
+ cls: U32
188
+ })
189
+ )(registry, { txt: 'foo', num: 0x123456, cls: 123 }).toString()
190
+ ).toEqual('{"txt":"foo","num":1193046,"cls":123}');
191
+ });
192
+
193
+ it('exposes the properties on the object', (): void => {
194
+ const struct = new (
195
+ Struct.with({
196
+ txt: Text,
197
+ u32: U32
198
+ })
199
+ )(registry, { txt: 'foo', u32: 0x123456 });
200
+
201
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access,@typescript-eslint/no-unsafe-call
202
+ expect((struct as any).txt.toString()).toEqual('foo');
203
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access,@typescript-eslint/no-unsafe-call
204
+ expect((struct as any).u32.toNumber()).toEqual(0x123456);
205
+ });
206
+
207
+ it('correctly encodes length', (): void => {
208
+ expect(
209
+ new (
210
+ Struct.with({
211
+ txt: Text,
212
+ u32: U32
213
+ })
214
+ )(registry, { foo: 'bazzing', bar: 69 }).encodedLength
215
+ ).toEqual(5);
216
+ });
217
+
218
+ it('exposes the types', (): void => {
219
+ expect(
220
+ new Struct(registry, {
221
+ foo: Text,
222
+ bar: Text,
223
+ baz: U32
224
+ }, {
225
+ foo: 'foo',
226
+ bar: 'bar',
227
+ baz: 3
228
+ }).Type
229
+ ).toEqual({
230
+ foo: 'Text',
231
+ bar: 'Text',
232
+ baz: 'u32'
233
+ });
234
+ });
235
+
236
+ it('gets the value at a particular index', (): void => {
237
+ expect(
238
+ new (
239
+ Struct.with({
240
+ txt: Text,
241
+ u32: U32
242
+ })
243
+ )(registry, { txt: 'foo', u32: 1234 })
244
+ .getAtIndex(1)
245
+ .toString()
246
+ ).toEqual('1234');
247
+ });
248
+
249
+ describe('utils', (): void => {
250
+ it('compares against other objects', (): void => {
251
+ const test = {
252
+ foo: 'foo',
253
+ bar: 'bar',
254
+ baz: 3
255
+ };
256
+
257
+ expect(
258
+ new Struct(registry, {
259
+ foo: Text,
260
+ bar: Text,
261
+ baz: U32
262
+ }, test).eq(test)
263
+ ).toBe(true);
264
+ });
265
+
266
+ it('has a sane toPrimitive', (): void => {
267
+ const S = Struct.with({
268
+ name: 'Text',
269
+ description: 'Vec<Text>',
270
+ fooA: 'Bytes',
271
+ fooB: 'Bytes',
272
+ fooC: Struct.with({
273
+ a: 'u32',
274
+ b: 'u128',
275
+ c: 'Compact<u128>',
276
+ d: 'bool'
277
+ })
278
+ });
279
+
280
+ expect(
281
+ new S(registry, {
282
+ name: 'Something',
283
+ description: ['One line', 'Another line'],
284
+ fooA: 'hello world!',
285
+ fooB: '0x123456',
286
+ fooC: {
287
+ a: 1234,
288
+ b: BigInt('1234567890111213141516'),
289
+ c: 123456,
290
+ d: true
291
+ }
292
+ }).toPrimitive()
293
+ ).toEqual({
294
+ name: 'Something',
295
+ description: ['One line', 'Another line'],
296
+ fooA: 'hello world!',
297
+ fooB: '0x123456',
298
+ fooC: {
299
+ a: 1234,
300
+ b: '1234567890111213141516',
301
+ c: 123456,
302
+ d: true
303
+ }
304
+ });
305
+ });
306
+
307
+ it('generates sane toRawType', (): void => {
308
+ expect(
309
+ new Struct(registry, {
310
+ accountId: 'AccountId',
311
+ balanceCompact: registry.createClass('Compact<Balance>'),
312
+ blockNumber: registry.createClass('BlockNumber'),
313
+ compactNumber: registry.createClass('Compact<BlockNumber>'),
314
+ optionNumber: registry.createClass('Option<BlockNumber>'),
315
+ counter: U32,
316
+ vector: Vec.with('AccountId')
317
+ }).toRawType()
318
+ ).toEqual(stringify({
319
+ accountId: 'AccountId',
320
+ balanceCompact: 'Compact<Balance>', // Override in Uint
321
+ blockNumber: 'BlockNumber',
322
+ compactNumber: 'Compact<BlockNumber>',
323
+ optionNumber: 'Option<BlockNumber>',
324
+ counter: 'u32',
325
+ vector: 'Vec<AccountId>'
326
+ }));
327
+ });
328
+
329
+ it('generates sane toRawType (via with)', (): void => {
330
+ const Type = Struct.with({
331
+ accountId: 'AccountId',
332
+ balance: registry.createClass('Balance')
333
+ });
334
+
335
+ expect(
336
+ new Type(registry).toRawType()
337
+ ).toEqual(stringify({
338
+ accountId: 'AccountId',
339
+ balance: 'Balance' // Override in Uint
340
+ }));
341
+ });
342
+ });
343
+
344
+ it('allows toString with large numbers', (): void => {
345
+ // replicate https://github.com/polkadot-js/api/issues/640
346
+ expect(
347
+ new Struct(registry, {
348
+ blockNumber: registry.createClass('Option<BlockNumber>')
349
+ }, { blockNumber: '0x0000000010abcdef' }).toString()
350
+ ).toEqual('{"blockNumber":279694831}');
351
+ });
352
+
353
+ describe('toU8a', (): void => {
354
+ const def: Record<string, any> = {
355
+ foo: 'Bytes',
356
+ method: 'Bytes',
357
+ bar: 'Option<u32>',
358
+ baz: 'bool'
359
+ };
360
+ const val = {
361
+ foo: '0x4269',
362
+ method: '0x99',
363
+ bar: 1,
364
+ baz: true
365
+ };
366
+
367
+ it('generates toU8a with undefined', (): void => {
368
+ expect(
369
+ new Struct(registry, def, val).toU8a()
370
+ ).toEqual(new Uint8Array([2 << 2, 0x42, 0x69, 1 << 2, 0x99, 1, 1, 0, 0, 0, 1]));
371
+ });
372
+
373
+ it('generates toU8a with true', (): void => {
374
+ expect(
375
+ new Struct(registry, def, val).toU8a(true)
376
+ ).toEqual(new Uint8Array([0x42, 0x69, 0x99, 1, 0, 0, 0, 1]));
377
+ });
378
+
379
+ it('generates toU8a with { method: true }', (): void => {
380
+ expect(
381
+ new Struct(registry, def, val).toU8a({ method: true })
382
+ ).toEqual(new Uint8Array([2 << 2, 0x42, 0x69, 0x99, 1, 1, 0, 0, 0, 1]));
383
+ });
384
+
385
+ it('has a sane inspect', (): void => {
386
+ expect(
387
+ new Struct(registry, def, val).inspect()
388
+ ).toEqual({
389
+ inner: [
390
+ {
391
+ name: 'foo',
392
+ outer: [new Uint8Array([2 << 2]), new Uint8Array([0x42, 0x69])]
393
+ },
394
+ {
395
+ name: 'method',
396
+ outer: [new Uint8Array([1 << 2]), new Uint8Array([0x99])]
397
+ },
398
+ {
399
+ inner: undefined,
400
+ name: 'bar',
401
+ outer: [new Uint8Array([1]), new Uint8Array([1, 0, 0, 0])]
402
+ },
403
+ {
404
+ name: 'baz',
405
+ outer: [new Uint8Array([1])]
406
+ }
407
+ ]
408
+ });
409
+ });
410
+ });
411
+ });