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 jasmine.Spec.prototype.log = function(message) {
 41   return this.results_.log(message);
 42 };
 43 
 44 /** @deprecated */
 45 jasmine.Spec.prototype.getResults = function() {
 46   return this.results_;
 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 jasmine.Spec.prototype.addMatcherResult = function(result) {
 64   this.results_.addResult(result);
 65 };
 66 
 67 jasmine.Spec.prototype.expect = function(actual) {
 68   return new (this.getMatchersClass_())(this.env, actual, this);
 69 };
 70 
 71 jasmine.Spec.prototype.waits = function(timeout) {
 72   var waitsFunc = new jasmine.WaitsBlock(this.env, timeout, this);
 73   this.addToQueue(waitsFunc);
 74   return this;
 75 };
 76 
 77 jasmine.Spec.prototype.waitsFor = function(timeout, latchFunction, timeoutMessage) {
 78   var waitsForFunc = new jasmine.WaitsForBlock(this.env, timeout, latchFunction, timeoutMessage, this);
 79   this.addToQueue(waitsForFunc);
 80   return this;
 81 };
 82 
 83 jasmine.Spec.prototype.fail = function (e) {
 84   var expectationResult = new jasmine.ExpectationResult({
 85     passed: false,
 86     message: e ? jasmine.util.formatException(e) : 'Exception'
 87   });
 88   this.results_.addResult(expectationResult);
 89 };
 90 
 91 jasmine.Spec.prototype.getMatchersClass_ = function() {
 92   return this.matchersClass || this.env.matchersClass;
 93 };
 94 
 95 jasmine.Spec.prototype.addMatchers = function(matchersPrototype) {
 96   var parent = this.getMatchersClass_();
 97   var newMatchersClass = function() {
 98     parent.apply(this, arguments);
 99   };
100   jasmine.util.inherit(newMatchersClass, parent);
101   for (var method in matchersPrototype) {
102     newMatchersClass.prototype[method] = matchersPrototype[method];
103   }
104   this.matchersClass = newMatchersClass;
105 };
106 
107 jasmine.Spec.prototype.finishCallback = function() {
108   this.env.reporter.reportSpecResults(this);
109 };
110 
111 jasmine.Spec.prototype.finish = function(onComplete) {
112   this.removeAllSpies();
113   this.finishCallback();
114   if (onComplete) {
115     onComplete();
116   }
117 };
118 
119 jasmine.Spec.prototype.after = function(doAfter, test) {
120 
121   if (this.queue.isRunning()) {
122     this.queue.add(new jasmine.Block(this.env, doAfter, this));
123   } else {
124     this.afterCallbacks.unshift(doAfter);
125   }
126 };
127 
128 jasmine.Spec.prototype.execute = function(onComplete) {
129   var spec = this;
130   if (!spec.env.specFilter(spec)) {
131     spec.results_.skipped = true;
132     spec.finish(onComplete);
133     return;
134   }
135   this.env.reporter.log('>> Jasmine Running ' + this.suite.description + ' ' + this.description + '...');
136 
137   spec.env.currentSpec = spec;
138 
139   spec.addBeforesAndAftersToQueue();
140 
141   spec.queue.start(function () {
142     spec.finish(onComplete);
143   });
144 };
145 
146 jasmine.Spec.prototype.addBeforesAndAftersToQueue = function() {
147   var runner = this.env.currentRunner();
148   for (var suite = this.suite; suite; suite = suite.parentSuite) {
149     for (var i = 0; i < suite.before_.length; i++) {
150       this.queue.addBefore(new jasmine.Block(this.env, suite.before_[i], this));
151     }
152   }
153   for (var i = 0; i < runner.before_.length; i++) {
154     this.queue.addBefore(new jasmine.Block(this.env, runner.before_[i], this));
155   }
156   for (i = 0; i < this.afterCallbacks.length; i++) {
157     this.queue.add(new jasmine.Block(this.env, this.afterCallbacks[i], this));
158   }
159   for (suite = this.suite; suite; suite = suite.parentSuite) {
160     for (var i = 0; i < suite.after_.length; i++) {
161       this.queue.add(new jasmine.Block(this.env, suite.after_[i], this));
162     }
163   }
164   for (var i = 0; i < runner.after_.length; i++) {
165     this.queue.add(new jasmine.Block(this.env, runner.after_[i], this));
166   }
167 };
168 
169 jasmine.Spec.prototype.explodes = function() {
170   throw 'explodes function should not have been called';
171 };
172 
173 jasmine.Spec.prototype.spyOn = function(obj, methodName, ignoreMethodDoesntExist) {
174   if (obj == jasmine.undefined) {
175     throw "spyOn could not find an object to spy upon for " + methodName + "()";
176   }
177 
178   if (!ignoreMethodDoesntExist && obj[methodName] === jasmine.undefined) {
179     throw methodName + '() method does not exist';
180   }
181 
182   if (!ignoreMethodDoesntExist && obj[methodName] && obj[methodName].isSpy) {
183     throw new Error(methodName + ' has already been spied upon');
184   }
185 
186   var spyObj = jasmine.createSpy(methodName);
187 
188   this.spies_.push(spyObj);
189   spyObj.baseObj = obj;
190   spyObj.methodName = methodName;
191   spyObj.originalValue = obj[methodName];
192 
193   obj[methodName] = spyObj;
194 
195   return spyObj;
196 };
197 
198 jasmine.Spec.prototype.removeAllSpies = function() {
199   for (var i = 0; i < this.spies_.length; i++) {
200     var spy = this.spies_[i];
201     spy.baseObj[spy.methodName] = spy.originalValue;
202   }
203   this.spies_ = [];
204 };
205 
206