import { parse, html } from '../diff2html'; import { DiffFile, LineType, OutputFormatType } from '../types'; 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, }, ]; 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); expect(result).toMatchInlineSnapshot(` Array [ Object { "addedLines": 1, "blocks": Array [ Object { "header": "@@ -1 +1 @@", "lines": Array [ Object { "content": "-test", "newNumber": undefined, "oldNumber": 1, "type": "delete", }, Object { "content": "+test1", "newNumber": 1, "oldNumber": undefined, "type": "insert", }, ], "newStartLine": 1, "oldStartLine": 1, "oldStartLine2": null, }, ], "checksumAfter": "0ddf2ba", "checksumBefore": "0000001", "deletedLines": 1, "isCombined": false, "isGitDiff": true, "language": undefined, "newName": "sample", "oldName": "sample", }, ] `); }); // 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).toMatchInlineSnapshot(` Array [ Object { "addedLines": 4, "blocks": Array [ Object { "header": "@@ -50,5 +50,7 @@ case class Response[+A](value: Option[A],", "lines": Array [ Object { "content": " object ResponseErrorCode extends JsonEnumeration {", "newNumber": 50, "oldNumber": 50, "type": "context", }, Object { "content": " val NoError, ServiceError, JsonError,", "newNumber": 51, "oldNumber": 51, "type": "context", }, Object { "content": " InvalidPermissions, MissingPermissions, GenericError,", "newNumber": 52, "oldNumber": 52, "type": "context", }, Object { "content": "- TokenRevoked, MissingToken = Value", "newNumber": undefined, "oldNumber": 53, "type": "delete", }, Object { "content": "-}", "newNumber": undefined, "oldNumber": 54, "type": "delete", }, Object { "content": "+ TokenRevoked, MissingToken,", "newNumber": 53, "oldNumber": undefined, "type": "insert", }, Object { "content": "+ IndexLock, RepositoryError, NotValidRepo, PullRequestNotMergeable, BranchError,", "newNumber": 54, "oldNumber": undefined, "type": "insert", }, Object { "content": "+ PluginError, CodeParserError, EngineError = Value", "newNumber": 55, "oldNumber": undefined, "type": "insert", }, Object { "content": "+}", "newNumber": 56, "oldNumber": undefined, "type": "insert", }, ], "newStartLine": 50, "oldStartLine": 50, "oldStartLine2": null, }, ], "checksumAfter": "8b2fc3e", "checksumBefore": "b583263", "deletedLines": 2, "isCombined": false, "isGitDiff": true, "language": "8b2fc3e", "mode": "100644", "newName": "8b2fc3e", "oldName": "b583263..8b2fc3e", }, ] `); }); it('should generate pretty line by line html from diff', () => { const result = html(diffExample1, { drawFileList: false }); expect(result).toMatchInlineSnapshot(` "
sample CHANGED
@@ -1 +1 @@
1
- test
1
+ test1
" `); }); it('should generate pretty line by line html from json', () => { const result = html(jsonExample1, { drawFileList: false }); expect(result).toMatchInlineSnapshot(` "
sample CHANGED
@@ -1 +1 @@
1
- test
1
+ test1
" `); }); it('should generate pretty diff with files summary', () => { const result = html(diffExample1, { drawFileList: true }); expect(result).toMatchInlineSnapshot(` "
Files changed (1) hide show
  1. sample +1 -1
sample CHANGED
@@ -1 +1 @@
1
- test
1
+ test1
" `); }); it('should generate pretty side by side html from diff', () => { const result = html(diffExample1, { outputFormat: OutputFormatType.SIDE_BY_SIDE, drawFileList: false }); expect(result).toMatchInlineSnapshot(` "
sample CHANGED
@@ -1 +1 @@
1
- test
1
+ test1
" `); }); it('should generate pretty side by side html from json', () => { const result = html(jsonExample1, { outputFormat: OutputFormatType.SIDE_BY_SIDE, drawFileList: false }); expect(result).toMatchInlineSnapshot(` "
sample CHANGED
@@ -1 +1 @@
1
- test
1
+ test1
" `); }); it('should generate pretty side by side html from diff 2', () => { const result = html(diffExample1, { outputFormat: OutputFormatType.SIDE_BY_SIDE, drawFileList: true }); expect(result).toMatchInlineSnapshot(` "
Files changed (1) hide show
  1. sample +1 -1
sample CHANGED
@@ -1 +1 @@
1
- test
1
+ test1
" `); }); 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="<table><tr><td>- 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 result = html(diffExample2, { drawFileList: false }); expect(result).toMatchInlineSnapshot(` "
CHANGELOG.md CHANGED
@@ -1,7 +1,6 @@
1
1
  # Change Log
2
2
  All notable changes to this project will be documented in this file.
3
3
  This project adheres to [Semantic Versioning](http://semver.org/).
4
- $a="<table><tr><td>Use the following format for additions: \` - VERSION: [feature/patch (if applicable)] Short description of change. Links to relevant issues/PRs.\`
5
4
  $a="<table><tr><td>
6
5
  $a="<table><tr><td>- 1.1.9: Fix around ubuntu's inability to cache promises. [#877](https://github.com/FredrikNoren/ungit/pull/878)
7
6
  - 1.1.8:
@@ -11,7 +10,7 @@ $a="<table><tr><td>- 1.1.9: Fix around ubuntu's inability to cache promises. [#8
11
10
  - 1.1.7:
12
11
  - Fix diff flickering issue and optimization [#865](https://github.com/FredrikNoren/ungit/pull/865)
13
12
  - Fix credential dialog issue [#864](https://github.com/FredrikNoren/ungit/pull/864)
14
- - Fix HEAD branch order when redraw [#858](https://github.com/FredrikNoren/ungit/issues/858)
13
+ 4 - Fix HEAD branch order when redraw [#858](https://github.com/FredrikNoren/ungit/issues/858)
15
14
  - 1.1.6: Fix path auto complete [#861](https://github.com/FredrikNoren/ungit/issues/861)
16
15
  - 1.1.5: Update "Toggle all" button after commit or changing selected files [#859](https://github.com/FredrikNoren/ungit/issues/859)
17
16
  - 1.1.4: [patch] Promise refactoring
18
17
 
" `); }); }); });