forked from kpdecker/jsdiff
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathline.js
126 lines (112 loc) · 4.54 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
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>');
});
});
// 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 diffResult1 = diffTrimmedLines(
'line\nvalue \nline',
'line\nvalue\nline');
expect(convertChangesToXML(diffResult1)).to.equal('line\nvalue\nline');
const diffResult2 = diffTrimmedLines(
'line\nvalue\nline',
'line\nvalue \nline');
expect(convertChangesToXML(diffResult2)).to.equal('line\nvalue \nline');
const diffResult3 = diffTrimmedLines(
'line\n value\nline',
'line\nvalue\nline');
expect(convertChangesToXML(diffResult3)).to.equal('line\nvalue\nline');
const diffResult4 = diffTrimmedLines(
'line\nvalue\nline',
'line\n value\nline');
expect(convertChangesToXML(diffResult4)).to.equal('line\n value\nline');
});
it('should keep leading and trailing whitespace in the output', function() {
function stringify(value) {
return JSON.stringify(value, null, 2);
}
const diffResult = diffTrimmedLines(
stringify([10, 20, 30]),
stringify({ data: [10, 42, 30] }));
expect(convertChangesToXML(diffResult)).to.equal([
'<del>[\n</del>',
'<ins>{\n',
' "data": [\n</ins>',
' 10,\n',
'<del> 20,\n</del>',
'<ins> 42,\n</ins>',
' 30\n',
'<del>]</del><ins> ]\n',
'}</ins>'
].join('').replace(/"/g, '"'));
});
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}
]);
});
});