@@ -172,3 +172,54 @@ func TestFormObjectForECPart(t *testing.T) {
172172 require .Equal (t , checksum .NewTillichZemor (tz .Sum (part )), phh )
173173 })
174174}
175+
176+ func TestDecodePartInfoFromAttributes (t * testing.T ) {
177+ t .Run ("missing" , func (t * testing.T ) {
178+ pi , err := iec .DecodePartInfoFromAttributes ("" , "" )
179+ require .NoError (t , err )
180+ require .EqualValues (t , - 1 , pi .RuleIndex )
181+ })
182+
183+ t .Run ("failure" , func (t * testing.T ) {
184+ for _ , tc := range []struct {
185+ name string
186+ ruleIdx string
187+ partIdx string
188+ assertErr func (t * testing.T , err error )
189+ }{
190+ {name : "non-int rule index" , ruleIdx : "not_an_int" , partIdx : "34" , assertErr : func (t * testing.T , err error ) {
191+ require .EqualError (t , err , `decode rule index: strconv.ParseUint: parsing "not_an_int": invalid syntax` )
192+ }},
193+ {name : "negative rule index" , ruleIdx : "-12" , partIdx : "34" , assertErr : func (t * testing.T , err error ) {
194+ require .EqualError (t , err , `decode rule index: strconv.ParseUint: parsing "-12": invalid syntax` )
195+ }},
196+ {name : "rule index overflow" , ruleIdx : "256" , partIdx : "34" , assertErr : func (t * testing.T , err error ) {
197+ require .EqualError (t , err , "rule index out of range" )
198+ }},
199+ {name : "non-int part index" , ruleIdx : "12" , partIdx : "not_an_int" , assertErr : func (t * testing.T , err error ) {
200+ require .EqualError (t , err , `decode part index: strconv.ParseUint: parsing "not_an_int": invalid syntax` )
201+ }},
202+ {name : "negative part index" , ruleIdx : "12" , partIdx : "-34" , assertErr : func (t * testing.T , err error ) {
203+ require .EqualError (t , err , `decode part index: strconv.ParseUint: parsing "-34": invalid syntax` )
204+ }},
205+ {name : "part index overflow" , ruleIdx : "12" , partIdx : "256" , assertErr : func (t * testing.T , err error ) {
206+ require .EqualError (t , err , "part index out of range" )
207+ }},
208+ {name : "rule index without part index" , ruleIdx : "12" , partIdx : "" , assertErr : func (t * testing.T , err error ) {
209+ require .EqualError (t , err , "rule index is set, part index is not" )
210+ }},
211+ {name : "part index without rule index" , ruleIdx : "" , partIdx : "34" , assertErr : func (t * testing.T , err error ) {
212+ require .EqualError (t , err , "part index is set, rule index is not" )
213+ }},
214+ } {
215+ t .Run (tc .name , func (t * testing.T ) {
216+ _ , err := iec .DecodePartInfoFromAttributes (tc .ruleIdx , tc .partIdx )
217+ tc .assertErr (t , err )
218+ })
219+ }
220+ })
221+
222+ pi , err := iec .DecodePartInfoFromAttributes ("12" , "34" )
223+ require .NoError (t , err )
224+ require .Equal (t , iec.PartInfo {RuleIndex : 12 , Index : 34 }, pi )
225+ }
0 commit comments