jasmine.js 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421
  1. /**
  2. * Top level namespace for Jasmine, a lightweight JavaScript BDD/spec/testing framework.
  3. *
  4. * @namespace
  5. */
  6. var jasmine = {};
  7. /**
  8. * @private
  9. */
  10. jasmine.unimplementedMethod_ = function() {
  11. throw new Error("unimplemented method");
  12. };
  13. /**
  14. * Use <code>jasmine.undefined</code> instead of <code>undefined</code>, since <code>undefined</code> is just
  15. * a plain old variable and may be redefined by somebody else.
  16. *
  17. * @private
  18. */
  19. jasmine.undefined = jasmine.___undefined___;
  20. /**
  21. * Default interval in milliseconds for event loop yields (e.g. to allow network activity or to refresh the screen with the HTML-based runner). Small values here may result in slow test running. Zero means no updates until all tests have completed.
  22. *
  23. */
  24. jasmine.DEFAULT_UPDATE_INTERVAL = 250;
  25. /**
  26. * Default timeout interval in milliseconds for waitsFor() blocks.
  27. */
  28. jasmine.DEFAULT_TIMEOUT_INTERVAL = 5000;
  29. jasmine.getGlobal = function() {
  30. function getGlobal() {
  31. return this;
  32. }
  33. return getGlobal();
  34. };
  35. /**
  36. * Allows for bound functions to be compared. Internal use only.
  37. *
  38. * @ignore
  39. * @private
  40. * @param base {Object} bound 'this' for the function
  41. * @param name {Function} function to find
  42. */
  43. jasmine.bindOriginal_ = function(base, name) {
  44. var original = base[name];
  45. if (original.apply) {
  46. return function() {
  47. return original.apply(base, arguments);
  48. };
  49. } else {
  50. // IE support
  51. return jasmine.getGlobal()[name];
  52. }
  53. };
  54. jasmine.setTimeout = jasmine.bindOriginal_(jasmine.getGlobal(), 'setTimeout');
  55. jasmine.clearTimeout = jasmine.bindOriginal_(jasmine.getGlobal(), 'clearTimeout');
  56. jasmine.setInterval = jasmine.bindOriginal_(jasmine.getGlobal(), 'setInterval');
  57. jasmine.clearInterval = jasmine.bindOriginal_(jasmine.getGlobal(), 'clearInterval');
  58. jasmine.MessageResult = function(values) {
  59. this.type = 'log';
  60. this.values = values;
  61. this.trace = new Error(); // todo: test better
  62. };
  63. jasmine.MessageResult.prototype.toString = function() {
  64. var text = "";
  65. for(var i = 0; i < this.values.length; i++) {
  66. if (i > 0) text += " ";
  67. if (jasmine.isString_(this.values[i])) {
  68. text += this.values[i];
  69. } else {
  70. text += jasmine.pp(this.values[i]);
  71. }
  72. }
  73. return text;
  74. };
  75. jasmine.ExpectationResult = function(params) {
  76. this.type = 'expect';
  77. this.matcherName = params.matcherName;
  78. this.passed_ = params.passed;
  79. this.expected = params.expected;
  80. this.actual = params.actual;
  81. this.message = this.passed_ ? 'Passed.' : params.message;
  82. this.trace = this.passed_ ? '' : new Error(this.message);
  83. };
  84. jasmine.ExpectationResult.prototype.toString = function () {
  85. return this.message;
  86. };
  87. jasmine.ExpectationResult.prototype.passed = function () {
  88. return this.passed_;
  89. };
  90. /**
  91. * Getter for the Jasmine environment. Ensures one gets created
  92. */
  93. jasmine.getEnv = function() {
  94. return jasmine.currentEnv_ = jasmine.currentEnv_ || new jasmine.Env();
  95. };
  96. /**
  97. * @ignore
  98. * @private
  99. * @param value
  100. * @returns {Boolean}
  101. */
  102. jasmine.isArray_ = function(value) {
  103. return jasmine.isA_("Array", value);
  104. };
  105. /**
  106. * @ignore
  107. * @private
  108. * @param value
  109. * @returns {Boolean}
  110. */
  111. jasmine.isString_ = function(value) {
  112. return jasmine.isA_("String", value);
  113. };
  114. /**
  115. * @ignore
  116. * @private
  117. * @param value
  118. * @returns {Boolean}
  119. */
  120. jasmine.isNumber_ = function(value) {
  121. return jasmine.isA_("Number", value);
  122. };
  123. /**
  124. * @ignore
  125. * @private
  126. * @param {String} typeName
  127. * @param value
  128. * @returns {Boolean}
  129. */
  130. jasmine.isA_ = function(typeName, value) {
  131. return Object.prototype.toString.apply(value) === '[object ' + typeName + ']';
  132. };
  133. /**
  134. * Pretty printer for expecations. Takes any object and turns it into a human-readable string.
  135. *
  136. * @param value {Object} an object to be outputted
  137. * @returns {String}
  138. */
  139. jasmine.pp = function(value) {
  140. var stringPrettyPrinter = new jasmine.StringPrettyPrinter();
  141. stringPrettyPrinter.format(value);
  142. return stringPrettyPrinter.string;
  143. };
  144. /**
  145. * Returns true if the object is a DOM Node.
  146. *
  147. * @param {Object} obj object to check
  148. * @returns {Boolean}
  149. */
  150. jasmine.isDomNode = function(obj) {
  151. return obj['nodeType'] > 0;
  152. };
  153. /**
  154. * Returns a matchable 'generic' object of the class type. For use in expecations of type when values don't matter.
  155. *
  156. * @example
  157. * // don't care about which function is passed in, as long as it's a function
  158. * expect(mySpy).toHaveBeenCalledWith(jasmine.any(Function));
  159. *
  160. * @param {Class} clazz
  161. * @returns matchable object of the type clazz
  162. */
  163. jasmine.any = function(clazz) {
  164. return new jasmine.Matchers.Any(clazz);
  165. };
  166. /**
  167. * Jasmine Spies are test doubles that can act as stubs, spies, fakes or when used in an expecation, mocks.
  168. *
  169. * Spies should be created in test setup, before expectations. They can then be checked, using the standard Jasmine
  170. * expectation syntax. Spies can be checked if they were called or not and what the calling params were.
  171. *
  172. * A Spy has the following fields: wasCalled, callCount, mostRecentCall, and argsForCall (see docs).
  173. *
  174. * Spies are torn down at the end of every spec.
  175. *
  176. * Note: Do <b>not</b> call new jasmine.Spy() directly - a spy must be created using spyOn, jasmine.createSpy or jasmine.createSpyObj.
  177. *
  178. * @example
  179. * // a stub
  180. * var myStub = jasmine.createSpy('myStub'); // can be used anywhere
  181. *
  182. * // spy example
  183. * var foo = {
  184. * not: function(bool) { return !bool; }
  185. * }
  186. *
  187. * // actual foo.not will not be called, execution stops
  188. * spyOn(foo, 'not');
  189. // foo.not spied upon, execution will continue to implementation
  190. * spyOn(foo, 'not').andCallThrough();
  191. *
  192. * // fake example
  193. * var foo = {
  194. * not: function(bool) { return !bool; }
  195. * }
  196. *
  197. * // foo.not(val) will return val
  198. * spyOn(foo, 'not').andCallFake(function(value) {return value;});
  199. *
  200. * // mock example
  201. * foo.not(7 == 7);
  202. * expect(foo.not).toHaveBeenCalled();
  203. * expect(foo.not).toHaveBeenCalledWith(true);
  204. *
  205. * @constructor
  206. * @see spyOn, jasmine.createSpy, jasmine.createSpyObj
  207. * @param {String} name
  208. */
  209. jasmine.Spy = function(name) {
  210. /**
  211. * The name of the spy, if provided.
  212. */
  213. this.identity = name || 'unknown';
  214. /**
  215. * Is this Object a spy?
  216. */
  217. this.isSpy = true;
  218. /**
  219. * The actual function this spy stubs.
  220. */
  221. this.plan = function() {
  222. };
  223. /**
  224. * Tracking of the most recent call to the spy.
  225. * @example
  226. * var mySpy = jasmine.createSpy('foo');
  227. * mySpy(1, 2);
  228. * mySpy.mostRecentCall.args = [1, 2];
  229. */
  230. this.mostRecentCall = {};
  231. /**
  232. * Holds arguments for each call to the spy, indexed by call count
  233. * @example
  234. * var mySpy = jasmine.createSpy('foo');
  235. * mySpy(1, 2);
  236. * mySpy(7, 8);
  237. * mySpy.mostRecentCall.args = [7, 8];
  238. * mySpy.argsForCall[0] = [1, 2];
  239. * mySpy.argsForCall[1] = [7, 8];
  240. */
  241. this.argsForCall = [];
  242. this.calls = [];
  243. };
  244. /**
  245. * Tells a spy to call through to the actual implemenatation.
  246. *
  247. * @example
  248. * var foo = {
  249. * bar: function() { // do some stuff }
  250. * }
  251. *
  252. * // defining a spy on an existing property: foo.bar
  253. * spyOn(foo, 'bar').andCallThrough();
  254. */
  255. jasmine.Spy.prototype.andCallThrough = function() {
  256. this.plan = this.originalValue;
  257. return this;
  258. };
  259. /**
  260. * For setting the return value of a spy.
  261. *
  262. * @example
  263. * // defining a spy from scratch: foo() returns 'baz'
  264. * var foo = jasmine.createSpy('spy on foo').andReturn('baz');
  265. *
  266. * // defining a spy on an existing property: foo.bar() returns 'baz'
  267. * spyOn(foo, 'bar').andReturn('baz');
  268. *
  269. * @param {Object} value
  270. */
  271. jasmine.Spy.prototype.andReturn = function(value) {
  272. this.plan = function() {
  273. return value;
  274. };
  275. return this;
  276. };
  277. /**
  278. * For throwing an exception when a spy is called.
  279. *
  280. * @example
  281. * // defining a spy from scratch: foo() throws an exception w/ message 'ouch'
  282. * var foo = jasmine.createSpy('spy on foo').andThrow('baz');
  283. *
  284. * // defining a spy on an existing property: foo.bar() throws an exception w/ message 'ouch'
  285. * spyOn(foo, 'bar').andThrow('baz');
  286. *
  287. * @param {String} exceptionMsg
  288. */
  289. jasmine.Spy.prototype.andThrow = function(exceptionMsg) {
  290. this.plan = function() {
  291. throw exceptionMsg;
  292. };
  293. return this;
  294. };
  295. /**
  296. * Calls an alternate implementation when a spy is called.
  297. *
  298. * @example
  299. * var baz = function() {
  300. * // do some stuff, return something
  301. * }
  302. * // defining a spy from scratch: foo() calls the function baz
  303. * var foo = jasmine.createSpy('spy on foo').andCall(baz);
  304. *
  305. * // defining a spy on an existing property: foo.bar() calls an anonymnous function
  306. * spyOn(foo, 'bar').andCall(function() { return 'baz';} );
  307. *
  308. * @param {Function} fakeFunc
  309. */
  310. jasmine.Spy.prototype.andCallFake = function(fakeFunc) {
  311. this.plan = fakeFunc;
  312. return this;
  313. };
  314. /**
  315. * Resets all of a spy's the tracking variables so that it can be used again.
  316. *
  317. * @example
  318. * spyOn(foo, 'bar');
  319. *
  320. * foo.bar();
  321. *
  322. * expect(foo.bar.callCount).toEqual(1);
  323. *
  324. * foo.bar.reset();
  325. *
  326. * expect(foo.bar.callCount).toEqual(0);
  327. */
  328. jasmine.Spy.prototype.reset = function() {
  329. this.wasCalled = false;
  330. this.callCount = 0;
  331. this.argsForCall = [];
  332. this.calls = [];
  333. this.mostRecentCall = {};
  334. };
  335. jasmine.createSpy = function(name) {
  336. var spyObj = function() {
  337. spyObj.wasCalled = true;
  338. spyObj.callCount++;
  339. var args = jasmine.util.argsToArray(arguments);
  340. spyObj.mostRecentCall.object = this;
  341. spyObj.mostRecentCall.args = args;
  342. spyObj.argsForCall.push(args);
  343. spyObj.calls.push({object: this, args: args});
  344. return spyObj.plan.apply(this, arguments);
  345. };
  346. var spy = new jasmine.Spy(name);
  347. for (var prop in spy) {
  348. spyObj[prop] = spy[prop];
  349. }
  350. spyObj.reset();
  351. return spyObj;
  352. };
  353. /**
  354. * Determines whether an object is a spy.
  355. *
  356. * @param {jasmine.Spy|Object} putativeSpy
  357. * @returns {Boolean}
  358. */
  359. jasmine.isSpy = function(putativeSpy) {
  360. return putativeSpy && putativeSpy.isSpy;
  361. };
  362. /**
  363. * Creates a more complicated spy: an Object that has every property a function that is a spy. Used for stubbing something
  364. * large in one call.
  365. *
  366. * @param {String} baseName name of spy class
  367. * @param {Array} methodNames array of names of methods to make spies
  368. */
  369. jasmine.createSpyObj = function(baseName, methodNames) {
  370. if (!jasmine.isArray_(methodNames) || methodNames.length == 0) {
  371. throw new Error('createSpyObj requires a non-empty array of method names to create spies for');
  372. }
  373. var obj = {};
  374. for (var i = 0; i < methodNames.length; i++) {
  375. obj[methodNames[i]] = jasmine.createSpy(baseName + '.' + methodNames[i]);
  376. }
  377. return obj;
  378. };
  379. /**
  380. * All parameters are pretty-printed and concatenated together, then written to the current spec's output.
  381. *
  382. * Be careful not to leave calls to <code>jasmine.log</code> in production code.
  383. */
  384. jasmine.log = function() {
  385. var spec = jasmine.getEnv().currentSpec;
  386. spec.log.apply(spec, arguments);
  387. };
  388. /**
  389. * Function that installs a spy on an existing object's method name. Used within a Spec to create a spy.
  390. *
  391. * @example
  392. * // spy example
  393. * var foo = {
  394. * not: function(bool) { return !bool; }
  395. * }
  396. * spyOn(foo, 'not'); // actual foo.not will not be called, execution stops
  397. *
  398. * @see jasmine.createSpy
  399. * @param obj
  400. * @param methodName
  401. * @returns a Jasmine spy that can be chained with all spy methods
  402. */
  403. var spyOn = function(obj, methodName) {
  404. return jasmine.getEnv().currentSpec.spyOn(obj, methodName);
  405. };
  406. /**
  407. * Creates a Jasmine spec that will be added to the current suite.
  408. *
  409. * // TODO: pending tests
  410. *
  411. * @example
  412. * it('should be true', function() {
  413. * expect(true).toEqual(true);
  414. * });
  415. *
  416. * @param {String} desc description of this specification
  417. * @param {Function} func defines the preconditions and expectations of the spec
  418. */
  419. var it = function(desc, func) {
  420. return jasmine.getEnv().it(desc, func);
  421. };
  422. /**
  423. * Creates a <em>disabled</em> Jasmine spec.
  424. *
  425. * A convenience method that allows existing specs to be disabled temporarily during development.
  426. *
  427. * @param {String} desc description of this specification
  428. * @param {Function} func defines the preconditions and expectations of the spec
  429. */
  430. var xit = function(desc, func) {
  431. return jasmine.getEnv().xit(desc, func);
  432. };
  433. /**
  434. * Starts a chain for a Jasmine expectation.
  435. *
  436. * It is passed an Object that is the actual value and should chain to one of the many
  437. * jasmine.Matchers functions.
  438. *
  439. * @param {Object} actual Actual value to test against and expected value
  440. */
  441. var expect = function(actual) {
  442. return jasmine.getEnv().currentSpec.expect(actual);
  443. };
  444. /**
  445. * Defines part of a jasmine spec. Used in cominbination with waits or waitsFor in asynchrnous specs.
  446. *
  447. * @param {Function} func Function that defines part of a jasmine spec.
  448. */
  449. var runs = function(func) {
  450. jasmine.getEnv().currentSpec.runs(func);
  451. };
  452. /**
  453. * Waits a fixed time period before moving to the next block.
  454. *
  455. * @deprecated Use waitsFor() instead
  456. * @param {Number} timeout milliseconds to wait
  457. */
  458. var waits = function(timeout) {
  459. jasmine.getEnv().currentSpec.waits(timeout);
  460. };
  461. /**
  462. * Waits for the latchFunction to return true before proceeding to the next block.
  463. *
  464. * @param {Function} latchFunction
  465. * @param {String} optional_timeoutMessage
  466. * @param {Number} optional_timeout
  467. */
  468. var waitsFor = function(latchFunction, optional_timeoutMessage, optional_timeout) {
  469. jasmine.getEnv().currentSpec.waitsFor.apply(jasmine.getEnv().currentSpec, arguments);
  470. };
  471. /**
  472. * A function that is called before each spec in a suite.
  473. *
  474. * Used for spec setup, including validating assumptions.
  475. *
  476. * @param {Function} beforeEachFunction
  477. */
  478. var beforeEach = function(beforeEachFunction) {
  479. jasmine.getEnv().beforeEach(beforeEachFunction);
  480. };
  481. /**
  482. * A function that is called after each spec in a suite.
  483. *
  484. * Used for restoring any state that is hijacked during spec execution.
  485. *
  486. * @param {Function} afterEachFunction
  487. */
  488. var afterEach = function(afterEachFunction) {
  489. jasmine.getEnv().afterEach(afterEachFunction);
  490. };
  491. /**
  492. * Defines a suite of specifications.
  493. *
  494. * Stores the description and all defined specs in the Jasmine environment as one suite of specs. Variables declared
  495. * are accessible by calls to beforeEach, it, and afterEach. Describe blocks can be nested, allowing for specialization
  496. * of setup in some tests.
  497. *
  498. * @example
  499. * // TODO: a simple suite
  500. *
  501. * // TODO: a simple suite with a nested describe block
  502. *
  503. * @param {String} description A string, usually the class under test.
  504. * @param {Function} specDefinitions function that defines several specs.
  505. */
  506. var describe = function(description, specDefinitions) {
  507. return jasmine.getEnv().describe(description, specDefinitions);
  508. };
  509. /**
  510. * Disables a suite of specifications. Used to disable some suites in a file, or files, temporarily during development.
  511. *
  512. * @param {String} description A string, usually the class under test.
  513. * @param {Function} specDefinitions function that defines several specs.
  514. */
  515. var xdescribe = function(description, specDefinitions) {
  516. return jasmine.getEnv().xdescribe(description, specDefinitions);
  517. };
  518. // Provide the XMLHttpRequest class for IE 5.x-6.x:
  519. jasmine.XmlHttpRequest = (typeof XMLHttpRequest == "undefined") ? function() {
  520. try {
  521. return new ActiveXObject("Msxml2.XMLHTTP.6.0");
  522. } catch(e) {
  523. }
  524. try {
  525. return new ActiveXObject("Msxml2.XMLHTTP.3.0");
  526. } catch(e) {
  527. }
  528. try {
  529. return new ActiveXObject("Msxml2.XMLHTTP");
  530. } catch(e) {
  531. }
  532. try {
  533. return new ActiveXObject("Microsoft.XMLHTTP");
  534. } catch(e) {
  535. }
  536. throw new Error("This browser does not support XMLHttpRequest.");
  537. } : XMLHttpRequest;
  538. /**
  539. * @namespace
  540. */
  541. jasmine.util = {};
  542. /**
  543. * Declare that a child class inherit it's prototype from the parent class.
  544. *
  545. * @private
  546. * @param {Function} childClass
  547. * @param {Function} parentClass
  548. */
  549. jasmine.util.inherit = function(childClass, parentClass) {
  550. /**
  551. * @private
  552. */
  553. var subclass = function() {
  554. };
  555. subclass.prototype = parentClass.prototype;
  556. childClass.prototype = new subclass;
  557. };
  558. jasmine.util.formatException = function(e) {
  559. var lineNumber;
  560. if (e.line) {
  561. lineNumber = e.line;
  562. }
  563. else if (e.lineNumber) {
  564. lineNumber = e.lineNumber;
  565. }
  566. var file;
  567. if (e.sourceURL) {
  568. file = e.sourceURL;
  569. }
  570. else if (e.fileName) {
  571. file = e.fileName;
  572. }
  573. var message = (e.name && e.message) ? (e.name + ': ' + e.message) : e.toString();
  574. if (file && lineNumber) {
  575. message += ' in ' + file + ' (line ' + lineNumber + ')';
  576. }
  577. return message;
  578. };
  579. jasmine.util.htmlEscape = function(str) {
  580. if (!str) return str;
  581. return str.replace(/&/g, '&amp;')
  582. .replace(/</g, '&lt;')
  583. .replace(/>/g, '&gt;');
  584. };
  585. jasmine.util.argsToArray = function(args) {
  586. var arrayOfArgs = [];
  587. for (var i = 0; i < args.length; i++) arrayOfArgs.push(args[i]);
  588. return arrayOfArgs;
  589. };
  590. jasmine.util.extend = function(destination, source) {
  591. for (var property in source) destination[property] = source[property];
  592. return destination;
  593. };
  594. /**
  595. * Environment for Jasmine
  596. *
  597. * @constructor
  598. */
  599. jasmine.Env = function() {
  600. this.currentSpec = null;
  601. this.currentSuite = null;
  602. this.currentRunner_ = new jasmine.Runner(this);
  603. this.reporter = new jasmine.MultiReporter();
  604. this.updateInterval = jasmine.DEFAULT_UPDATE_INTERVAL;
  605. this.defaultTimeoutInterval = jasmine.DEFAULT_TIMEOUT_INTERVAL;
  606. this.lastUpdate = 0;
  607. this.specFilter = function() {
  608. return true;
  609. };
  610. this.nextSpecId_ = 0;
  611. this.nextSuiteId_ = 0;
  612. this.equalityTesters_ = [];
  613. // wrap matchers
  614. this.matchersClass = function() {
  615. jasmine.Matchers.apply(this, arguments);
  616. };
  617. jasmine.util.inherit(this.matchersClass, jasmine.Matchers);
  618. jasmine.Matchers.wrapInto_(jasmine.Matchers.prototype, this.matchersClass);
  619. };
  620. jasmine.Env.prototype.setTimeout = jasmine.setTimeout;
  621. jasmine.Env.prototype.clearTimeout = jasmine.clearTimeout;
  622. jasmine.Env.prototype.setInterval = jasmine.setInterval;
  623. jasmine.Env.prototype.clearInterval = jasmine.clearInterval;
  624. /**
  625. * @returns an object containing jasmine version build info, if set.
  626. */
  627. jasmine.Env.prototype.version = function () {
  628. if (jasmine.version_) {
  629. return jasmine.version_;
  630. } else {
  631. throw new Error('Version not set');
  632. }
  633. };
  634. /**
  635. * @returns string containing jasmine version build info, if set.
  636. */
  637. jasmine.Env.prototype.versionString = function() {
  638. if (jasmine.version_) {
  639. var version = this.version();
  640. return version.major + "." + version.minor + "." + version.build + " revision " + version.revision;
  641. } else {
  642. return "version unknown";
  643. }
  644. };
  645. /**
  646. * @returns a sequential integer starting at 0
  647. */
  648. jasmine.Env.prototype.nextSpecId = function () {
  649. return this.nextSpecId_++;
  650. };
  651. /**
  652. * @returns a sequential integer starting at 0
  653. */
  654. jasmine.Env.prototype.nextSuiteId = function () {
  655. return this.nextSuiteId_++;
  656. };
  657. /**
  658. * Register a reporter to receive status updates from Jasmine.
  659. * @param {jasmine.Reporter} reporter An object which will receive status updates.
  660. */
  661. jasmine.Env.prototype.addReporter = function(reporter) {
  662. this.reporter.addReporter(reporter);
  663. };
  664. jasmine.Env.prototype.execute = function() {
  665. this.currentRunner_.execute();
  666. };
  667. jasmine.Env.prototype.describe = function(description, specDefinitions) {
  668. var suite = new jasmine.Suite(this, description, specDefinitions, this.currentSuite);
  669. var parentSuite = this.currentSuite;
  670. if (parentSuite) {
  671. parentSuite.add(suite);
  672. } else {
  673. this.currentRunner_.add(suite);
  674. }
  675. this.currentSuite = suite;
  676. var declarationError = null;
  677. try {
  678. specDefinitions.call(suite);
  679. } catch(e) {
  680. declarationError = e;
  681. }
  682. this.currentSuite = parentSuite;
  683. if (declarationError) {
  684. this.it("encountered a declaration exception", function() {
  685. throw declarationError;
  686. });
  687. }
  688. return suite;
  689. };
  690. jasmine.Env.prototype.beforeEach = function(beforeEachFunction) {
  691. if (this.currentSuite) {
  692. this.currentSuite.beforeEach(beforeEachFunction);
  693. } else {
  694. this.currentRunner_.beforeEach(beforeEachFunction);
  695. }
  696. };
  697. jasmine.Env.prototype.currentRunner = function () {
  698. return this.currentRunner_;
  699. };
  700. jasmine.Env.prototype.afterEach = function(afterEachFunction) {
  701. if (this.currentSuite) {
  702. this.currentSuite.afterEach(afterEachFunction);
  703. } else {
  704. this.currentRunner_.afterEach(afterEachFunction);
  705. }
  706. };
  707. jasmine.Env.prototype.xdescribe = function(desc, specDefinitions) {
  708. return {
  709. execute: function() {
  710. }
  711. };
  712. };
  713. jasmine.Env.prototype.it = function(description, func) {
  714. var spec = new jasmine.Spec(this, this.currentSuite, description);
  715. this.currentSuite.add(spec);
  716. this.currentSpec = spec;
  717. if (func) {
  718. spec.runs(func);
  719. }
  720. return spec;
  721. };
  722. jasmine.Env.prototype.xit = function(desc, func) {
  723. return {
  724. id: this.nextSpecId(),
  725. runs: function() {
  726. }
  727. };
  728. };
  729. jasmine.Env.prototype.compareObjects_ = function(a, b, mismatchKeys, mismatchValues) {
  730. if (a.__Jasmine_been_here_before__ === b && b.__Jasmine_been_here_before__ === a) {
  731. return true;
  732. }
  733. a.__Jasmine_been_here_before__ = b;
  734. b.__Jasmine_been_here_before__ = a;
  735. var hasKey = function(obj, keyName) {
  736. return obj != null && obj[keyName] !== jasmine.undefined;
  737. };
  738. for (var property in b) {
  739. if (!hasKey(a, property) && hasKey(b, property)) {
  740. mismatchKeys.push("expected has key '" + property + "', but missing from actual.");
  741. }
  742. }
  743. for (property in a) {
  744. if (!hasKey(b, property) && hasKey(a, property)) {
  745. mismatchKeys.push("expected missing key '" + property + "', but present in actual.");
  746. }
  747. }
  748. for (property in b) {
  749. if (property == '__Jasmine_been_here_before__') continue;
  750. if (!this.equals_(a[property], b[property], mismatchKeys, mismatchValues)) {
  751. mismatchValues.push("'" + property + "' was '" + (b[property] ? jasmine.util.htmlEscape(b[property].toString ? b[property].toString() : '' + b[property]) : b[property]) + "' in expected, but was '" + (a[property] ? jasmine.util.htmlEscape(a[property].toString ? a[property].toString() : '' + a[property]) : a[property]) + "' in actual.");
  752. }
  753. }
  754. if (jasmine.isArray_(a) && jasmine.isArray_(b) && a.length != b.length) {
  755. mismatchValues.push("arrays were not the same length");
  756. }
  757. delete a.__Jasmine_been_here_before__;
  758. delete b.__Jasmine_been_here_before__;
  759. return (mismatchKeys.length == 0 && mismatchValues.length == 0);
  760. };
  761. jasmine.Env.prototype.equals_ = function(a, b, mismatchKeys, mismatchValues) {
  762. mismatchKeys = mismatchKeys || [];
  763. mismatchValues = mismatchValues || [];
  764. for (var i = 0; i < this.equalityTesters_.length; i++) {
  765. var equalityTester = this.equalityTesters_[i];
  766. var result = equalityTester(a, b, this, mismatchKeys, mismatchValues);
  767. if (result !== jasmine.undefined) return result;
  768. }
  769. if (a === b) return true;
  770. if (a === jasmine.undefined || a === null || b === jasmine.undefined || b === null) {
  771. return (a == jasmine.undefined && b == jasmine.undefined);
  772. }
  773. if (jasmine.isDomNode(a) && jasmine.isDomNode(b)) {
  774. return a === b;
  775. }
  776. if (a instanceof Date && b instanceof Date) {
  777. return a.getTime() == b.getTime();
  778. }
  779. if (a instanceof jasmine.Matchers.Any) {
  780. return a.matches(b);
  781. }
  782. if (b instanceof jasmine.Matchers.Any) {
  783. return b.matches(a);
  784. }
  785. if (jasmine.isString_(a) && jasmine.isString_(b)) {
  786. return (a == b);
  787. }
  788. if (jasmine.isNumber_(a) && jasmine.isNumber_(b)) {
  789. return (a == b);
  790. }
  791. if (typeof a === "object" && typeof b === "object") {
  792. return this.compareObjects_(a, b, mismatchKeys, mismatchValues);
  793. }
  794. //Straight check
  795. return (a === b);
  796. };
  797. jasmine.Env.prototype.contains_ = function(haystack, needle) {
  798. if (jasmine.isArray_(haystack)) {
  799. for (var i = 0; i < haystack.length; i++) {
  800. if (this.equals_(haystack[i], needle)) return true;
  801. }
  802. return false;
  803. }
  804. return haystack.indexOf(needle) >= 0;
  805. };
  806. jasmine.Env.prototype.addEqualityTester = function(equalityTester) {
  807. this.equalityTesters_.push(equalityTester);
  808. };
  809. /** No-op base class for Jasmine reporters.
  810. *
  811. * @constructor
  812. */
  813. jasmine.Reporter = function() {
  814. };
  815. //noinspection JSUnusedLocalSymbols
  816. jasmine.Reporter.prototype.reportRunnerStarting = function(runner) {
  817. };
  818. //noinspection JSUnusedLocalSymbols
  819. jasmine.Reporter.prototype.reportRunnerResults = function(runner) {
  820. };
  821. //noinspection JSUnusedLocalSymbols
  822. jasmine.Reporter.prototype.reportSuiteResults = function(suite) {
  823. };
  824. //noinspection JSUnusedLocalSymbols
  825. jasmine.Reporter.prototype.reportSpecStarting = function(spec) {
  826. };
  827. //noinspection JSUnusedLocalSymbols
  828. jasmine.Reporter.prototype.reportSpecResults = function(spec) {
  829. };
  830. //noinspection JSUnusedLocalSymbols
  831. jasmine.Reporter.prototype.log = function(str) {
  832. };
  833. /**
  834. * Blocks are functions with executable code that make up a spec.
  835. *
  836. * @constructor
  837. * @param {jasmine.Env} env
  838. * @param {Function} func
  839. * @param {jasmine.Spec} spec
  840. */
  841. jasmine.Block = function(env, func, spec) {
  842. this.env = env;
  843. this.func = func;
  844. this.spec = spec;
  845. };
  846. jasmine.Block.prototype.execute = function(onComplete) {
  847. try {
  848. this.func.apply(this.spec);
  849. } catch (e) {
  850. this.spec.fail(e);
  851. }
  852. onComplete();
  853. };
  854. /** JavaScript API reporter.
  855. *
  856. * @constructor
  857. */
  858. jasmine.JsApiReporter = function() {
  859. this.started = false;
  860. this.finished = false;
  861. this.suites_ = [];
  862. this.results_ = {};
  863. };
  864. jasmine.JsApiReporter.prototype.reportRunnerStarting = function(runner) {
  865. this.started = true;
  866. var suites = runner.topLevelSuites();
  867. for (var i = 0; i < suites.length; i++) {
  868. var suite = suites[i];
  869. this.suites_.push(this.summarize_(suite));
  870. }
  871. };
  872. jasmine.JsApiReporter.prototype.suites = function() {
  873. return this.suites_;
  874. };
  875. jasmine.JsApiReporter.prototype.summarize_ = function(suiteOrSpec) {
  876. var isSuite = suiteOrSpec instanceof jasmine.Suite;
  877. var summary = {
  878. id: suiteOrSpec.id,
  879. name: suiteOrSpec.description,
  880. type: isSuite ? 'suite' : 'spec',
  881. children: []
  882. };
  883. if (isSuite) {
  884. var children = suiteOrSpec.children();
  885. for (var i = 0; i < children.length; i++) {
  886. summary.children.push(this.summarize_(children[i]));
  887. }
  888. }
  889. return summary;
  890. };
  891. jasmine.JsApiReporter.prototype.results = function() {
  892. return this.results_;
  893. };
  894. jasmine.JsApiReporter.prototype.resultsForSpec = function(specId) {
  895. return this.results_[specId];
  896. };
  897. //noinspection JSUnusedLocalSymbols
  898. jasmine.JsApiReporter.prototype.reportRunnerResults = function(runner) {
  899. this.finished = true;
  900. };
  901. //noinspection JSUnusedLocalSymbols
  902. jasmine.JsApiReporter.prototype.reportSuiteResults = function(suite) {
  903. };
  904. //noinspection JSUnusedLocalSymbols
  905. jasmine.JsApiReporter.prototype.reportSpecResults = function(spec) {
  906. this.results_[spec.id] = {
  907. messages: spec.results().getItems(),
  908. result: spec.results().failedCount > 0 ? "failed" : "passed"
  909. };
  910. };
  911. //noinspection JSUnusedLocalSymbols
  912. jasmine.JsApiReporter.prototype.log = function(str) {
  913. };
  914. jasmine.JsApiReporter.prototype.resultsForSpecs = function(specIds){
  915. var results = {};
  916. for (var i = 0; i < specIds.length; i++) {
  917. var specId = specIds[i];
  918. results[specId] = this.summarizeResult_(this.results_[specId]);
  919. }
  920. return results;
  921. };
  922. jasmine.JsApiReporter.prototype.summarizeResult_ = function(result){
  923. var summaryMessages = [];
  924. var messagesLength = result.messages.length;
  925. for (var messageIndex = 0; messageIndex < messagesLength; messageIndex++) {
  926. var resultMessage = result.messages[messageIndex];
  927. summaryMessages.push({
  928. text: resultMessage.type == 'log' ? resultMessage.toString() : jasmine.undefined,
  929. passed: resultMessage.passed ? resultMessage.passed() : true,
  930. type: resultMessage.type,
  931. message: resultMessage.message,
  932. trace: {
  933. stack: resultMessage.passed && !resultMessage.passed() ? resultMessage.trace.stack : jasmine.undefined
  934. }
  935. });
  936. }
  937. return {
  938. result : result.result,
  939. messages : summaryMessages
  940. };
  941. };
  942. /**
  943. * @constructor
  944. * @param {jasmine.Env} env
  945. * @param actual
  946. * @param {jasmine.Spec} spec
  947. */
  948. jasmine.Matchers = function(env, actual, spec, opt_isNot) {
  949. this.env = env;
  950. this.actual = actual;
  951. this.spec = spec;
  952. this.isNot = opt_isNot || false;
  953. this.reportWasCalled_ = false;
  954. };
  955. // todo: @deprecated as of Jasmine 0.11, remove soon [xw]
  956. jasmine.Matchers.pp = function(str) {
  957. throw new Error("jasmine.Matchers.pp() is no longer supported, please use jasmine.pp() instead!");
  958. };
  959. // todo: @deprecated Deprecated as of Jasmine 0.10. Rewrite your custom matchers to return true or false. [xw]
  960. jasmine.Matchers.prototype.report = function(result, failing_message, details) {
  961. throw new Error("As of jasmine 0.11, custom matchers must be implemented differently -- please see jasmine docs");
  962. };
  963. jasmine.Matchers.wrapInto_ = function(prototype, matchersClass) {
  964. for (var methodName in prototype) {
  965. if (methodName == 'report') continue;
  966. var orig = prototype[methodName];
  967. matchersClass.prototype[methodName] = jasmine.Matchers.matcherFn_(methodName, orig);
  968. }
  969. };
  970. jasmine.Matchers.matcherFn_ = function(matcherName, matcherFunction) {
  971. return function() {
  972. var matcherArgs = jasmine.util.argsToArray(arguments);
  973. var result = matcherFunction.apply(this, arguments);
  974. if (this.isNot) {
  975. result = !result;
  976. }
  977. if (this.reportWasCalled_) return result;
  978. var message;
  979. if (!result) {
  980. if (this.message) {
  981. message = this.message.apply(this, arguments);
  982. if (jasmine.isArray_(message)) {
  983. message = message[this.isNot ? 1 : 0];
  984. }
  985. } else {
  986. var englishyPredicate = matcherName.replace(/[A-Z]/g, function(s) { return ' ' + s.toLowerCase(); });
  987. message = "Expected " + jasmine.pp(this.actual) + (this.isNot ? " not " : " ") + englishyPredicate;
  988. if (matcherArgs.length > 0) {
  989. for (var i = 0; i < matcherArgs.length; i++) {
  990. if (i > 0) message += ",";
  991. message += " " + jasmine.pp(matcherArgs[i]);
  992. }
  993. }
  994. message += ".";
  995. }
  996. }
  997. var expectationResult = new jasmine.ExpectationResult({
  998. matcherName: matcherName,
  999. passed: result,
  1000. expected: matcherArgs.length > 1 ? matcherArgs : matcherArgs[0],
  1001. actual: this.actual,
  1002. message: message
  1003. });
  1004. this.spec.addMatcherResult(expectationResult);
  1005. return jasmine.undefined;
  1006. };
  1007. };
  1008. /**
  1009. * toBe: compares the actual to the expected using ===
  1010. * @param expected
  1011. */
  1012. jasmine.Matchers.prototype.toBe = function(expected) {
  1013. return this.actual === expected;
  1014. };
  1015. /**
  1016. * toNotBe: compares the actual to the expected using !==
  1017. * @param expected
  1018. * @deprecated as of 1.0. Use not.toBe() instead.
  1019. */
  1020. jasmine.Matchers.prototype.toNotBe = function(expected) {
  1021. return this.actual !== expected;
  1022. };
  1023. /**
  1024. * toEqual: compares the actual to the expected using common sense equality. Handles Objects, Arrays, etc.
  1025. *
  1026. * @param expected
  1027. */
  1028. jasmine.Matchers.prototype.toEqual = function(expected) {
  1029. return this.env.equals_(this.actual, expected);
  1030. };
  1031. /**
  1032. * toNotEqual: compares the actual to the expected using the ! of jasmine.Matchers.toEqual
  1033. * @param expected
  1034. * @deprecated as of 1.0. Use not.toNotEqual() instead.
  1035. */
  1036. jasmine.Matchers.prototype.toNotEqual = function(expected) {
  1037. return !this.env.equals_(this.actual, expected);
  1038. };
  1039. /**
  1040. * Matcher that compares the actual to the expected using a regular expression. Constructs a RegExp, so takes
  1041. * a pattern or a String.
  1042. *
  1043. * @param expected
  1044. */
  1045. jasmine.Matchers.prototype.toMatch = function(expected) {
  1046. return new RegExp(expected).test(this.actual);
  1047. };
  1048. /**
  1049. * Matcher that compares the actual to the expected using the boolean inverse of jasmine.Matchers.toMatch
  1050. * @param expected
  1051. * @deprecated as of 1.0. Use not.toMatch() instead.
  1052. */
  1053. jasmine.Matchers.prototype.toNotMatch = function(expected) {
  1054. return !(new RegExp(expected).test(this.actual));
  1055. };
  1056. /**
  1057. * Matcher that compares the actual to jasmine.undefined.
  1058. */
  1059. jasmine.Matchers.prototype.toBeDefined = function() {
  1060. return (this.actual !== jasmine.undefined);
  1061. };
  1062. /**
  1063. * Matcher that compares the actual to jasmine.undefined.
  1064. */
  1065. jasmine.Matchers.prototype.toBeUndefined = function() {
  1066. return (this.actual === jasmine.undefined);
  1067. };
  1068. /**
  1069. * Matcher that compares the actual to null.
  1070. */
  1071. jasmine.Matchers.prototype.toBeNull = function() {
  1072. return (this.actual === null);
  1073. };
  1074. /**
  1075. * Matcher that boolean not-nots the actual.
  1076. */
  1077. jasmine.Matchers.prototype.toBeTruthy = function() {
  1078. return !!this.actual;
  1079. };
  1080. /**
  1081. * Matcher that boolean nots the actual.
  1082. */
  1083. jasmine.Matchers.prototype.toBeFalsy = function() {
  1084. return !this.actual;
  1085. };
  1086. /**
  1087. * Matcher that checks to see if the actual, a Jasmine spy, was called.
  1088. */
  1089. jasmine.Matchers.prototype.toHaveBeenCalled = function() {
  1090. if (arguments.length > 0) {
  1091. throw new Error('toHaveBeenCalled does not take arguments, use toHaveBeenCalledWith');
  1092. }
  1093. if (!jasmine.isSpy(this.actual)) {
  1094. throw new Error('Expected a spy, but got ' + jasmine.pp(this.actual) + '.');
  1095. }
  1096. this.message = function() {
  1097. return [
  1098. "Expected spy " + this.actual.identity + " to have been called.",
  1099. "Expected spy " + this.actual.identity + " not to have been called."
  1100. ];
  1101. };
  1102. return this.actual.wasCalled;
  1103. };
  1104. /** @deprecated Use expect(xxx).toHaveBeenCalled() instead */
  1105. jasmine.Matchers.prototype.wasCalled = jasmine.Matchers.prototype.toHaveBeenCalled;
  1106. /**
  1107. * Matcher that checks to see if the actual, a Jasmine spy, was not called.
  1108. *
  1109. * @deprecated Use expect(xxx).not.toHaveBeenCalled() instead
  1110. */
  1111. jasmine.Matchers.prototype.wasNotCalled = function() {
  1112. if (arguments.length > 0) {
  1113. throw new Error('wasNotCalled does not take arguments');
  1114. }
  1115. if (!jasmine.isSpy(this.actual)) {
  1116. throw new Error('Expected a spy, but got ' + jasmine.pp(this.actual) + '.');
  1117. }
  1118. this.message = function() {
  1119. return [
  1120. "Expected spy " + this.actual.identity + " to not have been called.",
  1121. "Expected spy " + this.actual.identity + " to have been called."
  1122. ];
  1123. };
  1124. return !this.actual.wasCalled;
  1125. };
  1126. /**
  1127. * Matcher that checks to see if the actual, a Jasmine spy, was called with a set of parameters.
  1128. *
  1129. * @example
  1130. *
  1131. */
  1132. jasmine.Matchers.prototype.toHaveBeenCalledWith = function() {
  1133. var expectedArgs = jasmine.util.argsToArray(arguments);
  1134. if (!jasmine.isSpy(this.actual)) {
  1135. throw new Error('Expected a spy, but got ' + jasmine.pp(this.actual) + '.');
  1136. }
  1137. this.message = function() {
  1138. if (this.actual.callCount == 0) {
  1139. // todo: what should the failure message for .not.toHaveBeenCalledWith() be? is this right? test better. [xw]
  1140. return [
  1141. "Expected spy to have been called with " + jasmine.pp(expectedArgs) + " but it was never called.",
  1142. "Expected spy not to have been called with " + jasmine.pp(expectedArgs) + " but it was."
  1143. ];
  1144. } else {
  1145. return [
  1146. "Expected spy to have been called with " + jasmine.pp(expectedArgs) + " but was called with " + jasmine.pp(this.actual.argsForCall),
  1147. "Expected spy not to have been called with " + jasmine.pp(expectedArgs) + " but was called with " + jasmine.pp(this.actual.argsForCall)
  1148. ];
  1149. }
  1150. };
  1151. return this.env.contains_(this.actual.argsForCall, expectedArgs);
  1152. };
  1153. /** @deprecated Use expect(xxx).toHaveBeenCalledWith() instead */
  1154. jasmine.Matchers.prototype.wasCalledWith = jasmine.Matchers.prototype.toHaveBeenCalledWith;
  1155. /** @deprecated Use expect(xxx).not.toHaveBeenCalledWith() instead */
  1156. jasmine.Matchers.prototype.wasNotCalledWith = function() {
  1157. var expectedArgs = jasmine.util.argsToArray(arguments);
  1158. if (!jasmine.isSpy(this.actual)) {
  1159. throw new Error('Expected a spy, but got ' + jasmine.pp(this.actual) + '.');
  1160. }
  1161. this.message = function() {
  1162. return [
  1163. "Expected spy not to have been called with " + jasmine.pp(expectedArgs) + " but it was",
  1164. "Expected spy to have been called with " + jasmine.pp(expectedArgs) + " but it was"
  1165. ]
  1166. };
  1167. return !this.env.contains_(this.actual.argsForCall, expectedArgs);
  1168. };
  1169. /**
  1170. * Matcher that checks that the expected item is an element in the actual Array.
  1171. *
  1172. * @param {Object} expected
  1173. */
  1174. jasmine.Matchers.prototype.toContain = function(expected) {
  1175. return this.env.contains_(this.actual, expected);
  1176. };
  1177. /**
  1178. * Matcher that checks that the expected item is NOT an element in the actual Array.
  1179. *
  1180. * @param {Object} expected
  1181. * @deprecated as of 1.0. Use not.toNotContain() instead.
  1182. */
  1183. jasmine.Matchers.prototype.toNotContain = function(expected) {
  1184. return !this.env.contains_(this.actual, expected);
  1185. };
  1186. jasmine.Matchers.prototype.toBeLessThan = function(expected) {
  1187. return this.actual < expected;
  1188. };
  1189. jasmine.Matchers.prototype.toBeGreaterThan = function(expected) {
  1190. return this.actual > expected;
  1191. };
  1192. /**
  1193. * Matcher that checks that the expected exception was thrown by the actual.
  1194. *
  1195. * @param {String} expected
  1196. */
  1197. jasmine.Matchers.prototype.toThrow = function(expected) {
  1198. var result = false;
  1199. var exception;
  1200. if (typeof this.actual != 'function') {
  1201. throw new Error('Actual is not a function');
  1202. }
  1203. try {
  1204. this.actual();
  1205. } catch (e) {
  1206. exception = e;
  1207. }
  1208. if (exception) {
  1209. result = (expected === jasmine.undefined || this.env.equals_(exception.message || exception, expected.message || expected));
  1210. }
  1211. var not = this.isNot ? "not " : "";
  1212. this.message = function() {
  1213. if (exception && (expected === jasmine.undefined || !this.env.equals_(exception.message || exception, expected.message || expected))) {
  1214. return ["Expected function " + not + "to throw", expected ? expected.message || expected : " an exception", ", but it threw", exception.message || exception].join(' ');
  1215. } else {
  1216. return "Expected function to throw an exception.";
  1217. }
  1218. };
  1219. return result;
  1220. };
  1221. jasmine.Matchers.Any = function(expectedClass) {
  1222. this.expectedClass = expectedClass;
  1223. };
  1224. jasmine.Matchers.Any.prototype.matches = function(other) {
  1225. if (this.expectedClass == String) {
  1226. return typeof other == 'string' || other instanceof String;
  1227. }
  1228. if (this.expectedClass == Number) {
  1229. return typeof other == 'number' || other instanceof Number;
  1230. }
  1231. if (this.expectedClass == Function) {
  1232. return typeof other == 'function' || other instanceof Function;
  1233. }
  1234. if (this.expectedClass == Object) {
  1235. return typeof other == 'object';
  1236. }
  1237. return other instanceof this.expectedClass;
  1238. };
  1239. jasmine.Matchers.Any.prototype.toString = function() {
  1240. return '<jasmine.any(' + this.expectedClass + ')>';
  1241. };
  1242. /**
  1243. * @constructor
  1244. */
  1245. jasmine.MultiReporter = function() {
  1246. this.subReporters_ = [];
  1247. };
  1248. jasmine.util.inherit(jasmine.MultiReporter, jasmine.Reporter);
  1249. jasmine.MultiReporter.prototype.addReporter = function(reporter) {
  1250. this.subReporters_.push(reporter);
  1251. };
  1252. (function() {
  1253. var functionNames = [
  1254. "reportRunnerStarting",
  1255. "reportRunnerResults",
  1256. "reportSuiteResults",
  1257. "reportSpecStarting",
  1258. "reportSpecResults",
  1259. "log"
  1260. ];
  1261. for (var i = 0; i < functionNames.length; i++) {
  1262. var functionName = functionNames[i];
  1263. jasmine.MultiReporter.prototype[functionName] = (function(functionName) {
  1264. return function() {
  1265. for (var j = 0; j < this.subReporters_.length; j++) {
  1266. var subReporter = this.subReporters_[j];
  1267. if (subReporter[functionName]) {
  1268. subReporter[functionName].apply(subReporter, arguments);
  1269. }
  1270. }
  1271. };
  1272. })(functionName);
  1273. }
  1274. })();
  1275. /**
  1276. * Holds results for a set of Jasmine spec. Allows for the results array to hold another jasmine.NestedResults
  1277. *
  1278. * @constructor
  1279. */
  1280. jasmine.NestedResults = function() {
  1281. /**
  1282. * The total count of results
  1283. */
  1284. this.totalCount = 0;
  1285. /**
  1286. * Number of passed results
  1287. */
  1288. this.passedCount = 0;
  1289. /**
  1290. * Number of failed results
  1291. */
  1292. this.failedCount = 0;
  1293. /**
  1294. * Was this suite/spec skipped?
  1295. */
  1296. this.skipped = false;
  1297. /**
  1298. * @ignore
  1299. */
  1300. this.items_ = [];
  1301. };
  1302. /**
  1303. * Roll up the result counts.
  1304. *
  1305. * @param result
  1306. */
  1307. jasmine.NestedResults.prototype.rollupCounts = function(result) {
  1308. this.totalCount += result.totalCount;
  1309. this.passedCount += result.passedCount;
  1310. this.failedCount += result.failedCount;
  1311. };
  1312. /**
  1313. * Adds a log message.
  1314. * @param values Array of message parts which will be concatenated later.
  1315. */
  1316. jasmine.NestedResults.prototype.log = function(values) {
  1317. this.items_.push(new jasmine.MessageResult(values));
  1318. };
  1319. /**
  1320. * Getter for the results: message & results.
  1321. */
  1322. jasmine.NestedResults.prototype.getItems = function() {
  1323. return this.items_;
  1324. };
  1325. /**
  1326. * Adds a result, tracking counts (total, passed, & failed)
  1327. * @param {jasmine.ExpectationResult|jasmine.NestedResults} result
  1328. */
  1329. jasmine.NestedResults.prototype.addResult = function(result) {
  1330. if (result.type != 'log') {
  1331. if (result.items_) {
  1332. this.rollupCounts(result);
  1333. } else {
  1334. this.totalCount++;
  1335. if (result.passed()) {
  1336. this.passedCount++;
  1337. } else {
  1338. this.failedCount++;
  1339. }
  1340. }
  1341. }
  1342. this.items_.push(result);
  1343. };
  1344. /**
  1345. * @returns {Boolean} True if <b>everything</b> below passed
  1346. */
  1347. jasmine.NestedResults.prototype.passed = function() {
  1348. return this.passedCount === this.totalCount;
  1349. };
  1350. /**
  1351. * Base class for pretty printing for expectation results.
  1352. */
  1353. jasmine.PrettyPrinter = function() {
  1354. this.ppNestLevel_ = 0;
  1355. };
  1356. /**
  1357. * Formats a value in a nice, human-readable string.
  1358. *
  1359. * @param value
  1360. */
  1361. jasmine.PrettyPrinter.prototype.format = function(value) {
  1362. if (this.ppNestLevel_ > 40) {
  1363. throw new Error('jasmine.PrettyPrinter: format() nested too deeply!');
  1364. }
  1365. this.ppNestLevel_++;
  1366. try {
  1367. if (value === jasmine.undefined) {
  1368. this.emitScalar('undefined');
  1369. } else if (value === null) {
  1370. this.emitScalar('null');
  1371. } else if (value === jasmine.getGlobal()) {
  1372. this.emitScalar('<global>');
  1373. } else if (value instanceof jasmine.Matchers.Any) {
  1374. this.emitScalar(value.toString());
  1375. } else if (typeof value === 'string') {
  1376. this.emitString(value);
  1377. } else if (jasmine.isSpy(value)) {
  1378. this.emitScalar("spy on " + value.identity);
  1379. } else if (value instanceof RegExp) {
  1380. this.emitScalar(value.toString());
  1381. } else if (typeof value === 'function') {
  1382. this.emitScalar('Function');
  1383. } else if (typeof value.nodeType === 'number') {
  1384. this.emitScalar('HTMLNode');
  1385. } else if (value instanceof Date) {
  1386. this.emitScalar('Date(' + value + ')');
  1387. } else if (value.__Jasmine_been_here_before__) {
  1388. this.emitScalar('<circular reference: ' + (jasmine.isArray_(value) ? 'Array' : 'Object') + '>');
  1389. } else if (jasmine.isArray_(value) || typeof value == 'object') {
  1390. value.__Jasmine_been_here_before__ = true;
  1391. if (jasmine.isArray_(value)) {
  1392. this.emitArray(value);
  1393. } else {
  1394. this.emitObject(value);
  1395. }
  1396. delete value.__Jasmine_been_here_before__;
  1397. } else {
  1398. this.emitScalar(value.toString());
  1399. }
  1400. } finally {
  1401. this.ppNestLevel_--;
  1402. }
  1403. };
  1404. jasmine.PrettyPrinter.prototype.iterateObject = function(obj, fn) {
  1405. for (var property in obj) {
  1406. if (property == '__Jasmine_been_here_before__') continue;
  1407. fn(property, obj.__lookupGetter__ ? (obj.__lookupGetter__(property) != null) : false);
  1408. }
  1409. };
  1410. jasmine.PrettyPrinter.prototype.emitArray = jasmine.unimplementedMethod_;
  1411. jasmine.PrettyPrinter.prototype.emitObject = jasmine.unimplementedMethod_;
  1412. jasmine.PrettyPrinter.prototype.emitScalar = jasmine.unimplementedMethod_;
  1413. jasmine.PrettyPrinter.prototype.emitString = jasmine.unimplementedMethod_;
  1414. jasmine.StringPrettyPrinter = function() {
  1415. jasmine.PrettyPrinter.call(this);
  1416. this.string = '';
  1417. };
  1418. jasmine.util.inherit(jasmine.StringPrettyPrinter, jasmine.PrettyPrinter);
  1419. jasmine.StringPrettyPrinter.prototype.emitScalar = function(value) {
  1420. this.append(value);
  1421. };
  1422. jasmine.StringPrettyPrinter.prototype.emitString = function(value) {
  1423. this.append("'" + value + "'");
  1424. };
  1425. jasmine.StringPrettyPrinter.prototype.emitArray = function(array) {
  1426. this.append('[ ');
  1427. for (var i = 0; i < array.length; i++) {
  1428. if (i > 0) {
  1429. this.append(', ');
  1430. }
  1431. this.format(array[i]);
  1432. }
  1433. this.append(' ]');
  1434. };
  1435. jasmine.StringPrettyPrinter.prototype.emitObject = function(obj) {
  1436. var self = this;
  1437. this.append('{ ');
  1438. var first = true;
  1439. this.iterateObject(obj, function(property, isGetter) {
  1440. if (first) {
  1441. first = false;
  1442. } else {
  1443. self.append(', ');
  1444. }
  1445. self.append(property);
  1446. self.append(' : ');
  1447. if (isGetter) {
  1448. self.append('<getter>');
  1449. } else {
  1450. self.format(obj[property]);
  1451. }
  1452. });
  1453. this.append(' }');
  1454. };
  1455. jasmine.StringPrettyPrinter.prototype.append = function(value) {
  1456. this.string += value;
  1457. };
  1458. jasmine.Queue = function(env) {
  1459. this.env = env;
  1460. this.blocks = [];
  1461. this.running = false;
  1462. this.index = 0;
  1463. this.offset = 0;
  1464. this.abort = false;
  1465. };
  1466. jasmine.Queue.prototype.addBefore = function(block) {
  1467. this.blocks.unshift(block);
  1468. };
  1469. jasmine.Queue.prototype.add = function(block) {
  1470. this.blocks.push(block);
  1471. };
  1472. jasmine.Queue.prototype.insertNext = function(block) {
  1473. this.blocks.splice((this.index + this.offset + 1), 0, block);
  1474. this.offset++;
  1475. };
  1476. jasmine.Queue.prototype.start = function(onComplete) {
  1477. this.running = true;
  1478. this.onComplete = onComplete;
  1479. this.next_();
  1480. };
  1481. jasmine.Queue.prototype.isRunning = function() {
  1482. return this.running;
  1483. };
  1484. jasmine.Queue.LOOP_DONT_RECURSE = true;
  1485. jasmine.Queue.prototype.next_ = function() {
  1486. var self = this;
  1487. var goAgain = true;
  1488. while (goAgain) {
  1489. goAgain = false;
  1490. if (self.index < self.blocks.length && !this.abort) {
  1491. var calledSynchronously = true;
  1492. var completedSynchronously = false;
  1493. var onComplete = function () {
  1494. if (jasmine.Queue.LOOP_DONT_RECURSE && calledSynchronously) {
  1495. completedSynchronously = true;
  1496. return;
  1497. }
  1498. if (self.blocks[self.index].abort) {
  1499. self.abort = true;
  1500. }
  1501. self.offset = 0;
  1502. self.index++;
  1503. var now = new Date().getTime();
  1504. if (self.env.updateInterval && now - self.env.lastUpdate > self.env.updateInterval) {
  1505. self.env.lastUpdate = now;
  1506. self.env.setTimeout(function() {
  1507. self.next_();
  1508. }, 0);
  1509. } else {
  1510. if (jasmine.Queue.LOOP_DONT_RECURSE && completedSynchronously) {
  1511. goAgain = true;
  1512. } else {
  1513. self.next_();
  1514. }
  1515. }
  1516. };
  1517. self.blocks[self.index].execute(onComplete);
  1518. calledSynchronously = false;
  1519. if (completedSynchronously) {
  1520. onComplete();
  1521. }
  1522. } else {
  1523. self.running = false;
  1524. if (self.onComplete) {
  1525. self.onComplete();
  1526. }
  1527. }
  1528. }
  1529. };
  1530. jasmine.Queue.prototype.results = function() {
  1531. var results = new jasmine.NestedResults();
  1532. for (var i = 0; i < this.blocks.length; i++) {
  1533. if (this.blocks[i].results) {
  1534. results.addResult(this.blocks[i].results());
  1535. }
  1536. }
  1537. return results;
  1538. };
  1539. /**
  1540. * Runner
  1541. *
  1542. * @constructor
  1543. * @param {jasmine.Env} env
  1544. */
  1545. jasmine.Runner = function(env) {
  1546. var self = this;
  1547. self.env = env;
  1548. self.queue = new jasmine.Queue(env);
  1549. self.before_ = [];
  1550. self.after_ = [];
  1551. self.suites_ = [];
  1552. };
  1553. jasmine.Runner.prototype.execute = function() {
  1554. var self = this;
  1555. if (self.env.reporter.reportRunnerStarting) {
  1556. self.env.reporter.reportRunnerStarting(this);
  1557. }
  1558. self.queue.start(function () {
  1559. self.finishCallback();
  1560. });
  1561. };
  1562. jasmine.Runner.prototype.beforeEach = function(beforeEachFunction) {
  1563. beforeEachFunction.typeName = 'beforeEach';
  1564. this.before_.splice(0,0,beforeEachFunction);
  1565. };
  1566. jasmine.Runner.prototype.afterEach = function(afterEachFunction) {
  1567. afterEachFunction.typeName = 'afterEach';
  1568. this.after_.splice(0,0,afterEachFunction);
  1569. };
  1570. jasmine.Runner.prototype.finishCallback = function() {
  1571. this.env.reporter.reportRunnerResults(this);
  1572. };
  1573. jasmine.Runner.prototype.addSuite = function(suite) {
  1574. this.suites_.push(suite);
  1575. };
  1576. jasmine.Runner.prototype.add = function(block) {
  1577. if (block instanceof jasmine.Suite) {
  1578. this.addSuite(block);
  1579. }
  1580. this.queue.add(block);
  1581. };
  1582. jasmine.Runner.prototype.specs = function () {
  1583. var suites = this.suites();
  1584. var specs = [];
  1585. for (var i = 0; i < suites.length; i++) {
  1586. specs = specs.concat(suites[i].specs());
  1587. }
  1588. return specs;
  1589. };
  1590. jasmine.Runner.prototype.suites = function() {
  1591. return this.suites_;
  1592. };
  1593. jasmine.Runner.prototype.topLevelSuites = function() {
  1594. var topLevelSuites = [];
  1595. for (var i = 0; i < this.suites_.length; i++) {
  1596. if (!this.suites_[i].parentSuite) {
  1597. topLevelSuites.push(this.suites_[i]);
  1598. }
  1599. }
  1600. return topLevelSuites;
  1601. };
  1602. jasmine.Runner.prototype.results = function() {
  1603. return this.queue.results();
  1604. };
  1605. /**
  1606. * Internal representation of a Jasmine specification, or test.
  1607. *
  1608. * @constructor
  1609. * @param {jasmine.Env} env
  1610. * @param {jasmine.Suite} suite
  1611. * @param {String} description
  1612. */
  1613. jasmine.Spec = function(env, suite, description) {
  1614. if (!env) {
  1615. throw new Error('jasmine.Env() required');
  1616. }
  1617. if (!suite) {
  1618. throw new Error('jasmine.Suite() required');
  1619. }
  1620. var spec = this;
  1621. spec.id = env.nextSpecId ? env.nextSpecId() : null;
  1622. spec.env = env;
  1623. spec.suite = suite;
  1624. spec.description = description;
  1625. spec.queue = new jasmine.Queue(env);
  1626. spec.afterCallbacks = [];
  1627. spec.spies_ = [];
  1628. spec.results_ = new jasmine.NestedResults();
  1629. spec.results_.description = description;
  1630. spec.matchersClass = null;
  1631. };
  1632. jasmine.Spec.prototype.getFullName = function() {
  1633. return this.suite.getFullName() + ' ' + this.description + '.';
  1634. };
  1635. jasmine.Spec.prototype.results = function() {
  1636. return this.results_;
  1637. };
  1638. /**
  1639. * All parameters are pretty-printed and concatenated together, then written to the spec's output.
  1640. *
  1641. * Be careful not to leave calls to <code>jasmine.log</code> in production code.
  1642. */
  1643. jasmine.Spec.prototype.log = function() {
  1644. return this.results_.log(arguments);
  1645. };
  1646. jasmine.Spec.prototype.runs = function (func) {
  1647. var block = new jasmine.Block(this.env, func, this);
  1648. this.addToQueue(block);
  1649. return this;
  1650. };
  1651. jasmine.Spec.prototype.addToQueue = function (block) {
  1652. if (this.queue.isRunning()) {
  1653. this.queue.insertNext(block);
  1654. } else {
  1655. this.queue.add(block);
  1656. }
  1657. };
  1658. /**
  1659. * @param {jasmine.ExpectationResult} result
  1660. */
  1661. jasmine.Spec.prototype.addMatcherResult = function(result) {
  1662. this.results_.addResult(result);
  1663. };
  1664. jasmine.Spec.prototype.expect = function(actual) {
  1665. var positive = new (this.getMatchersClass_())(this.env, actual, this);
  1666. positive.not = new (this.getMatchersClass_())(this.env, actual, this, true);
  1667. return positive;
  1668. };
  1669. /**
  1670. * Waits a fixed time period before moving to the next block.
  1671. *
  1672. * @deprecated Use waitsFor() instead
  1673. * @param {Number} timeout milliseconds to wait
  1674. */
  1675. jasmine.Spec.prototype.waits = function(timeout) {
  1676. var waitsFunc = new jasmine.WaitsBlock(this.env, timeout, this);
  1677. this.addToQueue(waitsFunc);
  1678. return this;
  1679. };
  1680. /**
  1681. * Waits for the latchFunction to return true before proceeding to the next block.
  1682. *
  1683. * @param {Function} latchFunction
  1684. * @param {String} optional_timeoutMessage
  1685. * @param {Number} optional_timeout
  1686. */
  1687. jasmine.Spec.prototype.waitsFor = function(latchFunction, optional_timeoutMessage, optional_timeout) {
  1688. var latchFunction_ = null;
  1689. var optional_timeoutMessage_ = null;
  1690. var optional_timeout_ = null;
  1691. for (var i = 0; i < arguments.length; i++) {
  1692. var arg = arguments[i];
  1693. switch (typeof arg) {
  1694. case 'function':
  1695. latchFunction_ = arg;
  1696. break;
  1697. case 'string':
  1698. optional_timeoutMessage_ = arg;
  1699. break;
  1700. case 'number':
  1701. optional_timeout_ = arg;
  1702. break;
  1703. }
  1704. }
  1705. var waitsForFunc = new jasmine.WaitsForBlock(this.env, optional_timeout_, latchFunction_, optional_timeoutMessage_, this);
  1706. this.addToQueue(waitsForFunc);
  1707. return this;
  1708. };
  1709. jasmine.Spec.prototype.fail = function (e) {
  1710. var expectationResult = new jasmine.ExpectationResult({
  1711. passed: false,
  1712. message: e ? jasmine.util.formatException(e) : 'Exception'
  1713. });
  1714. this.results_.addResult(expectationResult);
  1715. };
  1716. jasmine.Spec.prototype.getMatchersClass_ = function() {
  1717. return this.matchersClass || this.env.matchersClass;
  1718. };
  1719. jasmine.Spec.prototype.addMatchers = function(matchersPrototype) {
  1720. var parent = this.getMatchersClass_();
  1721. var newMatchersClass = function() {
  1722. parent.apply(this, arguments);
  1723. };
  1724. jasmine.util.inherit(newMatchersClass, parent);
  1725. jasmine.Matchers.wrapInto_(matchersPrototype, newMatchersClass);
  1726. this.matchersClass = newMatchersClass;
  1727. };
  1728. jasmine.Spec.prototype.finishCallback = function() {
  1729. this.env.reporter.reportSpecResults(this);
  1730. };
  1731. jasmine.Spec.prototype.finish = function(onComplete) {
  1732. this.removeAllSpies();
  1733. this.finishCallback();
  1734. if (onComplete) {
  1735. onComplete();
  1736. }
  1737. };
  1738. jasmine.Spec.prototype.after = function(doAfter) {
  1739. if (this.queue.isRunning()) {
  1740. this.queue.add(new jasmine.Block(this.env, doAfter, this));
  1741. } else {
  1742. this.afterCallbacks.unshift(doAfter);
  1743. }
  1744. };
  1745. jasmine.Spec.prototype.execute = function(onComplete) {
  1746. var spec = this;
  1747. if (!spec.env.specFilter(spec)) {
  1748. spec.results_.skipped = true;
  1749. spec.finish(onComplete);
  1750. return;
  1751. }
  1752. this.env.reporter.reportSpecStarting(this);
  1753. spec.env.currentSpec = spec;
  1754. spec.addBeforesAndAftersToQueue();
  1755. spec.queue.start(function () {
  1756. spec.finish(onComplete);
  1757. });
  1758. };
  1759. jasmine.Spec.prototype.addBeforesAndAftersToQueue = function() {
  1760. var runner = this.env.currentRunner();
  1761. var i;
  1762. for (var suite = this.suite; suite; suite = suite.parentSuite) {
  1763. for (i = 0; i < suite.before_.length; i++) {
  1764. this.queue.addBefore(new jasmine.Block(this.env, suite.before_[i], this));
  1765. }
  1766. }
  1767. for (i = 0; i < runner.before_.length; i++) {
  1768. this.queue.addBefore(new jasmine.Block(this.env, runner.before_[i], this));
  1769. }
  1770. for (i = 0; i < this.afterCallbacks.length; i++) {
  1771. this.queue.add(new jasmine.Block(this.env, this.afterCallbacks[i], this));
  1772. }
  1773. for (suite = this.suite; suite; suite = suite.parentSuite) {
  1774. for (i = 0; i < suite.after_.length; i++) {
  1775. this.queue.add(new jasmine.Block(this.env, suite.after_[i], this));
  1776. }
  1777. }
  1778. for (i = 0; i < runner.after_.length; i++) {
  1779. this.queue.add(new jasmine.Block(this.env, runner.after_[i], this));
  1780. }
  1781. };
  1782. jasmine.Spec.prototype.explodes = function() {
  1783. throw 'explodes function should not have been called';
  1784. };
  1785. jasmine.Spec.prototype.spyOn = function(obj, methodName, ignoreMethodDoesntExist) {
  1786. if (obj == jasmine.undefined) {
  1787. throw "spyOn could not find an object to spy upon for " + methodName + "()";
  1788. }
  1789. if (!ignoreMethodDoesntExist && obj[methodName] === jasmine.undefined) {
  1790. throw methodName + '() method does not exist';
  1791. }
  1792. if (!ignoreMethodDoesntExist && obj[methodName] && obj[methodName].isSpy) {
  1793. throw new Error(methodName + ' has already been spied upon');
  1794. }
  1795. var spyObj = jasmine.createSpy(methodName);
  1796. this.spies_.push(spyObj);
  1797. spyObj.baseObj = obj;
  1798. spyObj.methodName = methodName;
  1799. spyObj.originalValue = obj[methodName];
  1800. obj[methodName] = spyObj;
  1801. return spyObj;
  1802. };
  1803. jasmine.Spec.prototype.removeAllSpies = function() {
  1804. for (var i = 0; i < this.spies_.length; i++) {
  1805. var spy = this.spies_[i];
  1806. spy.baseObj[spy.methodName] = spy.originalValue;
  1807. }
  1808. this.spies_ = [];
  1809. };
  1810. /**
  1811. * Internal representation of a Jasmine suite.
  1812. *
  1813. * @constructor
  1814. * @param {jasmine.Env} env
  1815. * @param {String} description
  1816. * @param {Function} specDefinitions
  1817. * @param {jasmine.Suite} parentSuite
  1818. */
  1819. jasmine.Suite = function(env, description, specDefinitions, parentSuite) {
  1820. var self = this;
  1821. self.id = env.nextSuiteId ? env.nextSuiteId() : null;
  1822. self.description = description;
  1823. self.queue = new jasmine.Queue(env);
  1824. self.parentSuite = parentSuite;
  1825. self.env = env;
  1826. self.before_ = [];
  1827. self.after_ = [];
  1828. self.children_ = [];
  1829. self.suites_ = [];
  1830. self.specs_ = [];
  1831. };
  1832. jasmine.Suite.prototype.getFullName = function() {
  1833. var fullName = this.description;
  1834. for (var parentSuite = this.parentSuite; parentSuite; parentSuite = parentSuite.parentSuite) {
  1835. fullName = parentSuite.description + ' ' + fullName;
  1836. }
  1837. return fullName;
  1838. };
  1839. jasmine.Suite.prototype.finish = function(onComplete) {
  1840. this.env.reporter.reportSuiteResults(this);
  1841. this.finished = true;
  1842. if (typeof(onComplete) == 'function') {
  1843. onComplete();
  1844. }
  1845. };
  1846. jasmine.Suite.prototype.beforeEach = function(beforeEachFunction) {
  1847. beforeEachFunction.typeName = 'beforeEach';
  1848. this.before_.unshift(beforeEachFunction);
  1849. };
  1850. jasmine.Suite.prototype.afterEach = function(afterEachFunction) {
  1851. afterEachFunction.typeName = 'afterEach';
  1852. this.after_.unshift(afterEachFunction);
  1853. };
  1854. jasmine.Suite.prototype.results = function() {
  1855. return this.queue.results();
  1856. };
  1857. jasmine.Suite.prototype.add = function(suiteOrSpec) {
  1858. this.children_.push(suiteOrSpec);
  1859. if (suiteOrSpec instanceof jasmine.Suite) {
  1860. this.suites_.push(suiteOrSpec);
  1861. this.env.currentRunner().addSuite(suiteOrSpec);
  1862. } else {
  1863. this.specs_.push(suiteOrSpec);
  1864. }
  1865. this.queue.add(suiteOrSpec);
  1866. };
  1867. jasmine.Suite.prototype.specs = function() {
  1868. return this.specs_;
  1869. };
  1870. jasmine.Suite.prototype.suites = function() {
  1871. return this.suites_;
  1872. };
  1873. jasmine.Suite.prototype.children = function() {
  1874. return this.children_;
  1875. };
  1876. jasmine.Suite.prototype.execute = function(onComplete) {
  1877. var self = this;
  1878. this.queue.start(function () {
  1879. self.finish(onComplete);
  1880. });
  1881. };
  1882. jasmine.WaitsBlock = function(env, timeout, spec) {
  1883. this.timeout = timeout;
  1884. jasmine.Block.call(this, env, null, spec);
  1885. };
  1886. jasmine.util.inherit(jasmine.WaitsBlock, jasmine.Block);
  1887. jasmine.WaitsBlock.prototype.execute = function (onComplete) {
  1888. this.env.reporter.log('>> Jasmine waiting for ' + this.timeout + ' ms...');
  1889. this.env.setTimeout(function () {
  1890. onComplete();
  1891. }, this.timeout);
  1892. };
  1893. /**
  1894. * A block which waits for some condition to become true, with timeout.
  1895. *
  1896. * @constructor
  1897. * @extends jasmine.Block
  1898. * @param {jasmine.Env} env The Jasmine environment.
  1899. * @param {Number} timeout The maximum time in milliseconds to wait for the condition to become true.
  1900. * @param {Function} latchFunction A function which returns true when the desired condition has been met.
  1901. * @param {String} message The message to display if the desired condition hasn't been met within the given time period.
  1902. * @param {jasmine.Spec} spec The Jasmine spec.
  1903. */
  1904. jasmine.WaitsForBlock = function(env, timeout, latchFunction, message, spec) {
  1905. this.timeout = timeout || env.defaultTimeoutInterval;
  1906. this.latchFunction = latchFunction;
  1907. this.message = message;
  1908. this.totalTimeSpentWaitingForLatch = 0;
  1909. jasmine.Block.call(this, env, null, spec);
  1910. };
  1911. jasmine.util.inherit(jasmine.WaitsForBlock, jasmine.Block);
  1912. jasmine.WaitsForBlock.TIMEOUT_INCREMENT = 10;
  1913. jasmine.WaitsForBlock.prototype.execute = function(onComplete) {
  1914. this.env.reporter.log('>> Jasmine waiting for ' + (this.message || 'something to happen'));
  1915. var latchFunctionResult;
  1916. try {
  1917. latchFunctionResult = this.latchFunction.apply(this.spec);
  1918. } catch (e) {
  1919. this.spec.fail(e);
  1920. onComplete();
  1921. return;
  1922. }
  1923. if (latchFunctionResult) {
  1924. onComplete();
  1925. } else if (this.totalTimeSpentWaitingForLatch >= this.timeout) {
  1926. var message = 'timed out after ' + this.timeout + ' msec waiting for ' + (this.message || 'something to happen');
  1927. this.spec.fail({
  1928. name: 'timeout',
  1929. message: message
  1930. });
  1931. this.abort = true;
  1932. onComplete();
  1933. } else {
  1934. this.totalTimeSpentWaitingForLatch += jasmine.WaitsForBlock.TIMEOUT_INCREMENT;
  1935. var self = this;
  1936. this.env.setTimeout(function() {
  1937. self.execute(onComplete);
  1938. }, jasmine.WaitsForBlock.TIMEOUT_INCREMENT);
  1939. }
  1940. };
  1941. // Mock setTimeout, clearTimeout
  1942. // Contributed by Pivotal Computer Systems, www.pivotalsf.com
  1943. jasmine.FakeTimer = function() {
  1944. this.reset();
  1945. var self = this;
  1946. self.setTimeout = function(funcToCall, millis) {
  1947. self.timeoutsMade++;
  1948. self.scheduleFunction(self.timeoutsMade, funcToCall, millis, false);
  1949. return self.timeoutsMade;
  1950. };
  1951. self.setInterval = function(funcToCall, millis) {
  1952. self.timeoutsMade++;
  1953. self.scheduleFunction(self.timeoutsMade, funcToCall, millis, true);
  1954. return self.timeoutsMade;
  1955. };
  1956. self.clearTimeout = function(timeoutKey) {
  1957. self.scheduledFunctions[timeoutKey] = jasmine.undefined;
  1958. };
  1959. self.clearInterval = function(timeoutKey) {
  1960. self.scheduledFunctions[timeoutKey] = jasmine.undefined;
  1961. };
  1962. };
  1963. jasmine.FakeTimer.prototype.reset = function() {
  1964. this.timeoutsMade = 0;
  1965. this.scheduledFunctions = {};
  1966. this.nowMillis = 0;
  1967. };
  1968. jasmine.FakeTimer.prototype.tick = function(millis) {
  1969. var oldMillis = this.nowMillis;
  1970. var newMillis = oldMillis + millis;
  1971. this.runFunctionsWithinRange(oldMillis, newMillis);
  1972. this.nowMillis = newMillis;
  1973. };
  1974. jasmine.FakeTimer.prototype.runFunctionsWithinRange = function(oldMillis, nowMillis) {
  1975. var scheduledFunc;
  1976. var funcsToRun = [];
  1977. for (var timeoutKey in this.scheduledFunctions) {
  1978. scheduledFunc = this.scheduledFunctions[timeoutKey];
  1979. if (scheduledFunc != jasmine.undefined &&
  1980. scheduledFunc.runAtMillis >= oldMillis &&
  1981. scheduledFunc.runAtMillis <= nowMillis) {
  1982. funcsToRun.push(scheduledFunc);
  1983. this.scheduledFunctions[timeoutKey] = jasmine.undefined;
  1984. }
  1985. }
  1986. if (funcsToRun.length > 0) {
  1987. funcsToRun.sort(function(a, b) {
  1988. return a.runAtMillis - b.runAtMillis;
  1989. });
  1990. for (var i = 0; i < funcsToRun.length; ++i) {
  1991. try {
  1992. var funcToRun = funcsToRun[i];
  1993. this.nowMillis = funcToRun.runAtMillis;
  1994. funcToRun.funcToCall();
  1995. if (funcToRun.recurring) {
  1996. this.scheduleFunction(funcToRun.timeoutKey,
  1997. funcToRun.funcToCall,
  1998. funcToRun.millis,
  1999. true);
  2000. }
  2001. } catch(e) {
  2002. }
  2003. }
  2004. this.runFunctionsWithinRange(oldMillis, nowMillis);
  2005. }
  2006. };
  2007. jasmine.FakeTimer.prototype.scheduleFunction = function(timeoutKey, funcToCall, millis, recurring) {
  2008. this.scheduledFunctions[timeoutKey] = {
  2009. runAtMillis: this.nowMillis + millis,
  2010. funcToCall: funcToCall,
  2011. recurring: recurring,
  2012. timeoutKey: timeoutKey,
  2013. millis: millis
  2014. };
  2015. };
  2016. /**
  2017. * @namespace
  2018. */
  2019. jasmine.Clock = {
  2020. defaultFakeTimer: new jasmine.FakeTimer(),
  2021. reset: function() {
  2022. jasmine.Clock.assertInstalled();
  2023. jasmine.Clock.defaultFakeTimer.reset();
  2024. },
  2025. tick: function(millis) {
  2026. jasmine.Clock.assertInstalled();
  2027. jasmine.Clock.defaultFakeTimer.tick(millis);
  2028. },
  2029. runFunctionsWithinRange: function(oldMillis, nowMillis) {
  2030. jasmine.Clock.defaultFakeTimer.runFunctionsWithinRange(oldMillis, nowMillis);
  2031. },
  2032. scheduleFunction: function(timeoutKey, funcToCall, millis, recurring) {
  2033. jasmine.Clock.defaultFakeTimer.scheduleFunction(timeoutKey, funcToCall, millis, recurring);
  2034. },
  2035. useMock: function() {
  2036. if (!jasmine.Clock.isInstalled()) {
  2037. var spec = jasmine.getEnv().currentSpec;
  2038. spec.after(jasmine.Clock.uninstallMock);
  2039. jasmine.Clock.installMock();
  2040. }
  2041. },
  2042. installMock: function() {
  2043. jasmine.Clock.installed = jasmine.Clock.defaultFakeTimer;
  2044. },
  2045. uninstallMock: function() {
  2046. jasmine.Clock.assertInstalled();
  2047. jasmine.Clock.installed = jasmine.Clock.real;
  2048. },
  2049. real: {
  2050. setTimeout: jasmine.getGlobal().setTimeout,
  2051. clearTimeout: jasmine.getGlobal().clearTimeout,
  2052. setInterval: jasmine.getGlobal().setInterval,
  2053. clearInterval: jasmine.getGlobal().clearInterval
  2054. },
  2055. assertInstalled: function() {
  2056. if (!jasmine.Clock.isInstalled()) {
  2057. throw new Error("Mock clock is not installed, use jasmine.Clock.useMock()");
  2058. }
  2059. },
  2060. isInstalled: function() {
  2061. return jasmine.Clock.installed == jasmine.Clock.defaultFakeTimer;
  2062. },
  2063. installed: null
  2064. };
  2065. jasmine.Clock.installed = jasmine.Clock.real;
  2066. //else for IE support
  2067. jasmine.getGlobal().setTimeout = function(funcToCall, millis) {
  2068. if (jasmine.Clock.installed.setTimeout.apply) {
  2069. return jasmine.Clock.installed.setTimeout.apply(this, arguments);
  2070. } else {
  2071. return jasmine.Clock.installed.setTimeout(funcToCall, millis);
  2072. }
  2073. };
  2074. jasmine.getGlobal().setInterval = function(funcToCall, millis) {
  2075. if (jasmine.Clock.installed.setInterval.apply) {
  2076. return jasmine.Clock.installed.setInterval.apply(this, arguments);
  2077. } else {
  2078. return jasmine.Clock.installed.setInterval(funcToCall, millis);
  2079. }
  2080. };
  2081. jasmine.getGlobal().clearTimeout = function(timeoutKey) {
  2082. if (jasmine.Clock.installed.clearTimeout.apply) {
  2083. return jasmine.Clock.installed.clearTimeout.apply(this, arguments);
  2084. } else {
  2085. return jasmine.Clock.installed.clearTimeout(timeoutKey);
  2086. }
  2087. };
  2088. jasmine.getGlobal().clearInterval = function(timeoutKey) {
  2089. if (jasmine.Clock.installed.clearTimeout.apply) {
  2090. return jasmine.Clock.installed.clearInterval.apply(this, arguments);
  2091. } else {
  2092. return jasmine.Clock.installed.clearInterval(timeoutKey);
  2093. }
  2094. };
  2095. jasmine.version_= {
  2096. "major": 1,
  2097. "minor": 0,
  2098. "build": "0.rc1",
  2099. "revision": 1282853377
  2100. };