| 1 |
const fs = require( 'fs' ); |
| 2 |
const path = require( 'path' ); |
| 3 |
const assert = require( 'assert' ); |
| 4 |
const { locate, getLocator } = require( '..' ); |
| 5 |
|
| 6 |
const sample = ` |
| 7 |
A flea and a fly in a flue |
| 8 |
Were imprisoned, so what could they do? |
| 9 |
Said the fly, "let us flee!" |
| 10 |
"Let us fly!" said the flea. |
| 11 |
So they flew through a flaw in the flue. |
| 12 |
`.trim(); |
| 13 |
|
| 14 |
describe( 'locate-character', () => { |
| 15 |
describe( 'getLocator', () => { |
| 16 |
const locator = getLocator( sample ); |
| 17 |
|
| 18 |
it( 'returns a function', () => { |
| 19 |
assert.equal( typeof getLocator( sample ), 'function' ); |
| 20 |
}); |
| 21 |
|
| 22 |
it( 'locates by character index', () => { |
| 23 |
assert.deepEqual( locator( 0 ), { line: 0, column: 0, character: 0 }); |
| 24 |
assert.deepEqual( locator( 1 ), { line: 0, column: 1, character: 1 }); |
| 25 |
assert.deepEqual( locator( 76 ), { line: 2, column: 9, character: 76 }); |
| 26 |
}); |
| 27 |
|
| 28 |
it( 'respects offsetLine: x and offsetColumn: x', () => { |
| 29 |
const locator = getLocator( sample, { offsetLine: 2, offsetColumn: 2 }); |
| 30 |
assert.deepEqual( |
| 31 |
locator( 0 ), |
| 32 |
{ line: 2, column: 2, character: 0 } |
| 33 |
); |
| 34 |
}); |
| 35 |
|
| 36 |
it( 'locates by search string', () => { |
| 37 |
assert.deepEqual( locator( 'fly' ), { line: 0, column: 13, character: 13 }); |
| 38 |
}); |
| 39 |
|
| 40 |
it( 'locates by search string with startIndex', () => { |
| 41 |
let location = locator( 'fly' ); |
| 42 |
assert.deepEqual( location, { line: 0, column: 13, character: 13 }); |
| 43 |
|
| 44 |
location = locator( 'fly', location.character + 1 ); |
| 45 |
assert.deepEqual( location, { line: 2, column: 9, character: 76 }); |
| 46 |
|
| 47 |
location = locator( 'fly', location.character + 1 ); |
| 48 |
assert.deepEqual( location, { line: 3, column: 8, character: 104 }); |
| 49 |
}); |
| 50 |
}); |
| 51 |
|
| 52 |
describe( 'locate', () => { |
| 53 |
it( 'locates a character by index', () => { |
| 54 |
assert.deepEqual( locate( sample, 13 ), { line: 0, column: 13, character: 13 }); |
| 55 |
}); |
| 56 |
|
| 57 |
it( 'locates a character by string', () => { |
| 58 |
assert.deepEqual( locate( sample, 'fly' ), { line: 0, column: 13, character: 13 }); |
| 59 |
}); |
| 60 |
|
| 61 |
it( 'locates a character by string with startIndex', () => { |
| 62 |
assert.deepEqual( locate( sample, 'fly', { startIndex: 14 }), { line: 2, column: 9, character: 76 }); |
| 63 |
assert.deepEqual( locate( sample, 'fly', { startIndex: 77 }), { line: 3, column: 8, character: 104 }); |
| 64 |
}); |
| 65 |
|
| 66 |
it( 'respects offsetLine: x and offsetColumn: x', () => { |
| 67 |
assert.deepEqual( |
| 68 |
locate( sample, 13, { offsetLine: 2, offsetColumn: 2 }), |
| 69 |
{ line: 2, column: 15, character: 13 } |
| 70 |
); |
| 71 |
}); |
| 72 |
}); |
| 73 |
}); |