var assert = require('assert');
var LineByLinePrinter = require('../src/line-by-line-printer.js').LineByLinePrinter;
describe('LineByLinePrinter', function() {
describe('_generateEmptyDiff', function() {
it('should return an empty diff', function() {
var lineByLinePrinter = new LineByLinePrinter({});
var fileHtml = lineByLinePrinter._generateEmptyDiff();
var expected = '
\n' +
' \n' +
' \n' +
' File without changes\n' +
' \n' +
' | \n' +
'
';
assert.equal(expected, fileHtml);
});
});
describe('makeLineHtml', function() {
it('should work for insertions', function() {
var diffParser = require('../src/diff-parser.js').DiffParser;
var lineByLinePrinter = new LineByLinePrinter({});
var fileHtml = lineByLinePrinter.makeLineHtml(false,
diffParser.LINE_TYPE.INSERTS, '', 30, 'test', '+');
fileHtml = fileHtml.replace(/\n\n+/g, '\n');
var expected = '\n' +
' \n' +
' \n' +
' 30 \n' +
' | \n' +
' \n' +
' \n' +
' +\n' +
' test\n' +
' \n' +
' | \n' +
'
';
assert.equal(expected, fileHtml);
});
it('should work for deletions', function() {
var diffParser = require('../src/diff-parser.js').DiffParser;
var lineByLinePrinter = new LineByLinePrinter({});
var fileHtml = lineByLinePrinter.makeLineHtml(false,
diffParser.LINE_TYPE.DELETES, 30, '', 'test', '-');
fileHtml = fileHtml.replace(/\n\n+/g, '\n');
var expected = '\n' +
' \n' +
' 30 \n' +
'\n' +
' | \n' +
' \n' +
' \n' +
' -\n' +
' test\n' +
' \n' +
' | \n' +
'
';
assert.equal(expected, fileHtml);
});
it('should convert indents into non breakin spaces (2 white spaces)', function() {
var diffParser = require('../src/diff-parser.js').DiffParser;
var lineByLinePrinter = new LineByLinePrinter({});
var fileHtml = lineByLinePrinter.makeLineHtml(false,
diffParser.LINE_TYPE.INSERTS, '', 30, ' test', '+');
fileHtml = fileHtml.replace(/\n\n+/g, '\n');
var expected = '\n' +
' \n' +
' \n' +
' 30 \n' +
' | \n' +
' \n' +
' \n' +
' +\n' +
' test\n' +
' \n' +
' | \n' +
'
';
assert.equal(expected, fileHtml);
});
it('should convert indents into non breakin spaces (4 white spaces)', function() {
var diffParser = require('../src/diff-parser.js').DiffParser;
var lineByLinePrinter = new LineByLinePrinter({});
var fileHtml = lineByLinePrinter.makeLineHtml(false,
diffParser.LINE_TYPE.INSERTS, '', 30, ' test', '+');
fileHtml = fileHtml.replace(/\n\n+/g, '\n');
var expected = '\n' +
' \n' +
' \n' +
' 30 \n' +
' | \n' +
' \n' +
' \n' +
' +\n' +
' test\n' +
' \n' +
' | \n' +
'
';
assert.equal(expected, fileHtml);
});
it('should preserve tabs', function() {
var diffParser = require('../src/diff-parser.js').DiffParser;
var lineByLinePrinter = new LineByLinePrinter({});
var fileHtml = lineByLinePrinter.makeLineHtml(false,
diffParser.LINE_TYPE.INSERTS, '', 30, '\ttest', '+');
fileHtml = fileHtml.replace(/\n\n+/g, '\n');
var expected = '\n' +
' \n' +
' \n' +
'' +
' 30 \n' +
' | \n' +
' \n' +
' \n' +
' +\n' +
' \ttest\n' +
' \n' +
' | \n' +
'
';
assert.equal(expected, fileHtml);
});
});
describe('makeFileDiffHtml', function() {
it('should work for simple file', function() {
var lineByLinePrinter = new LineByLinePrinter({});
var file = {
addedLines: 12,
deletedLines: 41,
language: 'js',
oldName: 'my/file/name.js',
newName: 'my/file/name.js'
};
var diffs = 'Random Html';
var fileHtml = lineByLinePrinter.makeFileDiffHtml(file, diffs);
var expected =
'\n' +
' \n' +
'
\n' +
'
\n' +
'
\n' +
' \n' +
' Random Html\n' +
' \n' +
'
\n' +
'
\n' +
'
\n' +
'
';
assert.equal(expected, fileHtml);
});
it('should work for simple added file', function() {
var lineByLinePrinter = new LineByLinePrinter({});
var file = {
addedLines: 12,
deletedLines: 0,
language: 'js',
oldName: 'dev/null',
newName: 'my/file/name.js',
isNew: true
};
var diffs = 'Random Html';
var fileHtml = lineByLinePrinter.makeFileDiffHtml(file, diffs);
var expected =
'\n' +
' \n' +
'
\n' +
'
\n' +
'
\n' +
' \n' +
' Random Html\n' +
' \n' +
'
\n' +
'
\n' +
'
\n' +
'
';
assert.equal(expected, fileHtml);
});
it('should work for simple deleted file', function() {
var lineByLinePrinter = new LineByLinePrinter({});
var file = {
addedLines: 0,
deletedLines: 41,
language: 'js',
oldName: 'my/file/name.js',
newName: 'dev/null',
isDeleted: true
};
var diffs = 'Random Html';
var fileHtml = lineByLinePrinter.makeFileDiffHtml(file, diffs);
var expected =
'\n' +
' \n' +
'
\n' +
'
\n' +
'
\n' +
' \n' +
' Random Html\n' +
' \n' +
'
\n' +
'
\n' +
'
\n' +
'
';
assert.equal(expected, fileHtml);
});
it('should work for simple renamed file', function() {
var lineByLinePrinter = new LineByLinePrinter({});
var file = {
addedLines: 12,
deletedLines: 41,
language: 'js',
oldName: 'my/file/name1.js',
newName: 'my/file/name2.js',
isRename: true
};
var diffs = 'Random Html';
var fileHtml = lineByLinePrinter.makeFileDiffHtml(file, diffs);
var expected =
'\n' +
' \n' +
'
\n' +
'
\n' +
'
\n' +
' \n' +
' Random Html\n' +
' \n' +
'
\n' +
'
\n' +
'
\n' +
'
';
assert.equal(expected, fileHtml);
});
it('should return empty when option renderNothingWhenEmpty is true and file blocks not present', function() {
var lineByLinePrinter = new LineByLinePrinter({
renderNothingWhenEmpty: true
});
var file = {
blocks: []
};
var diffs = 'Random Html';
var fileHtml = lineByLinePrinter.makeFileDiffHtml(file, diffs);
var expected = '';
assert.equal(expected, fileHtml);
});
});
describe('makeLineByLineHtmlWrapper', function() {
it('should work for simple content', function() {
var lineByLinePrinter = new LineByLinePrinter({});
var fileHtml = lineByLinePrinter.makeLineByLineHtmlWrapper('Random Html');
var expected =
'\n' +
' Random Html\n' +
'
';
assert.equal(expected, fileHtml);
});
});
describe('generateLineByLineJsonHtml', function() {
it('should work for list of files', function() {
var exampleJson = [
{
blocks: [
{
lines: [
{
content: '-test',
type: 'd2h-del',
oldNumber: 1,
newNumber: null
},
{
content: '+test1r',
type: 'd2h-ins',
oldNumber: null,
newNumber: 1
}
],
oldStartLine: '1',
oldStartLine2: null,
newStartLine: '1',
header: '@@ -1 +1 @@'
}
],
deletedLines: 1,
addedLines: 1,
checksumBefore: '0000001',
checksumAfter: '0ddf2ba',
oldName: 'sample',
language: undefined,
newName: 'sample',
isCombined: false
}
];
var lineByLinePrinter = new LineByLinePrinter({matching: 'lines'});
var html = lineByLinePrinter.generateLineByLineJsonHtml(exampleJson);
var expected =
'\n' +
'
\n' +
' \n' +
'
\n' +
'
\n' +
'
\n' +
' \n' +
' \n' +
' | \n' +
' \n' +
' @@ -1 +1 @@ \n' +
' | \n' +
'
\n' +
' \n' +
' 1 \n' +
'\n' +
' | \n' +
' \n' +
' \n' +
' -\n' +
' test\n' +
' \n' +
' | \n' +
'
\n' +
' \n' +
' \n' +
' 1 \n' +
' | \n' +
' \n' +
' \n' +
' +\n' +
' test1r\n' +
' \n' +
' | \n' +
'
\n' +
' \n' +
'
\n' +
'
\n' +
'
\n' +
'
\n' +
'
';
assert.equal(expected, html);
});
it('should work for empty blocks', function() {
var exampleJson = [{
blocks: [],
deletedLines: 0,
addedLines: 0,
oldName: 'sample',
language: 'js',
newName: 'sample',
isCombined: false
}];
var lineByLinePrinter = new LineByLinePrinter({ renderNothingWhenEmpty: false });
var html = lineByLinePrinter.generateLineByLineJsonHtml(exampleJson);
var expected =
'\n' +
'
\n' +
' \n' +
'
\n' +
'
\n' +
'
\n' +
' \n' +
' \n' +
' \n' +
' \n' +
' File without changes\n' +
' \n' +
' | \n' +
'
\n' +
' \n' +
'
\n' +
'
\n' +
'
\n' +
'
\n' +
'
';
assert.equal(expected, html);
});
});
describe('_processLines', function() {
it('should work for simple block header', function() {
var lineByLinePrinter = new LineByLinePrinter({});
var oldLines = [{
content: '-test',
type: 'd2h-del',
oldNumber: 1,
newNumber: null
}];
var newLines = [{
content: '+test1r',
type: 'd2h-ins',
oldNumber: null,
newNumber: 1
}];
var html = lineByLinePrinter._processLines(false, oldLines, newLines);
var expected =
'\n' +
' \n' +
' 1 \n' +
'\n' +
' | \n' +
' \n' +
' \n' +
' -\n' +
' test\n' +
' \n' +
' | \n' +
'
\n' +
' \n' +
' \n' +
' 1 \n' +
' | \n' +
' \n' +
' \n' +
' +\n' +
' test1r\n' +
' \n' +
' | \n' +
'
';
assert.equal(expected, html);
});
});
describe('_generateFileHtml', function() {
it('should work for simple file', function() {
var lineByLinePrinter = new LineByLinePrinter({});
var file = {
blocks: [
{
lines: [
{
content: ' one context line',
type: 'd2h-cntx',
oldNumber: 1,
newNumber: 1
},
{
content: '-test',
type: 'd2h-del',
oldNumber: 2,
newNumber: null
},
{
content: '+test1r',
type: 'd2h-ins',
oldNumber: null,
newNumber: 2
},
{
content: '+test2r',
type: 'd2h-ins',
oldNumber: null,
newNumber: 3
}
],
oldStartLine: '1',
oldStartLine2: null,
newStartLine: '1',
header: '@@ -1 +1 @@'
}
],
deletedLines: 1,
addedLines: 1,
checksumBefore: '0000001',
checksumAfter: '0ddf2ba',
oldName: 'sample',
language: undefined,
newName: 'sample',
isCombined: false
};
var html = lineByLinePrinter._generateFileHtml(file);
var expected =
'\n' +
' | \n' +
' \n' +
' @@ -1 +1 @@ \n' +
' | \n' +
'
\n' +
' \n' +
' 1 \n' +
'1 \n' +
' | \n' +
' \n' +
' \n' +
' \n' +
' one context line\n' +
' \n' +
' | \n' +
'
\n' +
' \n' +
' 2 \n' +
'\n' +
' | \n' +
' \n' +
' \n' +
' -\n' +
' test\n' +
' \n' +
' | \n' +
'
\n' +
' \n' +
' \n' +
' 2 \n' +
' | \n' +
' \n' +
' \n' +
' +\n' +
' test1r\n' +
' \n' +
' | \n' +
'
\n' +
' \n' +
' \n' +
' 3 \n' +
' | \n' +
' \n' +
' \n' +
' +\n' +
' test2r\n' +
' \n' +
' | \n' +
'
';
assert.equal(expected, html);
});
});
});