Add more tests to utils

This commit is contained in:
Rodrigo Fernandes 2016-04-25 17:33:43 +01:00
parent 4785c032a5
commit 67ba95e5df
No known key found for this signature in database
GPG key ID: 08E3C5F38969078E
2 changed files with 123 additions and 0 deletions

View file

@ -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 <del>myVar</del> = <del>2</del>;'},
second: {
prefix: 'v',
line: 'ar <ins>myVariable</ins> = <ins>3</ins>;'
}
}, 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 = <del>2</del>;'},
second: {
prefix: 'v',
line: 'ar myVar<ins>iable</ins> = <ins>3</ins>;'
}
}, result);
});
});
});

View file

@ -247,4 +247,48 @@ describe('SideBySidePrinter', function() {
assert.equal(expected, html); 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 =
'<tr>\n' +
' <td class="d2h-code-side-linenumber d2h-del">1</td>\n' +
' <td class="d2h-del">' +
' <div class="d2h-code-side-line d2h-del">' +
'<span class="d2h-code-line-prefix">-</span>' +
'<span class="d2h-code-line-ctn">test</span></div>' +
' </td>\n' +
' </tr>\n';
var expectedRight =
'<tr>\n' +
' <td class="d2h-code-side-linenumber d2h-ins">1</td>\n' +
' <td class="d2h-ins">' +
' <div class="d2h-code-side-line d2h-ins">' +
'<span class="d2h-code-line-prefix">+</span>' +
'<span class="d2h-code-line-ctn">test1r</span>' +
'</div>' +
' </td>\n' +
' </tr>\n';
assert.equal(expectedLeft, html.left);
assert.equal(expectedRight, html.right);
});
});
}); });