@@ -203,6 +203,54 @@ func TestDocument(t *testing.T) {
203
203
}()
204
204
}
205
205
})
206
+ t .Run ("Update Index Properly" , func (t * testing.T ) {
207
+ a , b , c , d , e := EC .Null ("a" ), EC .Null ("b" ), EC .Null ("c" ), EC .Null ("d" ), EC .Null ("e" )
208
+ testCases := []struct {
209
+ name string
210
+ elems [][]* Element
211
+ index [][]uint32
212
+ }{
213
+ {
214
+ "two" ,
215
+ [][]* Element {{a }, {b }},
216
+ [][]uint32 {{0 }, {1 , 0 }},
217
+ },
218
+ {
219
+ "three" ,
220
+ [][]* Element {{a }, {b }, {c }},
221
+ [][]uint32 {{0 }, {1 , 0 }, {2 , 1 , 0 }},
222
+ },
223
+ {
224
+ "four" ,
225
+ [][]* Element {{a }, {d }, {b }, {c }},
226
+ [][]uint32 {{0 }, {1 , 0 }, {2 , 0 , 1 }, {3 , 1 , 0 , 2 }},
227
+ },
228
+ {
229
+ "five" ,
230
+ [][]* Element {{a }, {d }, {e }, {b }, {c }},
231
+ [][]uint32 {{0 }, {1 , 0 }, {2 , 1 , 0 }, {3 , 0 , 2 , 1 }, {4 , 1 , 0 , 3 , 2 }},
232
+ },
233
+ }
234
+
235
+ for _ , tc := range testCases {
236
+ t .Run (tc .name , func (t * testing.T ) {
237
+ d := NewDocument ()
238
+ for idx := range tc .elems {
239
+ d .Prepend (tc .elems [idx ]... )
240
+ index := d .index
241
+ for jdx := range tc .index [idx ] {
242
+ if tc.index [idx ][jdx ] != index [jdx ] {
243
+ t .Errorf (
244
+ "Indexes do not match at %d: got %v; want %v" ,
245
+ idx , index , tc .index [idx ],
246
+ )
247
+ break
248
+ }
249
+ }
250
+ }
251
+ })
252
+ }
253
+ })
206
254
testCases := []struct {
207
255
name string
208
256
elems [][]* Element
@@ -396,34 +444,43 @@ func TestDocument(t *testing.T) {
396
444
t .Errorf ("Delete should return nil element when deleting with empty key. got %#v; want %#v" , got , want )
397
445
}
398
446
})
447
+ d , c , b , a := EC .Null ("d" ), EC .Null ("c" ), EC .Null ("b" ), EC .Null ("a" )
399
448
testCases := []struct {
400
449
name string
401
450
d * Document
402
- key []string
403
- want * Element
451
+ keys [] []string
452
+ want [] * Element
404
453
}{
405
- {"first" , (& Document {}).Append (EC .Null ("x" )), []string {"x" },
406
- & Element {& Value {start : 0 , offset : 3 }},
454
+ {"first" , (& Document {}).Append (EC .Null ("x" )), [][] string {{ "x" } },
455
+ [] * Element {{ & Value {start : 0 , offset : 3 } }},
407
456
},
408
457
{"depth-one" , (& Document {}).Append (EC .SubDocumentFromElements ("x" , EC .Null ("y" ))),
409
- []string {"x" , "y" },
410
- & Element {& Value {start : 0 , offset : 3 }},
458
+ [][] string {{ "x" , "y" } },
459
+ [] * Element {{ & Value {start : 0 , offset : 3 } }},
411
460
},
412
461
{"invalid-depth-traversal" , (& Document {}).Append (EC .Null ("x" )),
413
- []string {"x" , "y" },
414
- nil ,
462
+ [][] string {{ "x" , "y" } },
463
+ [] * Element { nil } ,
415
464
},
416
465
{"not-found" , (& Document {}).Append (EC .Null ("x" )),
417
- []string {"y" },
418
- nil ,
466
+ [][]string {{"y" }},
467
+ []* Element {nil },
468
+ },
469
+ {
470
+ "delete twice" ,
471
+ NewDocument (d , c , b , a ),
472
+ [][]string {{"d" }, {"c" }},
473
+ []* Element {d , c },
419
474
},
420
475
}
421
476
422
477
for _ , tc := range testCases {
423
478
t .Run (tc .name , func (t * testing.T ) {
424
- got := tc .d .Delete (tc .key ... )
425
- if ! elementEqual (got , tc .want ) {
426
- t .Errorf ("Returned element does not match expected element. got %#v; want %#v" , got , tc .want )
479
+ for idx := range tc .keys {
480
+ got := tc .d .Delete (tc .keys [idx ]... )
481
+ if ! elementEqual (got , tc .want [idx ]) {
482
+ t .Errorf ("Returned element does not match expected element. got %#v; want %#v" , got , tc .want [idx ])
483
+ }
427
484
}
428
485
})
429
486
}
0 commit comments