@@ -982,6 +982,56 @@ function compositeParserTests(
982982 } ,
983983 } ) ;
984984 } ) ;
985+
986+ it ( "standalone bit fields should work" , ( ) => {
987+ const parser = Parser . start ( ) . bit6 ( "one" ) . bit8 ( "two" ) ;
988+ const buffer = factory ( [ 0xa8 , 0x78 ] ) ;
989+ const result = parser . parse ( buffer ) ;
990+ deepStrictEqual ( result . one , 0xa8 >> 2 ) ;
991+ deepStrictEqual ( result . two , 0x78 >> 2 ) ;
992+ } ) ;
993+
994+ it ( "bit to nested bit should work" , ( ) => {
995+ const parser = Parser . start ( )
996+ . bit6 ( "one" )
997+ . nest ( "nested" , {
998+ type : new Parser ( ) . bit8 ( "two" ) . uint8 ( "three" ) ,
999+ } ) ;
1000+ const buffer = factory ( [ 0xa8 , 0x78 , 0x45 ] ) ;
1001+ const result = parser . parse ( buffer ) ;
1002+ deepStrictEqual ( result . one , 0xa8 >> 2 ) ;
1003+ deepStrictEqual ( result . nested . two , 0x78 >> 2 ) ;
1004+ // switching to uint8 should start at next byte (skipping two bits here)
1005+ deepStrictEqual ( result . nested . three , 0x45 ) ;
1006+ } ) ;
1007+
1008+ it ( "bit before nest should work" , ( ) => {
1009+ const parser = Parser . start ( )
1010+ . useContextVars ( )
1011+ . bit8 ( "items" )
1012+ . nest ( "data" , {
1013+ type : Parser . start ( )
1014+ . uint8 ( "length" )
1015+ . string ( "message" , { length : "length" } )
1016+ . array ( "value" , {
1017+ type : "uint8" ,
1018+ length : "$parent.items" ,
1019+ } ) ,
1020+ } ) ;
1021+
1022+ const buffer = factory ( [
1023+ 0x2 , 0xc , 0x68 , 0x65 , 0x6c , 0x6c , 0x6f , 0x2c , 0x20 , 0x77 , 0x6f , 0x72 ,
1024+ 0x6c , 0x64 , 0x01 , 0x02 , 0x02 , 0x02 ,
1025+ ] ) ;
1026+ deepStrictEqual ( parser . parse ( buffer ) , {
1027+ items : 2 ,
1028+ data : {
1029+ length : 12 ,
1030+ message : "hello, world" ,
1031+ value : [ 0x01 , 0x02 ] ,
1032+ } ,
1033+ } ) ;
1034+ } ) ;
9851035 } ) ;
9861036
9871037 describe ( "Constructors" , ( ) => {
0 commit comments