scratch – Blame information for rev 58

Subversion Repositories:
Rev:
Rev Author Line No. Line
58 office 1 (function(global) {
2 var UNDEFINED,
3 exportObject;
4  
5 if (typeof module !== "undefined" && module.exports) {
6 exportObject = exports;
7 } else {
8 exportObject = global.jasmineReporters = global.jasmineReporters || {};
9 }
10  
11 function elapsed(start, end) { return (end - start)/1000; }
12 function isFailed(obj) { return obj.status === "failed"; }
13 function isSkipped(obj) { return obj.status === "pending"; }
14 function isDisabled(obj) { return obj.status === "disabled"; }
15 function extend(dupe, obj) { // performs a shallow copy of all props of `obj` onto `dupe`
16 for (var prop in obj) {
17 if (obj.hasOwnProperty(prop)) {
18 dupe[prop] = obj[prop];
19 }
20 }
21 return dupe;
22 }
23 function log(str) {
24 var con = global.console || console;
25 if (con && con.log && str && str.length) {
26 con.log(str);
27 }
28 }
29  
30  
31 /**
32 * Basic reporter that outputs spec results to the terminal.
33 * Use this reporter in your build pipeline.
34 *
35 * Usage:
36 *
37 * jasmine.getEnv().addReporter(new jasmineReporters.TerminalReporter(options);
38 *
39 * @param {object} [options]
40 * @param {number} [options.verbosity] meaningful values are 0 through 3; anything
41 * greater than 3 is treated as 3 (default: 2)
42 * @param {boolean} [options.color] print in color or not (default: true)
43 * @param {boolean} [opts.showStack] show stack trace for failed specs (default: false)
44 */
45 var DEFAULT_VERBOSITY = 2,
46 ATTRIBUTES_TO_ANSI = {
47 "off": 0,
48 "bold": 1,
49 "red": 31,
50 "green": 32,
51 "yellow": 33,
52 "blue": 34,
53 "magenta": 35,
54 "cyan": 36
55 };
56  
57 exportObject.TerminalReporter = function(options) {
58 var self = this;
59 self.started = false;
60 self.finished = false;
61  
62 // sanitize arguments
63 options = options || {};
64 self.verbosity = typeof options.verbosity === "number" ? options.verbosity : DEFAULT_VERBOSITY;
65 self.color = options.color;
66 self.showStack = options.showStack;
67  
68 var indent_string = ' ',
69 startTime,
70 currentSuite = null,
71 totalSpecsExecuted = 0,
72 totalSpecsSkipped = 0,
73 totalSpecsDisabled = 0,
74 totalSpecsFailed = 0,
75 totalSpecsDefined,
76 // when use use fit, jasmine never calls suiteStarted / suiteDone, so make a fake one to use
77 fakeFocusedSuite = {
78 id: 'focused',
79 description: 'focused specs',
80 fullName: 'focused specs'
81 };
82  
83 var __suites = {}, __specs = {};
84 function getSuite(suite) {
85 __suites[suite.id] = extend(__suites[suite.id] || {}, suite);
86 return __suites[suite.id];
87 }
88 function getSpec(spec) {
89 __specs[spec.id] = extend(__specs[spec.id] || {}, spec);
90 return __specs[spec.id];
91 }
92  
93 self.jasmineStarted = function(summary) {
94 totalSpecsDefined = summary && summary.totalSpecsDefined || NaN;
95 startTime = exportObject.startTime = new Date();
96 self.started = true;
97 };
98 self.suiteStarted = function(suite) {
99 suite = getSuite(suite);
100 suite._specs = 0;
101 suite._nestedSpecs = 0;
102 suite._failures = 0;
103 suite._nestedFailures = 0;
104 suite._skipped = 0;
105 suite._nestedSkipped = 0;
106 suite._disabled = 0;
107 suite._nestedDisabled = 0;
108 suite._depth = currentSuite ? currentSuite._depth+1 : 1;
109 suite._parent = currentSuite;
110 currentSuite = suite;
111 if (self.verbosity > 2) {
112 log(indentWithLevel(suite._depth, inColor(suite.description, "bold")));
113 }
114 };
115 self.specStarted = function(spec) {
116 if (!currentSuite) {
117 // focused spec (fit) -- suiteStarted was never called
118 self.suiteStarted(fakeFocusedSuite);
119 }
120 spec = getSpec(spec);
121 spec._suite = currentSuite;
122 spec._depth = currentSuite._depth+1;
123 currentSuite._specs++;
124 if (self.verbosity > 2) {
125 log(indentWithLevel(spec._depth, spec.description + ' ...'));
126 }
127 };
128 self.specDone = function(spec) {
129 spec = getSpec(spec);
130 var failed = false,
131 skipped = false,
132 disabled = false,
133 color = 'green',
134 resultText = '';
135 if (isSkipped(spec)) {
136 skipped = true;
137 color = '';
138 spec._suite._skipped++;
139 totalSpecsSkipped++;
140 }
141 if (isFailed(spec)) {
142 failed = true;
143 color = 'red';
144 spec._suite._failures++;
145 totalSpecsFailed++;
146 }
147 if (isDisabled(spec)) {
148 disabled = true;
149 color = 'yellow';
150 spec._suite._disabled++;
151 totalSpecsDisabled++;
152 }
153 totalSpecsExecuted++;
154  
155 if (self.verbosity === 2) {
156 resultText = failed ? 'F' : skipped ? 'S' : disabled ? 'D' : '.';
157 } else if (self.verbosity > 2) {
158 resultText = ' ' + (failed ? 'Failed' : skipped ? 'Skipped' : disabled ? 'Disabled' : 'Passed');
159 }
160 log(inColor(resultText, color));
161  
162 if (failed) {
163 if (self.verbosity === 1) {
164 log(spec.fullName);
165 } else if (self.verbosity === 2) {
166 log(' ');
167 log(indentWithLevel(spec._depth, spec.fullName));
168 }
169  
170 for (var i = 0; i < spec.failedExpectations.length; i++) {
171 log(inColor(indentWithLevel(spec._depth, indent_string + spec.failedExpectations[i].message), color));
172 if (self.showStack){
173 logStackLines(spec._depth, spec.failedExpectations[i].stack.split('\n'));
174 }
175 }
176 }
177 };
178 self.suiteDone = function(suite) {
179 suite = getSuite(suite);
180 if (suite._parent === UNDEFINED) {
181 // disabled suite (xdescribe) -- suiteStarted was never called
182 self.suiteStarted(suite);
183 }
184 if (suite._parent) {
185 suite._parent._specs += suite._specs + suite._nestedSpecs;
186 suite._parent._failures += suite._failures + suite._nestedFailures;
187 suite._parent._skipped += suite._skipped + suite._nestedSkipped;
188 suite._parent._disabled += suite._disabled + suite._nestedDisabled;
189  
190 }
191 currentSuite = suite._parent;
192 if (self.verbosity < 3) {
193 return;
194 }
195  
196 var total = suite._specs + suite._nestedSpecs,
197 failed = suite._failures + suite._nestedFailures,
198 skipped = suite._skipped + suite._nestedSkipped,
199 disabled = suite._disabled + suite._nestedDisabled,
200 passed = total - failed - skipped,
201 color = failed ? 'red+bold' : 'green+bold',
202 str = passed + ' of ' + total + ' passed (' + skipped + ' skipped, ' + disabled + ' disabled)';
203 log(indentWithLevel(suite._depth, inColor(str+'.', color)));
204 };
205 self.jasmineDone = function() {
206 if (currentSuite) {
207 // focused spec (fit) -- suiteDone was never called
208 self.suiteDone(fakeFocusedSuite);
209 }
210 var now = new Date(),
211 dur = elapsed(startTime, now),
212 total = totalSpecsDefined || totalSpecsExecuted,
213 disabled = total - totalSpecsExecuted + totalSpecsDisabled,
214 skipped = totalSpecsSkipped,
215 spec_str = total + (total === 1 ? " spec, " : " specs, "),
216 fail_str = totalSpecsFailed + (totalSpecsFailed === 1 ? " failure, " : " failures, "),
217 skip_str = skipped + " skipped, ",
218 disabled_str = disabled + " disabled in ",
219 summary_str = spec_str + fail_str + skip_str + disabled_str + dur + "s.",
220 result_str = (totalSpecsFailed && "FAILURE: " || "SUCCESS: ") + summary_str,
221 result_color = totalSpecsFailed && "red+bold" || "green+bold";
222  
223 if (self.verbosity === 2) {
224 log('');
225 }
226  
227 if (self.verbosity > 0) {
228 log(inColor(result_str, result_color));
229 }
230 //log("Specs skipped but not reported (entire suite skipped or targeted to specific specs)", totalSpecsDefined - totalSpecsExecuted + totalSpecsDisabled);
231  
232 self.finished = true;
233 // this is so phantomjs-testrunner.js can tell if we're done executing
234 exportObject.endTime = now;
235 };
236 function indentWithLevel(level, string) {
237 return new Array(level).join(indent_string) + string;
238 }
239 function logStackLines(depth, lines) {
240 lines.forEach(function(line){
241 log(inColor(indentWithLevel(depth, indent_string + line), 'magenta'));
242 });
243 }
244 function inColor(string, color) {
245 var color_attributes = color && color.split("+"),
246 ansi_string = "",
247 i;
248  
249 if (!self.color || !color_attributes) {
250 return string;
251 }
252  
253 for(i = 0; i < color_attributes.length; i++) {
254 ansi_string += "\033[" + ATTRIBUTES_TO_ANSI[color_attributes[i]] + "m";
255 }
256 ansi_string += string + "\033[" + ATTRIBUTES_TO_ANSI["off"] + "m";
257  
258 return ansi_string;
259 }
260 };
261 })(this);