Merge pull request #46 from rtfpessoa/test-html-prefixes
Test html line prefixes
This commit is contained in:
commit
8558bab3ca
8 changed files with 160 additions and 42 deletions
|
|
@ -6,9 +6,12 @@
|
|||
"else"
|
||||
],
|
||||
"disallowMixedSpacesAndTabs": true,
|
||||
"disallowMultipleVarDecl": "exceptUndefined",
|
||||
"disallowMultipleVarDecl": {
|
||||
"allExcept": [
|
||||
"undefined"
|
||||
]
|
||||
},
|
||||
"disallowNewlineBeforeBlockStatements": true,
|
||||
"disallowQuotedKeysInObjects": true,
|
||||
"disallowSpaceAfterObjectKeys": true,
|
||||
"disallowSpaceAfterPrefixUnaryOperators": true,
|
||||
"disallowSpacesInFunction": {
|
||||
|
|
@ -16,7 +19,7 @@
|
|||
},
|
||||
"disallowSpacesInsideParentheses": true,
|
||||
"disallowTrailingWhitespace": true,
|
||||
"maximumLineLength": 120,
|
||||
"maximumLineLength": 130,
|
||||
"requireCamelCaseOrUpperCaseIdentifiers": true,
|
||||
"requireCapitalizedComments": true,
|
||||
"requireCapitalizedConstructors": true,
|
||||
|
|
@ -44,6 +47,5 @@
|
|||
},
|
||||
"requireTrailingComma": null,
|
||||
"validateIndentation": 2,
|
||||
"validateLineBreaks": "LF",
|
||||
"validateQuoteMarks": "'"
|
||||
"validateLineBreaks": "LF"
|
||||
}
|
||||
|
|
@ -38,6 +38,7 @@
|
|||
"scripts": {
|
||||
"release": "bash release.sh",
|
||||
"test": "mocha",
|
||||
"style": "jscs src test",
|
||||
"codacy": "istanbul cover ./node_modules/mocha/bin/_mocha --report lcovonly -- -R spec && cat ./coverage/lcov.info | ./node_modules/.bin/codacy-coverage && rm -rf ./coverage"
|
||||
},
|
||||
"main": "./src/diff2html.js",
|
||||
|
|
@ -46,11 +47,13 @@
|
|||
},
|
||||
"devDependencies": {
|
||||
"codacy-coverage": "^1.1.3",
|
||||
"fast-html-parser": "^1.0.1",
|
||||
"istanbul": "^0.4.1",
|
||||
"less": "2.5.*",
|
||||
"jscs": "^2.7.0",
|
||||
"less": "^2.5.3",
|
||||
"mocha": "^2.3.4",
|
||||
"uglifyjs": "2.4.*",
|
||||
"webpack": "1.12.*"
|
||||
"uglifyjs": "^2.4.10",
|
||||
"webpack": "^1.12.9"
|
||||
},
|
||||
"license": "MIT",
|
||||
"files": [
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@
|
|||
}
|
||||
|
||||
FileListPrinter.prototype.generateFileList = function(diffFiles) {
|
||||
var hideId = utils.getRandomId('d2h-hide'); //necessary if there are 2 elements like this in the same page
|
||||
var hideId = utils.getRandomId('d2h-hide'); // Necessary if there are 2 elements like this in the same page
|
||||
var showId = utils.getRandomId('d2h-show');
|
||||
return '<div class="d2h-file-list-wrapper">\n' +
|
||||
' <div class="d2h-file-list-header">Files changed (' + diffFiles.length + ')  </div>\n' +
|
||||
|
|
@ -32,7 +32,8 @@
|
|||
' <td class="d2h-lines-deleted">\n' +
|
||||
' <span>-' + file.deletedLines + '</span>\n' +
|
||||
' </td>\n' +
|
||||
' <td class="d2h-file-name"><a href="#' + printerUtils.getHtmlId(file) + '"> ' + printerUtils.getDiffName(file) + '</a></td>\n' +
|
||||
' <td class="d2h-file-name"><a href="#' + printerUtils.getHtmlId(file) + '">' +
|
||||
' ' + printerUtils.getDiffName(file) + '</a></td>\n' +
|
||||
' </tr>\n';
|
||||
}).join('\n') +
|
||||
'</table></div>\n';
|
||||
|
|
|
|||
|
|
@ -19,7 +19,9 @@
|
|||
var i, chr, len;
|
||||
var hash = 0;
|
||||
|
||||
if (text.length === 0) return hash;
|
||||
if (text.length === 0) {
|
||||
return hash;
|
||||
}
|
||||
|
||||
for (i = 0, len = text.length; i < len; i++) {
|
||||
chr = text.charCodeAt(i);
|
||||
|
|
@ -75,7 +77,7 @@
|
|||
if (!config.charByChar && config.matching === 'words') {
|
||||
var treshold = 0.25;
|
||||
|
||||
if (typeof(config.matchWordsThreshold) !== 'undefined') {
|
||||
if (typeof (config.matchWordsThreshold) !== 'undefined') {
|
||||
treshold = config.matchWordsThreshold;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -19,23 +19,33 @@
|
|||
|
||||
/*
|
||||
Copyright (c) 2011 Andrei Mackenzie
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
|
||||
documentation files (the "Software"), to deal in the Software without restriction, including without limitation
|
||||
the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
|
||||
and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
|
||||
THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
||||
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
function levenshtein(a, b) {
|
||||
if (a.length == 0) return b.length;
|
||||
if (b.length == 0) return a.length;
|
||||
if (a.length == 0) {
|
||||
return b.length;
|
||||
}
|
||||
if (b.length == 0) {
|
||||
return a.length;
|
||||
}
|
||||
|
||||
var matrix = [];
|
||||
|
||||
// increment along the first column of each row
|
||||
// Increment along the first column of each row
|
||||
var i;
|
||||
for (i = 0; i <= b.length; i++) {
|
||||
matrix[i] = [i];
|
||||
}
|
||||
|
||||
// increment each column in the first row
|
||||
// Increment each column in the first row
|
||||
var j;
|
||||
for (j = 0; j <= a.length; j++) {
|
||||
matrix[0][j] = j;
|
||||
|
|
@ -47,9 +57,9 @@
|
|||
if (b.charAt(i - 1) == a.charAt(j - 1)) {
|
||||
matrix[i][j] = matrix[i - 1][j - 1];
|
||||
} else {
|
||||
matrix[i][j] = Math.min(matrix[i - 1][j - 1] + 1, // substitution
|
||||
Math.min(matrix[i][j - 1] + 1, // insertion
|
||||
matrix[i - 1][j] + 1)); // deletion
|
||||
matrix[i][j] = Math.min(matrix[i - 1][j - 1] + 1, // Substitution
|
||||
Math.min(matrix[i][j - 1] + 1, // Insertion
|
||||
matrix[i - 1][j] + 1)); // Deletion
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -99,7 +109,7 @@
|
|||
}
|
||||
|
||||
function group(a, b, level, cache) {
|
||||
if (typeof(cache) === "undefined") {
|
||||
if (typeof (cache) === "undefined") {
|
||||
cache = {};
|
||||
}
|
||||
|
||||
|
|
@ -113,14 +123,14 @@
|
|||
return [[a, b]];
|
||||
}
|
||||
|
||||
var a1 = a.slice(0, bm.indexA),
|
||||
b1 = b.slice(0, bm.indexB),
|
||||
aMatch = [a[bm.indexA]],
|
||||
bMatch = [b[bm.indexB]],
|
||||
tailA = bm.indexA + 1,
|
||||
tailB = bm.indexB + 1,
|
||||
a2 = a.slice(tailA),
|
||||
b2 = b.slice(tailB);
|
||||
var a1 = a.slice(0, bm.indexA);
|
||||
var b1 = b.slice(0, bm.indexB);
|
||||
var aMatch = [a[bm.indexA]];
|
||||
var bMatch = [b[bm.indexB]];
|
||||
var tailA = bm.indexA + 1;
|
||||
var tailB = bm.indexB + 1;
|
||||
var a2 = a.slice(tailA);
|
||||
var b2 = b.slice(tailB);
|
||||
|
||||
var group1 = group(a1, b1, level + 1, cache);
|
||||
var groupMatch = group(aMatch, bMatch, level + 1, cache);
|
||||
|
|
|
|||
|
|
@ -12,6 +12,13 @@
|
|||
var utils = require('./utils.js').Utils;
|
||||
var Rematch = require('./rematch.js').Rematch;
|
||||
|
||||
var matcher = Rematch.rematch(function(a, b) {
|
||||
var amod = a.content.substr(1);
|
||||
var bmod = b.content.substr(1);
|
||||
|
||||
return Rematch.distance(amod, bmod);
|
||||
});
|
||||
|
||||
function SideBySidePrinter(config) {
|
||||
this.config = config;
|
||||
}
|
||||
|
|
@ -65,12 +72,6 @@
|
|||
'</div>\n';
|
||||
};
|
||||
|
||||
var matcher = Rematch.rematch(function(a, b) {
|
||||
var amod = a.content.substr(1),
|
||||
bmod = b.content.substr(1);
|
||||
return Rematch.distance(amod, bmod);
|
||||
});
|
||||
|
||||
SideBySidePrinter.prototype.generateSideBySideFileHtml = function(file) {
|
||||
var that = this;
|
||||
var fileHtml = {};
|
||||
|
|
|
|||
|
|
@ -6,12 +6,12 @@ describe('Diff2Html', function() {
|
|||
describe('getJsonFromDiff', function() {
|
||||
it('should parse simple diff to json', function() {
|
||||
var diff =
|
||||
'diff --git a/sample b/sample\n'+
|
||||
'index 0000001..0ddf2ba\n'+
|
||||
'--- a/sample\n'+
|
||||
'+++ b/sample\n'+
|
||||
'@@ -1 +1 @@\n'+
|
||||
'-test\n'+
|
||||
'diff --git a/sample b/sample\n' +
|
||||
'index 0000001..0ddf2ba\n' +
|
||||
'--- a/sample\n' +
|
||||
'+++ b/sample\n' +
|
||||
'@@ -1 +1 @@\n' +
|
||||
'-test\n' +
|
||||
'+test1\n';
|
||||
var result = Diff2Html.getJsonFromDiff(diff);
|
||||
var file1 = result[0];
|
||||
|
|
|
|||
|
|
@ -21,4 +21,103 @@ describe('SideBySidePrinter', function() {
|
|||
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
|
||||
}], "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 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>' +
|
||||
' <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 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);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in a new issue