Skip to content

Commit

Permalink
Allow tests to test log messages as well. Add some log message tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Seth Kinast committed Mar 7, 2015
1 parent 75da720 commit 4655dd5
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 32 deletions.
14 changes: 5 additions & 9 deletions lib/dust-helpers.js
Expand Up @@ -264,19 +264,15 @@ var helpers = {
// operand can be null for "abs", ceil and floor
operand = params.operand,
round = params.round,
mathOut = null,
operError = function(){
_log("operand is required for this math method");
return null;
};
mathOut = null;

key = parseFloat(dust.helpers.tap(key, chunk, context));
operand = parseFloat(dust.helpers.tap(operand, chunk, context));
// TODO: handle and tests for negatives and floats in all math operations
switch(method) {
case "mod":
if(operand === 0 || operand === -0) {
_log("operand for divide operation is 0/-0: expect Nan!");
_log("Division by 0 in {@math} helper", "WARN");
}
mathOut = key % operand;
break;
Expand All @@ -291,7 +287,7 @@ var helpers = {
break;
case "divide":
if(operand === 0 || operand === -0) {
_log("operand for divide operation is 0/-0: expect Nan/Infinity!");
_log("Division by 0 in {@math} helper", "WARN");
}
mathOut = key / operand;
break;
Expand All @@ -311,7 +307,7 @@ var helpers = {
mathOut = parseInt(key, 10);
break;
default:
_log("method passed is not supported");
_log("{@math}: method " + method + " not supported");
}

if (mathOut !== null){
Expand Down Expand Up @@ -367,7 +363,7 @@ var helpers = {
_log("Missing body block in {@select}");
}
} else {
_log("No key provided for {@select}");
_log("No key provided for {@select}", "WARN");
}
return chunk;
},
Expand Down
27 changes: 16 additions & 11 deletions test/jasmine-test/spec/helpersTests.js
Expand Up @@ -49,7 +49,8 @@
source: '<div>{@math key="0" method="mod" operand="0"/}</div>',
context: {},
expected: "<div>NaN</div>",
message: "testing math/mod helper with zero as operand value"
message: "testing math/mod helper with zero as operand value",
log: "Division by 0 in {@math} helper"
},
{
name: "math/mod helper with negative zero as operand value",
Expand Down Expand Up @@ -101,7 +102,7 @@
message: "should test the math/mod helper using $idx without quotes"
},
{
name: "math helper mod using $idx without quotes with lizt size = 2",
name: "math helper mod using $idx without quotes with list size = 2",
source: '{#list}<div>{@math key=$idx method="mod" operand="2"/}</div>{/list}',
context: { list: [ { y: 'foo' }, { y: "bar"} ]},
expected: "<div>0</div><div>1</div>",
Expand Down Expand Up @@ -189,7 +190,8 @@
source: '{@math key="-0" method="blah" operand="-4"/}',
context: {},
expected: "",
message: "math helper blah operation"
message: "math helper blah operation",
log: "{@math}: method blah not supported"
},
{
name: "math helper key as zero",
Expand Down Expand Up @@ -439,14 +441,15 @@
source: "{@eq key=\"foo\" value=\"foo\"/}",
context: {},
expected: "",
message: "eq helper with no body silently fails with console log"
message: "eq helper with no body is silent"
},
{
name: "eq helper with no params",
source: "{@eq}Hello{:else}Goodbye{/eq}",
context: {},
expected: "",
message: "eq helper with no params does not execute"
message: "eq helper with no params does not execute",
log: "No key specified for filter in {@eq}"
},
{
name: "eq helper with key that resolves to undefined",
Expand Down Expand Up @@ -540,7 +543,7 @@
source: "{@ne key=\"foo\" value=\"foo\"/}",
context: {},
expected: "",
message: "ne helper with no body silently fails with console log"
message: "ne helper with no body is silent"
},
{
name: "ne helper with no params",
Expand Down Expand Up @@ -608,7 +611,7 @@
source: "{@lt key=\"2\" value=\"3\" type=\"number\"/}",
context: {},
expected: "",
message: "lt helper with no body silently fails with console log"
message: "lt helper with no body is silent"
},
{
name: "lt helper with no params",
Expand Down Expand Up @@ -711,21 +714,21 @@
source: "{@lte key=\"2\" value=\"3\" type=\"number\"/}",
context: {},
expected: "",
message: "lte helper with no body silently fails with console log"
message: "lte helper with no body is silent"
},
{
name: "gt helper with no body",
source: "{@gt key=\"5\" value=\"3\" type=\"number\"/}",
context: {},
expected: "",
message: "gt helper with no body silently fails with console log"
message: "gt helper with no body is silent"
},
{
name: "gte helper with no body",
source: "{@gte key=\"5\" value=\"3\" type=\"number\"/}",
context: {},
expected: "",
message: "gte helper with no body silently fails with console log"
message: "gte helper with no body is silent"
}
]
},
Expand All @@ -737,7 +740,8 @@
source: "{@select key=\"foo\"/}",
context: {},
expected: "",
message: "select helper with no body silently fails with console log"
message: "select helper with no body is silent",
log: "Missing body block in {@select}"
},
{
name: "select helper with a constant string and condition eq",
Expand Down Expand Up @@ -927,6 +931,7 @@
"{/select}{/b}"].join("\n"),
context: { b : { z: "foo", x: "bar" } },
expected: "",
log: "No key provided for {@select}",
message: "should test select helper with missing key in the context and hence no output"
},
{
Expand Down
44 changes: 32 additions & 12 deletions test/jasmine-test/spec/renderTestSpec.js
Expand Up @@ -12,19 +12,32 @@

dust.debugLevel = "DEBUG";

function render(test) {
return function() {
try {
dust.loadSource(dust.compile(test.source, test.name));
dust.render(test.name, test.context, function(err, output) {
expect(err).toBeNull();
expect(output).toEqual(test.expected);
});
}
catch (error) {
expect(error.message).toEqual(test.error || {} );
function messageInLog(log, message, level) {
var i;
for(i = 0; i < log.length; i++) {
if(log[i].message === message) {
return (!level || log[i].level === level);
}
}
};
return false;
}

function render(test) {
return function() {
try {
dust.loadSource(dust.compile(test.source, test.name));
dust.render(test.name, test.context, function(err, output) {
expect(err).toBeNull();
expect(output).toEqual(test.expected);
if(test.log) {
expect(messageInLog(dust.logQueue, test.log)).toEqual(true);
}
});
}
catch (error) {
expect(error.message).toEqual(test.error || {} );
}
};
}

function stream(test) {
Expand All @@ -34,6 +47,7 @@
flag = false;
output = "";
try {
dust.logQueue = [];
dust.loadSource(dust.compile(test.source, test.name));
dust.stream(test.name, test.context)
.on("data", function(data) {
Expand Down Expand Up @@ -61,6 +75,9 @@
} else {
expect(output).toEqual(test.expected);
}
if(test.log) {
expect(messageInLog(dust.logQueue, test.log)).toEqual(true);
}
});
};
}
Expand Down Expand Up @@ -101,6 +118,9 @@
} else {
expect(output).toEqual(test.expected);
}
if(test.log) {
expect(messageInLog(dust.logQueue, test.log)).toEqual(true);
}
});
};
}
Expand Down

0 comments on commit 4655dd5

Please sign in to comment.