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