1 /**
  2  * Internal representation of a Jasmine specification, or test.
  3  *
  4  * @constructor
  5  * @param {jasmine.Env} env
  6  * @param {jasmine.Suite} suite
  7  * @param {String} description
  8  */
  9 jasmine.Spec = function(env, suite, description) {
 10   if (!env) {
 11     throw new Error('jasmine.Env() required');
 12   }
 13   if (!suite) {
 14     throw new Error('jasmine.Suite() required');
 15   }
 16   var spec = this;
 17   spec.id = env.nextSpecId ? env.nextSpecId() : null;
 18   spec.env = env;
 19   spec.suite = suite;
 20   spec.description = description;
 21   spec.queue = new jasmine.Queue(env);
 22 
 23   spec.afterCallbacks = [];
 24   spec.spies_ = [];
 25 
 26   spec.results_ = new jasmine.NestedResults();
 27   spec.results_.description = description;
 28   spec.matchersClass = null;
 29 };
 30 
 31 jasmine.Spec.prototype.getFullName = function() {
 32   return this.suite.getFullName() + ' ' + this.description + '.';
 33 };
 34 
 35 
 36 jasmine.Spec.prototype.results = function() {
 37   return this.results_;
 38 };
 39 
 40 /**
 41  * All parameters are pretty-printed and concatenated together, then written to the spec's output.
 42  *
 43  * Be careful not to leave calls to <code>jasmine.log</code> in production code.
 44  */
 45 jasmine.Spec.prototype.log = function() {
 46   return this.results_.log(arguments);
 47 };
 48 
 49 jasmine.Spec.prototype.runs = function (func) {
 50   var block = new jasmine.Block(this.env, func, this);
 51   this.addToQueue(block);
 52   return this;
 53 };
 54 
 55 jasmine.Spec.prototype.addToQueue = function (block) {
 56   if (this.queue.isRunning()) {
 57     this.queue.insertNext(block);
 58   } else {
 59     this.queue.add(block);
 60   }
 61 };
 62 
 63 /**
 64  * @param {jasmine.ExpectationResult} result
 65  */
 66 jasmine.Spec.prototype.addMatcherResult = function(result) {
 67   this.results_.addResult(result);
 68 };
 69 
 70 jasmine.Spec.prototype.expect = function(actual) {
 71   var positive = new (this.getMatchersClass_())(this.env, actual, this);
 72   positive.not = new (this.getMatchersClass_())(this.env, actual, this, true);
 73   return positive;
 74 };
 75 
 76 jasmine.Spec.prototype.waits = function(timeout) {
 77   var waitsFunc = new jasmine.WaitsBlock(this.env, timeout, this);
 78   this.addToQueue(waitsFunc);
 79   return this;
 80 };
 81 
 82 jasmine.Spec.prototype.waitsFor = function(timeout, latchFunction, timeoutMessage) {
 83   var waitsForFunc = new jasmine.WaitsForBlock(this.env, timeout, latchFunction, timeoutMessage, this);
 84   this.addToQueue(waitsForFunc);
 85   return this;
 86 };
 87 
 88 jasmine.Spec.prototype.fail = function (e) {
 89   var expectationResult = new jasmine.ExpectationResult({
 90     passed: false,
 91     message: e ? jasmine.util.formatException(e) : 'Exception'
 92   });
 93   this.results_.addResult(expectationResult);
 94 };
 95 
 96 jasmine.Spec.prototype.getMatchersClass_ = function() {
 97   return this.matchersClass || this.env.matchersClass;
 98 };
 99 
100 jasmine.Spec.prototype.addMatchers = function(matchersPrototype) {
101   var parent = this.getMatchersClass_();
102   var newMatchersClass = function() {
103     parent.apply(this, arguments);
104   };
105   jasmine.util.inherit(newMatchersClass, parent);
106   jasmine.Matchers.wrapInto_(matchersPrototype, newMatchersClass);
107   this.matchersClass = newMatchersClass;
108 };
109 
110 jasmine.Spec.prototype.finishCallback = function() {
111   this.env.reporter.reportSpecResults(this);
112 };
113 
114 jasmine.Spec.prototype.finish = function(onComplete) {
115   this.removeAllSpies();
116   this.finishCallback();
117   if (onComplete) {
118     onComplete();
119   }
120 };
121 
122 jasmine.Spec.prototype.after = function(doAfter) {
123   if (this.queue.isRunning()) {
124     this.queue.add(new jasmine.Block(this.env, doAfter, this));
125   } else {
126     this.afterCallbacks.unshift(doAfter);
127   }
128 };
129 
130 jasmine.Spec.prototype.execute = function(onComplete) {
131   var spec = this;
132   if (!spec.env.specFilter(spec)) {
133     spec.results_.skipped = true;
134     spec.finish(onComplete);
135     return;
136   }
137 
138   this.env.reporter.reportSpecStarting(this);
139 
140   spec.env.currentSpec = spec;
141 
142   spec.addBeforesAndAftersToQueue();
143 
144   spec.queue.start(function () {
145     spec.finish(onComplete);
146   });
147 };
148 
149 jasmine.Spec.prototype.addBeforesAndAftersToQueue = function() {
150   var runner = this.env.currentRunner();
151   var i;
152 
153   for (var suite = this.suite; suite; suite = suite.parentSuite) {
154     for (i = 0; i < suite.before_.length; i++) {
155       this.queue.addBefore(new jasmine.Block(this.env, suite.before_[i], this));
156     }
157   }
158   for (i = 0; i < runner.before_.length; i++) {
159     this.queue.addBefore(new jasmine.Block(this.env, runner.before_[i], this));
160   }
161   for (i = 0; i < this.afterCallbacks.length; i++) {
162     this.queue.add(new jasmine.Block(this.env, this.afterCallbacks[i], this));
163   }
164   for (suite = this.suite; suite; suite = suite.parentSuite) {
165     for (i = 0; i < suite.after_.length; i++) {
166       this.queue.add(new jasmine.Block(this.env, suite.after_[i], this));
167     }
168   }
169   for (i = 0; i < runner.after_.length; i++) {
170     this.queue.add(new jasmine.Block(this.env, runner.after_[i], this));
171   }
172 };
173 
174 jasmine.Spec.prototype.explodes = function() {
175   throw 'explodes function should not have been called';
176 };
177 
178 jasmine.Spec.prototype.spyOn = function(obj, methodName, ignoreMethodDoesntExist) {
179   if (obj == jasmine.undefined) {
180     throw "spyOn could not find an object to spy upon for " + methodName + "()";
181   }
182 
183   if (!ignoreMethodDoesntExist && obj[methodName] === jasmine.undefined) {
184     throw methodName + '() method does not exist';
185   }
186 
187   if (!ignoreMethodDoesntExist && obj[methodName] && obj[methodName].isSpy) {
188     throw new Error(methodName + ' has already been spied upon');
189   }
190 
191   var spyObj = jasmine.createSpy(methodName);
192 
193   this.spies_.push(spyObj);
194   spyObj.baseObj = obj;
195   spyObj.methodName = methodName;
196   spyObj.originalValue = obj[methodName];
197 
198   obj[methodName] = spyObj;
199 
200   return spyObj;
201 };
202 
203 jasmine.Spec.prototype.removeAllSpies = function() {
204   for (var i = 0; i < this.spies_.length; i++) {
205     var spy = this.spies_[i];
206     spy.baseObj[spy.methodName] = spy.originalValue;
207   }
208   this.spies_ = [];
209 };
210 
211