| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
QUnit.module( 've.DiffMatchPatch' ); |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
QUnit.test( 'indexOf/lastIndexOf', ( assert ) => { |
| 12 |
const dmp = new ve.DiffMatchPatch( new ve.dm.HashValueStore(), new ve.dm.HashValueStore() ), |
| 13 |
textString = 'hello world', |
| 14 |
textArray = textString.split( '' ), |
| 15 |
cases = [ |
| 16 |
{ |
| 17 |
msg: 'search value present', |
| 18 |
searchValue: 'o' |
| 19 |
}, |
| 20 |
{ |
| 21 |
msg: 'search value present, from index negative', |
| 22 |
searchValue: 'o', |
| 23 |
fromIndex: -2 |
| 24 |
}, |
| 25 |
{ |
| 26 |
msg: 'search value present, from index 0', |
| 27 |
searchValue: 'o', |
| 28 |
fromIndex: 0 |
| 29 |
}, |
| 30 |
{ |
| 31 |
msg: 'search value present, from index before first occurrence', |
| 32 |
searchValue: 'o', |
| 33 |
fromIndex: 2 |
| 34 |
}, |
| 35 |
{ |
| 36 |
msg: 'search value present, from index first occurrence', |
| 37 |
searchValue: 'o', |
| 38 |
fromIndex: 4 |
| 39 |
}, |
| 40 |
{ |
| 41 |
msg: 'search value present, from index between first and last occurrence', |
| 42 |
searchValue: 'o', |
| 43 |
fromIndex: 6 |
| 44 |
}, |
| 45 |
{ |
| 46 |
msg: 'search value present, from index last occurrence', |
| 47 |
searchValue: 'o', |
| 48 |
fromIndex: 7 |
| 49 |
}, |
| 50 |
{ |
| 51 |
msg: 'search value present, from index array length', |
| 52 |
searchValue: 'o', |
| 53 |
fromIndex: 11 |
| 54 |
}, |
| 55 |
{ |
| 56 |
msg: 'search value present, from greater than array length', |
| 57 |
searchValue: 'o', |
| 58 |
fromIndex: 13 |
| 59 |
}, |
| 60 |
{ |
| 61 |
msg: 'empty string', |
| 62 |
searchValue: '' |
| 63 |
}, |
| 64 |
{ |
| 65 |
msg: 'empty string, negative index', |
| 66 |
searchValue: '', |
| 67 |
fromIndex: -2 |
| 68 |
}, |
| 69 |
{ |
| 70 |
msg: 'empty string, index within array', |
| 71 |
searchValue: '', |
| 72 |
fromIndex: 6 |
| 73 |
}, |
| 74 |
{ |
| 75 |
msg: 'empty string, index greater than array length', |
| 76 |
searchValue: '', |
| 77 |
fromIndex: 99 |
| 78 |
}, |
| 79 |
{ |
| 80 |
msg: 'search value not present', |
| 81 |
searchValue: 'heya' |
| 82 |
}, |
| 83 |
{ |
| 84 |
msg: 'search value multiple characters', |
| 85 |
searchValue: 'world' |
| 86 |
} |
| 87 |
]; |
| 88 |
|
| 89 |
cases.forEach( ( caseItem ) => { |
| 90 |
let expected = textString.indexOf( caseItem.searchValue, caseItem.fromIndex ); |
| 91 |
assert.strictEqual( |
| 92 |
dmp.indexOf( textArray, caseItem.searchValue, caseItem.fromIndex ), |
| 93 |
expected, |
| 94 |
caseItem.msg + ': indexOf is ' + expected |
| 95 |
); |
| 96 |
expected = textString.lastIndexOf( caseItem.searchValue, caseItem.fromIndex ); |
| 97 |
assert.strictEqual( |
| 98 |
dmp.lastIndexOf( textArray, caseItem.searchValue, caseItem.fromIndex ), |
| 99 |
expected, |
| 100 |
caseItem.msg + ': lastIndexOf is ' + expected |
| 101 |
); |
| 102 |
} ); |
| 103 |
} ); |