Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

make helper work with function-valued context variables

  • Loading branch information...
commit 5f59b596021955e299e7e55c362dbd2a1aea0af5 1 parent 87a2b1e
@jairodemorais jairodemorais authored
View
30 lib/dust-helpers.js
@@ -7,7 +7,7 @@ if (typeof exports !== "undefined")
/* make a safe version of console if it is not available
* currently supporting:
- * _console.log
+ * _console.log
* */
var _console = (typeof console !== 'undefined')? console: {
log: function(){
@@ -17,12 +17,12 @@ var _console = (typeof console !== 'undefined')? console: {
function isSelect(context) {
var value = context.current();
- return typeof value === "object" && value.isSelect === true;
+ return typeof value === "object" && value.isSelect === true;
}
function filter(chunk, context, bodies, params, filter) {
var params = params || {},
- actual,
+ actual,
expected;
if (params.key) {
actual = helpers.tap(params.key, chunk, context);
@@ -85,20 +85,24 @@ var helpers = {
var output = input;
// dust compiles a string to function, if there are references
if( typeof input === "function"){
- output = '';
- chunk.tap(function(data){
- output += data;
- return '';
- }).render(input, context).untap();
- if( output === '' ){
- output = false;
+ if( input.length == 0 ) { // just a plain function, not a dust `body` function
+ output = input();
+ } else {
+ output = '';
+ chunk.tap(function(data){
+ output += data;
+ return '';
+ }).render(input, context).untap();
+ if( output === '' ){
+ output = false;
+ }
}
- }
+ }
return output;
},
/**
- if helper
+ if helper
@param cond, either a string literal value or a dust reference
a string literal value, is enclosed in double quotes, e.g. cond="2>3"
a dust reference is also enclosed in double quotes, e.g. cond="'{val}'' > 3"
@@ -125,7 +129,7 @@ var helpers = {
},
/**
- select/eq/lt/lte/gt/gte/default helper
+ select/eq/lt/lte/gt/gte/default helper
@param key, either a string literal value or a dust reference
a string literal value, is enclosed in double quotes, e.g. key="foo"
a dust reference may or may not be enclosed in double quotes, e.g. key="{val}" and key=val are both valid
View
180 test/core.js
@@ -80,6 +80,186 @@ exports.coreSetup = function(suite, auto) {
}
})
});
+
+ suite.test("tap (plain text string literal)", function() {
+ var unit = this;
+ dust.helpers.tapper = function(chunk, context, bodies, params) {
+ var result = dust.helpers.tap(params.value,chunk,context);
+ chunk.write(result);
+ return chunk;
+ };
+ var base_context = { };
+ dust.renderSource("plain text. {@tapper value=\"plain text\"/}", base_context, function(err, out) {
+ try {
+ unit.ifError(err);
+ unit.equals(out, "plain text. plain text");
+ } catch(err) {
+ unit.fail(err);
+ return;
+ }
+ unit.pass();
+ });
+ });
+
+ suite.test("tap (string literal that includes a string-valued {context variable})", function() {
+ var unit = this;
+ dust.helpers.tapper = function(chunk, context, bodies, params) {
+ var result = dust.helpers.tap(params.value,chunk,context);
+ chunk.write(result);
+ return chunk;
+ };
+ var base_context = { a:"Alpha" };
+ dust.renderSource("a is {a}. {@tapper value=\"a is {a}\"/}", base_context, function(err, out) {
+ try {
+ unit.ifError(err);
+ unit.equals(out, "a is Alpha. a is Alpha");
+ } catch(err) {
+ unit.fail(err);
+ return;
+ }
+ unit.pass();
+ });
+ });
+
+ suite.test("tap (reference to string-valued context variable)", function() {
+ var unit = this;
+ dust.helpers.tapper = function(chunk, context, bodies, params) {
+ var result = dust.helpers.tap(params.value,chunk,context);
+ chunk.write(result);
+ return chunk;
+ };
+ var base_context = { a:"Alpha" };
+ dust.renderSource("{a}. {@tapper value=a/}", base_context, function(err, out) {
+ try {
+ unit.ifError(err);
+ unit.equals(out, "Alpha. Alpha");
+ } catch(err) {
+ unit.fail(err);
+ return;
+ }
+ unit.pass();
+ });
+ });
+
+ suite.test("tap (string literal that includes a string-valued {context function})", function() {
+ var unit = this;
+ dust.helpers.tapper = function(chunk, context, bodies, params) {
+ var result = dust.helpers.tap(params.value,chunk,context);
+ chunk.write(result);
+ return chunk;
+ };
+ var base_context = { "b":function() { return "beta"; } };
+ dust.renderSource("b is {b}. {@tapper value=\"b is {b}\"/}", base_context, function(err, out) {
+ try {
+ unit.ifError(err);
+ unit.equals(out, "b is beta. b is beta");
+ } catch(err) {
+ unit.fail(err);
+ return;
+ }
+ unit.pass();
+ });
+ });
+
+ suite.test("tap (reference to a a string-valued {context function})", function() {
+ var unit = this;
+ dust.helpers.tapper = function(chunk, context, bodies, params) {
+ var result = dust.helpers.tap(params.value,chunk,context);
+ chunk.write(result);
+ return chunk;
+ };
+ var base_context = { "b":function() { return "beta"; } };
+ dust.renderSource("{b}. {@tapper value=b/}", base_context, function(err, out) {
+ try {
+ unit.ifError(err);
+ unit.equals(out, "beta. beta");
+ } catch(err) {
+ unit.fail(err);
+ return;
+ }
+ unit.pass();
+ });
+ });
+
+ suite.test("tap (string literal that includes an object-valued {context variable})", function() {
+ var unit = this;
+ dust.helpers.tapper = function(chunk, context, bodies, params) {
+ var result = dust.helpers.tap(params.value,chunk,context);
+ chunk.write(result);
+ return chunk;
+ };
+ var base_context = { "a":{"foo":"bar"} };
+ dust.renderSource("a.foo is {a.foo}. {@tapper value=\"a.foo is {a.foo}\"/}", base_context, function(err, out) {
+ try {
+ unit.ifError(err);
+ unit.equals(out, "a.foo is bar. a.foo is bar");
+ } catch(err) {
+ unit.fail(err);
+ return;
+ }
+ unit.pass();
+ });
+ });
+
+ suite.test("tap (reference to an object-valued {context variable})", function() {
+ var unit = this;
+ dust.helpers.tapper = function(chunk, context, bodies, params) {
+ var result = dust.helpers.tap(params.value,chunk,context);
+ chunk.write(result);
+ return chunk;
+ };
+ var base_context = { "a":{"foo":"bar"} };
+ dust.renderSource("{a.foo}. {@tapper value=a.foo/}", base_context, function(err, out) {
+ try {
+ unit.ifError(err);
+ unit.equals(out, "bar. bar");
+ } catch(err) {
+ unit.fail(err);
+ return;
+ }
+ unit.pass();
+ });
+ });
+
+ suite.test("tap (string literal that calls a function within an object-valued {context variable})", function() {
+ var unit = this;
+ dust.helpers.tapper = function(chunk, context, bodies, params) {
+ var result = dust.helpers.tap(params.value,chunk,context);
+ chunk.write(result);
+ return chunk;
+ };
+ var base_context = { "a": {"foo":function() { return "bar"; } } };
+ dust.renderSource("a.foo is {a.foo}. {@tapper value=\"a.foo is {a.foo}\"/}", base_context, function(err, out) {
+ try {
+ unit.ifError(err);
+ unit.equals(out, "a.foo is bar. a.foo is bar");
+ } catch(err) {
+ unit.fail(err);
+ return;
+ }
+ unit.pass();
+ });
+ });
+
+ suite.test("tap (reference to a function within an object-valued {context variable})", function() {
+ var unit = this;
+ dust.helpers.tapper = function(chunk, context, bodies, params) {
+ var result = dust.helpers.tap(params.value,chunk,context);
+ chunk.write(result);
+ return chunk;
+ };
+ var base_context = { "a": {"foo":function() { return "bar"; } } };
+ dust.renderSource("{a.foo} {@tapper value=a.foo/}", base_context, function(err, out) {
+ try {
+ unit.ifError(err);
+ unit.equals(out, "bar bar");
+ } catch(err) {
+ unit.fail(err);
+ return;
+ }
+ unit.pass();
+ });
+ });
}
function testRender(unit, source, context, expected, error) {
View
8 test/jasmine-test/server/specRunner.js
@@ -5,9 +5,15 @@ var jasmine = require('jasmine-node'),
/* this should be declared global in order to access them in the spec*/
dust = require('dustjs-linkedin'),
dust.helpers = require("../../../lib/dust-helpers").helpers,
-
helpersTests = require('../spec/helpersTests');
+//Add the tapper helper to test the Tap helper.
+dust.helpers.tapper = function(chunk, context, bodies, params) {
+ var result = dust.helpers.tap(params.value,chunk,context);
+ chunk.write(result);
+ return chunk;
+};
+
for(key in jasmine)
global[key] = jasmine[key];
View
63 test/jasmine-test/spec/helpersTests.js
@@ -335,6 +335,69 @@ var helpersTests = [
context: {},
expected: "you have 0 new messages",
message: "should test if size Helper is working properly when the value is not submitted at all"
+ },
+ {
+ name: "tapHelper: Plain text string literal",
+ source: 'plain text. {@tapper value="plain text"/}',
+ context: {},
+ expected: "plain text. plain text",
+ message: "should test if tap Helper is working properly when the value is plain text"
+ },
+ {
+ name: "tapHelper: string literal that includes a string-valued {context variable}",
+ source: 'a is {a}. {@tapper value="a is {a}"/}',
+ context: { a : "Alpha" },
+ expected: "a is Alpha. a is Alpha",
+ message: "should test if tap Helper is working properly when the value is a text that inclues a string-valued {context variable}"
+ },
+ {
+ name: "tapHelper: reference to string-valued context variable",
+ source: '{a}. {@tapper value=a/}',
+ context: { a : "Alpha" },
+ expected: "Alpha. Alpha",
+ message: "should test if tap Helper is working properly when it makes referece to string-valued context variable"
+ },
+ {
+ name: "tapHelper: string literal that includes a string-valued {context function}",
+ source: 'b is {b}. {@tapper value="b is {b}"/}',
+ context: { "b" : function() { return "beta"; } },
+ expected: "b is beta. b is beta",
+ message: "should test if tap Helper is working properly when the value is a string literal that includes a string-valued {context function}"
+ },
+ {
+ name: "tapHelper: reference to a a string-valued {context function}",
+ source: '{b}. {@tapper value=b/}',
+ context: { "b" : function() { return "beta"; } },
+ expected: "beta. beta",
+ message: "should test if tap Helper is working properly when it makes reference to a a string-valued {context function}"
+ },
+ {
+ name: "tapHelper: string literal that includes an object-valued {context variable}",
+ source: 'a.foo is {a.foo}. {@tapper value="a.foo is {a.foo}"/}',
+ context: { "a" : {"foo":"bar"} },
+ expected: "a.foo is bar. a.foo is bar",
+ message: "should test if tap Helper is working properly when the value is a string literal that includes an object-valued {context variable}"
+ },
+ {
+ name: "tapHelper: reference to an object-valued {context variable}",
+ source: '{a.foo}. {@tapper value=a.foo/}',
+ context: { "a" : {"foo":"bar"} },
+ expected: "bar. bar",
+ message: "should test if tap Helper is working properly when it makes reference to an object-valued {context variable}"
+ },
+ {
+ name: "tapHelper: string literal that calls a function within an object-valued {context variable}",
+ source: 'a.foo is {a.foo}. {@tapper value="a.foo is {a.foo}"/}',
+ context: { "a" : {"foo" : function() { return "bar"; } } },
+ expected: "a.foo is bar. a.foo is bar",
+ message: "should test if tap Helper is working properly when the value is string literal that calls a function within an object-valued {context variable}"
+ },
+ {
+ name: "tapHelper: reference to a function within an object-valued {context variable}",
+ source: '{a.foo} {@tapper value=a.foo/}',
+ context: { "a" : {"foo" : function() { return "bar"; } } },
+ expected: "bar bar",
+ message: "should test if tap Helper is working properly when it makes reference to a function within an object-valued {context variable}"
}
];
Please sign in to comment.
Something went wrong with that request. Please try again.