Casper has a new Colorizer object which can add color to CLI output http://twitpic.com/6yy8vb
Showing
2 changed files
with
77 additions
and
12 deletions
... | @@ -66,6 +66,7 @@ | ... | @@ -66,6 +66,7 @@ |
66 | }; | 66 | }; |
67 | // local properties | 67 | // local properties |
68 | this.checker = null; | 68 | this.checker = null; |
69 | this.colorizer = new phantom.Casper.Colorizer(); | ||
69 | this.currentUrl = 'about:blank'; | 70 | this.currentUrl = 'about:blank'; |
70 | this.currentHTTPStatus = 200; | 71 | this.currentHTTPStatus = 200; |
71 | this.loadInProgress = false; | 72 | this.loadInProgress = false; |
... | @@ -832,6 +833,54 @@ | ... | @@ -832,6 +833,54 @@ |
832 | }; | 833 | }; |
833 | 834 | ||
834 | /** | 835 | /** |
836 | * This is a port of lime colorizer. | ||
837 | * http://trac.symfony-project.org/browser/tools/lime/trunk/lib/lime.php) | ||
838 | * | ||
839 | * (c) Fabien Potencier, Symfony project, MIT license | ||
840 | */ | ||
841 | phantom.Casper.Colorizer = function() { | ||
842 | var options = { bold: 1, underscore: 4, blink: 5, reverse: 7, conceal: 8 } | ||
843 | , foreground = { black: 30, red: 31, green: 32, yellow: 33, blue: 34, magenta: 35, cyan: 36, white: 37 } | ||
844 | , background = { black: 40, red: 41, green: 42, yellow: 43, blue: 44, magenta: 45, cyan: 46, white: 47 } | ||
845 | , styles = { | ||
846 | 'ERROR': { bg: 'red', fg: 'white', bold: true }, | ||
847 | 'INFO': { fg: 'green', bold: true }, | ||
848 | 'TRACE': { fg: 'green', bold: true }, | ||
849 | 'PARAMETER': { fg: 'cyan' }, | ||
850 | 'COMMENT': { fg: 'yellow' }, | ||
851 | 'GREEN_BAR': { fg: 'white', bg: 'green', bold: true }, | ||
852 | 'RED_BAR': { fg: 'white', bg: 'red', bold: true }, | ||
853 | 'INFO_BAR': { fg: 'cyan', bold: true } | ||
854 | }; | ||
855 | |||
856 | this.colorize = function(text, styleName) { | ||
857 | if (styleName in styles) { | ||
858 | return this.format(text, styles[styleName]); | ||
859 | } | ||
860 | return text; | ||
861 | }; | ||
862 | |||
863 | this.format = function(text, style) { | ||
864 | if (typeof style !== "object") { | ||
865 | return text; | ||
866 | } | ||
867 | var codes = []; | ||
868 | if (style.fg && foreground[style.fg]) { | ||
869 | codes.push(foreground[style.fg]); | ||
870 | } | ||
871 | if (style.bg && foreground[style.bg]) { | ||
872 | codes.push(foreground[style.bg]); | ||
873 | } | ||
874 | for (var option in options) { | ||
875 | if (style[option] === true) { | ||
876 | codes.push(options[option]); | ||
877 | } | ||
878 | } | ||
879 | return "\033[" + codes.join(';') + 'm' + text + "\033[0m"; | ||
880 | }; | ||
881 | }; | ||
882 | |||
883 | /** | ||
835 | * Creates a new WebPage instance for Casper use. | 884 | * Creates a new WebPage instance for Casper use. |
836 | * | 885 | * |
837 | * @param Casper casper A Casper instance | 886 | * @param Casper casper A Casper instance | ... | ... |
1 | var testResults = { | 1 | var testResults = { |
2 | passed: 0, | 2 | passed: 0, |
3 | failed: 0 | 3 | failed: 0 |
4 | }; | 4 | }, PASS = 'PASS', FAIL = 'FAIL'; |
5 | 5 | ||
6 | phantom.Casper.extend({ | 6 | phantom.Casper.extend({ |
7 | assert: function(condition, message) { | 7 | assert: function(condition, message) { |
8 | var status = '[PASS]'; | 8 | var status = PASS; |
9 | if (condition === true) { | 9 | if (condition === true) { |
10 | style = 'INFO'; | ||
10 | testResults.passed++; | 11 | testResults.passed++; |
11 | } else { | 12 | } else { |
12 | status = '[FAIL]'; | 13 | status = FAIL; |
14 | style = 'ERROR'; | ||
13 | testResults.failed++; | 15 | testResults.failed++; |
14 | } | 16 | } |
15 | this.echo([status, message].join(' ')); | 17 | this.echo([this.colorizer.colorize(status, style), this.formatMessage(message)].join(' ')); |
16 | }, | 18 | }, |
17 | 19 | ||
18 | assertEquals: function(testValue, expected, message) { | 20 | assertEquals: function(testValue, expected, message) { |
19 | if (expected === testValue) { | 21 | if (expected === testValue) { |
20 | this.echo('[PASS] ' + message); | 22 | this.echo(this.colorizer.colorize(PASS, 'INFO') + ' ' + this.formatMessage(message)); |
21 | testResults.passed++; | 23 | testResults.passed++; |
22 | } else { | 24 | } else { |
23 | this.echo('[FAIL] ' + message); | 25 | this.echo(this.colorizer.colorize(FAIL, 'ERROR') + ' ' + this.formatMessage(message)); |
24 | this.echo(' got: ' + testValue); | 26 | this.echo(' got: ' + testValue); |
25 | this.echo(' expected: ' + expected); | 27 | this.echo(' expected: ' + expected); |
26 | testResults.failed++; | 28 | testResults.failed++; |
27 | } | 29 | } |
28 | }, | 30 | }, |
... | @@ -49,11 +51,25 @@ phantom.Casper.extend({ | ... | @@ -49,11 +51,25 @@ phantom.Casper.extend({ |
49 | return this.assertMatch(this.getCurrentUrl(), pattern, message); | 51 | return this.assertMatch(this.getCurrentUrl(), pattern, message); |
50 | }, | 52 | }, |
51 | 53 | ||
54 | formatMessage: function(message) { | ||
55 | var parts = /(\w+\(\))(.*)/.exec(message); | ||
56 | if (!parts) { | ||
57 | return message; | ||
58 | } | ||
59 | return this.colorizer.colorize(parts[1], 'PARAMETER') + parts[2]; | ||
60 | }, | ||
61 | |||
52 | renderResults: function() { | 62 | renderResults: function() { |
53 | this.echo("=========================================="); | 63 | var total = testResults.passed + testResults.failed, status, style, result; |
54 | var total = testResults.passed + testResults.failed, | 64 | if (testResults.failed > 0) { |
55 | status = testResults.failed > 0 ? 'FAIL' : 'OK'; | 65 | status = FAIL; |
56 | this.echo(status + ': ' + total + ' tests executed, ' + testResults.passed + ' passed, ' + testResults.failed + ' failed.'); | 66 | style = 'RED_BAR'; |
67 | } else { | ||
68 | status = PASS; | ||
69 | style = 'GREEN_BAR'; | ||
70 | } | ||
71 | result = status + ' ' + total + ' tests executed, ' + testResults.passed + ' passed, ' + testResults.failed + ' failed.'; | ||
72 | this.echo(this.colorizer.colorize(result, style)); | ||
57 | this.exit(); | 73 | this.exit(); |
58 | } | 74 | } |
59 | }); | 75 | }); |
... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
-
Please register or sign in to post a comment