forked from dmonad/lib0
-
Notifications
You must be signed in to change notification settings - Fork 0
/
diff.test.js
38 lines (35 loc) · 1.21 KB
/
diff.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import { simpleDiff } from './diff.js'
import * as prng from './prng.js'
import * as t from './testing.js'
/**
* @param {string} a
* @param {string} b
* @param {{index: number,remove:number,insert:string}} expected
*/
function runDiffTest (a, b, expected) {
const result = simpleDiff(a, b)
t.compare(result, expected)
}
/**
* @param {t.TestCase} tc
*/
export const testDiffing = tc => {
runDiffTest('abc', 'axc', { index: 1, remove: 1, insert: 'x' })
runDiffTest('bc', 'xc', { index: 0, remove: 1, insert: 'x' })
runDiffTest('ab', 'ax', { index: 1, remove: 1, insert: 'x' })
runDiffTest('b', 'x', { index: 0, remove: 1, insert: 'x' })
runDiffTest('', 'abc', { index: 0, remove: 0, insert: 'abc' })
runDiffTest('abc', 'xyz', { index: 0, remove: 3, insert: 'xyz' })
runDiffTest('axz', 'au', { index: 1, remove: 2, insert: 'u' })
runDiffTest('ax', 'axy', { index: 2, remove: 0, insert: 'y' })
}
/**
* @param {t.TestCase} tc
*/
export const testRepeatDiffing = tc => {
const a = prng.word(tc.prng)
const b = prng.word(tc.prng)
const change = simpleDiff(a, b)
const recomposed = `${a.slice(0, change.index)}${change.insert}${a.slice(change.index + change.remove)}`
t.compareStrings(recomposed, b)
}