Adds test for whitespaces and indentation and adds template for the wrapper

This commit is contained in:
Paulo Bu 2016-01-28 16:03:20 +01:00 committed by Rodrigo Fernandes
parent da2cd63a75
commit e15cde2d6f
4 changed files with 89 additions and 1 deletions

View file

@ -25,6 +25,10 @@
return nunjucksEnv.render('file-diff.html', {'file': file, 'diffs': diffs}); return nunjucksEnv.render('file-diff.html', {'file': file, 'diffs': diffs});
}; };
LineByLinePrinter.prototype.makeLineByLineHtmlWrapper = function(content) {
return nunjucksEnv.render('wrapper.html', {'content': content});
};
LineByLinePrinter.prototype.generateLineByLineJsonHtml = function(diffFiles) { LineByLinePrinter.prototype.generateLineByLineJsonHtml = function(diffFiles) {
var that = this; var that = this;
var htmlDiffs = diffFiles.map(function(file) { var htmlDiffs = diffFiles.map(function(file) {
@ -37,7 +41,7 @@
return that.makeFileDiffHtml(file, diffs); return that.makeFileDiffHtml(file, diffs);
}); });
return '<div class="d2h-wrapper">\n' + htmlDiffs.join('\n') + '</div>\n'; return this.makeLineByLineHtmlWrapper(htmlDiffs.join('\n'));
}; };
var matcher = Rematch.rematch(function(a, b) { var matcher = Rematch.rematch(function(a, b) {

View file

@ -0,0 +1,3 @@
<div class="d2h-wrapper">
{{ content }}
</div>

View file

@ -1,4 +1,5 @@
var assert = require('assert'); var assert = require('assert');
var Utils = require('../src/utils.js').Utils;
var LineByLinePrinter = require('../src/line-by-line-printer.js').LineByLinePrinter; var LineByLinePrinter = require('../src/line-by-line-printer.js').LineByLinePrinter;
@ -62,6 +63,72 @@ describe('LineByLinePrinter', function() {
' </td>\n' + ' </td>\n' +
'</tr>\n'; '</tr>\n';
assert.equal(expected, fileHtml);
});
it('should convert indents into non breakin spaces (2 white spaces)', function() {
var diffParser = require('../src/diff-parser.js').DiffParser;
var lineByLinePrinter = new LineByLinePrinter({});
var fileHtml = lineByLinePrinter.makeLineHtml(
diffParser.LINE_TYPE.INSERTS, '', 30, '+', ' test');
fileHtml = fileHtml.replace(/\n\n+/g, '\n');
var expected = '<tr>\n' +
' <td class="d2h-code-linenumber d2h-ins">\n' +
' <div class="line-num1"></div>\n' +
' <div class="line-num2">30</div>\n' +
' </td>\n' +
' <td class="d2h-ins">\n' +
' <div class="d2h-code-line d2h-ins">\n' +
' <span class="d2h-code-line-prefix">&nbsp;&nbsp;test</span>\n' +
' <span class="d2h-code-line-ctn">+</span>\n' +
' </div>\n' +
' </td>\n' +
'</tr>\n';
assert.equal(expected, fileHtml);
});
it('should convert indents into non breakin spaces (4 white spaces)', function() {
var diffParser = require('../src/diff-parser.js').DiffParser;
var lineByLinePrinter = new LineByLinePrinter({});
var fileHtml = lineByLinePrinter.makeLineHtml(
diffParser.LINE_TYPE.INSERTS, '', 30, '+', ' test');
fileHtml = fileHtml.replace(/\n\n+/g, '\n');
var expected = '<tr>\n' +
' <td class="d2h-code-linenumber d2h-ins">\n' +
' <div class="line-num1"></div>\n' +
' <div class="line-num2">30</div>\n' +
' </td>\n' +
' <td class="d2h-ins">\n' +
' <div class="d2h-code-line d2h-ins">\n' +
' <span class="d2h-code-line-prefix">&nbsp;&nbsp;&nbsp;&nbsp;test</span>\n' +
' <span class="d2h-code-line-ctn">+</span>\n' +
' </div>\n' +
' </td>\n' +
'</tr>\n';
assert.equal(expected, fileHtml);
});
it('should convert indents into non breakin spaces (one tab)', function() {
var diffParser = require('../src/diff-parser.js').DiffParser;
var lineByLinePrinter = new LineByLinePrinter({});
var fileHtml = lineByLinePrinter.makeLineHtml(
diffParser.LINE_TYPE.INSERTS, '', 30, '+', Utils.escape('\ttest'));
fileHtml = fileHtml.replace(/\n\n+/g, '\n');
var expected = '<tr>\n' +
' <td class="d2h-code-linenumber d2h-ins">\n' +
' <div class="line-num1"></div>\n' +
' <div class="line-num2">30</div>\n' +
' </td>\n' +
' <td class="d2h-ins">\n' +
' <div class="d2h-code-line d2h-ins">\n' +
' <span class="d2h-code-line-prefix">&nbsp;&nbsp;&nbsp;&nbsp;test</span>\n' +
' <span class="d2h-code-line-ctn">+</span>\n' +
' </div>\n' +
' </td>\n' +
'</tr>\n';
assert.equal(expected, fileHtml); assert.equal(expected, fileHtml);
}); });
}); });

View file

@ -22,4 +22,18 @@ describe('Utils', function() {
assert.equal(expected, result); assert.equal(expected, result);
}); });
}); });
describe('convertWhiteSpaceToNonBreakingSpace', function() {
it('should escape 1 whitespaces with &nbsp;', function() {
var result = Utils.convertWhiteSpaceToNonBreakingSpace(' ');
assert.equal('&nbsp;', result);
});
it('should escape 2 whitespaces with &nbsp;', function() {
var result = Utils.convertWhiteSpaceToNonBreakingSpace(' ');
assert.equal('&nbsp;&nbsp;', result);
});
it('should escape 4 whitespaces with &nbsp;', function() {
var result = Utils.convertWhiteSpaceToNonBreakingSpace(' ');
assert.equal('&nbsp;&nbsp;&nbsp;&nbsp;', result);
});
});
}); });