Commit 43852c08 43852c08bd51f977360d03dffdbae7cb84807d7b by Nicolas Perriault

Casper has a new Colorizer object which can add color to CLI output http://twitpic.com/6yy8vb

1 parent 2a9e2c55
......@@ -66,6 +66,7 @@
};
// local properties
this.checker = null;
this.colorizer = new phantom.Casper.Colorizer();
this.currentUrl = 'about:blank';
this.currentHTTPStatus = 200;
this.loadInProgress = false;
......@@ -832,6 +833,54 @@
};
/**
* This is a port of lime colorizer.
* http://trac.symfony-project.org/browser/tools/lime/trunk/lib/lime.php)
*
* (c) Fabien Potencier, Symfony project, MIT license
*/
phantom.Casper.Colorizer = function() {
var options = { bold: 1, underscore: 4, blink: 5, reverse: 7, conceal: 8 }
, foreground = { black: 30, red: 31, green: 32, yellow: 33, blue: 34, magenta: 35, cyan: 36, white: 37 }
, background = { black: 40, red: 41, green: 42, yellow: 43, blue: 44, magenta: 45, cyan: 46, white: 47 }
, styles = {
'ERROR': { bg: 'red', fg: 'white', bold: true },
'INFO': { fg: 'green', bold: true },
'TRACE': { fg: 'green', bold: true },
'PARAMETER': { fg: 'cyan' },
'COMMENT': { fg: 'yellow' },
'GREEN_BAR': { fg: 'white', bg: 'green', bold: true },
'RED_BAR': { fg: 'white', bg: 'red', bold: true },
'INFO_BAR': { fg: 'cyan', bold: true }
};
this.colorize = function(text, styleName) {
if (styleName in styles) {
return this.format(text, styles[styleName]);
}
return text;
};
this.format = function(text, style) {
if (typeof style !== "object") {
return text;
}
var codes = [];
if (style.fg && foreground[style.fg]) {
codes.push(foreground[style.fg]);
}
if (style.bg && foreground[style.bg]) {
codes.push(foreground[style.bg]);
}
for (var option in options) {
if (style[option] === true) {
codes.push(options[option]);
}
}
return "\033[" + codes.join(';') + 'm' + text + "\033[0m";
};
};
/**
* Creates a new WebPage instance for Casper use.
*
* @param Casper casper A Casper instance
......
var testResults = {
passed: 0,
failed: 0
};
}, PASS = 'PASS', FAIL = 'FAIL';
phantom.Casper.extend({
assert: function(condition, message) {
var status = '[PASS]';
var status = PASS;
if (condition === true) {
style = 'INFO';
testResults.passed++;
} else {
status = '[FAIL]';
status = FAIL;
style = 'ERROR';
testResults.failed++;
}
this.echo([status, message].join(' '));
this.echo([this.colorizer.colorize(status, style), this.formatMessage(message)].join(' '));
},
assertEquals: function(testValue, expected, message) {
if (expected === testValue) {
this.echo('[PASS] ' + message);
this.echo(this.colorizer.colorize(PASS, 'INFO') + ' ' + this.formatMessage(message));
testResults.passed++;
} else {
this.echo('[FAIL] ' + message);
this.echo(' got: ' + testValue);
this.echo(' expected: ' + expected);
this.echo(this.colorizer.colorize(FAIL, 'ERROR') + ' ' + this.formatMessage(message));
this.echo(' got: ' + testValue);
this.echo(' expected: ' + expected);
testResults.failed++;
}
},
......@@ -49,11 +51,25 @@ phantom.Casper.extend({
return this.assertMatch(this.getCurrentUrl(), pattern, message);
},
formatMessage: function(message) {
var parts = /(\w+\(\))(.*)/.exec(message);
if (!parts) {
return message;
}
return this.colorizer.colorize(parts[1], 'PARAMETER') + parts[2];
},
renderResults: function() {
this.echo("==========================================");
var total = testResults.passed + testResults.failed,
status = testResults.failed > 0 ? 'FAIL' : 'OK';
this.echo(status + ': ' + total + ' tests executed, ' + testResults.passed + ' passed, ' + testResults.failed + ' failed.');
var total = testResults.passed + testResults.failed, status, style, result;
if (testResults.failed > 0) {
status = FAIL;
style = 'RED_BAR';
} else {
status = PASS;
style = 'GREEN_BAR';
}
result = status + ' ' + total + ' tests executed, ' + testResults.passed + ' passed, ' + testResults.failed + ' failed.';
this.echo(this.colorizer.colorize(result, style));
this.exit();
}
});
\ No newline at end of file
......