var assert = require('assert'); var SideBySidePrinter = require('../src/side-by-side-printer.js').SideBySidePrinter; describe('SideBySidePrinter', function() { describe('generateEmptyDiff', function() { it('should return an empty diff', function() { var sideBySidePrinter = new SideBySidePrinter({}); var fileHtml = sideBySidePrinter.generateEmptyDiff(); var expectedRight = ''; var expectedLeft = '\n' + ' \n' + '
\n' + ' File without changes\n' + '
\n' + ' \n' + ''; assert.equal(expectedRight, fileHtml.right); assert.equal(expectedLeft, fileHtml.left); }); }); describe('generateSideBySideFileHtml', function() { it('should generate lines with the right prefixes', function() { var sideBySidePrinter = new SideBySidePrinter({}); var file = { 'blocks': [ { 'lines': [ { 'content': ' context', 'type': 'd2h-cntx', 'oldNumber': 19, 'newNumber': 19 }, { 'content': '-removed', 'type': 'd2h-del', 'oldNumber': 20, 'newNumber': null }, { 'content': '+added', 'type': 'd2h-ins', 'oldNumber': null, 'newNumber': 20 }, { 'content': '+another added', 'type': 'd2h-ins', 'oldNumber': null, 'newNumber': 21 } ], 'oldStartLine': '19', 'newStartLine': '19', 'header': '@@ -19,7 +19,7 @@' } ], 'deletedLines': 1, 'addedLines': 1, 'checksumBefore': 'fc56817', 'checksumAfter': 'e8e7e49', 'mode': '100644', 'oldName': 'coverage.init', 'language': 'init', 'newName': 'coverage.init', 'isCombined': false }; var fileHtml = sideBySidePrinter.generateSideBySideFileHtml(file); var expectedLeft = '\n' + ' \n' + ' \n' + '
@@ -19,7 +19,7 @@
\n' + ' \n' + '\n' + ' \n' + ' 19\n' + ' \n' + ' \n' + '
\n' + '  \n' + ' context\n' + '
\n' + ' \n' + '\n' + ' \n' + ' 20\n' + ' \n' + ' \n' + '
\n' + ' -\n' + ' removed\n' + '
\n' + ' \n' + '\n' + ' \n' + ' ' + '\n' + ' \n' + ' \n' + '
\n' + '  \n' + '  \n' + '
\n' + ' \n' + ''; var expectedRight = '\n' + ' \n' + ' \n' + '
\n' + ' \n' + '\n' + ' \n' + ' 19\n' + ' \n' + ' \n' + '
\n' + '  \n' + ' context\n' + '
\n' + ' \n' + '\n' + ' \n' + ' 20\n' + ' \n' + ' \n' + '
\n' + ' +\n' + ' added\n' + '
\n' + ' \n' + '\n' + ' \n' + ' 21\n' + ' \n' + ' \n' + '
\n' + ' +\n' + ' another added\n' + '
\n' + ' \n' + ''; assert.equal(expectedLeft, fileHtml.left); assert.equal(expectedRight, fileHtml.right); }); }); describe('generateSingleLineHtml', function() { it('should work for insertions', function() { var diffParser = require('../src/diff-parser.js').DiffParser; var sideBySidePrinter = new SideBySidePrinter({}); var fileHtml = sideBySidePrinter.generateSingleLineHtml(false, diffParser.LINE_TYPE.INSERTS, 30, 'test', '+'); var expected = '\n' + ' \n' + ' 30\n' + ' \n' + ' \n' + '
\n' + ' +\n' + ' test\n' + '
\n' + ' \n' + ''; assert.equal(expected, fileHtml); }); it('should work for deletions', function() { var diffParser = require('../src/diff-parser.js').DiffParser; var sideBySidePrinter = new SideBySidePrinter({}); var fileHtml = sideBySidePrinter.generateSingleLineHtml(false, diffParser.LINE_TYPE.DELETES, 30, 'test', '-'); var expected = '\n' + ' \n' + ' 30\n' + ' \n' + ' \n' + '
\n' + ' -\n' + ' test\n' + '
\n' + ' \n' + ''; assert.equal(expected, fileHtml); }); }); describe('generateSideBySideJsonHtml', function() { it('should work for list of files', function() { var 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 } ]; var sideBySidePrinter = new SideBySidePrinter({matching: 'lines'}); var html = sideBySidePrinter.generateSideBySideJsonHtml(exampleJson); var expected = '
\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' + ' test1r\n' + '
\n' + '
\n' + '
\n' + '
\n' + '
\n' + '
\n' + '
'; assert.equal(expected, html); }); it('should work for files without blocks', function() { var exampleJson = [{ blocks: [], oldName: 'sample', language: 'js', newName: 'sample', isCombined: false }]; var sideBySidePrinter = new SideBySidePrinter(); var html = sideBySidePrinter.generateSideBySideJsonHtml(exampleJson); var expected = '
\n' + '
\n' + '
\n' + ' \n' + ' sample\n' + ' CHANGED\n' + '
\n' + '
\n' + '
\n' + '
\n' + ' \n' + ' \n' + ' \n' + ' \n' + '\n' + ' \n' + '
\n' + '
\n' + ' File without changes\n' + '
\n' + '
\n' + '
\n' + '
\n' + '
\n' + '
\n' + ' \n' + ' \n' + ' \n' + ' \n' + '
\n' + '
\n' + '
\n' + '
\n' + '
\n' + '
'; assert.equal(expected, html); }); }); describe('processLines', function() { it('should process file lines', function() { var oldLines = [{ content: '-test', type: 'd2h-del', oldNumber: 1, newNumber: null }]; var newLines = [{ content: '+test1r', type: 'd2h-ins', oldNumber: null, newNumber: 1 }]; var sideBySidePrinter = new SideBySidePrinter({matching: 'lines'}); var html = sideBySidePrinter.processLines(false, oldLines, newLines); var expectedLeft = '\n' + ' \n' + ' 1\n' + ' \n' + ' \n' + '
\n' + ' -\n' + ' test\n' + '
\n' + ' \n' + ''; var expectedRight = '\n' + ' \n' + ' 1\n' + ' \n' + ' \n' + '
\n' + ' +\n' + ' test1r\n' + '
\n' + ' \n' + ''; assert.equal(expectedLeft, html.left); assert.equal(expectedRight, html.right); }); }); });