33using Substrate . NetApi . Model . Types . Primitive ;
44using Substrate . NetApi . TypeConverters ;
55using NUnit . Framework ;
6+ using System ;
67
78namespace Substrate . NetApi . Test
89{
@@ -112,11 +113,22 @@ public enum PhaseState
112113 [ Test ]
113114 public void ExtEnumEncodingTest ( )
114115 {
115- var extEnumType = new BaseEnumExt < PhaseState , U8 , BaseVoid , BaseVoid > ( ) ;
116+ // Set up type decoder map for PhaseState
117+ var typeDecoderMap = new Dictionary < PhaseState , Type >
118+ {
119+ { PhaseState . None , typeof ( U8 ) } ,
120+ { PhaseState . Finalization , typeof ( BaseVoid ) } ,
121+ { PhaseState . Initialization , typeof ( BaseVoid ) }
122+ } ;
116123
124+ // Initialize BaseEnumRust with the decoder map
125+ var extEnumType = new BaseEnumRust < PhaseState > ( typeDecoderMap ) ;
126+
127+ // Decode the input data
117128 int p = 0 ;
118129 extEnumType . Decode ( new byte [ ] { 0x00 , 0x01 } , ref p ) ;
119130
131+ // Assertions to verify values
120132 Assert . AreEqual ( PhaseState . None , extEnumType . Value ) ;
121133 Assert . AreEqual ( "U8" , extEnumType . Value2 . GetType ( ) . Name ) ;
122134 Assert . AreEqual ( 1 , ( extEnumType . Value2 as U8 ) . Value ) ;
@@ -125,45 +137,75 @@ public void ExtEnumEncodingTest()
125137 [ Test ]
126138 public void ExtEnumDencodingTest ( )
127139 {
128- var extEnumType = new BaseEnumExt < PhaseState , U8 , BaseVoid , BaseVoid > ( ) ;
140+ // Set up type decoder map for PhaseState
141+ var typeDecoderMap = new Dictionary < PhaseState , Type >
142+ {
143+ { PhaseState . None , typeof ( U8 ) } ,
144+ { PhaseState . Finalization , typeof ( BaseVoid ) } ,
145+ { PhaseState . Initialization , typeof ( BaseVoid ) }
146+ } ;
147+
148+ // Initialize BaseEnumRust with the decoder map
149+ var extEnumType = new BaseEnumRust < PhaseState > ( typeDecoderMap ) ;
129150
151+ // Decode the input data
130152 int p = 0 ;
131153 extEnumType . Decode ( new byte [ ] { 0x00 , 0x01 } , ref p ) ;
132154
155+ // Assertions to verify values
133156 Assert . AreEqual ( PhaseState . None , extEnumType . Value ) ;
134157 Assert . AreEqual ( "U8" , extEnumType . Value2 . GetType ( ) . Name ) ;
135158 Assert . AreEqual ( 1 , ( extEnumType . Value2 as U8 ) . Value ) ;
136159
160+ // Verify the bytes are preserved correctly
137161 Assert . AreEqual ( new byte [ ] { 0x00 , 0x01 } , extEnumType . Bytes ) ;
138162 }
139163
140164 [ Test ]
141165 public void ExtEnumCreateTest ( )
142166 {
143- var u8 = new U8 ( 1 ) ;
144-
145- var vecExtEnumTypeFromCreateValue = new BaseEnumExt < PhaseState , U8 > ( ) ;
146- vecExtEnumTypeFromCreateValue . Create ( PhaseState . None , u8 ) ;
147-
148- var vecExtEnumTypeFromCreateByteArray = new BaseEnumExt < PhaseState , U8 > ( ) ;
149- vecExtEnumTypeFromCreateByteArray . Create ( new byte [ ] { 0 , 1 } ) ;
150-
151- var vecExtEnumTypeFromCreateHex = new BaseEnumExt < PhaseState , U8 > ( ) ;
152- vecExtEnumTypeFromCreateHex . Create ( "0x0001" ) ;
153-
154- Assert . That ( vecExtEnumTypeFromCreateValue . Bytes , Is . EqualTo ( vecExtEnumTypeFromCreateByteArray . Bytes ) ) ;
155- Assert . That ( vecExtEnumTypeFromCreateValue . Value , Is . EqualTo ( vecExtEnumTypeFromCreateByteArray . Value ) ) ;
156-
167+ var typeDecoderMap = new Dictionary < PhaseState , Type >
168+ {
169+ { PhaseState . None , typeof ( U8 ) } ,
170+ { PhaseState . Finalization , typeof ( BaseVoid ) } ,
171+ { PhaseState . Initialization , typeof ( BaseVoid ) }
172+ } ;
157173
158- Assert . That ( vecExtEnumTypeFromCreateValue . Bytes , Is . EqualTo ( vecExtEnumTypeFromCreateHex . Bytes ) ) ;
159- Assert . That ( vecExtEnumTypeFromCreateValue . Value , Is . EqualTo ( vecExtEnumTypeFromCreateHex . Value ) ) ;
174+ // Create instance using enum value and U8 value
175+ var u8 = new U8 ( 1 ) ;
176+ var byValue = new BaseEnumRust < PhaseState > ( typeDecoderMap ) ;
177+ byValue . Create ( PhaseState . None , u8 ) ;
178+
179+ // Create instance using byte array
180+ var byArray = new BaseEnumRust < PhaseState > ( typeDecoderMap ) ;
181+ byArray . Create ( new byte [ ] { 0x00 , 0x01 } ) ;
182+
183+ // Create instance using hex string
184+ var byHex = new BaseEnumRust < PhaseState > ( typeDecoderMap ) ;
185+ byHex . Create ( "0x0001" ) ;
186+
187+ // Assert equality between different creation methods
188+ Assert . That ( byValue . Bytes , Is . EqualTo ( byArray . Bytes ) ) ;
189+ Assert . That ( byValue . Value , Is . EqualTo ( byArray . Value ) ) ;
190+ Assert . That ( byValue . Bytes , Is . EqualTo ( byHex . Bytes ) ) ;
191+ Assert . That ( byValue . Value , Is . EqualTo ( byHex . Value ) ) ;
160192 }
161193
162194 [ Test ]
163- public void ExtEnumXXX ( )
195+ public void ExtEnumXXX_NewTest ( )
164196 {
165- var vecExtEnumType = new BaseVec < BaseEnumExt < PhaseState , BaseTuple < Arr4U8 , BaseVec < U8 > > , BaseVoid , BaseVoid , BaseVoid , BaseVoid , BaseVoid , BaseVoid , BaseVoid , BaseVoid > > ( ) ;
197+ // Create the type decoder map for PhaseState
198+ var typeDecoderMap = new Dictionary < PhaseState , Type >
199+ {
200+ { PhaseState . None , typeof ( BaseTuple < Arr4U8 , BaseVec < U8 > > ) } ,
201+ { PhaseState . Finalization , typeof ( BaseVoid ) } ,
202+ { PhaseState . Initialization , typeof ( BaseVoid ) }
203+ } ;
204+
205+ // Initialize the enum wrapper
206+ var baseEnum = new BaseEnumRust < PhaseState > ( typeDecoderMap ) ;
166207
208+ // Prepare values
167209 var u8 = new U8 ( ) ;
168210 u8 . Create ( byte . MaxValue ) ;
169211
@@ -173,17 +215,16 @@ public void ExtEnumXXX()
173215 var vec82 = new BaseVec < U8 > ( ) ;
174216 vec82 . Create ( new U8 [ ] { u8 } ) ;
175217
176- var tu = new BaseTuple < Arr4U8 , BaseVec < U8 > > ( ) ;
177- tu . Create ( vec8 , vec82 ) ;
178-
179- var it = new BaseEnumExt < PhaseState , BaseTuple < Arr4U8 , BaseVec < U8 > > , BaseVoid , BaseVoid , BaseVoid , BaseVoid , BaseVoid , BaseVoid , BaseVoid , BaseVoid > ( ) ;
180- it . Create ( PhaseState . None , tu ) ;
218+ var tuple = new BaseTuple < Arr4U8 , BaseVec < U8 > > ( ) ;
219+ tuple . Create ( vec8 , vec82 ) ;
181220
182- vecExtEnumType . Create ( new [ ] { it } ) ;
221+ // Create with PhaseState.None and tuple value
222+ baseEnum . Create ( PhaseState . None , tuple ) ;
183223
184- var encoded = vecExtEnumType . Encode ( ) ;
224+ // Encode and decode
225+ var encoded = baseEnum . Encode ( ) ;
185226 int p = 0 ;
186- vecExtEnumType . Decode ( encoded , ref p ) ;
227+ baseEnum . Decode ( encoded , ref p ) ;
187228
188229 Assert . Pass ( ) ;
189230 }
@@ -194,27 +235,34 @@ internal enum TestEnum26
194235 }
195236
196237 [ Test ]
197- public void ExtEnum26 ( )
238+ public void ExtEnum26_NewTest ( )
198239 {
199- var ext1 = new BaseEnumExt < TestEnum26 , U8 , U16 , U8 , U16 , U8 , U16 , U8 , U16 , U8 , U16 , U8 , U16 , U8 , U16 , U8 , U16 , U8 , U16 , U8 , U16 , U8 , U16 , U8 , U16 , U8 , U16 > ( ) ;
240+ // Create the type decoder map for TestEnum26
241+ var typeDecoderMap = new Dictionary < TestEnum26 , Type >
242+ {
243+ { TestEnum26 . T1 , typeof ( U8 ) } ,
244+ { TestEnum26 . T2 , typeof ( U16 ) } ,
245+ // Add other mappings as needed
246+ } ;
200247
201- var u8 = new U8 ( ) ;
202- u8 . Create ( byte . MaxValue ) ;
248+ // Initialize BaseEnumRust
249+ var baseEnum = new BaseEnumRust < TestEnum26 > ( typeDecoderMap ) ;
203250
251+ // Create U16 value
204252 var u16 = new U16 ( ) ;
205253 u16 . Create ( ushort . MaxValue ) ;
206254
207- ext1 . Create ( TestEnum26 . T2 , u16 ) ;
208-
209- var encoded = ext1 . Encode ( ) ;
210-
211- var ext2 = new BaseEnumExt < TestEnum26 , U8 , U16 , U8 , U16 , U8 , U16 , U8 , U16 , U8 , U16 , U8 , U16 , U8 , U16 , U8 , U16 , U8 , U16 , U8 , U16 , U8 , U16 , U8 , U16 , U8 , U16 > ( ) ;
255+ // Create an instance with TestEnum26.T2
256+ baseEnum . Create ( TestEnum26 . T2 , u16 ) ;
212257
258+ // Encode and decode
259+ var encoded = baseEnum . Encode ( ) ;
213260 int p = 0 ;
214- ext2 . Decode ( encoded , ref p ) ;
261+ baseEnum . Decode ( encoded , ref p ) ;
215262
216- Assert . AreEqual ( TestEnum26 . T2 , ext2 . Value ) ;
217- Assert . AreEqual ( ushort . MaxValue , ( ( U16 ) ext2 . Value2 ) . Value ) ;
263+ // Assertions
264+ Assert . AreEqual ( TestEnum26 . T2 , baseEnum . Value ) ;
265+ Assert . AreEqual ( ushort . MaxValue , ( ( U16 ) baseEnum . Value2 ) . Value ) ;
218266 }
219267
220268 [ Test ]
0 commit comments