Skip to content

Commit

Permalink
Added fix, workaround handlebars issue and added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Morten Henriksen authored and n1mmy committed Feb 7, 2013
1 parent d1de04d commit 7ca41e4
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 3 deletions.
9 changes: 9 additions & 0 deletions packages/handlebars/handlebars-client_tests.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<template name="test_helpers_00">
Hi
</template>

<!-- Test for Issue 617 - Type casting for helpers -->
<template name="test_helpers_10">
{{testTypeCasting 'string' true false 10 -10 }}
</template>

56 changes: 56 additions & 0 deletions packages/handlebars/handlebars-client_tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
(function () {
if (typeof Handlebars !== 'undefined') {
//{{getSession 'key'}}
Handlebars.registerHelper('testTypeCasting', function (myStr, myTrue, myFalse, myTen, myNegTen) {
//Expect 'string' true false 10 -10
var result = (myStr == 'string') ?'1':(myStr == undefined)?'u':'-';
result += (myTrue == true) ?'2':(myTrue == undefined)?'u':'-';
result += (myFalse == false) ?'3':(myFalse == undefined)?'u':'-';
result += (myTen == 10) ?'4':(myTen == undefined)?'u':'-';
result += (myNegTen == -10) ?'5':(myNegTen == undefined)?'u':'-';
return result;
});
}
})();

(function () {
Tinytest.add('Handlebars - init templates', function (test) {
var frag = Meteor.render(Template.test_helpers_00);
test.equal(canonicalizeHtml(DomUtils.fragmentToHtml(frag)), "Hi");
});

Tinytest.add('Handlebars - init test helpers', function (test) {
test.notEqual(Handlebars._default_helpers['testTypeCasting'], undefined, 'testTypeCasting: Handlebars loaded after session_helpers?');
});

Tinytest.add('Handlebars - helper typecast Issue #617', function (test) {
var frag = Meteor.render(Template.test_helpers_10);
var result = canonicalizeHtml(DomUtils.fragmentToHtml(frag));
test.equal(result.substr(0, 1), "1", 'Error in type casting string');
test.equal(result.substr(1, 1), "2", 'Error in type casting boolean true');
test.equal(result.substr(2, 1), "3", 'Error in type casting boolean false');
test.equal(result.substr(3, 1), "4", 'Error in type casting number');
test.equal(result.substr(4, 1), "5", 'Error in type casting number negative');
});


})();

//Test API:
//test.isFalse(v, msg)
//test.isTrue(v, msg)
//test.equalactual, expected, message, not
//test.length(obj, len)
//test.include(s, v)
//test.isNaN(v, msg)
//test.isUndefined(v, msg)
//test.isNotNull
//test.isNull
//test.throws(func)
//test.instanceOf(obj, klass)
//test.notEqual(actual, expected, message)
//test.runId()
//test.exception(exception)
//test.expect_fail()
//test.ok(doc)
//test.fail(doc)
12 changes: 12 additions & 0 deletions packages/handlebars/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,15 @@ Package.on_use(function (api) {
// making it the default default, providing the compiler code,
// depending on the node package (or packaging the compiler
// ourselves..)
Package.on_test(function (api) {
api.use(['tinytest',
'test-helpers',
'session',
'templating',
'mongo-livedata']);

api.add_files(['handlebars-client_tests.html',
'handlebars-client_tests.js',
], 'client');

});
13 changes: 10 additions & 3 deletions packages/handlebars/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,18 @@ Handlebars.to_json_ast = function (code) {
};

var value = function (node) {
//Work around handlebars.js Issue #422 - Negative integers for helpers get trapped as ID
if (node.type == 'ID' && node.string.slice(0,1) == '-' && !isNaN(node.string)) { //node.string eg. '-10'
//Reconstruct node
node.type = 'INTEGER';
node.integer = node.string;
} //EO Workaround handlebars.js Issue #422

var choices = {
ID: function (node) {return identifier(node);},
STRING: function (node) {return node.string;},
INTEGER: function (node) {return node.integer;},
BOOLEAN: function (node) {return node.bool;},
STRING: function (node) {return ''+node.string;},
INTEGER: function (node) {return +node.integer;},
BOOLEAN: function (node) {return (node.bool === 'true');},
};
if (!(node.type in choices))
throw new Error("got ast node " + node.type + " for value");
Expand Down

0 comments on commit 7ca41e4

Please sign in to comment.