@@ -32,6 +32,113 @@ describe('Struct', function() {
3232 it ( 'should parse test data to buffer' , function ( ) {
3333 assert ( dataBuf . equals ( Buffer . from ( '0474657374f40103040100020003000400020201' , 'hex' ) ) ) ;
3434 } ) ;
35+ it ( 'should encode missing fields with default values by default' , function ( ) {
36+ const S = Struct ( 'DefaultMissing' , {
37+ a : DataTypes . uint8 ,
38+ b : DataTypes . uint8 ,
39+ } ) ;
40+ const instance = new S ( { a : 42 } ) ;
41+ const buf = instance . toBuffer ( ) ;
42+
43+ // Both fields encoded: a=42, b=0 (default)
44+ assert . equal ( buf . length , 2 ) ;
45+ assert . equal ( buf [ 0 ] , 42 ) ;
46+ assert . equal ( buf [ 1 ] , 0 ) ;
47+ } ) ;
48+ it ( 'should skip missing fields in nested struct without padding' , function ( ) {
49+ const Inner = Struct ( 'InnerSkip' , {
50+ x : DataTypes . uint8 ,
51+ y : DataTypes . uint8 ,
52+ } , { encodeMissingFieldsBehavior : 'skip' } ) ;
53+
54+ const Outer = Struct ( 'OuterWithInner' , {
55+ id : DataTypes . uint8 ,
56+ inner : Inner ,
57+ } ) ;
58+
59+ const instance = new Outer ( {
60+ id : 5 ,
61+ inner : { x : 10 } ,
62+ } ) ;
63+
64+ const buf = instance . toBuffer ( ) ;
65+ // id (1 byte) + inner.x (1 byte), no padding for missing inner.y
66+ assert . equal ( buf . length , 2 ) ;
67+ assert . equal ( buf [ 0 ] , 5 ) ;
68+ assert . equal ( buf [ 1 ] , 10 ) ;
69+ } ) ;
70+
71+ it ( 'should skip missing fields in array of structs without padding' , function ( ) {
72+ const Item = Struct ( 'ItemSkip' , {
73+ a : DataTypes . uint8 ,
74+ b : DataTypes . uint8 ,
75+ } , { encodeMissingFieldsBehavior : 'skip' } ) ;
76+
77+ const S = Struct ( 'ArrayContainer' , {
78+ items : DataTypes . Array8 ( Item ) ,
79+ } ) ;
80+
81+ const instance = new S ( {
82+ items : [
83+ { a : 1 } ,
84+ { a : 2 , b : 20 } ,
85+ { b : 30 } ,
86+ ] ,
87+ } ) ;
88+
89+ const buf = instance . toBuffer ( ) ;
90+ // Array8: 1 byte length + items
91+ // item1: 1 byte (a=1)
92+ // item2: 2 bytes (a=2, b=20)
93+ // item3: 1 byte (b=30)
94+ // Total: 1 + 1 + 2 + 1 = 5
95+ assert . equal ( buf . length , 5 ) ;
96+ assert . equal ( buf [ 0 ] , 3 ) ; // array length
97+ assert . equal ( buf [ 1 ] , 1 ) ; // item1.a
98+ assert . equal ( buf [ 2 ] , 2 ) ; // item2.a
99+ assert . equal ( buf [ 3 ] , 20 ) ; // item2.b
100+ assert . equal ( buf [ 4 ] , 30 ) ; // item3.b
101+ } ) ;
102+
103+ it ( 'should handle mixed skip and default behavior in nested structures' , function ( ) {
104+ const SkipInner = Struct ( 'SkipInner' , {
105+ x : DataTypes . uint8 ,
106+ y : DataTypes . uint8 ,
107+ } , { encodeMissingFieldsBehavior : 'skip' } ) ;
108+
109+ const DefaultInner = Struct ( 'DefaultInner' , {
110+ p : DataTypes . uint8 ,
111+ q : DataTypes . uint8 ,
112+ } ) ;
113+
114+ const Outer = Struct ( 'MixedOuter' , {
115+ skip : SkipInner ,
116+ default : DefaultInner ,
117+ } ) ;
118+
119+ const instance = new Outer ( {
120+ skip : { x : 5 } ,
121+ default : { p : 10 } ,
122+ } ) ;
123+
124+ const buf = instance . toBuffer ( ) ;
125+ // skip: 1 byte (x=5)
126+ // default: 2 bytes (p=10, q=0)
127+ // Total: 3
128+ assert . equal ( buf . length , 3 ) ;
129+ assert . equal ( buf [ 0 ] , 5 ) ;
130+ assert . equal ( buf [ 1 ] , 10 ) ;
131+ assert . equal ( buf [ 2 ] , 0 ) ;
132+ } ) ;
133+ it ( 'should produce identical output with skip option when all fields provided' , function ( ) {
134+ const defsA = { a : DataTypes . uint8 , b : DataTypes . uint8 } ;
135+ const defsB = { a : DataTypes . uint8 , b : DataTypes . uint8 } ;
136+ const Default = Struct ( 'AllFieldsDefault' , defsA ) ;
137+ const Skip = Struct ( 'AllFieldsSkip' , defsB , { encodeMissingFieldsBehavior : 'skip' } ) ;
138+ const d = new Default ( { a : 1 , b : 2 } ) ;
139+ const s = new Skip ( { a : 1 , b : 2 } ) ;
140+ assert ( d . toBuffer ( ) . equals ( s . toBuffer ( ) ) ) ;
141+ } ) ;
35142 it ( 'should parse test data from buffer' , function ( ) {
36143 const refData = TestStruct . fromBuffer ( dataBuf ) ;
37144
0 commit comments