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