-
Notifications
You must be signed in to change notification settings - Fork 510
/
Copy pathline.js
130 lines (115 loc) · 4.98 KB
/
line.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
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
import {diffLines, diffTrimmedLines} from '../../lib/diff/line';
import {convertChangesToXML} from '../../lib/convert/xml';
import {expect} from 'chai';
describe('diff/line', function() {
// Line Diff
describe('#diffLines', function() {
it('should diff lines', function() {
const diffResult = diffLines(
'line\nold value\nline',
'line\nnew value\nline');
expect(convertChangesToXML(diffResult)).to.equal('line\n<del>old value\n</del><ins>new value\n</ins>line');
});
it('should the same lines in diff', function() {
const diffResult = diffLines(
'line\nvalue\nline',
'line\nvalue\nline');
expect(convertChangesToXML(diffResult)).to.equal('line\nvalue\nline');
});
it('should handle leading and trailing whitespace', function() {
const diffResult = diffLines(
'line\nvalue \nline',
'line\nvalue\nline');
expect(convertChangesToXML(diffResult)).to.equal('line\n<del>value \n</del><ins>value\n</ins>line');
});
it('should handle windows line endings', function() {
const diffResult = diffLines(
'line\r\nold value \r\nline',
'line\r\nnew value\r\nline');
expect(convertChangesToXML(diffResult)).to.equal('line\r\n<del>old value \r\n</del><ins>new value\r\n</ins>line');
});
it('should handle empty lines', function() {
const diffResult = diffLines(
'line\n\nold value \n\nline',
'line\n\nnew value\n\nline');
expect(convertChangesToXML(diffResult)).to.equal('line\n\n<del>old value \n</del><ins>new value\n</ins>\nline');
});
it('should handle empty input', function() {
const diffResult = diffLines(
'line\n\nold value \n\nline',
'');
expect(convertChangesToXML(diffResult)).to.equal('<del>line\n\nold value \n\nline</del>');
});
it('Should prefer to do deletions before insertions, like Unix diff does', function() {
const diffResult = diffLines('a\nb\nc\nd\n', 'a\nc\nb\nd\n');
// There are two possible diffs with equal edit distance here; either we
// can delete the "b" and insert it again later, or we can insert a "c"
// before the "b" and then delete the original "c" later.
// For consistency with the convention of other diff tools, we want to
// prefer the diff where we delete and then later insert over the one
// where we insert and then later delete.
expect(convertChangesToXML(diffResult)).to.equal('a\n<del>b\n</del>c\n<ins>b\n</ins>d\n');
const diffResult2 = diffLines('a\nc\nb\nd\n', 'a\nb\nc\nd\n');
expect(convertChangesToXML(diffResult2)).to.equal('a\n<del>c\n</del>b\n<ins>c\n</ins>d\n');
});
describe('given options.maxEditLength', function() {
it('terminates early', function() {
const diffResult = diffLines(
'line\nold value\nline',
'line\nnew value\nline', { maxEditLength: 1 });
expect(diffResult).to.be.undefined;
});
it('terminates early - async', function(done) {
function callback(diffResult) {
expect(diffResult).to.be.undefined;
done();
}
diffLines(
'line\nold value\nline',
'line\nnew value\nline', { callback, maxEditLength: 1 });
});
});
});
// Trimmed Line Diff
describe('#TrimmedLineDiff', function() {
it('should diff lines', function() {
const diffResult = diffTrimmedLines(
'line\nold value\nline',
'line\nnew value\nline');
expect(convertChangesToXML(diffResult)).to.equal('line\n<del>old value\n</del><ins>new value\n</ins>line');
});
it('should the same lines in diff', function() {
const diffResult = diffTrimmedLines(
'line\nvalue\nline',
'line\nvalue\nline');
expect(convertChangesToXML(diffResult)).to.equal('line\nvalue\nline');
});
it('should ignore leading and trailing whitespace', function() {
const diffResult = diffTrimmedLines(
'line\nvalue \nline',
'line\nvalue\nline');
expect(convertChangesToXML(diffResult)).to.equal('line\nvalue\nline');
});
it('should handle windows line endings', function() {
const diffResult = diffTrimmedLines(
'line\r\nold value \r\nline',
'line\r\nnew value\r\nline');
expect(convertChangesToXML(diffResult)).to.equal('line\r\n<del>old value\r\n</del><ins>new value\r\n</ins>line');
});
});
describe('#diffLinesNL', function() {
expect(diffLines('restaurant', 'restaurant\n', {newlineIsToken: true})).to.eql([
{value: 'restaurant', count: 1},
{value: '\n', count: 1, added: true, removed: undefined}
]);
expect(diffLines('restaurant', 'restaurant\nhello', {newlineIsToken: true})).to.eql([
{value: 'restaurant', count: 1},
{value: '\nhello', count: 2, added: true, removed: undefined}
]);
});
describe('Strip trailing CR', function() {
expect(diffLines('line\nline', 'line\r\nline', {stripTrailingCr: true})).to.eql([
{value: 'line\nline', count: 2}
]);
});
});