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