Add more tests to hogan templates
This commit is contained in:
parent
67ba95e5df
commit
2463cb0270
5 changed files with 90 additions and 35 deletions
56
dist/diff2html.js
vendored
56
dist/diff2html.js
vendored
|
|
@ -2323,11 +2323,14 @@ process.umask = function() { return 0; };
|
||||||
|
|
||||||
DiffParser.prototype.LINE_TYPE = LINE_TYPE;
|
DiffParser.prototype.LINE_TYPE = LINE_TYPE;
|
||||||
|
|
||||||
DiffParser.prototype.generateDiffJson = function(diffInput, config) {
|
DiffParser.prototype.generateDiffJson = function(diffInput, configuration) {
|
||||||
|
var config = configuration || {};
|
||||||
|
|
||||||
var files = [];
|
var files = [];
|
||||||
var currentFile = null;
|
var currentFile = null;
|
||||||
var currentBlock = null;
|
var currentBlock = null;
|
||||||
var oldLine = null;
|
var oldLine = null;
|
||||||
|
var oldLine2 = null; // Used for combined diff
|
||||||
var newLine = null;
|
var newLine = null;
|
||||||
|
|
||||||
var saveBlock = function() {
|
var saveBlock = function() {
|
||||||
|
|
@ -2367,22 +2370,41 @@ process.umask = function() { return 0; };
|
||||||
|
|
||||||
var values;
|
var values;
|
||||||
|
|
||||||
if (values = /^@@ -(\d+),\d+ \+(\d+),\d+ @@.*/.exec(line)) {
|
/**
|
||||||
|
* From Range:
|
||||||
|
* -<start line>[,<number of lines>]
|
||||||
|
*
|
||||||
|
* To Range:
|
||||||
|
* +<start line>[,<number of lines>]
|
||||||
|
*
|
||||||
|
* @@ from-file-range to-file-range @@
|
||||||
|
*
|
||||||
|
* @@@ from-file-range from-file-range to-file-range @@@
|
||||||
|
*
|
||||||
|
* number of lines is optional, if omited consider 0
|
||||||
|
*/
|
||||||
|
|
||||||
|
if (values = /^@@ -(\d+)(?:,\d+)? \+(\d+)(?:,\d+)? @@.*/.exec(line)) {
|
||||||
currentFile.isCombined = false;
|
currentFile.isCombined = false;
|
||||||
} else if (values = /^@@@ -(\d+),\d+ -\d+,\d+ \+(\d+),\d+ @@@.*/.exec(line)) {
|
oldLine = values[1];
|
||||||
|
newLine = values[2];
|
||||||
|
} else if (values = /^@@@ -(\d+)(?:,\d+)? -(\d+)(?:,\d+)? \+(\d+)(?:,\d+)? @@@.*/.exec(line)) {
|
||||||
currentFile.isCombined = true;
|
currentFile.isCombined = true;
|
||||||
|
oldLine = values[1];
|
||||||
|
oldLine2 = values[2];
|
||||||
|
newLine = values[3];
|
||||||
} else {
|
} else {
|
||||||
values = [0, 0];
|
console.error("Failed to parse lines, starting in 0!");
|
||||||
|
oldLine = 0;
|
||||||
|
newLine = 0;
|
||||||
currentFile.isCombined = false;
|
currentFile.isCombined = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
oldLine = values[1];
|
|
||||||
newLine = values[2];
|
|
||||||
|
|
||||||
/* Create block metadata */
|
/* Create block metadata */
|
||||||
currentBlock = {};
|
currentBlock = {};
|
||||||
currentBlock.lines = [];
|
currentBlock.lines = [];
|
||||||
currentBlock.oldStartLine = oldLine;
|
currentBlock.oldStartLine = oldLine;
|
||||||
|
currentBlock.oldStartLine2 = oldLine2;
|
||||||
currentBlock.newStartLine = newLine;
|
currentBlock.newStartLine = newLine;
|
||||||
currentBlock.header = line;
|
currentBlock.header = line;
|
||||||
};
|
};
|
||||||
|
|
@ -2751,23 +2773,19 @@ process.umask = function() { return 0; };
|
||||||
|
|
||||||
var hogan = require('hogan.js');
|
var hogan = require('hogan.js');
|
||||||
|
|
||||||
var hoganTemplates;
|
var hoganTemplates = require('./templates/diff2html-templates.js');
|
||||||
|
|
||||||
var templatesPath = path.resolve(__dirname, 'templates');
|
var templatesPath = path.resolve(__dirname, 'templates');
|
||||||
var templatesCache = {};
|
var templatesCache = {};
|
||||||
|
|
||||||
function HoganJsUtils() {
|
function HoganJsUtils() {
|
||||||
try {
|
|
||||||
hoganTemplates = require('./templates/diff2html-templates.js');
|
|
||||||
} catch (_ignore) {
|
|
||||||
hoganTemplates = {};
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
HoganJsUtils.prototype.render = function(namespace, view, params) {
|
HoganJsUtils.prototype.render = function(namespace, view, params, configuration) {
|
||||||
|
var config = configuration || {};
|
||||||
var templateKey = this._templateKey(namespace, view);
|
var templateKey = this._templateKey(namespace, view);
|
||||||
|
|
||||||
var template = this._getTemplate(templateKey);
|
var template = this._getTemplate(templateKey, config);
|
||||||
if (template) {
|
if (template) {
|
||||||
return template.render(params);
|
return template.render(params);
|
||||||
}
|
}
|
||||||
|
|
@ -2775,8 +2793,12 @@ process.umask = function() { return 0; };
|
||||||
return null;
|
return null;
|
||||||
};
|
};
|
||||||
|
|
||||||
HoganJsUtils.prototype._getTemplate = function(templateKey) {
|
HoganJsUtils.prototype._getTemplate = function(templateKey, config) {
|
||||||
var template = this._readFromCache(templateKey);
|
var template;
|
||||||
|
|
||||||
|
if (!config.noCache) {
|
||||||
|
template = this._readFromCache(templateKey);
|
||||||
|
}
|
||||||
|
|
||||||
if (!template) {
|
if (!template) {
|
||||||
template = this._loadTemplate(templateKey);
|
template = this._loadTemplate(templateKey);
|
||||||
|
|
|
||||||
4
dist/diff2html.min.js
vendored
4
dist/diff2html.min.js
vendored
File diff suppressed because one or more lines are too long
|
|
@ -18,7 +18,6 @@
|
||||||
|
|
||||||
<!-- diff2html -->
|
<!-- diff2html -->
|
||||||
<link rel="stylesheet" type="text/css" href="../dist/diff2html.css">
|
<link rel="stylesheet" type="text/css" href="../dist/diff2html.css">
|
||||||
<script type="text/javascript" src="../dist/diff2html-templates.js"></script>
|
|
||||||
<script type="text/javascript" src="../dist/diff2html.js"></script>
|
<script type="text/javascript" src="../dist/diff2html.js"></script>
|
||||||
<script type="text/javascript" src="../dist/diff2html-ui.js"></script>
|
<script type="text/javascript" src="../dist/diff2html-ui.js"></script>
|
||||||
<!-- -->
|
<!-- -->
|
||||||
|
|
|
||||||
|
|
@ -12,23 +12,19 @@
|
||||||
|
|
||||||
var hogan = require('hogan.js');
|
var hogan = require('hogan.js');
|
||||||
|
|
||||||
var hoganTemplates;
|
var hoganTemplates = require('./templates/diff2html-templates.js');
|
||||||
|
|
||||||
var templatesPath = path.resolve(__dirname, 'templates');
|
var templatesPath = path.resolve(__dirname, 'templates');
|
||||||
var templatesCache = {};
|
var templatesCache = {};
|
||||||
|
|
||||||
function HoganJsUtils() {
|
function HoganJsUtils() {
|
||||||
try {
|
|
||||||
hoganTemplates = require('./templates/diff2html-templates.js');
|
|
||||||
} catch (_ignore) {
|
|
||||||
hoganTemplates = {};
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
HoganJsUtils.prototype.render = function(namespace, view, params) {
|
HoganJsUtils.prototype.render = function(namespace, view, params, configuration) {
|
||||||
|
var config = configuration || {};
|
||||||
var templateKey = this._templateKey(namespace, view);
|
var templateKey = this._templateKey(namespace, view);
|
||||||
|
|
||||||
var template = this._getTemplate(templateKey);
|
var template = this._getTemplate(templateKey, config);
|
||||||
if (template) {
|
if (template) {
|
||||||
return template.render(params);
|
return template.render(params);
|
||||||
}
|
}
|
||||||
|
|
@ -36,8 +32,12 @@
|
||||||
return null;
|
return null;
|
||||||
};
|
};
|
||||||
|
|
||||||
HoganJsUtils.prototype._getTemplate = function(templateKey) {
|
HoganJsUtils.prototype._getTemplate = function(templateKey, config) {
|
||||||
var template = this._readFromCache(templateKey);
|
var template;
|
||||||
|
|
||||||
|
if (!config.noCache) {
|
||||||
|
template = this._readFromCache(templateKey);
|
||||||
|
}
|
||||||
|
|
||||||
if (!template) {
|
if (!template) {
|
||||||
template = this._loadTemplate(templateKey);
|
template = this._loadTemplate(templateKey);
|
||||||
|
|
@ -48,11 +48,16 @@
|
||||||
|
|
||||||
HoganJsUtils.prototype._loadTemplate = function(templateKey) {
|
HoganJsUtils.prototype._loadTemplate = function(templateKey) {
|
||||||
var template;
|
var template;
|
||||||
if (fs.readFileSync) {
|
|
||||||
var templatePath = path.join(templatesPath, templateKey);
|
try {
|
||||||
var templateContent = fs.readFileSync(templatePath + '.mustache', 'utf8');
|
if (fs.readFileSync) {
|
||||||
template = hogan.compile(templateContent);
|
var templatePath = path.join(templatesPath, templateKey);
|
||||||
templatesCache[templateKey] = template;
|
var templateContent = fs.readFileSync(templatePath + '.mustache', 'utf8');
|
||||||
|
template = hogan.compile(templateContent);
|
||||||
|
templatesCache[templateKey] = template;
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.error('Failed to read (template: ' + templateKey + ') from fs: ' + e.message);
|
||||||
}
|
}
|
||||||
|
|
||||||
return template;
|
return template;
|
||||||
|
|
|
||||||
29
test/hogan-cache-tests.js
Normal file
29
test/hogan-cache-tests.js
Normal file
|
|
@ -0,0 +1,29 @@
|
||||||
|
var assert = require('assert');
|
||||||
|
|
||||||
|
var HoganJsUtils = require('../src/hoganjs-utils.js').HoganJsUtils;
|
||||||
|
|
||||||
|
describe('HoganJsUtils', function() {
|
||||||
|
describe('render', function() {
|
||||||
|
var emptyDiffHtml =
|
||||||
|
'<tr>\n' +
|
||||||
|
' <td class="">\n' +
|
||||||
|
' <div class="d2h-code-line ">\n' +
|
||||||
|
' File without changes\n' +
|
||||||
|
' </div>\n' +
|
||||||
|
' </td>\n' +
|
||||||
|
'</tr>';
|
||||||
|
|
||||||
|
it('should render view', function() {
|
||||||
|
var result = HoganJsUtils.render('line-by-line', 'empty-diff', {});
|
||||||
|
assert.equal(emptyDiffHtml, result);
|
||||||
|
});
|
||||||
|
it('should render view without cache', function() {
|
||||||
|
var result = HoganJsUtils.render('line-by-line', 'empty-diff', {}, {noCache: true});
|
||||||
|
assert.equal(emptyDiffHtml + '\n', result);
|
||||||
|
});
|
||||||
|
it('should return null if template is missing', function() {
|
||||||
|
var result = HoganJsUtils.render('line-by-line', 'missing-template', {}, {noCache: true});
|
||||||
|
assert.equal(null, result);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
Loading…
Reference in a new issue