Commit ff9e8845 ff9e88459131ced3893439a02f35a581c307a5f3 by Nicolas Perriault

added event emitting to Tester.assertMatch()

1 parent e6c6640a
...@@ -121,7 +121,7 @@ var Tester = function(casper, options) { ...@@ -121,7 +121,7 @@ var Tester = function(casper, options) {
121 this.comment(' got: ' + utils.serialize(testValue)); 121 this.comment(' got: ' + utils.serialize(testValue));
122 this.comment(' expected: ' + utils.serialize(expected)); 122 this.comment(' expected: ' + utils.serialize(expected));
123 this.testResults.failed++; 123 this.testResults.failed++;
124 exporter.addFailure("unknown", message, "test failed; expected: " + expected + "; got: " + testValue, "assertEquals"); 124 exporter.addFailure("unknown", message, f("test failed; expected: %s; got: %s", expected, testValue), "assertEquals");
125 } 125 }
126 this.emit(eventName, { 126 this.emit(eventName, {
127 message: message, 127 message: message,
...@@ -170,17 +170,24 @@ var Tester = function(casper, options) { ...@@ -170,17 +170,24 @@ var Tester = function(casper, options) {
170 * @param String message Test description 170 * @param String message Test description
171 */ 171 */
172 this.assertMatch = function(subject, pattern, message) { 172 this.assertMatch = function(subject, pattern, message) {
173 var eventName;
173 if (pattern.test(subject)) { 174 if (pattern.test(subject)) {
175 eventName = "success";
174 casper.echo(this.colorize(PASS, 'INFO') + ' ' + this.formatMessage(message)); 176 casper.echo(this.colorize(PASS, 'INFO') + ' ' + this.formatMessage(message));
175 this.testResults.passed++; 177 this.testResults.passed++;
176 exporter.addSuccess("unknown", message); 178 exporter.addSuccess("unknown", message);
177 } else { 179 } else {
180 eventName = "fail";
178 casper.echo(this.colorize(FAIL, 'RED_BAR') + ' ' + this.formatMessage(message, 'WARNING')); 181 casper.echo(this.colorize(FAIL, 'RED_BAR') + ' ' + this.formatMessage(message, 'WARNING'));
179 this.comment(' subject: ' + subject); 182 this.comment(' subject: ' + subject);
180 this.comment(' pattern: ' + pattern.toString()); 183 this.comment(' pattern: ' + pattern.toString());
181 this.testResults.failed++; 184 this.testResults.failed++;
182 exporter.addFailure("unknown", message, "test failed; subject: " + subject + "; pattern: " + pattern.toString(), "assertMatch"); 185 exporter.addFailure("unknown", message, f("test failed; subject: %s; pattern: %s", subject, pattern.toString()), "assertMatch");
183 } 186 }
187 this.emit(eventName, {
188 message: message,
189 file: this.currentTestFile
190 });
184 }; 191 };
185 192
186 /** 193 /**
...@@ -403,14 +410,14 @@ var Tester = function(casper, options) { ...@@ -403,14 +410,14 @@ var Tester = function(casper, options) {
403 } 410 }
404 casper.echo(f("\nDetails for the %d failed tests:\n", failures.length), "PARAMETER"); 411 casper.echo(f("\nDetails for the %d failed tests:\n", failures.length), "PARAMETER");
405 failures.forEach(function(failure) { 412 failures.forEach(function(failure) {
406 casper.echo('In ' + failure.file + ':'); 413 casper.echo(f('In %s:', failure.file));
407 var message; 414 var message;
408 if (utils.isType(failure.message, "object") && failure.message.stack) { 415 if (utils.isType(failure.message, "object") && failure.message.stack) {
409 message = failure.message.stack; 416 message = failure.message.stack;
410 } else { 417 } else {
411 message = failure.message; 418 message = failure.message;
412 } 419 }
413 casper.echo(' ' + message, "COMMENT"); 420 casper.echo(f(' %s', message), "COMMENT");
414 }); 421 });
415 }; 422 };
416 423
...@@ -425,7 +432,7 @@ var Tester = function(casper, options) { ...@@ -425,7 +432,7 @@ var Tester = function(casper, options) {
425 if (total === 0) { 432 if (total === 0) {
426 statusText = FAIL; 433 statusText = FAIL;
427 style = 'RED_BAR'; 434 style = 'RED_BAR';
428 result = statusText + " Looks like you didn't run any test."; 435 result = f("%s Looks like you didn't run any test.", statusText);
429 } else { 436 } else {
430 if (this.testResults.failed > 0) { 437 if (this.testResults.failed > 0) {
431 statusText = FAIL; 438 statusText = FAIL;
...@@ -434,7 +441,8 @@ var Tester = function(casper, options) { ...@@ -434,7 +441,8 @@ var Tester = function(casper, options) {
434 statusText = PASS; 441 statusText = PASS;
435 style = 'GREEN_BAR'; 442 style = 'GREEN_BAR';
436 } 443 }
437 result = statusText + ' ' + total + ' tests executed, ' + this.testResults.passed + ' passed, ' + this.testResults.failed + ' failed.'; 444 result = f('%s %s tests executed, %d passed, %d failed.',
445 statusText, total, this.testResults.passed, this.testResults.failed);
438 } 446 }
439 casper.echo(this.colorize(utils.fillBlanks(result), style)); 447 casper.echo(this.colorize(utils.fillBlanks(result), style));
440 if (this.testResults.failed > 0) { 448 if (this.testResults.failed > 0) {
...@@ -443,9 +451,9 @@ var Tester = function(casper, options) { ...@@ -443,9 +451,9 @@ var Tester = function(casper, options) {
443 if (save && utils.isFunction(require)) { 451 if (save && utils.isFunction(require)) {
444 try { 452 try {
445 fs.write(save, exporter.getXML(), 'w'); 453 fs.write(save, exporter.getXML(), 'w');
446 casper.echo('result log stored in ' + save, 'INFO'); 454 casper.echo(f('Result log stored in %s', save), 'INFO');
447 } catch (e) { 455 } catch (e) {
448 casper.echo('unable to write results to ' + save + '; ' + e, 'ERROR'); 456 casper.echo(f('Unable to write results to %s: %s', save, e), 'ERROR');
449 } 457 }
450 } 458 }
451 if (exit === true) { 459 if (exit === true) {
...@@ -464,7 +472,7 @@ var Tester = function(casper, options) { ...@@ -464,7 +472,7 @@ var Tester = function(casper, options) {
464 } 472 }
465 Array.prototype.forEach.call(arguments, function(path) { 473 Array.prototype.forEach.call(arguments, function(path) {
466 if (!fs.exists(path)) { 474 if (!fs.exists(path)) {
467 self.bar("Path " + path + " doesn't exist", "RED_BAR"); 475 self.bar(f("Path %s doesn't exist", path), "RED_BAR");
468 } 476 }
469 if (fs.isDirectory(path)) { 477 if (fs.isDirectory(path)) {
470 testFiles = testFiles.concat(self.findTestFiles(path)); 478 testFiles = testFiles.concat(self.findTestFiles(path));
...@@ -496,7 +504,7 @@ var Tester = function(casper, options) { ...@@ -496,7 +504,7 @@ var Tester = function(casper, options) {
496 * 504 *
497 */ 505 */
498 this.runTest = function(testFile) { 506 this.runTest = function(testFile) {
499 this.bar('Test file: ' + testFile, 'INFO_BAR'); 507 this.bar(f('Test file: %s', testFile), 'INFO_BAR');
500 this.running = true; // this.running is set back to false with done() 508 this.running = true; // this.running is set back to false with done()
501 try { 509 try {
502 this.exec(testFile); 510 this.exec(testFile);
......