@@ -14,6 +14,7 @@ import (
14
14
"go/token"
15
15
"iter"
16
16
"log"
17
+ "math/rand"
17
18
"path/filepath"
18
19
"reflect"
19
20
"slices"
@@ -332,8 +333,31 @@ func TestCursor_FindNode(t *testing.T) {
332
333
}
333
334
}
334
335
}
336
+ }
337
+
338
+ // TestCursor_FindPos_order ensures that FindPos does not assume files are in Pos order.
339
+ func TestCursor_FindPos_order (t * testing.T ) {
340
+ // Pick an arbitrary decl.
341
+ target := netFiles [7 ].Decls [0 ]
342
+
343
+ // Find the target decl by its position.
344
+ cur , ok := cursor .Root (netInspect ).FindPos (target .Pos (), target .End ())
345
+ if ! ok || cur .Node () != target {
346
+ t .Fatalf ("unshuffled: FindPos(%T) = (%v, %t)" , target , cur , ok )
347
+ }
335
348
336
- // TODO(adonovan): FindPos needs a test (not just a benchmark).
349
+ // Shuffle the files out of Pos order.
350
+ files := slices .Clone (netFiles )
351
+ rand .Shuffle (len (files ), func (i , j int ) {
352
+ files [i ], files [j ] = files [j ], files [i ]
353
+ })
354
+
355
+ // Find it again.
356
+ inspect := inspector .New (files )
357
+ cur , ok = cursor .Root (inspect ).FindPos (target .Pos (), target .End ())
358
+ if ! ok || cur .Node () != target {
359
+ t .Fatalf ("shuffled: FindPos(%T) = (%v, %t)" , target , cur , ok )
360
+ }
337
361
}
338
362
339
363
func TestCursor_Edge (t * testing.T ) {
0 commit comments