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   this.env.reporter.log('>> Jasmine Running ' + this.suite.description + ' ' + this.description + '...');
138 
139   spec.env.currentSpec = spec;
140 
141   spec.addBeforesAndAftersToQueue();
142 
143   spec.queue.start(function () {
144     spec.finish(onComplete);
145   });
146 };
147 
148 jasmine.Spec.prototype.addBeforesAndAftersToQueue = function() {
149   var runner = this.env.currentRunner();
150   var i;
151 
152   for (var suite = this.suite; suite; suite = suite.parentSuite) {
153     for (i = 0; i < suite.before_.length; i++) {
154       this.queue.addBefore(new jasmine.Block(this.env, suite.before_[i], this));
155     }
156   }
157   for (i = 0; i < runner.before_.length; i++) {
158     this.queue.addBefore(new jasmine.Block(this.env, runner.before_[i], this));
159   }
160   for (i = 0; i < this.afterCallbacks.length; i++) {
161     this.queue.add(new jasmine.Block(this.env, this.afterCallbacks[i], this));
162   }
163   for (suite = this.suite; suite; suite = suite.parentSuite) {
164     for (i = 0; i < suite.after_.length; i++) {
165       this.queue.add(new jasmine.Block(this.env, suite.after_[i], this));
166     }
167   }
168   for (i = 0; i < runner.after_.length; i++) {
169     this.queue.add(new jasmine.Block(this.env, runner.after_[i], this));
170   }
171 };
172 
173 jasmine.Spec.prototype.explodes = function() {
174   throw 'explodes function should not have been called';
175 };
176 
177 jasmine.Spec.prototype.spyOn = function(obj, methodName, ignoreMethodDoesntExist) {
178   if (obj == jasmine.undefined) {
179     throw "spyOn could not find an object to spy upon for " + methodName + "()";
180   }
181 
182   if (!ignoreMethodDoesntExist && obj[methodName] === jasmine.undefined) {
183     throw methodName + '() method does not exist';
184   }
185 
186   if (!ignoreMethodDoesntExist && obj[methodName] && obj[methodName].isSpy) {
187     throw new Error(methodName + ' has already been spied upon');
188   }
189 
190   var spyObj = jasmine.createSpy(methodName);
191 
192   this.spies_.push(spyObj);
193   spyObj.baseObj = obj;
194   spyObj.methodName = methodName;
195   spyObj.originalValue = obj[methodName];
196 
197   obj[methodName] = spyObj;
198 
199   return spyObj;
200 };
201 
202 jasmine.Spec.prototype.removeAllSpies = function() {
203   for (var i = 0; i < this.spies_.length; i++) {
204     var spy = this.spies_[i];
205     spy.baseObj[spy.methodName] = spy.originalValue;
206   }
207   this.spies_ = [];
208 };
209 
210