var assert = require('assert'); var Diff2Html = require('../src/diff2html.js').Diff2Html; var 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'; var 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 } ]; var 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' + '
'; var 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' + '
'; var htmlLineExample1WithFilesSummary = filesExample1 + htmlLineExample1; var 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' + '
'; var htmlSideExample1WithFilesSummary = filesExample1 + htmlSideExample1; describe('Diff2Html', function() { describe('getJsonFromDiff', function() { it('should parse simple diff to json', function() { var 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'; var result = Diff2Html.getJsonFromDiff(diff); var file1 = result[0]; assert.equal(1, result.length); assert.equal(1, file1.addedLines); assert.equal(1, file1.deletedLines); assert.equal('sample', file1.oldName); assert.equal('sample', file1.newName); assert.equal(1, file1.blocks.length); }); // Test case for issue #49 it('should parse diff with added EOF', function() { var 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'; var result = Diff2Html.getJsonFromDiff(diff); assert.equal(50, result[0].blocks[0].lines[0].oldNumber); assert.equal(50, result[0].blocks[0].lines[0].newNumber); assert.equal(51, result[0].blocks[0].lines[1].oldNumber); assert.equal(51, result[0].blocks[0].lines[1].newNumber); assert.equal(52, result[0].blocks[0].lines[2].oldNumber); assert.equal(52, result[0].blocks[0].lines[2].newNumber); assert.equal(53, result[0].blocks[0].lines[3].oldNumber); assert.equal(null, result[0].blocks[0].lines[3].newNumber); assert.equal(54, result[0].blocks[0].lines[4].oldNumber); assert.equal(null, result[0].blocks[0].lines[4].newNumber); assert.equal(null, result[0].blocks[0].lines[5].oldNumber); assert.equal(53, result[0].blocks[0].lines[5].newNumber); assert.equal(null, result[0].blocks[0].lines[6].oldNumber); assert.equal(54, result[0].blocks[0].lines[6].newNumber); assert.equal(null, result[0].blocks[0].lines[7].oldNumber); assert.equal(55, result[0].blocks[0].lines[7].newNumber); assert.equal(null, result[0].blocks[0].lines[8].oldNumber); assert.equal(56, result[0].blocks[0].lines[8].newNumber); }); it('should generate pretty line by line html from diff', function() { var result = Diff2Html.getPrettyHtmlFromDiff(diffExample1); assert.equal(htmlLineExample1, result); }); it('should generate pretty line by line html from json', function() { var result = Diff2Html.getPrettyHtmlFromJson(jsonExample1); assert.equal(htmlLineExample1, result); }); it('should generate pretty diff with files summary', function() { var result = Diff2Html.getPrettyHtmlFromDiff(diffExample1, {showFiles: true}); assert.equal(htmlLineExample1WithFilesSummary, result); }); it('should generate pretty side by side html from diff', function() { var result = Diff2Html.getPrettySideBySideHtmlFromDiff(diffExample1); assert.equal(htmlSideExample1, result); }); it('should generate pretty side by side html from json', function() { var result = Diff2Html.getPrettySideBySideHtmlFromJson(jsonExample1); assert.equal(htmlSideExample1, result); }); it('should generate pretty side by side html from diff', function() { var result = Diff2Html.getPrettySideBySideHtmlFromDiff(diffExample1, {showFiles: true}); assert.equal(htmlSideExample1WithFilesSummary, result); }); it('should generate pretty side by side html from diff with html on headers', function() { var 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'; var 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' + '
'; var result = Diff2Html.getPrettyHtmlFromDiff(diffExample2); assert.equal(result, htmlExample2); }); }); });