@@ -2,13 +2,17 @@ import { describe, expect, it } from "@jest/globals";
2
2
3
3
import {
4
4
Layout ,
5
- serializeLayout ,
6
- deserializeLayout ,
5
+ LayoutToType ,
6
+ RoArray ,
7
7
addFixedValues ,
8
- layoutDiscriminator ,
9
8
bitsetItem ,
10
- } from './../src/index.js' ;
9
+ column ,
10
+ deserializeLayout ,
11
+ layoutDiscriminator ,
12
+ serializeLayout ,
13
+ } from "./../src/index.js" ;
11
14
15
+ // prettier-ignore
12
16
const testLayout = [
13
17
{ name : "uintFixedPrimitive" , binary : "uint" , size : 1 , custom : 3 } ,
14
18
{
@@ -105,6 +109,7 @@ const testLayout = [
105
109
// type DynamicItems = DynamicItemsOfLayout<typeof testLayout>;
106
110
// type DynamicValues = LayoutToType<DynamicItems>;
107
111
112
+ // prettier-ignore
108
113
describe ( "Layout tests" , function ( ) {
109
114
110
115
const completeValues = {
@@ -337,3 +342,61 @@ describe("Layout tests", function () {
337
342
338
343
} ) ;
339
344
} ) ;
345
+
346
+ describe ( "Switch Layout Size Tests" , ( ) => {
347
+ it ( "Can discriminate a set of layouts" , ( ) => {
348
+ const layouta = [
349
+ {
350
+ name : "payload" ,
351
+ binary : "bytes" ,
352
+ lengthSize : 2 ,
353
+ layout : [
354
+ {
355
+ name : "payload" ,
356
+ binary : "switch" ,
357
+ idSize : 1 ,
358
+ layouts : [
359
+ [ [ 0 , "Direct" ] , [ ] ] ,
360
+ [ [ 1 , "Payload" ] , [ { name : "data" , binary : "bytes" , lengthSize : 4 } ] ] ,
361
+ ] ,
362
+ } ,
363
+ ] ,
364
+ } ,
365
+ ] as const satisfies Layout ;
366
+
367
+ const layoutb = [
368
+ {
369
+ name : "payload" ,
370
+ binary : "bytes" ,
371
+ lengthSize : 3 ,
372
+ layout : [
373
+ {
374
+ name : "payload" ,
375
+ binary : "switch" ,
376
+ idSize : 1 ,
377
+ layouts : [
378
+ [ [ 0 , "Nothing" ] , [ ] ] ,
379
+ [ [ 1 , "Data" ] , [ { name : "data" , binary : "bytes" , lengthSize : 4 } ] ] ,
380
+ ] ,
381
+ } ,
382
+ ] ,
383
+ } ,
384
+ ] as const satisfies Layout ;
385
+
386
+ const messageLayouts = [
387
+ [ "Layout" , layouta ] ,
388
+ [ "LayoutB" , layoutb ] ,
389
+ ] as const satisfies RoArray < [ string , Layout ] > ;
390
+ const messageDiscriminator = layoutDiscriminator ( column ( messageLayouts , 1 ) ) ;
391
+
392
+ const b : LayoutToType < typeof layoutb > = {
393
+ payload : {
394
+ payload : { id : "Data" , data : new Uint8Array ( [ 0 , 0 , 0 , 0 ] ) } ,
395
+ } ,
396
+ } ;
397
+
398
+ const data = serializeLayout ( layoutb , b ) ;
399
+ const idx = messageDiscriminator ( data ) ;
400
+ expect ( idx ) . toEqual ( 1 ) ;
401
+ } ) ;
402
+ } ) ;
0 commit comments