jasmine.js 78 KB

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