Commit 04ff7bb1 04ff7bb132c81a4d04a868fd320ea022318c682e by Nicolas Perriault

added more tests for FunctionArgsInjector

1 parent 6b1f3b76
......@@ -88,8 +88,8 @@
exporter.addSuccess("unknown", message);
} else {
casper.echo(this.colorize(FAIL, 'RED_BAR') + ' ' + this.formatMessage(message, 'WARNING'));
this.comment(' got: ' + testValue);
this.comment(' expected: ' + expected);
this.comment(' got: ' + serialize(testValue));
this.comment(' expected: ' + serialize(expected));
this.testResults.failed++;
exporter.addFailure("unknown", message, "test failed; expected: " + expected + "; got: " + testValue, "assertEquals");
}
......
......@@ -146,12 +146,7 @@ function createPage(casper) {
* @param Mixed value
*/
function dump(value) {
if (isType(value, "array")) {
value = value.map(function(prop) {
return isType(prop, "function") ? prop.toString().replace(/\s{2,}/, '') : prop;
});
}
console.log(JSON.stringify(value, null, 4));
console.log(serialize(value));
}
/**
......@@ -265,3 +260,18 @@ function replaceFunctionPlaceholders(fn, replacements) {
}
return fn;
}
/**
* Serializes a value using JSON.
*
* @param Mixed value
* @return String
*/
function serialize(value) {
if (isType(value, "array")) {
value = value.map(function(prop) {
return isType(prop, "function") ? prop.toString().replace(/\s{2,}/, '') : prop;
});
}
return JSON.stringify(value, null, 4);
}
......
......@@ -13,6 +13,40 @@
t.assertEquals(extract.body, 'return a + b;', 'FunctionArgsInjector.extract() process function body as expected');
t.assertEquals(extract.args, ['a', 'b'], 'FunctionArgsInjector.extract() process function args as expected');
function Plop(foo, bar) {
return 'foo: ' + foo +', bar: ' + bar;
}
function Plip() { return 'plop'; }
function foo_bar(boz) {}
var gni = function ($bubu_bibi, __popo__) {};
var gno = function ( arg1, /*plop*/ arg2 ) { };
t.assertEquals(injector.extract(Plop), {
name: 'Plop',
args: ['foo', 'bar'],
body: "return 'foo: ' + foo +', bar: ' + bar;"
}, 'FunctionArgsInjector.extract() handles named functions with arguments and body');
t.assertEquals(injector.extract(Plip), {
name: 'Plip',
args: [],
body: "return 'plop';"
}, 'FunctionArgsInjector.extract() handles functions with no arguments');
t.assertEquals(injector.extract(foo_bar), {
name: 'foo_bar',
args: ['boz'],
body: ""
}, 'FunctionArgsInjector.extract() handles functions with no body');
t.assertEquals(injector.extract(gni), {
name: null,
args: ['$bubu_bibi', '__popo__'],
body: ""
}, 'FunctionArgsInjector.extract() handles anonymous functions with complex args passed');
t.assertEquals(injector.extract(gno), {
name: null,
args: ['arg1', 'arg2'],
body: ""
}, 'FunctionArgsInjector.extract() handles can filter comments in function args');
t.comment('FunctionArgsInjector.process()');
var processed;
eval('processed = ' + injector.process({ a: 1, b: 2 }));
......