Commit 04ff7bb1 04ff7bb132c81a4d04a868fd320ea022318c682e by Nicolas Perriault

added more tests for FunctionArgsInjector

1 parent 6b1f3b76
...@@ -88,8 +88,8 @@ ...@@ -88,8 +88,8 @@
88 exporter.addSuccess("unknown", message); 88 exporter.addSuccess("unknown", message);
89 } else { 89 } else {
90 casper.echo(this.colorize(FAIL, 'RED_BAR') + ' ' + this.formatMessage(message, 'WARNING')); 90 casper.echo(this.colorize(FAIL, 'RED_BAR') + ' ' + this.formatMessage(message, 'WARNING'));
91 this.comment(' got: ' + testValue); 91 this.comment(' got: ' + serialize(testValue));
92 this.comment(' expected: ' + expected); 92 this.comment(' expected: ' + serialize(expected));
93 this.testResults.failed++; 93 this.testResults.failed++;
94 exporter.addFailure("unknown", message, "test failed; expected: " + expected + "; got: " + testValue, "assertEquals"); 94 exporter.addFailure("unknown", message, "test failed; expected: " + expected + "; got: " + testValue, "assertEquals");
95 } 95 }
......
...@@ -146,12 +146,7 @@ function createPage(casper) { ...@@ -146,12 +146,7 @@ function createPage(casper) {
146 * @param Mixed value 146 * @param Mixed value
147 */ 147 */
148 function dump(value) { 148 function dump(value) {
149 if (isType(value, "array")) { 149 console.log(serialize(value));
150 value = value.map(function(prop) {
151 return isType(prop, "function") ? prop.toString().replace(/\s{2,}/, '') : prop;
152 });
153 }
154 console.log(JSON.stringify(value, null, 4));
155 } 150 }
156 151
157 /** 152 /**
...@@ -265,3 +260,18 @@ function replaceFunctionPlaceholders(fn, replacements) { ...@@ -265,3 +260,18 @@ function replaceFunctionPlaceholders(fn, replacements) {
265 } 260 }
266 return fn; 261 return fn;
267 } 262 }
263
264 /**
265 * Serializes a value using JSON.
266 *
267 * @param Mixed value
268 * @return String
269 */
270 function serialize(value) {
271 if (isType(value, "array")) {
272 value = value.map(function(prop) {
273 return isType(prop, "function") ? prop.toString().replace(/\s{2,}/, '') : prop;
274 });
275 }
276 return JSON.stringify(value, null, 4);
277 }
......
...@@ -13,6 +13,40 @@ ...@@ -13,6 +13,40 @@
13 t.assertEquals(extract.body, 'return a + b;', 'FunctionArgsInjector.extract() process function body as expected'); 13 t.assertEquals(extract.body, 'return a + b;', 'FunctionArgsInjector.extract() process function body as expected');
14 t.assertEquals(extract.args, ['a', 'b'], 'FunctionArgsInjector.extract() process function args as expected'); 14 t.assertEquals(extract.args, ['a', 'b'], 'FunctionArgsInjector.extract() process function args as expected');
15 15
16 function Plop(foo, bar) {
17 return 'foo: ' + foo +', bar: ' + bar;
18 }
19 function Plip() { return 'plop'; }
20 function foo_bar(boz) {}
21 var gni = function ($bubu_bibi, __popo__) {};
22 var gno = function ( arg1, /*plop*/ arg2 ) { };
23 t.assertEquals(injector.extract(Plop), {
24 name: 'Plop',
25 args: ['foo', 'bar'],
26 body: "return 'foo: ' + foo +', bar: ' + bar;"
27 }, 'FunctionArgsInjector.extract() handles named functions with arguments and body');
28 t.assertEquals(injector.extract(Plip), {
29 name: 'Plip',
30 args: [],
31 body: "return 'plop';"
32 }, 'FunctionArgsInjector.extract() handles functions with no arguments');
33 t.assertEquals(injector.extract(foo_bar), {
34 name: 'foo_bar',
35 args: ['boz'],
36 body: ""
37 }, 'FunctionArgsInjector.extract() handles functions with no body');
38 t.assertEquals(injector.extract(gni), {
39 name: null,
40 args: ['$bubu_bibi', '__popo__'],
41 body: ""
42 }, 'FunctionArgsInjector.extract() handles anonymous functions with complex args passed');
43 t.assertEquals(injector.extract(gno), {
44 name: null,
45 args: ['arg1', 'arg2'],
46 body: ""
47 }, 'FunctionArgsInjector.extract() handles can filter comments in function args');
48
49 t.comment('FunctionArgsInjector.process()');
16 var processed; 50 var processed;
17 eval('processed = ' + injector.process({ a: 1, b: 2 })); 51 eval('processed = ' + injector.process({ a: 1, b: 2 }));
18 52
......