Improve branch coverage

This commit is contained in:
Rodrigo Fernandes 2016-04-25 19:24:35 +01:00
parent 95322eb87c
commit f08137f8ba
No known key found for this signature in database
GPG key ID: 08E3C5F38969078E
10 changed files with 267 additions and 146 deletions

View file

@ -1,6 +1,6 @@
{
"name": "diff2html",
"version": "2.0.0-beta10",
"version": "2.0.0-beta15",
"homepage": "http://rtfpessoa.github.io/diff2html/",
"description": "Fast Diff to colorized HTML",
"keywords": [

25
dist/diff2html.js vendored
View file

@ -2758,7 +2758,7 @@ process.umask = function() { return 0; };
})();
},{"./printer-utils.js":28}],25:[function(require,module,exports){
(function (global,__dirname){
(function (__dirname){
/*
*
* Utils (hoganjs-utils.js)
@ -2776,7 +2776,6 @@ process.umask = function() { return 0; };
var hoganTemplates = require('./templates/diff2html-templates.js');
var templatesPath = path.resolve(__dirname, 'templates');
var templatesCache = {};
function HoganJsUtils() {
}
@ -2815,7 +2814,7 @@ process.umask = function() { return 0; };
var templatePath = path.join(templatesPath, templateKey);
var templateContent = fs.readFileSync(templatePath + '.mustache', 'utf8');
template = hogan.compile(templateContent);
templatesCache[templateKey] = template;
hoganTemplates[templateKey] = template;
}
} catch (e) {
console.error('Failed to read (template: ' + templateKey + ') from fs: ' + e.message);
@ -2825,9 +2824,7 @@ process.umask = function() { return 0; };
};
HoganJsUtils.prototype._readFromCache = function(templateKey) {
return global.browserTemplates && global.browserTemplates[templateKey] ||
hoganTemplates[templateKey] ||
templatesCache[templateKey];
return hoganTemplates[templateKey];
};
HoganJsUtils.prototype._templateKey = function(namespace, view) {
@ -2838,7 +2835,7 @@ process.umask = function() { return 0; };
})();
}).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {},"/src")
}).call(this,"/src")
},{"./templates/diff2html-templates.js":31,"fs":1,"hogan.js":18,"path":20}],26:[function(require,module,exports){
/*
*
@ -3084,10 +3081,6 @@ process.umask = function() { return 0; };
var i, chr, len;
var hash = 0;
if (text.length === 0) {
return hash;
}
for (i = 0, len = text.length; i < len; i++) {
chr = text.charCodeAt(i);
hash = ((hash << 5) - hash) + chr;
@ -3104,8 +3097,7 @@ process.umask = function() { return 0; };
var oldFilename = file.oldName;
var newFilename = file.newName;
if (oldFilename && newFilename && oldFilename !== newFilename &&
!isDevNullName(oldFilename) && !isDevNullName(newFilename)) {
if (oldFilename && newFilename && oldFilename !== newFilename && !isDevNullName(oldFilename) && !isDevNullName(newFilename)) {
return oldFilename + ' -> ' + newFilename;
} else if (newFilename && !isDevNullName(newFilename)) {
return newFilename;
@ -3226,13 +3218,6 @@ process.umask = function() { return 0; };
(function() {
var Rematch = {};
Rematch.arrayToString = function arrayToString(a) {
if (Object.prototype.toString.apply(a, []) === "[object Array]") {
return "[" + a.map(arrayToString).join(", ") + "]";
} else {
return a;
}
};
/*
Copyright (c) 2011 Andrei Mackenzie

File diff suppressed because one or more lines are too long

View file

@ -1,6 +1,6 @@
{
"name": "diff2html",
"version": "2.0.0-beta13",
"version": "2.0.0-beta15",
"homepage": "http://rtfpessoa.github.io/diff2html/",
"description": "Fast Diff to colorized HTML",
"keywords": [
@ -38,7 +38,7 @@
"scripts": {
"release": "./scripts/release.sh",
"templates": "./scripts/hulk.js --wrapper node --variable 'browserTemplates' ./src/templates/*.mustache > ./src/templates/diff2html-templates.js",
"test": "istanbul cover _mocha -- -u exports -R spec ./test/**/*",
"test": "jscs src/*.js src/ui/js/*.js && istanbul cover _mocha -- -u exports -R spec ./test/**/* && istanbul check-coverage --statements 90 --functions 90 --branches 85 --lines 90 ./coverage/coverage.json",
"style": "jscs src/*.js src/ui/js/*.js",
"codacy": "istanbul cover _mocha -- -u exports -R spec ./test/**/* && cat ./coverage/lcov.info | codacy-coverage"
},

View file

@ -15,7 +15,6 @@
var hoganTemplates = require('./templates/diff2html-templates.js');
var templatesPath = path.resolve(__dirname, 'templates');
var templatesCache = {};
function HoganJsUtils() {
}
@ -54,7 +53,7 @@
var templatePath = path.join(templatesPath, templateKey);
var templateContent = fs.readFileSync(templatePath + '.mustache', 'utf8');
template = hogan.compile(templateContent);
templatesCache[templateKey] = template;
hoganTemplates[templateKey] = template;
}
} catch (e) {
console.error('Failed to read (template: ' + templateKey + ') from fs: ' + e.message);
@ -64,9 +63,7 @@
};
HoganJsUtils.prototype._readFromCache = function(templateKey) {
return global.browserTemplates && global.browserTemplates[templateKey] ||
hoganTemplates[templateKey] ||
templatesCache[templateKey];
return hoganTemplates[templateKey];
};
HoganJsUtils.prototype._templateKey = function(namespace, view) {

View file

@ -19,10 +19,6 @@
var i, chr, len;
var hash = 0;
if (text.length === 0) {
return hash;
}
for (i = 0, len = text.length; i < len; i++) {
chr = text.charCodeAt(i);
hash = ((hash << 5) - hash) + chr;
@ -39,8 +35,7 @@
var oldFilename = file.oldName;
var newFilename = file.newName;
if (oldFilename && newFilename && oldFilename !== newFilename &&
!isDevNullName(oldFilename) && !isDevNullName(newFilename)) {
if (oldFilename && newFilename && oldFilename !== newFilename && !isDevNullName(oldFilename) && !isDevNullName(newFilename)) {
return oldFilename + ' -> ' + newFilename;
} else if (newFilename && !isDevNullName(newFilename)) {
return newFilename;

View file

@ -9,13 +9,6 @@
(function() {
var Rematch = {};
Rematch.arrayToString = function arrayToString(a) {
if (Object.prototype.toString.apply(a, []) === "[object Array]") {
return "[" + a.map(arrayToString).join(", ") + "]";
} else {
return a;
}
};
/*
Copyright (c) 2011 Andrei Mackenzie

View file

@ -15,7 +15,7 @@ describe('LineByLinePrinter', function() {
' File without changes\n' +
' </div>\n' +
' </td>\n' +
'</tr>';
'</tr>\n';
assert.equal(expected, fileHtml);
});
@ -282,21 +282,54 @@ describe('LineByLinePrinter', function() {
assert.equal(expected, html);
});
});
describe('makeColumnLineNumberHtml', function() {
it('should work for simple block header', function() {
var lineByLinePrinter = new LineByLinePrinter({});
var html = lineByLinePrinter.makeColumnLineNumberHtml({
header: '<span>So much html</span>'
});
it('should work for empty blocks', function() {
var exampleJson = [{
blocks: [],
deletedLines: 0,
addedLines: 0,
oldName: 'sample',
language: 'js',
newName: 'sample',
isCombined: false
}];
var lineByLinePrinter = new LineByLinePrinter();
var html = lineByLinePrinter.generateLineByLineJsonHtml(exampleJson);
var expected =
'<div class="d2h-wrapper">\n' +
' <div id="d2h-675094" class="d2h-file-wrapper" data-lang="js">\n' +
' <div class="d2h-file-header">\n' +
' <span class="d2h-file-stats">\n' +
' <span class="d2h-lines-added">\n' +
' <span>+0</span>\n' +
' </span>\n' +
' <span class="d2h-lines-deleted">\n' +
' <span>-0</span>\n' +
' </span>\n' +
' </span>\n' +
' <span class="d2h-file-name-wrapper">\n' +
' <span class="d2h-file-name">&nbsp;sample</span>\n' +
' </span>\n' +
' </div>\n' +
' <div class="d2h-file-diff">\n' +
' <div class="d2h-code-wrapper">\n' +
' <table class="d2h-diff-table">\n' +
' <tbody class="d2h-diff-tbody">\n' +
' <tr>\n' +
' <td class="d2h-code-linenumber d2h-info"></td>\n' +
' <td class="d2h-info">\n' +
' <div class="d2h-code-line d2h-info"></div>\n' +
' <div class="d2h-code-line d2h-info">\n' +
' File without changes\n' +
' </div>\n' +
' </td>\n' +
'</tr>';
'</tr>\n' +
'\n' +
' </tbody>\n' +
' </table>\n' +
' </div>\n' +
' </div>\n' +
'</div>\n' +
'</div>';
assert.equal(expected, html);
});
@ -353,16 +386,28 @@ describe('LineByLinePrinter', function() {
var file = {
blocks: [{
lines: [{
content: ' one context line',
type: 'd2h-cntx',
oldNumber: 1,
newNumber: 1
},
{
content: '-test',
type: 'd2h-del',
oldNumber: 1,
oldNumber: 2,
newNumber: null
},
{
content: '+test1r',
type: 'd2h-ins',
oldNumber: null,
newNumber: 1
newNumber: 2
},
{
content: '+test2r',
type: 'd2h-ins',
oldNumber: null,
newNumber: 3
}],
oldStartLine: '1',
oldStartLine2: null,
@ -388,8 +433,18 @@ describe('LineByLinePrinter', function() {
' <div class="d2h-code-line d2h-info"></div>\n' +
' </td>\n' +
'</tr><tr>\n' +
' <td class="d2h-code-linenumber d2h-del">\n' +
' <td class="d2h-code-linenumber d2h-cntx">\n' +
' <div class="line-num1">1</div>\n' +
' <div class="line-num2">1</div>\n' +
' </td>\n' +
' <td class="d2h-cntx">\n' +
' <div class="d2h-code-line d2h-cntx">\n' +
' <span class="d2h-code-line-ctn">&nbsp;one&nbsp;context&nbsp;line</span>\n' +
' </div>\n' +
' </td>\n' +
'</tr><tr>\n' +
' <td class="d2h-code-linenumber d2h-del">\n' +
' <div class="line-num1">2</div>\n' +
' <div class="line-num2"></div>\n' +
' </td>\n' +
' <td class="d2h-del">\n' +
@ -401,7 +456,7 @@ describe('LineByLinePrinter', function() {
'</tr><tr>\n' +
' <td class="d2h-code-linenumber d2h-ins">\n' +
' <div class="line-num1"></div>\n' +
' <div class="line-num2">1</div>\n' +
' <div class="line-num2">2</div>\n' +
' </td>\n' +
' <td class="d2h-ins">\n' +
' <div class="d2h-code-line d2h-ins">\n' +
@ -409,6 +464,16 @@ describe('LineByLinePrinter', function() {
' <span class="d2h-code-line-ctn"><ins>test1r</ins></span>\n' +
' </div>\n' +
' </td>\n' +
'</tr><tr>\n' +
' <td class="d2h-code-linenumber d2h-ins">\n' +
' <div class="line-num1"></div>\n' +
' <div class="line-num2">3</div>\n' +
' </td>\n' +
' <td class="d2h-ins">\n' +
' <div class="d2h-code-line d2h-ins">\n' +
' <span class="d2h-code-line-ctn">+test2r</span>\n' +
' </div>\n' +
' </td>\n' +
'</tr>';
assert.equal(expected, html);

View file

@ -11,6 +11,13 @@ describe('Utils', function() {
});
assert.equal('d2h-960013', result);
});
it('should generate file unique id for empty hashes', function() {
var result = PrinterUtils.getHtmlId({
oldName: 'sample.js',
newName: 'sample.js'
});
assert.equal('d2h-960013', result);
});
});
describe('getDiffName', function() {
@ -42,36 +49,68 @@ describe('Utils', function() {
});
assert.equal('src/my/file.js', result);
});
it('should generate handle undefined filenames', function() {
var result = PrinterUtils.getDiffName({});
assert.equal('Unknown filename', result);
});
});
describe('diffHighlight', function() {
it('should highlight two lines', function() {
var result = PrinterUtils.diffHighlight(
'var myVar = 2;',
'var myVariable = 3;',
'-var myVar = 2;',
'+var myVariable = 3;',
{matching: 'words'}
);
assert.deepEqual({
first: {prefix: 'v', line: 'ar <del>myVar</del> = <del>2</del>;'},
first: {
prefix: '-',
line: 'var <del>myVar</del> = <del>2</del>;'
},
second: {
prefix: 'v',
line: 'ar <ins>myVariable</ins> = <ins>3</ins>;'
prefix: '+',
line: 'var <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;',
'-var myVar = 2;',
'+var myVariable = 3;',
{charByChar: true}
);
assert.deepEqual({
first: {prefix: 'v', line: 'ar myVar = <del>2</del>;'},
first: {
prefix: '-',
line: 'var myVar = <del>2</del>;'
},
second: {
prefix: 'v',
line: 'ar myVar<ins>iable</ins> = <ins>3</ins>;'
prefix: '+',
line: 'var myVar<ins>iable</ins> = <ins>3</ins>;'
}
}, result);
});
it('should highlight combined diff lines', function() {
var result = PrinterUtils.diffHighlight(
' -var myVar = 2;',
' +var myVariable = 3;',
{
isCombined: true,
matching: 'words',
matchWordsThreshold: 1.00
}
);
assert.deepEqual({
first: {
prefix: ' -',
line: 'var <del class="d2h-change">myVar</del> = <del class="d2h-change">2</del>;'
},
second: {
prefix: ' +',
line: 'var <ins class="d2h-change">myVariable</ins> = <ins class="d2h-change">3</ins>;'
}
}, result);
});

View file

@ -28,17 +28,35 @@ describe('SideBySidePrinter', function() {
var file = {
"blocks": [{
"lines": [{
"lines": [
{
"content": " context",
"type": "d2h-cntx",
"oldNumber": 19,
"newNumber": 19
}, {"content": "-removed", "type": "d2h-del", "oldNumber": 20, "newNumber": null}, {
},
{
"content": "-removed",
"type": "d2h-del",
"oldNumber": 20,
"newNumber": null
},
{
"content": "+added",
"type": "d2h-ins",
"oldNumber": null,
"newNumber": 20
}], "oldStartLine": "19", "newStartLine": "19", "header": "@@ -19,7 +19,7 @@"
},
{
"content": "+another added",
"type": "d2h-ins",
"oldNumber": null,
"newNumber": 21
}
],
"oldStartLine": "19",
"newStartLine": "19",
"header": "@@ -19,7 +19,7 @@"
}],
"deletedLines": 1,
"addedLines": 1,
@ -53,71 +71,48 @@ describe('SideBySidePrinter', function() {
var fileHtml = sideBySidePrinter.generateSideBySideFileHtml(file);
var expectedRight = '<tr>' +
' <td class="d2h-code-side-linenumber d2h-info"></td>' +
' <td class="d2h-info">' +
' <div class="d2h-code-side-line d2h-info"></div>' +
' </td>' +
'</tr>' +
'<tr>' +
' <td class="d2h-code-side-linenumber d2h-cntx">19</td>' +
' <td class="d2h-cntx">' +
' <div class="d2h-code-side-line d2h-cntx">' +
' <span class="d2h-code-line-prefix"> </span>' +
' <span class="d2h-code-line-ctn">context</span>' +
' </div>' +
' </td>' +
'</tr>' +
'<tr>' +
' <td class="d2h-code-side-linenumber d2h-ins">20</td>' +
' <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"><ins>added</ins></span>' +
' </div>' +
' </td>' +
'</tr>';
var expectedLeft =
'<tr>\n' +
' <td class="d2h-code-side-linenumber d2h-info"></td>\n' +
' <td class="d2h-info">\n' +
' <div class="d2h-code-side-line d2h-info">@@ -19,7 +19,7 @@</div>\n' +
' </td>\n' +
'</tr>\n' +
'<tr>\n' +
' <td class="d2h-code-side-linenumber d2h-cntx">19</td>\n' +
' <td class="d2h-cntx"> <div class="d2h-code-side-line d2h-cntx"><span class="d2h-code-line-prefix"> </span><span class="d2h-code-line-ctn">context</span></div> </td>\n' +
' </tr>\n' +
'<tr>\n' +
' <td class="d2h-code-side-linenumber d2h-del">20</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"><del>removed</del></span></div> </td>\n' +
' </tr>\n' +
'<tr>\n' +
' <td class="d2h-code-side-linenumber d2h-cntx"></td>\n' +
' <td class="d2h-cntx"> <div class="d2h-code-side-line d2h-cntx"></div> </td>\n' +
' </tr>\n';
var expectedLeft = '<tr>' +
' <td class="d2h-code-side-linenumber d2h-info"></td>' +
' <td class="d2h-info">' +
' <div class="d2h-code-side-line d2h-info"> @@ -19,7 +19,7 @@</div>' +
' </td>' +
'</tr>' +
'<tr>' +
' <td class="d2h-code-side-linenumber d2h-cntx">19</td>' +
' <td class="d2h-cntx">' +
' <div class="d2h-code-side-line d2h-cntx">' +
' <span class="d2h-code-line-prefix"> </span>' +
' <span class="d2h-code-line-ctn">context</span>' +
' </div>' +
' </td>' +
'</tr>' +
'<tr>' +
' <td class="d2h-code-side-linenumber d2h-del">20</td>' +
' <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"><del>removed</del></span>' +
' </div>' +
' </td>' +
'</tr>';
var expectedRight =
'<tr>\n' +
' <td class="d2h-code-side-linenumber d2h-info"></td>\n' +
' <td class="d2h-info">\n' +
' <div class="d2h-code-side-line d2h-info"></div>\n' +
' </td>\n' +
'</tr>\n' +
'<tr>\n' +
' <td class="d2h-code-side-linenumber d2h-cntx">19</td>\n' +
' <td class="d2h-cntx"> <div class="d2h-code-side-line d2h-cntx"><span class="d2h-code-line-prefix"> </span><span class="d2h-code-line-ctn">context</span></div> </td>\n' +
' </tr>\n' +
'<tr>\n' +
' <td class="d2h-code-side-linenumber d2h-ins">20</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"><ins>added</ins></span></div> </td>\n' +
' </tr>\n' +
'<tr>\n' +
' <td class="d2h-code-side-linenumber d2h-ins">21</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">another added</span></div> </td>\n' +
' </tr>\n';
var HTMLParser = require('fast-html-parser');
var prefixTag = '.d2h-code-line-prefix';
var parsedExpectedRight = HTMLParser.parse(expectedRight);
var parsedFileRight = HTMLParser.parse(fileHtml.right);
assert.equal(parsedExpectedRight.querySelectorAll(prefixTag).length > 0, true);
assert.equal(parsedExpectedRight.querySelectorAll(prefixTag).length,
parsedFileRight.querySelectorAll(prefixTag).length);
var parsedExpectedLeft = HTMLParser.parse(expectedLeft);
var parsedFileLeft = HTMLParser.parse(fileHtml.left);
assert.equal(parsedExpectedLeft.querySelectorAll(prefixTag).length > 0, true);
assert.equal(parsedExpectedLeft.querySelectorAll(prefixTag).length,
parsedFileLeft.querySelectorAll(prefixTag).length);
assert.equal(expectedLeft, fileHtml.left);
assert.equal(expectedRight, fileHtml.right);
});
});
@ -246,6 +241,59 @@ describe('SideBySidePrinter', function() {
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 =
'<div class="d2h-wrapper">\n' +
'<div id="d2h-675094" class="d2h-file-wrapper" data-lang="js">\n' +
' <div class="d2h-file-header">\n' +
' <span class="d2h-file-stats">\n' +
' <span class="d2h-lines-added">\n' +
' <span>+undefined</span>\n' +
' </span>\n' +
' <span class="d2h-lines-deleted">\n' +
' <span>-undefined</span>\n' +
' </span>\n' +
' </span>\n' +
' <span class="d2h-file-name-wrapper">\n' +
' <span class="d2h-file-name">sample</span>\n' +
' </span>\n' +
' </div>\n' +
' <div class="d2h-files-diff">\n' +
' <div class="d2h-file-side-diff">\n' +
' <div class="d2h-code-wrapper">\n' +
' <table class="d2h-diff-table">\n' +
' <tbody class="d2h-diff-tbody">\n' +
' <tr>\n' +
' <td class="d2h-info"> <div class="d2h-code-side-line d2h-info">File without changes </div> </td>\n' +
'</tr>\n' +
' </tbody>\n' +
' </table>\n' +
' </div>\n' +
' </div>\n' +
' <div class="d2h-file-side-diff">\n' +
' <div class="d2h-code-wrapper">\n' +
' <table class="d2h-diff-table">\n' +
' <tbody class="d2h-diff-tbody">\n' +
' </tbody>\n' +
' </table>\n' +
' </div>\n' +
' </div>\n' +
' </div>\n' +
' </div>\n' +
'</div>\n';
assert.equal(expected, html);
});
});
describe('processLines', function() {