summaryrefslogtreecommitdiffstats
path: root/spec/string-params.js
diff options
context:
space:
mode:
authorkpdecker <kpdecker@gmail.com>2013-06-01 23:45:43 -0500
committerkpdecker <kpdecker@gmail.com>2013-06-01 23:45:43 -0500
commitadda0569e0ec3fc363af9efbb23f5304c6d80fe2 (patch)
tree9952d7aed77f8710a53731bb98340d5468c9e931 /spec/string-params.js
parentd13ae310d36a27fcbc333570f9b7ae9c7e641392 (diff)
downloadhandlebars.js-adda0569e0ec3fc363af9efbb23f5304c6d80fe2.zip
handlebars.js-adda0569e0ec3fc363af9efbb23f5304c6d80fe2.tar.gz
handlebars.js-adda0569e0ec3fc363af9efbb23f5304c6d80fe2.tar.bz2
Refactor qunit unit tests
Allows for testing node, browser, and precompiled modes in the node tests. Also reorganizes the qunit spec file to provide better organization.
Diffstat (limited to 'spec/string-params.js')
-rw-r--r--spec/string-params.js145
1 files changed, 145 insertions, 0 deletions
diff --git a/spec/string-params.js b/spec/string-params.js
new file mode 100644
index 0000000..1ebb583
--- /dev/null
+++ b/spec/string-params.js
@@ -0,0 +1,145 @@
+describe('string params mode', function() {
+ it("arguments to helpers can be retrieved from options hash in string form", function() {
+ var template = CompilerContext.compile('{{wycats is.a slave.driver}}', {stringParams: true});
+
+ var helpers = {
+ wycats: function(passiveVoice, noun) {
+ return "HELP ME MY BOSS " + passiveVoice + ' ' + noun;
+ }
+ };
+
+ var result = template({}, {helpers: helpers});
+
+ equals(result, "HELP ME MY BOSS is.a slave.driver", "String parameters output");
+ });
+
+ it("when using block form, arguments to helpers can be retrieved from options hash in string form", function() {
+ var template = CompilerContext.compile('{{#wycats is.a slave.driver}}help :({{/wycats}}', {stringParams: true});
+
+ var helpers = {
+ wycats: function(passiveVoice, noun, options) {
+ return "HELP ME MY BOSS " + passiveVoice + ' ' +
+ noun + ': ' + options.fn(this);
+ }
+ };
+
+ var result = template({}, {helpers: helpers});
+
+ equals(result, "HELP ME MY BOSS is.a slave.driver: help :(", "String parameters output");
+ });
+
+ it("when inside a block in String mode, .. passes the appropriate context in the options hash", function() {
+ var template = CompilerContext.compile('{{#with dale}}{{tomdale ../need dad.joke}}{{/with}}', {stringParams: true});
+
+ var helpers = {
+ tomdale: function(desire, noun, options) {
+ return "STOP ME FROM READING HACKER NEWS I " +
+ options.contexts[0][desire] + " " + noun;
+ },
+
+ "with": function(context, options) {
+ return options.fn(options.contexts[0][context]);
+ }
+ };
+
+ var result = template({
+ dale: {},
+
+ need: 'need-a'
+ }, {helpers: helpers});
+
+ equals(result, "STOP ME FROM READING HACKER NEWS I need-a dad.joke", "Proper context variable output");
+ });
+
+ it("information about the types is passed along", function() {
+ var template = CompilerContext.compile('{{tomdale "need" dad.joke true false}}', { stringParams: true });
+
+ var helpers = {
+ tomdale: function(desire, noun, trueBool, falseBool, options) {
+ equal(options.types[0], 'STRING', "the string type is passed");
+ equal(options.types[1], 'ID', "the expression type is passed");
+ equal(options.types[2], 'BOOLEAN', "the expression type is passed");
+ equal(desire, "need", "the string form is passed for strings");
+ equal(noun, "dad.joke", "the string form is passed for expressions");
+ equal(trueBool, true, "raw booleans are passed through");
+ equal(falseBool, false, "raw booleans are passed through");
+ return "Helper called";
+ }
+ };
+
+ var result = template({}, { helpers: helpers });
+ equal(result, "Helper called");
+ });
+
+ it("hash parameters get type information", function() {
+ var template = CompilerContext.compile('{{tomdale he.says desire="need" noun=dad.joke bool=true}}', { stringParams: true });
+
+ var helpers = {
+ tomdale: function(exclamation, options) {
+ equal(exclamation, "he.says");
+ equal(options.types[0], "ID");
+
+ equal(options.hashTypes.desire, "STRING");
+ equal(options.hashTypes.noun, "ID");
+ equal(options.hashTypes.bool, "BOOLEAN");
+ equal(options.hash.desire, "need");
+ equal(options.hash.noun, "dad.joke");
+ equal(options.hash.bool, true);
+ return "Helper called";
+ }
+ };
+
+ var result = template({}, { helpers: helpers });
+ equal(result, "Helper called");
+ });
+
+ it("hash parameters get context information", function() {
+ var template = CompilerContext.compile('{{#with dale}}{{tomdale he.says desire="need" noun=../dad/joke bool=true}}{{/with}}', { stringParams: true });
+
+ var context = {dale: {}};
+
+ var helpers = {
+ tomdale: function(exclamation, options) {
+ equal(exclamation, "he.says");
+ equal(options.types[0], "ID");
+
+ equal(options.contexts.length, 1);
+ equal(options.hashContexts.noun, context);
+ equal(options.hash.desire, "need");
+ equal(options.hash.noun, "dad.joke");
+ equal(options.hash.bool, true);
+ return "Helper called";
+ },
+ "with": function(context, options) {
+ return options.fn(options.contexts[0][context]);
+ }
+ };
+
+ var result = template(context, { helpers: helpers });
+ equal(result, "Helper called");
+ });
+
+ it("when inside a block in String mode, .. passes the appropriate context in the options hash to a block helper", function() {
+ var template = CompilerContext.compile('{{#with dale}}{{#tomdale ../need dad.joke}}wot{{/tomdale}}{{/with}}', {stringParams: true});
+
+ var helpers = {
+ tomdale: function(desire, noun, options) {
+ return "STOP ME FROM READING HACKER NEWS I " +
+ options.contexts[0][desire] + " " + noun + " " +
+ options.fn(this);
+ },
+
+ "with": function(context, options) {
+ return options.fn(options.contexts[0][context]);
+ }
+ };
+
+ var result = template({
+ dale: {},
+
+ need: 'need-a'
+ }, {helpers: helpers});
+
+ equals(result, "STOP ME FROM READING HACKER NEWS I need-a dad.joke wot", "Proper context variable output");
+ });
+});