1 /** JavaScript API reporter. 2 * 3 * @constructor 4 */ 5 jasmine.EnvjsReporter = function() { 6 this.started = false; 7 this.finished = false; 8 this.suites_ = []; 9 this.results_ = {}; 10 }; 11 12 jasmine.EnvjsReporter.prototype.reportRunnerStarting = function(runner) { 13 this.started = true; 14 var suites = runner.suites(); 15 for (var i = 0; i < suites.length; i++) { 16 var suite = suites[i]; 17 this.suites_.push(this.summarize_(suite)); 18 } 19 this.startedAt = new Date(); 20 }; 21 22 jasmine.EnvjsReporter.prototype.suites = function() { 23 return this.suites_; 24 }; 25 26 jasmine.EnvjsReporter.prototype.summarize_ = function(suiteOrSpec) { 27 var isSuite = suiteOrSpec instanceof jasmine.Suite 28 var summary = { 29 id: suiteOrSpec.id, 30 name: suiteOrSpec.description, 31 type: isSuite ? 'suite' : 'spec', 32 children: [] 33 }; 34 if (isSuite) { 35 var specs = suiteOrSpec.specs(); 36 for (var i = 0; i < specs.length; i++) { 37 summary.children.push(this.summarize_(specs[i])); 38 } 39 } 40 return summary; 41 }; 42 43 jasmine.EnvjsReporter.prototype.results = function() { 44 return this.results_; 45 }; 46 47 jasmine.EnvjsReporter.prototype.resultsForSpec = function(specId) { 48 return this.results_[specId]; 49 }; 50 51 //noinspection JSUnusedLocalSymbols 52 jasmine.EnvjsReporter.prototype.reportRunnerResults = function(runner) { 53 this.finished = true; 54 var results = runner.results(); 55 var specs = runner.specs(); 56 var specCount = specs.length; 57 print(); 58 var count = 1; 59 var red = "\x1b[31m"; 60 var normal = "\x1b[0m"; 61 var green = "\x1b[32m"; 62 for(var i in this.results_) { 63 var result = this.results_[i]; 64 if( result.result == "failed" ) { 65 var messages = result.messages; 66 for(var j in messages) { 67 var expectation = messages[j]; 68 if( !expectation.passed() ) { 69 print(); 70 print(count++ + ")"); 71 print(red+result.spec.description+normal); 72 var message; 73 message = expectation.message.replace(/(<br \/>)+/g, " "); 74 print(red+message+normal); 75 print_exception(expectation.trace); 76 } 77 } 78 } 79 } 80 var message = "\nFinished in " + ((new Date().getTime() - this.startedAt.getTime()) / 1000) + "s"; 81 print(message); 82 message = "\n" + specCount + " spec" + (specCount == 1 ? "" : "s" ) + ", " + results.failedCount + " failure" + ((results.failedCount == 1) ? "" : "s"); 83 if(results.failedCount > 0){ 84 message = red + message; 85 } else { 86 message = green + message; 87 } 88 print(message+normal); 89 }; 90 91 //noinspection JSUnusedLocalSymbols 92 jasmine.EnvjsReporter.prototype.reportSuiteResults = function(suite) { 93 }; 94 95 //noinspection JSUnusedLocalSymbols 96 jasmine.EnvjsReporter.prototype.reportSpecResults = function(spec) { 97 puts(spec.results().failedCount > 0 ? "\x1b[31mF\x1b[0m" : "\x1b[32m.\x1b[0m"); 98 this.results_[spec.id] = { 99 spec: spec, 100 messages: spec.results().getItems(), 101 result: spec.results().failedCount > 0 ? "failed" : "passed" 102 }; 103 }; 104 105 //noinspection JSUnusedLocalSymbols 106 jasmine.EnvjsReporter.prototype.log = function(str) { 107 // print(str); 108 }; 109 110 jasmine.EnvjsReporter.prototype.resultsForSpecs = function(specIds){ 111 var results = {}; 112 for (var i = 0; i < specIds.length; i++) { 113 var specId = specIds[i]; 114 results[specId] = this.summarizeResult_(this.results_[specId]); 115 } 116 return results; 117 }; 118 119 jasmine.EnvjsReporter.prototype.summarizeResult_ = function(result){ 120 var summaryMessages = []; 121 for (var messageIndex in result.messages) { 122 var resultMessage = result.messages[messageIndex]; 123 summaryMessages.push({ 124 text: resultMessage.text, 125 passed: resultMessage.passed ? resultMessage.passed() : true, 126 type: resultMessage.type, 127 message: resultMessage.message, 128 trace: { 129 stack: resultMessage.passed && !resultMessage.passed() ? resultMessage.trace.stack : undefined 130 } 131 }); 132 }; 133 134 var summaryResult = { 135 result : result.result, 136 messages : summaryMessages 137 }; 138 139 return summaryResult; 140 }; 141 142