From 67ba95e5df50c6edffcde092a01b616957ff2e9f Mon Sep 17 00:00:00 2001 From: Rodrigo Fernandes Date: Mon, 25 Apr 2016 17:33:43 +0100 Subject: [PATCH] Add more tests to utils --- test/printer-utils-tests.js | 79 ++++++++++++++++++++++++++++++ test/side-by-side-printer-tests.js | 44 +++++++++++++++++ 2 files changed, 123 insertions(+) create mode 100644 test/printer-utils-tests.js diff --git a/test/printer-utils-tests.js b/test/printer-utils-tests.js new file mode 100644 index 0000000..46b9f34 --- /dev/null +++ b/test/printer-utils-tests.js @@ -0,0 +1,79 @@ +var assert = require('assert'); + +var PrinterUtils = require('../src/printer-utils.js').PrinterUtils; + +describe('Utils', function() { + describe('getHtmlId', function() { + it('should generate file unique id', function() { + var result = PrinterUtils.getHtmlId({ + oldName: 'sample.js', + newName: 'sample.js' + }); + assert.equal('d2h-960013', result); + }); + }); + + describe('getDiffName', function() { + it('should generate the file name for a changed file', function() { + var result = PrinterUtils.getDiffName({ + oldName: 'sample.js', + newName: 'sample.js' + }); + assert.equal('sample.js', result); + }); + it('should generate the file name for a changed file and renamed', function() { + var result = PrinterUtils.getDiffName({ + oldName: 'sample1.js', + newName: 'sample2.js' + }); + assert.equal('sample1.js -> sample2.js', result); + }); + it('should generate the file name for a deleted file', function() { + var result = PrinterUtils.getDiffName({ + oldName: 'src/my/file.js', + newName: '/dev/null' + }); + assert.equal('src/my/file.js', result); + }); + it('should generate the file name for a new file', function() { + var result = PrinterUtils.getDiffName({ + oldName: '/dev/null', + newName: 'src/my/file.js' + }); + assert.equal('src/my/file.js', result); + }); + }); + + describe('diffHighlight', function() { + it('should highlight two lines', function() { + var result = PrinterUtils.diffHighlight( + 'var myVar = 2;', + 'var myVariable = 3;', + {matching: 'words'} + ); + + assert.deepEqual({ + first: {prefix: 'v', line: 'ar myVar = 2;'}, + second: { + prefix: 'v', + line: 'ar myVariable = 3;' + } + }, result); + }); + it('should highlight two lines char by char', function() { + var result = PrinterUtils.diffHighlight( + 'var myVar = 2;', + 'var myVariable = 3;', + {charByChar: true} + ); + + assert.deepEqual({ + first: {prefix: 'v', line: 'ar myVar = 2;'}, + second: { + prefix: 'v', + line: 'ar myVariable = 3;' + } + }, result); + }); + }); +}); diff --git a/test/side-by-side-printer-tests.js b/test/side-by-side-printer-tests.js index c0d71cb..b7cc190 100644 --- a/test/side-by-side-printer-tests.js +++ b/test/side-by-side-printer-tests.js @@ -247,4 +247,48 @@ describe('SideBySidePrinter', function() { 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(oldLines, newLines); + var expectedLeft = + '\n' + + ' 1\n' + + ' ' + + '
' + + '-' + + 'test
' + + ' \n' + + ' \n'; + + var expectedRight = + '\n' + + ' 1\n' + + ' ' + + '
' + + '+' + + 'test1r' + + '
' + + ' \n' + + ' \n'; + + assert.equal(expectedLeft, html.left); + assert.equal(expectedRight, html.right); + }); + }); });