forked from microsoft/vscode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiff.test.ts
More file actions
180 lines (138 loc) · 5.89 KB
/
diff.test.ts
File metadata and controls
180 lines (138 loc) · 5.89 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as assert from 'assert';
import { IDiffChange, LcsDiff, StringDiffSequence } from 'vs/base/common/diff/diff';
function createArray<T>(length: number, value: T): T[] {
const r: T[] = [];
for (let i = 0; i < length; i++) {
r[i] = value;
}
return r;
}
function maskBasedSubstring(str: string, mask: boolean[]): string {
let r = '';
for (let i = 0; i < str.length; i++) {
if (mask[i]) {
r += str.charAt(i);
}
}
return r;
}
function assertAnswer(originalStr: string, modifiedStr: string, changes: IDiffChange[], answerStr: string, onlyLength: boolean = false): void {
const originalMask = createArray(originalStr.length, true);
const modifiedMask = createArray(modifiedStr.length, true);
let i, j, change;
for (i = 0; i < changes.length; i++) {
change = changes[i];
if (change.originalLength) {
for (j = 0; j < change.originalLength; j++) {
originalMask[change.originalStart + j] = false;
}
}
if (change.modifiedLength) {
for (j = 0; j < change.modifiedLength; j++) {
modifiedMask[change.modifiedStart + j] = false;
}
}
}
const originalAnswer = maskBasedSubstring(originalStr, originalMask);
const modifiedAnswer = maskBasedSubstring(modifiedStr, modifiedMask);
if (onlyLength) {
assert.strictEqual(originalAnswer.length, answerStr.length);
assert.strictEqual(modifiedAnswer.length, answerStr.length);
} else {
assert.strictEqual(originalAnswer, answerStr);
assert.strictEqual(modifiedAnswer, answerStr);
}
}
function lcsInnerTest(originalStr: string, modifiedStr: string, answerStr: string, onlyLength: boolean = false): void {
const diff = new LcsDiff(new StringDiffSequence(originalStr), new StringDiffSequence(modifiedStr));
const changes = diff.ComputeDiff(false).changes;
assertAnswer(originalStr, modifiedStr, changes, answerStr, onlyLength);
}
function stringPower(str: string, power: number): string {
let r = str;
for (let i = 0; i < power; i++) {
r += r;
}
return r;
}
function lcsTest(originalStr: string, modifiedStr: string, answerStr: string) {
lcsInnerTest(originalStr, modifiedStr, answerStr);
for (let i = 2; i <= 5; i++) {
lcsInnerTest(stringPower(originalStr, i), stringPower(modifiedStr, i), stringPower(answerStr, i), true);
}
}
suite('Diff', () => {
test('LcsDiff - different strings tests', function () {
this.timeout(10000);
lcsTest('heLLo world', 'hello orlando', 'heo orld');
lcsTest('abcde', 'acd', 'acd'); // simple
lcsTest('abcdbce', 'bcede', 'bcde'); // skip
lcsTest('abcdefgabcdefg', 'bcehafg', 'bceafg'); // long
lcsTest('abcde', 'fgh', ''); // no match
lcsTest('abcfabc', 'fabc', 'fabc');
lcsTest('0azby0', '9axbzby9', 'azby');
lcsTest('0abc00000', '9a1b2c399999', 'abc');
lcsTest('fooBar', 'myfooBar', 'fooBar'); // all insertions
lcsTest('fooBar', 'fooMyBar', 'fooBar'); // all insertions
lcsTest('fooBar', 'fooBar', 'fooBar'); // identical sequences
});
});
suite('Diff - Ported from VS', () => {
test('using continue processing predicate to quit early', function () {
const left = 'abcdef';
const right = 'abxxcyyydzzzzezzzzzzzzzzzzzzzzzzzzf';
// We use a long non-matching portion at the end of the right-side string, so the backwards tracking logic
// doesn't get there first.
let predicateCallCount = 0;
let diff = new LcsDiff(new StringDiffSequence(left), new StringDiffSequence(right), function (leftIndex, longestMatchSoFar) {
assert.strictEqual(predicateCallCount, 0);
predicateCallCount++;
assert.strictEqual(leftIndex, 1);
// cancel processing
return false;
});
let changes = diff.ComputeDiff(true).changes;
assert.strictEqual(predicateCallCount, 1);
// Doesn't include 'c', 'd', or 'e', since we quit on the first request
assertAnswer(left, right, changes, 'abf');
// Cancel after the first match ('c')
diff = new LcsDiff(new StringDiffSequence(left), new StringDiffSequence(right), function (leftIndex, longestMatchSoFar) {
assert(longestMatchSoFar <= 1); // We never see a match of length > 1
// Continue processing as long as there hasn't been a match made.
return longestMatchSoFar < 1;
});
changes = diff.ComputeDiff(true).changes;
assertAnswer(left, right, changes, 'abcf');
// Cancel after the second match ('d')
diff = new LcsDiff(new StringDiffSequence(left), new StringDiffSequence(right), function (leftIndex, longestMatchSoFar) {
assert(longestMatchSoFar <= 2); // We never see a match of length > 2
// Continue processing as long as there hasn't been a match made.
return longestMatchSoFar < 2;
});
changes = diff.ComputeDiff(true).changes;
assertAnswer(left, right, changes, 'abcdf');
// Cancel *one iteration* after the second match ('d')
let hitSecondMatch = false;
diff = new LcsDiff(new StringDiffSequence(left), new StringDiffSequence(right), function (leftIndex, longestMatchSoFar) {
assert(longestMatchSoFar <= 2); // We never see a match of length > 2
const hitYet = hitSecondMatch;
hitSecondMatch = longestMatchSoFar > 1;
// Continue processing as long as there hasn't been a match made.
return !hitYet;
});
changes = diff.ComputeDiff(true).changes;
assertAnswer(left, right, changes, 'abcdf');
// Cancel after the third and final match ('e')
diff = new LcsDiff(new StringDiffSequence(left), new StringDiffSequence(right), function (leftIndex, longestMatchSoFar) {
assert(longestMatchSoFar <= 3); // We never see a match of length > 3
// Continue processing as long as there hasn't been a match made.
return longestMatchSoFar < 3;
});
changes = diff.ComputeDiff(true).changes;
assertAnswer(left, right, changes, 'abcdef');
});
});