@@ -237,10 +237,11 @@ define(function (require, exports, module) {
237237 } ) ;
238238
239239 describe ( "posFromIndex" , function ( ) {
240+ const TEST_CONTENT = "line0\nline1\nline2\n" ;
240241 let myEditor , myDocument ;
241242
242243 beforeEach ( function ( ) {
243- const mocks = SpecRunnerUtils . createMockEditor ( "line0\nline1\nline2\n" , "unknown" ) ;
244+ const mocks = SpecRunnerUtils . createMockEditor ( TEST_CONTENT , "unknown" ) ;
244245 myDocument = mocks . doc ;
245246 myEditor = mocks . editor ;
246247 } ) ;
@@ -253,29 +254,36 @@ define(function (require, exports, module) {
253254 }
254255 } ) ;
255256
257+ // Verify the character at a given index matches the char at the returned position
258+ function expectCharAtIndex ( index , expectedChar ) {
259+ const pos = myDocument . posFromIndex ( index ) ;
260+ const actualChar = myDocument . getLine ( pos . line ) . charAt ( pos . ch ) ;
261+ expect ( actualChar ) . toBe ( expectedChar ) ;
262+ return pos ;
263+ }
264+
256265 it ( "should return {0,0} for index 0" , function ( ) {
257- const pos = myDocument . posFromIndex ( 0 ) ;
266+ const pos = expectCharAtIndex ( 0 , "l" ) ;
258267 expect ( pos . line ) . toBe ( 0 ) ;
259268 expect ( pos . ch ) . toBe ( 0 ) ;
260269 } ) ;
261270
262271 it ( "should return correct position within first line" , function ( ) {
263- // "line0" — index 3 is 'e'
264- const pos = myDocument . posFromIndex ( 3 ) ;
272+ const pos = expectCharAtIndex ( 3 , "e" ) ;
265273 expect ( pos . line ) . toBe ( 0 ) ;
266274 expect ( pos . ch ) . toBe ( 3 ) ;
267275 } ) ;
268276
269277 it ( "should return start of second line after newline" , function ( ) {
270- // "line0\n" is 6 chars, so index 6 is start of line 1
271- const pos = myDocument . posFromIndex ( 6 ) ;
278+ // "line0\n" is 6 chars, so index 6 is 'l' at start of "line1"
279+ const pos = expectCharAtIndex ( 6 , "l" ) ;
272280 expect ( pos . line ) . toBe ( 1 ) ;
273281 expect ( pos . ch ) . toBe ( 0 ) ;
274282 } ) ;
275283
276284 it ( "should return correct position on third line" , function ( ) {
277285 // "line0\nline1\n" is 12 chars, index 14 is 'n' in "line2"
278- const pos = myDocument . posFromIndex ( 14 ) ;
286+ const pos = expectCharAtIndex ( 14 , "n" ) ;
279287 expect ( pos . line ) . toBe ( 2 ) ;
280288 expect ( pos . ch ) . toBe ( 2 ) ;
281289 } ) ;
@@ -286,20 +294,23 @@ define(function (require, exports, module) {
286294 SpecRunnerUtils . destroyMockEditor ( myDocument ) ;
287295 myEditor = null ;
288296
289- // Content is "line0\nline1\nline2\n" — same as beforeEach
290297 expect ( myDocument . _masterEditor ) . toBe ( null ) ;
291298
299+ // Verify against raw string since getLine is unavailable without editor
292300 let pos = myDocument . posFromIndex ( 0 ) ;
293301 expect ( pos . line ) . toBe ( 0 ) ;
294302 expect ( pos . ch ) . toBe ( 0 ) ;
303+ expect ( TEST_CONTENT [ 0 ] ) . toBe ( "l" ) ;
295304
296305 pos = myDocument . posFromIndex ( 6 ) ;
297306 expect ( pos . line ) . toBe ( 1 ) ;
298307 expect ( pos . ch ) . toBe ( 0 ) ;
308+ expect ( TEST_CONTENT [ 6 ] ) . toBe ( "l" ) ;
299309
300310 pos = myDocument . posFromIndex ( 14 ) ;
301311 expect ( pos . line ) . toBe ( 2 ) ;
302312 expect ( pos . ch ) . toBe ( 2 ) ;
313+ expect ( TEST_CONTENT [ 14 ] ) . toBe ( "n" ) ;
303314 } ) ;
304315 } ) ;
305316 } ) ;
0 commit comments