Skip to content

Commit

Permalink
Extend contextDump helper to support current/full stack dump and dump…
Browse files Browse the repository at this point in the history
… to console or output. Make it dump functions readably. Add tests.
  • Loading branch information
rragan committed Aug 25, 2012
1 parent 71a4fc9 commit 39aa3a7
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 4 deletions.
38 changes: 35 additions & 3 deletions lib/dust-helpers.js
Expand Up @@ -20,6 +20,14 @@ function isSelect(context) {
return typeof value === "object" && value.isSelect === true;
}

// Display functions as strings for contextDump
function jsonFilter(key, value) {
if (typeof value === "function") {
return value.toString();
}
return value;
}

function filter(chunk, context, bodies, params, filter) {
var params = params || {},
actual,
Expand Down Expand Up @@ -74,9 +82,33 @@ var helpers = {
return bodies.block(chunk, context.push(context.stack.index));
},

contextDump: function(chunk, context, bodies) {
_console.log(JSON.stringify(context.stack.head));
return chunk;
/**
* contextDump helper
* @param key specifies how much to dump.
* "current" dumps current context. "full" dumps the full context stack.
* @param to specifies where to write dump output.
* Values can be "console" or "output". Default is output.
*/
contextDump: function(chunk, context, bodies, params) {
var p = params || {},
to = p.to || 'output',
key = p.key || 'current',
dump;
to = dust.helpers.tap(to, chunk, context),
key = dust.helpers.tap(key, chunk, context);
if (key === 'full') {
dump = JSON.stringify(context.stack, jsonFilter, 2);
}
else {
dump = JSON.stringify(context.stack.head, jsonFilter, 2);
}
if (to === 'console') {
_console.log(dump);
return chunk;
}
else {
return chunk.write(dump);
}
},

// Utility helping to resolve dust references in the given chunk
Expand Down
30 changes: 29 additions & 1 deletion test/jasmine-test/spec/helpersTests.js
Expand Up @@ -531,11 +531,39 @@ var helpersTests = [
context: {"A": [ {"B": [ {"C": ["Ca1", "C2"]}, {"C": ["Ca2", "Ca22"]} ] }, {"B": [ {"C": ["Cb1", "C2"]}, {"C": ["Cb2", "Ca2"]} ] } ] },
expected: "A loop:0-2,B loop:0-2C[0]=Ca1 B loop:1-2C[0]=Ca2 A loop trailing: 0-2A loop:1-2,B loop:0-2C[0]=Cb1 B loop:1-2C[0]=Cb2 A loop trailing: 1-2",
message: "test array reference $idx/$len nested loops"
},
{
name: "contextDump simple test",
source: "{@contextDump/}",
context: {A:2, B:3},
expected: "{\n \"A\": 2,\n \"B\": 3\n}",
message: "contextDump simple test"
},
{
name: "contextDump simple test dump to console",
source: "{@contextDump to=\"console\"/}",
context: {A:2, B:3},
expected: "",
message: "contextDump simple test"
},
{
name: "contextDump full test",
source: "{@contextDump key=\"full\"/}",
context: {aa:{A:2, B:3}},
expected: "{\n \"isObject\": true,\n \"head\": {\n \"aa\": {\n \"A\": 2,\n \"B\": 3\n }\n }\n}",
message: "contextDump full test"
},
{
name: "contextDump function dump test",
source: "{#aa param=\"{p}\"}{@contextDump key=\"full\"/}{/aa}",
context: {aa:["a"],p:42},
expected: "{\n \"tail\": {\n \"tail\": {\n \"isObject\": true,\n \"head\": {\n \"aa\": [\n \"a\"\n ],\n \"p\": 42\n }\n },\n \"isObject\": true,\n \"head\": {\n \"param\": \"function body_2(chk,ctx){return chk.reference(ctx.get(\\\"p\\\"),ctx,\\\"h\\\");}\",\n \"$len\": 1,\n \"$idx\": 0\n }\n },\n \"isObject\": false,\n \"head\": \"a\",\n \"index\": 0,\n \"of\": 1\n}",
message: "contextDump function dump test"
}
];

if (typeof module !== "undefined" && typeof require !== "undefined") {
module.exports = helpersTests; // We're on node.js
} else {
window.helpersTests = helpersTests; // We're on the browser
}
}

0 comments on commit 39aa3a7

Please sign in to comment.