255 lines
8.5 KiB
HTML
255 lines
8.5 KiB
HTML
<!DOCTYPE html>
|
||
<!--
|
||
Copyright (C) 2016 The Android Open Source Project
|
||
|
||
Licensed under the Apache License, Version 2.0 (the "License");
|
||
you may not use this file except in compliance with the License.
|
||
You may obtain a copy of the License at
|
||
|
||
http://www.apache.org/licenses/LICENSE-2.0
|
||
|
||
Unless required by applicable law or agreed to in writing, software
|
||
distributed under the License is distributed on an "AS IS" BASIS,
|
||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||
See the License for the specific language governing permissions and
|
||
limitations under the License.
|
||
-->
|
||
|
||
<meta name="viewport" content="width=device-width, minimum-scale=1.0, initial-scale=1.0, user-scalable=yes">
|
||
<title>gr-diff-builder</title>
|
||
|
||
<script src="../../../bower_components/web-component-tester/browser.js"></script>
|
||
<script src="gr-diff-line.js"></script>
|
||
<script src="gr-diff-group.js"></script>
|
||
<script src="gr-diff-builder.js"></script>
|
||
|
||
<script>
|
||
suite('gr-diff-builder tests', function() {
|
||
|
||
test('process loaded content', function() {
|
||
var content = [
|
||
{
|
||
ab: [
|
||
'<!DOCTYPE html>',
|
||
'<meta charset="utf-8">',
|
||
]
|
||
},
|
||
{
|
||
a: [
|
||
' Welcome ',
|
||
' to the wooorld of tomorrow!',
|
||
],
|
||
b: [
|
||
' Hello, world!',
|
||
],
|
||
},
|
||
{
|
||
ab: [
|
||
'Leela: This is the only place the ship can’t hear us, so ',
|
||
'everyone pretend to shower.',
|
||
'Fry: Same as every day. Got it.',
|
||
]
|
||
},
|
||
];
|
||
var groups = [];
|
||
GrDiffBuilder.prototype._processContent(content, groups, -1);
|
||
|
||
assert.equal(groups.length, 3);
|
||
|
||
var group = groups[0];
|
||
assert.equal(group.type, GrDiffGroup.Type.BOTH);
|
||
assert.equal(group.lines.length, 2);
|
||
assert.equal(group.lines.length, 2);
|
||
|
||
function beforeNumberFn(l) { return l.beforeNumber; }
|
||
function afterNumberFn(l) { return l.afterNumber; }
|
||
function textFn(l) { return l.text; }
|
||
|
||
assert.deepEqual(group.lines.map(beforeNumberFn), [1, 2]);
|
||
assert.deepEqual(group.lines.map(afterNumberFn), [1, 2]);
|
||
assert.deepEqual(group.lines.map(textFn), [
|
||
'<!DOCTYPE html>',
|
||
'<meta charset="utf-8">',
|
||
]);
|
||
|
||
group = groups[1];
|
||
assert.equal(group.type, GrDiffGroup.Type.DELTA);
|
||
assert.equal(group.lines.length, 3);
|
||
assert.equal(group.adds.length, 1);
|
||
assert.equal(group.removes.length, 2);
|
||
assert.deepEqual(group.removes.map(beforeNumberFn), [3, 4]);
|
||
assert.deepEqual(group.adds.map(afterNumberFn), [3]);
|
||
assert.deepEqual(group.removes.map(textFn), [
|
||
' Welcome ',
|
||
' to the wooorld of tomorrow!',
|
||
]);
|
||
assert.deepEqual(group.adds.map(textFn), [
|
||
' Hello, world!',
|
||
]);
|
||
|
||
group = groups[2];
|
||
assert.equal(group.type, GrDiffGroup.Type.BOTH);
|
||
assert.equal(group.lines.length, 3);
|
||
assert.deepEqual(group.lines.map(beforeNumberFn), [5, 6, 7]);
|
||
assert.deepEqual(group.lines.map(afterNumberFn), [4, 5, 6]);
|
||
assert.deepEqual(group.lines.map(textFn), [
|
||
'Leela: This is the only place the ship can’t hear us, so ',
|
||
'everyone pretend to shower.',
|
||
'Fry: Same as every day. Got it.',
|
||
]);
|
||
});
|
||
|
||
test('insert context groups', function() {
|
||
var content = [
|
||
{ab: []},
|
||
{a: ['all work and no play make andybons a dull boy']},
|
||
{ab: []},
|
||
{b: ['elgoog elgoog elgoog']},
|
||
{ab: []},
|
||
];
|
||
for (var i = 0; i < 100; i++) {
|
||
content[0].ab.push('all work and no play make jack a dull boy');
|
||
content[4].ab.push('all work and no play make jill a dull girl');
|
||
}
|
||
for (var i = 0; i < 5; i++) {
|
||
content[2].ab.push('no tv and no beer make homer go crazy');
|
||
}
|
||
var groups = [];
|
||
var context = 10;
|
||
|
||
GrDiffBuilder.prototype._processContent(content, groups, context);
|
||
|
||
assert.equal(groups[0].type, GrDiffGroup.Type.CONTEXT_CONTROL);
|
||
assert.equal(groups[0].lines[0].contextLines.length, 90);
|
||
groups[0].lines[0].contextLines.forEach(function(l) {
|
||
assert.equal(l.text, content[0].ab[0]);
|
||
});
|
||
|
||
assert.equal(groups[1].type, GrDiffGroup.Type.BOTH);
|
||
assert.equal(groups[1].lines.length, context);
|
||
groups[1].lines.forEach(function(l) {
|
||
assert.equal(l.text, content[0].ab[0]);
|
||
});
|
||
|
||
assert.equal(groups[2].type, GrDiffGroup.Type.DELTA);
|
||
assert.equal(groups[2].lines.length, 1);
|
||
assert.equal(groups[2].removes.length, 1);
|
||
assert.equal(groups[2].removes[0].text,
|
||
'all work and no play make andybons a dull boy');
|
||
|
||
assert.equal(groups[3].type, GrDiffGroup.Type.BOTH);
|
||
assert.equal(groups[3].lines.length, 5);
|
||
groups[3].lines.forEach(function(l) {
|
||
assert.equal(l.text, content[2].ab[0]);
|
||
});
|
||
|
||
assert.equal(groups[4].type, GrDiffGroup.Type.DELTA);
|
||
assert.equal(groups[4].lines.length, 1);
|
||
assert.equal(groups[4].adds.length, 1);
|
||
assert.equal(groups[4].adds[0].text, 'elgoog elgoog elgoog');
|
||
|
||
assert.equal(groups[5].type, GrDiffGroup.Type.BOTH);
|
||
assert.equal(groups[5].lines.length, context);
|
||
groups[5].lines.forEach(function(l) {
|
||
assert.equal(l.text, content[4].ab[0]);
|
||
});
|
||
|
||
assert.equal(groups[6].type, GrDiffGroup.Type.CONTEXT_CONTROL);
|
||
assert.equal(groups[6].lines[0].contextLines.length, 90);
|
||
groups[6].lines[0].contextLines.forEach(function(l) {
|
||
assert.equal(l.text, content[4].ab[0]);
|
||
});
|
||
|
||
content = [
|
||
{a: ['all work and no play make andybons a dull boy']},
|
||
{ab: []},
|
||
{b: ['elgoog elgoog elgoog']},
|
||
];
|
||
for (var i = 0; i < 50; i++) {
|
||
content[1].ab.push('no tv and no beer make homer go crazy');
|
||
}
|
||
groups = [];
|
||
|
||
GrDiffBuilder.prototype._processContent(content, groups, 10);
|
||
|
||
assert.equal(groups[0].type, GrDiffGroup.Type.DELTA);
|
||
assert.equal(groups[0].lines.length, 1);
|
||
assert.equal(groups[0].removes.length, 1);
|
||
assert.equal(groups[0].removes[0].text,
|
||
'all work and no play make andybons a dull boy');
|
||
|
||
assert.equal(groups[1].type, GrDiffGroup.Type.BOTH);
|
||
assert.equal(groups[1].lines.length, context);
|
||
groups[1].lines.forEach(function(l) {
|
||
assert.equal(l.text, content[1].ab[0]);
|
||
});
|
||
|
||
assert.equal(groups[2].type, GrDiffGroup.Type.CONTEXT_CONTROL);
|
||
assert.equal(groups[2].lines[0].contextLines.length, 30);
|
||
groups[2].lines[0].contextLines.forEach(function(l) {
|
||
assert.equal(l.text, content[1].ab[0]);
|
||
});
|
||
|
||
assert.equal(groups[3].type, GrDiffGroup.Type.BOTH);
|
||
assert.equal(groups[3].lines.length, context);
|
||
groups[3].lines.forEach(function(l) {
|
||
assert.equal(l.text, content[1].ab[0]);
|
||
});
|
||
|
||
assert.equal(groups[4].type, GrDiffGroup.Type.DELTA);
|
||
assert.equal(groups[4].lines.length, 1);
|
||
assert.equal(groups[4].adds.length, 1);
|
||
assert.equal(groups[4].adds[0].text, 'elgoog elgoog elgoog');
|
||
});
|
||
|
||
test('newlines', function() {
|
||
var prefs = {
|
||
line_length: 10,
|
||
tab_size: 4,
|
||
};
|
||
var builder = new GrDiffBuilder({content: []}, prefs);
|
||
|
||
var text = 'abcdef';
|
||
assert.equal(builder._addNewlines(text, text), text);
|
||
text = 'a'.repeat(20);
|
||
assert.equal(builder._addNewlines(text, text),
|
||
'a'.repeat(10) +
|
||
GrDiffBuilder.LINE_FEED_HTML +
|
||
'a'.repeat(10));
|
||
|
||
text = '<span class="thumbsup">👍</span>';
|
||
var html = '<span class="thumbsup">👍</span>';
|
||
assert.equal(builder._addNewlines(text, html),
|
||
'<span clas' +
|
||
GrDiffBuilder.LINE_FEED_HTML +
|
||
's="thumbsu' +
|
||
GrDiffBuilder.LINE_FEED_HTML +
|
||
'p">👍</spa' +
|
||
GrDiffBuilder.LINE_FEED_HTML +
|
||
'n>');
|
||
|
||
text = '01234\t56789';
|
||
assert.equal(builder._addNewlines(text, text),
|
||
'01234\t5' +
|
||
GrDiffBuilder.LINE_FEED_HTML +
|
||
'6789');
|
||
});
|
||
|
||
test('tab wrapper insertion', function() {
|
||
var prefs = {
|
||
show_tabs: true,
|
||
tab_size: 4,
|
||
};
|
||
var builder = new GrDiffBuilder({content: []}, prefs);
|
||
|
||
var html = 'abc\tdef';
|
||
var wrapper = builder._getTabWrapper(prefs.tab_size, prefs.show_tabs);
|
||
assert.equal(builder._addTabWrappers(html), 'abc' + wrapper + 'def');
|
||
assert.throws(builder._getTabWrapper.bind(
|
||
builder,
|
||
'"><img src="/" onerror="alert(1);"><span class="',
|
||
true));
|
||
});
|
||
});
|
||
</script>
|