const Diff2Html = require("../diff2html.js").Diff2Html; const diffExample1 = "diff --git a/sample b/sample\n" + "index 0000001..0ddf2ba\n" + "--- a/sample\n" + "+++ b/sample\n" + "@@ -1 +1 @@\n" + "-test\n" + "+test1\n"; const jsonExample1 = [ { blocks: [ { lines: [ { content: "-test", type: "d2h-del", oldNumber: 1, newNumber: null }, { content: "+test1", 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 filesExample1 = '
\n' + '
\n' + ' Files changed (1)\n' + ' hide\n' + ' show\n' + "
\n" + '
    \n' + '
  1. \n' + ' \n' + ' sample\n' + ' \n' + ' +1\n' + ' -1\n' + " \n" + " \n" + "
  2. \n" + "
\n" + "
"; const htmlLineExample1 = '
\n' + '
\n' + '
\n' + ' \n' + ' sample\n' + ' CHANGED\n' + "
\n" + '
\n' + '
\n' + ' \n' + ' \n' + " \n" + ' \n' + ' \n" + "\n" + ' \n" + ' \n" + "\n" + ' \n" + ' \n" + "\n" + " \n" + "
\n' + '
@@ -1 +1 @@
\n' + "
\n' + '
1
\n' + '
\n' + "
\n' + '
\n' + ' -\n' + ' test\n' + "
\n" + "
\n' + '
\n' + '
1
\n' + "
\n' + '
\n' + ' +\n' + ' test1\n' + "
\n" + "
\n" + "
\n" + "
\n" + "
\n" + "
"; const htmlLineExample1WithFilesSummary = filesExample1 + htmlLineExample1; const htmlSideExample1 = '
\n' + '
\n' + '
\n' + ' \n' + ' sample\n' + ' CHANGED\n' + "
\n" + '
\n' + '
\n' + '
\n' + ' \n' + ' \n' + " \n" + ' \n' + ' \n" + "\n" + ' \n" + ' \n" + "\n" + " \n" + "
\n' + '
@@ -1 +1 @@
\n' + "
\n' + " 1\n" + " \n' + '
\n' + ' -\n' + ' test\n' + "
\n" + "
\n" + "
\n" + "
\n" + '
\n' + '
\n' + ' \n' + ' \n' + " \n" + ' \n' + ' \n" + "\n" + ' \n" + ' \n" + "\n" + " \n" + "
\n' + '
\n' + "
\n' + " 1\n" + " \n' + '
\n' + ' +\n' + ' test1\n' + "
\n" + "
\n" + "
\n" + "
\n" + "
\n" + "
\n" + "
"; const htmlSideExample1WithFilesSummary = filesExample1 + htmlSideExample1; describe("Diff2Html", function() { describe("getJsonFromDiff", function() { it("should parse simple diff to json", function() { const diff = "diff --git a/sample b/sample\n" + "index 0000001..0ddf2ba\n" + "--- a/sample\n" + "+++ b/sample\n" + "@@ -1 +1 @@\n" + "-test\n" + "+test1\n"; const result = Diff2Html.getJsonFromDiff(diff); const file1 = result[0]; expect(1).toEqual(result.length); expect(1).toEqual(file1.addedLines); expect(1).toEqual(file1.deletedLines); expect("sample").toEqual(file1.oldName); expect("sample").toEqual(file1.newName); expect(1).toEqual(file1.blocks.length); }); // Test case for issue #49 it("should parse diff with added EOF", function() { const diff = "diff --git a/sample.scala b/sample.scala\n" + "index b583263..8b2fc3e 100644\n" + "--- a/b583263..8b2fc3e\n" + "+++ b/8b2fc3e\n" + "@@ -50,5 +50,7 @@ case class Response[+A](value: Option[A],\n" + " object ResponseErrorCode extends JsonEnumeration {\n" + " val NoError, ServiceError, JsonError,\n" + " InvalidPermissions, MissingPermissions, GenericError,\n" + "- TokenRevoked, MissingToken = Value\n" + "-}\n" + "\\ No newline at end of file\n" + "+ TokenRevoked, MissingToken,\n" + "+ IndexLock, RepositoryError, NotValidRepo, PullRequestNotMergeable, BranchError,\n" + "+ PluginError, CodeParserError, EngineError = Value\n" + "+}\n"; const result = Diff2Html.getJsonFromDiff(diff); expect(50).toEqual(result[0].blocks[0].lines[0].oldNumber); expect(50).toEqual(result[0].blocks[0].lines[0].newNumber); expect(51).toEqual(result[0].blocks[0].lines[1].oldNumber); expect(51).toEqual(result[0].blocks[0].lines[1].newNumber); expect(52).toEqual(result[0].blocks[0].lines[2].oldNumber); expect(52).toEqual(result[0].blocks[0].lines[2].newNumber); expect(53).toEqual(result[0].blocks[0].lines[3].oldNumber); expect(null).toEqual(result[0].blocks[0].lines[3].newNumber); expect(54).toEqual(result[0].blocks[0].lines[4].oldNumber); expect(null).toEqual(result[0].blocks[0].lines[4].newNumber); expect(null).toEqual(result[0].blocks[0].lines[5].oldNumber); expect(53).toEqual(result[0].blocks[0].lines[5].newNumber); expect(null).toEqual(result[0].blocks[0].lines[6].oldNumber); expect(54).toEqual(result[0].blocks[0].lines[6].newNumber); expect(null).toEqual(result[0].blocks[0].lines[7].oldNumber); expect(55).toEqual(result[0].blocks[0].lines[7].newNumber); expect(null).toEqual(result[0].blocks[0].lines[8].oldNumber); expect(56).toEqual(result[0].blocks[0].lines[8].newNumber); }); it("should generate pretty line by line html from diff", function() { const result = Diff2Html.getPrettyHtmlFromDiff(diffExample1); expect(htmlLineExample1).toEqual(result); }); it("should generate pretty line by line html from json", function() { const result = Diff2Html.getPrettyHtmlFromJson(jsonExample1); expect(htmlLineExample1).toEqual(result); }); it("should generate pretty diff with files summary", function() { const result = Diff2Html.getPrettyHtmlFromDiff(diffExample1, { showFiles: true }); expect(htmlLineExample1WithFilesSummary).toEqual(result); }); it("should generate pretty side by side html from diff", function() { const result = Diff2Html.getPrettySideBySideHtmlFromDiff(diffExample1); expect(htmlSideExample1).toEqual(result); }); it("should generate pretty side by side html from json", function() { const result = Diff2Html.getPrettySideBySideHtmlFromJson(jsonExample1); expect(htmlSideExample1).toEqual(result); }); it("should generate pretty side by side html from diff 2", function() { const result = Diff2Html.getPrettySideBySideHtmlFromDiff(diffExample1, { showFiles: true }); expect(htmlSideExample1WithFilesSummary).toEqual(result); }); it("should generate pretty side by side html from diff with html on headers", function() { const diffExample2 = "diff --git a/CHANGELOG.md b/CHANGELOG.md\n" + "index fc3e3f4..b486d10 100644\n" + "--- a/CHANGELOG.md\n" + "+++ b/CHANGELOG.md\n" + "@@ -1,7 +1,6 @@\n" + " # Change Log\n" + " All notable changes to this project will be documented in this file.\n" + " This project adheres to [Semantic Versioning](http://semver.org/).\n" + '-$a="
Use the following format for additions: ` - VERSION: [feature/patch (if applicable)] Short description of change. Links to relevant issues/PRs.`\n' + ' $a="
\n' + " $a=\"
- 1.1.9: Fix around ubuntu's inability to cache promises. [#877](https://github.com/FredrikNoren/ungit/pull/878)\n" + " - 1.1.8:\n" + "@@ -11,7 +10,7 @@ $a=\"
- 1.1.9: Fix around ubuntu's inability to cache promises. [#8\n" + " - 1.1.7:\n" + " - Fix diff flickering issue and optimization [#865](https://github.com/FredrikNoren/ungit/pull/865)\n" + " - Fix credential dialog issue [#864](https://github.com/FredrikNoren/ungit/pull/864)\n" + "- - Fix HEAD branch order when redraw [#858](https://github.com/FredrikNoren/ungit/issues/858)\n" + "+4 - Fix HEAD branch order when redraw [#858](https://github.com/FredrikNoren/ungit/issues/858)\n" + " - 1.1.6: Fix path auto complete [#861](https://github.com/FredrikNoren/ungit/issues/861)\n" + ' - 1.1.5: Update "Toggle all" button after commit or changing selected files [#859](https://github.com/FredrikNoren/ungit/issues/859)\n' + " - 1.1.4: [patch] Promise refactoring\n" + " \n"; const htmlExample2 = '
\n' + '
\n' + '
\n' + ' \n' + ' CHANGELOG.md\n' + ' CHANGED\n' + "
\n" + '
\n' + '
\n' + ' \n' + ' \n' + " \n" + ' \n' + ' \n" + "\n" + ' \n" + ' \n" + "\n" + ' \n" + ' \n" + "\n" + ' \n" + ' \n" + "\n" + ' \n" + ' \n" + "\n" + ' \n" + ' \n" + "\n" + ' \n" + ' \n" + "\n" + ' \n" + ' \n" + "\n" + "\n" + ' \n' + ' \n" + "\n" + ' \n" + ' \n" + "\n" + ' \n" + ' \n" + "\n" + ' \n" + ' \n" + "\n" + ' \n" + ' \n" + "\n" + ' \n" + ' \n" + "\n" + ' \n" + ' \n" + "\n" + ' \n" + ' \n" + "\n" + ' \n" + ' \n" + "\n" + ' \n" + ' \n" + "\n" + " \n" + "
\n' + '
@@ -1,7 +1,6 @@
\n' + "
\n' + '
1
\n' + '
1
\n' + "
\n' + '
\n' + '  \n' + ' # Change Log\n' + "
\n" + "
\n' + '
2
\n' + '
2
\n' + "
\n' + '
\n' + '  \n' + ' All notable changes to this project will be documented in this file.\n' + "
\n" + "
\n' + '
3
\n' + '
3
\n' + "
\n' + '
\n' + '  \n' + ' This project adheres to [Semantic Versioning](http://semver.org/).\n' + "
\n" + "
\n' + '
4
\n' + '
\n' + "
\n' + '
\n' + ' -\n' + ' $a="<table><tr><td>Use the following format for additions: ` - VERSION: [feature/patch (if applicable)] Short description of change. Links to relevant issues/PRs.`\n' + "
\n" + "
\n' + '
5
\n' + '
4
\n' + "
\n' + '
\n' + '  \n' + ' $a="<table><tr><td>\n' + "
\n" + "
\n' + '
6
\n' + '
5
\n' + "
\n' + '
\n' + '  \n' + ' $a="<table><tr><td>- 1.1.9: Fix around ubuntu's inability to cache promises. [#877](https://github.com/FredrikNoren/ungit/pull/878)\n' + "
\n" + "
\n' + '
7
\n' + '
6
\n' + "
\n' + '
\n' + '  \n' + ' - 1.1.8:\n' + "
\n" + "
\n' + '
@@ -11,7 +10,7 @@ $a="<table><tr><td>- 1.1.9: Fix around ubuntu's inability to cache promises. [#8
\n' + "
\n' + '
11
\n' + '
10
\n' + "
\n' + '
\n' + '  \n' + ' - 1.1.7:\n' + "
\n" + "
\n' + '
12
\n' + '
11
\n' + "
\n' + '
\n' + '  \n' + ' - Fix diff flickering issue and optimization [#865](https://github.com/FredrikNoren/ungit/pull/865)\n' + "
\n" + "
\n' + '
13
\n' + '
12
\n' + "
\n' + '
\n' + '  \n' + ' - Fix credential dialog issue [#864](https://github.com/FredrikNoren/ungit/pull/864)\n' + "
\n" + "
\n' + '
14
\n' + '
\n' + "
\n' + '
\n' + ' -\n' + ' - Fix HEAD branch order when redraw [#858](https://github.com/FredrikNoren/ungit/issues/858)\n' + "
\n" + "
\n' + '
\n' + '
13
\n' + "
\n' + '
\n' + ' +\n' + ' 4 - Fix HEAD branch order when redraw [#858](https://github.com/FredrikNoren/ungit/issues/858)\n' + "
\n" + "
\n' + '
15
\n' + '
14
\n' + "
\n' + '
\n' + '  \n' + ' - 1.1.6: Fix path auto complete [#861](https://github.com/FredrikNoren/ungit/issues/861)\n' + "
\n" + "
\n' + '
16
\n' + '
15
\n' + "
\n' + '
\n' + '  \n' + ' - 1.1.5: Update "Toggle all" button after commit or changing selected files [#859](https://github.com/FredrikNoren/ungit/issues/859)\n' + "
\n" + "
\n' + '
17
\n' + '
16
\n' + "
\n' + '
\n' + '  \n' + ' - 1.1.4: [patch] Promise refactoring\n' + "
\n" + "
\n' + '
18
\n' + '
17
\n' + "
\n' + '
\n' + '  \n' + "
\n" + "
\n" + "
\n" + "
\n" + "
\n" + "
"; const result = Diff2Html.getPrettyHtmlFromDiff(diffExample2); expect(result).toEqual(htmlExample2); }); }); });