import { parse, html } from "../diff2html"; import { DiffFile, LineType } from "../render-utils"; 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: DiffFile[] = [ { blocks: [ { lines: [ { content: "-test", type: LineType.DELETE, oldNumber: 1, newNumber: undefined }, { content: "+test1", type: LineType.INSERT, oldNumber: undefined, newNumber: 1 } ], oldStartLine: 1, oldStartLine2: undefined, newStartLine: 1, header: "@@ -1 +1 @@" } ], deletedLines: 1, addedLines: 1, checksumBefore: "0000001", checksumAfter: "0ddf2ba", oldName: "sample", newName: "sample", language: "", isCombined: false, isGitDiff: true } ]; 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", () => { describe("getJsonFromDiff", () => { it("should parse simple diff to json", () => { 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 = parse(diff); const file1 = result[0]; expect(result.length).toEqual(1); expect(file1.addedLines).toEqual(1); expect(file1.deletedLines).toEqual(1); expect(file1.oldName).toEqual("sample"); expect(file1.newName).toEqual("sample"); expect(file1.blocks.length).toEqual(1); }); // Test case for issue #49 it("should parse diff with added EOF", () => { 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 = parse(diff); expect(result[0].blocks[0].lines[0].oldNumber).toEqual(50); expect(result[0].blocks[0].lines[0].newNumber).toEqual(50); expect(result[0].blocks[0].lines[1].oldNumber).toEqual(51); expect(result[0].blocks[0].lines[1].newNumber).toEqual(51); expect(result[0].blocks[0].lines[2].oldNumber).toEqual(52); expect(result[0].blocks[0].lines[2].newNumber).toEqual(52); expect(result[0].blocks[0].lines[3].oldNumber).toEqual(53); expect(result[0].blocks[0].lines[3].newNumber).toBeUndefined(); expect(result[0].blocks[0].lines[4].oldNumber).toEqual(54); expect(result[0].blocks[0].lines[4].newNumber).toBeUndefined(); expect(result[0].blocks[0].lines[5].oldNumber).toBeUndefined(); expect(result[0].blocks[0].lines[5].newNumber).toEqual(53); expect(result[0].blocks[0].lines[6].oldNumber).toBeUndefined(); expect(result[0].blocks[0].lines[6].newNumber).toEqual(54); expect(result[0].blocks[0].lines[7].oldNumber).toBeUndefined(); expect(result[0].blocks[0].lines[7].newNumber).toEqual(55); expect(result[0].blocks[0].lines[8].oldNumber).toBeUndefined(); expect(result[0].blocks[0].lines[8].newNumber).toEqual(56); }); it("should generate pretty line by line html from diff", () => { const result = html(diffExample1); expect(result).toEqual(htmlLineExample1); }); it("should generate pretty line by line html from json", () => { const result = html(jsonExample1); expect(result).toEqual(htmlLineExample1); }); it("should generate pretty diff with files summary", () => { const result = html(diffExample1, { showFiles: true }); expect(result).toEqual(htmlLineExample1WithFilesSummary); }); it("should generate pretty side by side html from diff", () => { const result = html(diffExample1, { outputFormat: "side-by-side" }); expect(result).toEqual(htmlSideExample1); }); it("should generate pretty side by side html from json", () => { const result = html(jsonExample1, { outputFormat: "side-by-side" }); expect(result).toEqual(htmlSideExample1); }); it("should generate pretty side by side html from diff 2", () => { const result = html(diffExample1, { outputFormat: "side-by-side", showFiles: true }); expect(result).toEqual(htmlSideExample1WithFilesSummary); }); it("should generate pretty side by side html from diff with html on headers", () => { 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 = html(diffExample2); expect(htmlExample2).toEqual(result); }); }); });