From a394b564f494da0211426b83d48c9774dd8fc062 Mon Sep 17 00:00:00 2001 From: ragaskar Date: Mon, 28 Sep 2009 16:23:21 -0700 Subject: [PATCH] runner-wide beforeEach, afterEach support --- README.markdown | 44 +- doc/files.html | 2 +- doc/index.html | 2 +- doc/symbols/_global_.html | 2 +- doc/symbols/jasmine.Block.html | 2 +- doc/symbols/jasmine.Env.html | 2 +- doc/symbols/jasmine.JsApiReporter.html | 2 +- doc/symbols/jasmine.MultiReporter.html | 2 +- doc/symbols/jasmine.NestedResults.html | 2 +- doc/symbols/jasmine.Reporter.html | 2 +- doc/symbols/jasmine.Runner.html | 2 +- doc/symbols/jasmine.Spec.html | 2 +- doc/symbols/jasmine.Spy.html | 2 +- doc/symbols/jasmine.Suite.html | 2 +- doc/symbols/jasmine.html | 2 +- doc/symbols/jasmine.util.html | 2 +- doc/symbols/src/lib_jasmine-0.9.0.js.html | 2751 +++++++++++---------- lib/jasmine-0.9.0.js | 75 +- spec/suites/ExceptionsSpec.js | 2 +- spec/suites/ReporterSpec.js | 2 +- spec/suites/RunnerSpec.js | 93 +- spec/suites/SpecRunningSpec.js | 25 +- src/Env.js | 23 +- src/Runner.js | 13 + src/Spec.js | 27 +- src/Suite.js | 10 +- 26 files changed, 1659 insertions(+), 1436 deletions(-) diff --git a/README.markdown b/README.markdown index 9b23c2c4..6a2466e5 100644 --- a/README.markdown +++ b/README.markdown @@ -268,6 +268,25 @@ A suite can have a beforeEach declaration. It takes a function that is run befor }; }); +A runner can also have beforeEach declarations. Runner beforeEach functions are executed before every spec in all suites, and execute BEFORE suite beforeEach functions. For example: + + var runnerWideFoo = []; + + beforeEach(function () { + runnerWideFoo.push('runner'); + }); + + describe('some suite', function () { + + beforeEach(function () { + runnerWideFoo.push('suite'); + } + + it('should equal bar', function () { + expect(runnerWideFoo).toEqual(['runner', 'suite']); + }; + }); + #### afterEach Similarly, there is an afterEach declaration. It takes a function that is run after each spec. For example: @@ -284,7 +303,30 @@ Similarly, there is an afterEach declaration. It takes a function that is run a }; it('should equal 0 after', function () { - expect(suiteWideFoo).toEqual(0); + expect(suiteWideFoo).toEqual(0); + }; + }); + +A runner can also have an afterEach declarations. Runner afterEach functions are executed after every spec in all suites, and execute AFTER suite afterEach functions. For example: + + var runnerWideFoo = []; + + afterEach(function () { + runnerWideFoo.push('runner'); + }); + + describe('some suite', function () { + + afterEach(function () { + runnerWideFoo.push('suite'); + } + + it('should be empty', function () { + expect(runnerWideFoo).toEqual([]); + }; + + it('should be populated after', function () { + expect(runnerWideFoo).toEqual(['suite', 'runner']); }; }); diff --git a/doc/files.html b/doc/files.html index 11410a16..f9c2a662 100644 --- a/doc/files.html +++ b/doc/files.html @@ -258,7 +258,7 @@ ul.inheritsList
- Documentation generated by JsDoc Toolkit 2.1.0 on Mon Sep 28 2009 11:12:11 GMT-0700 (PDT) + Documentation generated by JsDoc Toolkit 2.1.0 on Mon Sep 28 2009 16:21:33 GMT-0700 (PDT)
\ No newline at end of file diff --git a/doc/index.html b/doc/index.html index 1fadab8b..39530364 100644 --- a/doc/index.html +++ b/doc/index.html @@ -300,7 +300,7 @@ ul.inheritsList
- Documentation generated by JsDoc Toolkit 2.1.0 on Mon Sep 28 2009 11:12:11 GMT-0700 (PDT) + Documentation generated by JsDoc Toolkit 2.1.0 on Mon Sep 28 2009 16:21:33 GMT-0700 (PDT)
\ No newline at end of file diff --git a/doc/symbols/_global_.html b/doc/symbols/_global_.html index 562431d5..ff8e406e 100644 --- a/doc/symbols/_global_.html +++ b/doc/symbols/_global_.html @@ -908,7 +908,7 @@ A convenience method that allows existing specs to be disabled temporarily durin
- Documentation generated by JsDoc Toolkit 2.1.0 on Mon Sep 28 2009 11:12:11 GMT-0700 (PDT) + Documentation generated by JsDoc Toolkit 2.1.0 on Mon Sep 28 2009 16:21:32 GMT-0700 (PDT)
diff --git a/doc/symbols/jasmine.Block.html b/doc/symbols/jasmine.Block.html index 904f1762..f9ad8a1f 100644 --- a/doc/symbols/jasmine.Block.html +++ b/doc/symbols/jasmine.Block.html @@ -339,7 +339,7 @@ ul.inheritsList
- Documentation generated by JsDoc Toolkit 2.1.0 on Mon Sep 28 2009 11:12:11 GMT-0700 (PDT) + Documentation generated by JsDoc Toolkit 2.1.0 on Mon Sep 28 2009 16:21:32 GMT-0700 (PDT)
diff --git a/doc/symbols/jasmine.Env.html b/doc/symbols/jasmine.Env.html index e850c9cb..ddabd8fd 100644 --- a/doc/symbols/jasmine.Env.html +++ b/doc/symbols/jasmine.Env.html @@ -507,7 +507,7 @@ ul.inheritsList
- Documentation generated by JsDoc Toolkit 2.1.0 on Mon Sep 28 2009 11:12:11 GMT-0700 (PDT) + Documentation generated by JsDoc Toolkit 2.1.0 on Mon Sep 28 2009 16:21:32 GMT-0700 (PDT)
diff --git a/doc/symbols/jasmine.JsApiReporter.html b/doc/symbols/jasmine.JsApiReporter.html index ff64c781..ead711b3 100644 --- a/doc/symbols/jasmine.JsApiReporter.html +++ b/doc/symbols/jasmine.JsApiReporter.html @@ -316,7 +316,7 @@ ul.inheritsList
- Documentation generated by JsDoc Toolkit 2.1.0 on Mon Sep 28 2009 11:12:11 GMT-0700 (PDT) + Documentation generated by JsDoc Toolkit 2.1.0 on Mon Sep 28 2009 16:21:32 GMT-0700 (PDT)
diff --git a/doc/symbols/jasmine.MultiReporter.html b/doc/symbols/jasmine.MultiReporter.html index 4b290987..11909945 100644 --- a/doc/symbols/jasmine.MultiReporter.html +++ b/doc/symbols/jasmine.MultiReporter.html @@ -316,7 +316,7 @@ ul.inheritsList
- Documentation generated by JsDoc Toolkit 2.1.0 on Mon Sep 28 2009 11:12:11 GMT-0700 (PDT) + Documentation generated by JsDoc Toolkit 2.1.0 on Mon Sep 28 2009 16:21:32 GMT-0700 (PDT)
diff --git a/doc/symbols/jasmine.NestedResults.html b/doc/symbols/jasmine.NestedResults.html index cbd9d734..ca7cc40f 100644 --- a/doc/symbols/jasmine.NestedResults.html +++ b/doc/symbols/jasmine.NestedResults.html @@ -700,7 +700,7 @@ ul.inheritsList
- Documentation generated by JsDoc Toolkit 2.1.0 on Mon Sep 28 2009 11:12:11 GMT-0700 (PDT) + Documentation generated by JsDoc Toolkit 2.1.0 on Mon Sep 28 2009 16:21:32 GMT-0700 (PDT)
diff --git a/doc/symbols/jasmine.Reporter.html b/doc/symbols/jasmine.Reporter.html index 3a54b0fb..cd7b660b 100644 --- a/doc/symbols/jasmine.Reporter.html +++ b/doc/symbols/jasmine.Reporter.html @@ -316,7 +316,7 @@ ul.inheritsList
- Documentation generated by JsDoc Toolkit 2.1.0 on Mon Sep 28 2009 11:12:11 GMT-0700 (PDT) + Documentation generated by JsDoc Toolkit 2.1.0 on Mon Sep 28 2009 16:21:33 GMT-0700 (PDT)
diff --git a/doc/symbols/jasmine.Runner.html b/doc/symbols/jasmine.Runner.html index 7afa2121..bb0a5b8a 100644 --- a/doc/symbols/jasmine.Runner.html +++ b/doc/symbols/jasmine.Runner.html @@ -418,7 +418,7 @@ ul.inheritsList
- Documentation generated by JsDoc Toolkit 2.1.0 on Mon Sep 28 2009 11:12:11 GMT-0700 (PDT) + Documentation generated by JsDoc Toolkit 2.1.0 on Mon Sep 28 2009 16:21:33 GMT-0700 (PDT)
diff --git a/doc/symbols/jasmine.Spec.html b/doc/symbols/jasmine.Spec.html index 6629e1d7..2ad34e44 100644 --- a/doc/symbols/jasmine.Spec.html +++ b/doc/symbols/jasmine.Spec.html @@ -396,7 +396,7 @@ ul.inheritsList
- Documentation generated by JsDoc Toolkit 2.1.0 on Mon Sep 28 2009 11:12:11 GMT-0700 (PDT) + Documentation generated by JsDoc Toolkit 2.1.0 on Mon Sep 28 2009 16:21:33 GMT-0700 (PDT)
diff --git a/doc/symbols/jasmine.Spy.html b/doc/symbols/jasmine.Spy.html index 2607bf1c..bb0fc831 100644 --- a/doc/symbols/jasmine.Spy.html +++ b/doc/symbols/jasmine.Spy.html @@ -845,7 +845,7 @@ expect(foo.bar.callCount).toEqual(0);
- Documentation generated by JsDoc Toolkit 2.1.0 on Mon Sep 28 2009 11:12:11 GMT-0700 (PDT) + Documentation generated by JsDoc Toolkit 2.1.0 on Mon Sep 28 2009 16:21:33 GMT-0700 (PDT)
diff --git a/doc/symbols/jasmine.Suite.html b/doc/symbols/jasmine.Suite.html index b8a3775e..c19a0ba9 100644 --- a/doc/symbols/jasmine.Suite.html +++ b/doc/symbols/jasmine.Suite.html @@ -436,7 +436,7 @@ ul.inheritsList
- Documentation generated by JsDoc Toolkit 2.1.0 on Mon Sep 28 2009 11:12:11 GMT-0700 (PDT) + Documentation generated by JsDoc Toolkit 2.1.0 on Mon Sep 28 2009 16:21:33 GMT-0700 (PDT)
diff --git a/doc/symbols/jasmine.html b/doc/symbols/jasmine.html index 3d4227ff..6355af29 100644 --- a/doc/symbols/jasmine.html +++ b/doc/symbols/jasmine.html @@ -678,7 +678,7 @@ Jasmine environment.
- Documentation generated by JsDoc Toolkit 2.1.0 on Mon Sep 28 2009 11:12:11 GMT-0700 (PDT) + Documentation generated by JsDoc Toolkit 2.1.0 on Mon Sep 28 2009 16:21:32 GMT-0700 (PDT)
diff --git a/doc/symbols/jasmine.util.html b/doc/symbols/jasmine.util.html index 7e04f24d..cdcc06ca 100644 --- a/doc/symbols/jasmine.util.html +++ b/doc/symbols/jasmine.util.html @@ -316,7 +316,7 @@ ul.inheritsList
- Documentation generated by JsDoc Toolkit 2.1.0 on Mon Sep 28 2009 11:12:11 GMT-0700 (PDT) + Documentation generated by JsDoc Toolkit 2.1.0 on Mon Sep 28 2009 16:21:33 GMT-0700 (PDT)
diff --git a/doc/symbols/src/lib_jasmine-0.9.0.js.html b/doc/symbols/src/lib_jasmine-0.9.0.js.html index e35deae9..c061f8b2 100644 --- a/doc/symbols/src/lib_jasmine-0.9.0.js.html +++ b/doc/symbols/src/lib_jasmine-0.9.0.js.html @@ -524,7 +524,7 @@ 517 "major": 0, 518 "minor": 9, 519 "build": 0, -520 "revision": 1254161493 +520 "revision": 1254179423 521 }; 522 /** 523 * @namespace @@ -593,7 +593,7 @@ 586 jasmine.Env = function() { 587 this.currentSpec = null; 588 this.currentSuite = null; -589 this.currentRunner = new jasmine.Runner(this); +589 this.currentRunner_ = new jasmine.Runner(this); 590 this.currentlyRunningTests = false; 591 592 this.reporter = new jasmine.MultiReporter(); @@ -651,7 +651,7 @@ 644 }; 645 646 jasmine.Env.prototype.execute = function() { -647 this.currentRunner.execute(); +647 this.currentRunner_.execute(); 648 }; 649 650 jasmine.Env.prototype.describe = function(description, specDefinitions) { @@ -661,7 +661,7 @@ 654 if (parentSuite) { 655 parentSuite.add(suite); 656 } else { -657 this.currentRunner.add(suite); +657 this.currentRunner_.add(suite); 658 } 659 660 this.currentSuite = suite; @@ -674,1422 +674,1455 @@ 667 }; 668 669 jasmine.Env.prototype.beforeEach = function(beforeEachFunction) { -670 this.currentSuite.beforeEach(beforeEachFunction); -671 }; -672 -673 jasmine.Env.prototype.afterEach = function(afterEachFunction) { -674 this.currentSuite.afterEach(afterEachFunction); +670 if (this.currentSuite) { +671 this.currentSuite.beforeEach(beforeEachFunction); +672 } else { +673 this.currentRunner_.beforeEach(beforeEachFunction); +674 } 675 }; 676 -677 jasmine.Env.prototype.xdescribe = function(desc, specDefinitions) { -678 return { -679 execute: function() { -680 } -681 }; -682 }; -683 -684 jasmine.Env.prototype.it = function(description, func) { -685 var spec = new jasmine.Spec(this, this.currentSuite, description); -686 this.currentSuite.add(spec); -687 this.currentSpec = spec; -688 -689 if (func) { -690 spec.runs(func); -691 } -692 -693 return spec; -694 }; -695 -696 jasmine.Env.prototype.xit = function(desc, func) { -697 return { -698 id: this.nextSpecId(), -699 runs: function() { -700 } -701 }; -702 }; -703 -704 jasmine.Env.prototype.compareObjects_ = function(a, b, mismatchKeys, mismatchValues) { -705 if (a.__Jasmine_been_here_before__ === b && b.__Jasmine_been_here_before__ === a) { -706 return true; -707 } +677 jasmine.Env.prototype.currentRunner = function () { +678 return this.currentRunner_; +679 }; +680 +681 jasmine.Env.prototype.afterEach = function(afterEachFunction) { +682 if (this.currentSuite) { +683 this.currentSuite.afterEach(afterEachFunction); +684 } else { +685 this.currentRunner_.afterEach(afterEachFunction); +686 } +687 +688 }; +689 +690 jasmine.Env.prototype.xdescribe = function(desc, specDefinitions) { +691 return { +692 execute: function() { +693 } +694 }; +695 }; +696 +697 jasmine.Env.prototype.it = function(description, func) { +698 var spec = new jasmine.Spec(this, this.currentSuite, description); +699 this.currentSuite.add(spec); +700 this.currentSpec = spec; +701 +702 if (func) { +703 spec.runs(func); +704 } +705 +706 return spec; +707 }; 708 -709 a.__Jasmine_been_here_before__ = b; -710 b.__Jasmine_been_here_before__ = a; -711 -712 var hasKey = function(obj, keyName) { -713 return obj != null && obj[keyName] !== undefined; +709 jasmine.Env.prototype.xit = function(desc, func) { +710 return { +711 id: this.nextSpecId(), +712 runs: function() { +713 } 714 }; -715 -716 for (var property in b) { -717 if (!hasKey(a, property) && hasKey(b, property)) { -718 mismatchKeys.push("expected has key '" + property + "', but missing from <b>actual</b>."); -719 } +715 }; +716 +717 jasmine.Env.prototype.compareObjects_ = function(a, b, mismatchKeys, mismatchValues) { +718 if (a.__Jasmine_been_here_before__ === b && b.__Jasmine_been_here_before__ === a) { +719 return true; 720 } -721 for (property in a) { -722 if (!hasKey(b, property) && hasKey(a, property)) { -723 mismatchKeys.push("<b>expected</b> missing key '" + property + "', but present in actual."); -724 } -725 } -726 for (property in b) { -727 if (property == '__Jasmine_been_here_before__') continue; -728 if (!this.equals_(a[property], b[property], mismatchKeys, mismatchValues)) { -729 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 />"); -730 } -731 } -732 -733 if (jasmine.isArray_(a) && jasmine.isArray_(b) && a.length != b.length) { -734 mismatchValues.push("arrays were not the same length"); -735 } -736 -737 delete a.__Jasmine_been_here_before__; -738 delete b.__Jasmine_been_here_before__; -739 return (mismatchKeys.length == 0 && mismatchValues.length == 0); -740 }; -741 -742 jasmine.Env.prototype.equals_ = function(a, b, mismatchKeys, mismatchValues) { -743 mismatchKeys = mismatchKeys || []; -744 mismatchValues = mismatchValues || []; +721 +722 a.__Jasmine_been_here_before__ = b; +723 b.__Jasmine_been_here_before__ = a; +724 +725 var hasKey = function(obj, keyName) { +726 return obj != null && obj[keyName] !== undefined; +727 }; +728 +729 for (var property in b) { +730 if (!hasKey(a, property) && hasKey(b, property)) { +731 mismatchKeys.push("expected has key '" + property + "', but missing from <b>actual</b>."); +732 } +733 } +734 for (property in a) { +735 if (!hasKey(b, property) && hasKey(a, property)) { +736 mismatchKeys.push("<b>expected</b> missing key '" + property + "', but present in actual."); +737 } +738 } +739 for (property in b) { +740 if (property == '__Jasmine_been_here_before__') continue; +741 if (!this.equals_(a[property], b[property], mismatchKeys, mismatchValues)) { +742 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 />"); +743 } +744 } 745 -746 if (a === b) return true; -747 -748 if (a === undefined || a === null || b === undefined || b === null) { -749 return (a == undefined && b == undefined); -750 } -751 -752 if (jasmine.isDomNode(a) && jasmine.isDomNode(b)) { -753 return a === b; -754 } -755 -756 if (a instanceof Date && b instanceof Date) { -757 return a.getTime() == b.getTime(); -758 } -759 -760 if (a instanceof jasmine.Matchers.Any) { -761 return a.matches(b); -762 } -763 -764 if (b instanceof jasmine.Matchers.Any) { -765 return b.matches(a); -766 } -767 -768 if (typeof a === "object" && typeof b === "object") { -769 return this.compareObjects_(a, b, mismatchKeys, mismatchValues); -770 } -771 -772 for (var i = 0; i < this.equalityTesters_.length; i++) { -773 var equalityTester = this.equalityTesters_[i]; -774 var result = equalityTester(a, b, this, mismatchKeys, mismatchValues); -775 if (result !== undefined) return result; -776 } -777 -778 //Straight check -779 return (a === b); -780 }; -781 -782 jasmine.Env.prototype.contains_ = function(haystack, needle) { -783 if (jasmine.isArray_(haystack)) { -784 for (var i = 0; i < haystack.length; i++) { -785 if (this.equals_(haystack[i], needle)) return true; -786 } -787 return false; -788 } -789 return haystack.indexOf(needle) >= 0; -790 }; -791 -792 jasmine.Env.prototype.addEqualityTester = function(equalityTester) { -793 this.equalityTesters_.push(equalityTester); -794 }; -795 /** No-op base class for Jasmine reporters. -796 * -797 * @constructor -798 */ -799 jasmine.Reporter = function() { -800 }; -801 -802 //noinspection JSUnusedLocalSymbols -803 jasmine.Reporter.prototype.reportRunnerStarting = function(runner) { -804 }; -805 -806 //noinspection JSUnusedLocalSymbols -807 jasmine.Reporter.prototype.reportRunnerResults = function(runner) { -808 }; -809 -810 //noinspection JSUnusedLocalSymbols -811 jasmine.Reporter.prototype.reportSuiteResults = function(suite) { -812 }; -813 -814 //noinspection JSUnusedLocalSymbols -815 jasmine.Reporter.prototype.reportSpecResults = function(spec) { -816 }; -817 -818 //noinspection JSUnusedLocalSymbols -819 jasmine.Reporter.prototype.log = function(str) { -820 }; -821 -822 /** -823 * Blocks are functions with executable code that make up a spec. -824 * -825 * @constructor -826 * @param {jasmine.Env} env -827 * @param {Function} func -828 * @param {jasmine.Spec} spec -829 */ -830 jasmine.Block = function(env, func, spec) { -831 this.env = env; -832 this.func = func; -833 this.spec = spec; -834 }; -835 -836 jasmine.Block.prototype.execute = function(onComplete) { -837 try { -838 this.func.apply(this.spec); -839 } catch (e) { -840 this.spec.fail(e); -841 } -842 onComplete(); -843 }; -844 /** JavaScript API reporter. -845 * -846 * @constructor -847 */ -848 jasmine.JsApiReporter = function() { -849 this.started = false; -850 this.finished = false; -851 this.suites_ = []; -852 this.results_ = {}; -853 }; -854 -855 jasmine.JsApiReporter.prototype.reportRunnerStarting = function(runner) { -856 this.started = true; -857 var suites = runner.suites(); -858 for (var i = 0; i < suites.length; i++) { -859 var suite = suites[i]; -860 this.suites_.push(this.summarize_(suite)); -861 } -862 }; -863 -864 jasmine.JsApiReporter.prototype.suites = function() { -865 return this.suites_; +746 if (jasmine.isArray_(a) && jasmine.isArray_(b) && a.length != b.length) { +747 mismatchValues.push("arrays were not the same length"); +748 } +749 +750 delete a.__Jasmine_been_here_before__; +751 delete b.__Jasmine_been_here_before__; +752 return (mismatchKeys.length == 0 && mismatchValues.length == 0); +753 }; +754 +755 jasmine.Env.prototype.equals_ = function(a, b, mismatchKeys, mismatchValues) { +756 mismatchKeys = mismatchKeys || []; +757 mismatchValues = mismatchValues || []; +758 +759 if (a === b) return true; +760 +761 if (a === undefined || a === null || b === undefined || b === null) { +762 return (a == undefined && b == undefined); +763 } +764 +765 if (jasmine.isDomNode(a) && jasmine.isDomNode(b)) { +766 return a === b; +767 } +768 +769 if (a instanceof Date && b instanceof Date) { +770 return a.getTime() == b.getTime(); +771 } +772 +773 if (a instanceof jasmine.Matchers.Any) { +774 return a.matches(b); +775 } +776 +777 if (b instanceof jasmine.Matchers.Any) { +778 return b.matches(a); +779 } +780 +781 if (typeof a === "object" && typeof b === "object") { +782 return this.compareObjects_(a, b, mismatchKeys, mismatchValues); +783 } +784 +785 for (var i = 0; i < this.equalityTesters_.length; i++) { +786 var equalityTester = this.equalityTesters_[i]; +787 var result = equalityTester(a, b, this, mismatchKeys, mismatchValues); +788 if (result !== undefined) return result; +789 } +790 +791 //Straight check +792 return (a === b); +793 }; +794 +795 jasmine.Env.prototype.contains_ = function(haystack, needle) { +796 if (jasmine.isArray_(haystack)) { +797 for (var i = 0; i < haystack.length; i++) { +798 if (this.equals_(haystack[i], needle)) return true; +799 } +800 return false; +801 } +802 return haystack.indexOf(needle) >= 0; +803 }; +804 +805 jasmine.Env.prototype.addEqualityTester = function(equalityTester) { +806 this.equalityTesters_.push(equalityTester); +807 }; +808 /** No-op base class for Jasmine reporters. +809 * +810 * @constructor +811 */ +812 jasmine.Reporter = function() { +813 }; +814 +815 //noinspection JSUnusedLocalSymbols +816 jasmine.Reporter.prototype.reportRunnerStarting = function(runner) { +817 }; +818 +819 //noinspection JSUnusedLocalSymbols +820 jasmine.Reporter.prototype.reportRunnerResults = function(runner) { +821 }; +822 +823 //noinspection JSUnusedLocalSymbols +824 jasmine.Reporter.prototype.reportSuiteResults = function(suite) { +825 }; +826 +827 //noinspection JSUnusedLocalSymbols +828 jasmine.Reporter.prototype.reportSpecResults = function(spec) { +829 }; +830 +831 //noinspection JSUnusedLocalSymbols +832 jasmine.Reporter.prototype.log = function(str) { +833 }; +834 +835 /** +836 * Blocks are functions with executable code that make up a spec. +837 * +838 * @constructor +839 * @param {jasmine.Env} env +840 * @param {Function} func +841 * @param {jasmine.Spec} spec +842 */ +843 jasmine.Block = function(env, func, spec) { +844 this.env = env; +845 this.func = func; +846 this.spec = spec; +847 }; +848 +849 jasmine.Block.prototype.execute = function(onComplete) { +850 try { +851 this.func.apply(this.spec); +852 } catch (e) { +853 this.spec.fail(e); +854 } +855 onComplete(); +856 }; +857 /** JavaScript API reporter. +858 * +859 * @constructor +860 */ +861 jasmine.JsApiReporter = function() { +862 this.started = false; +863 this.finished = false; +864 this.suites_ = []; +865 this.results_ = {}; 866 }; 867 -868 jasmine.JsApiReporter.prototype.summarize_ = function(suiteOrSpec) { -869 var isSuite = suiteOrSpec instanceof jasmine.Suite -870 var summary = { -871 id: suiteOrSpec.id, -872 name: suiteOrSpec.description, -873 type: isSuite ? 'suite' : 'spec', -874 children: [] -875 }; -876 if (isSuite) { -877 var specs = suiteOrSpec.specs(); -878 for (var i = 0; i < specs.length; i++) { -879 summary.children.push(this.summarize_(specs[i])); -880 } -881 } -882 return summary; -883 }; -884 -885 jasmine.JsApiReporter.prototype.results = function() { -886 return this.results_; -887 }; -888 -889 jasmine.JsApiReporter.prototype.resultsForSpec = function(specId) { -890 return this.results_[specId]; -891 }; -892 -893 //noinspection JSUnusedLocalSymbols -894 jasmine.JsApiReporter.prototype.reportRunnerResults = function(runner) { -895 this.finished = true; +868 jasmine.JsApiReporter.prototype.reportRunnerStarting = function(runner) { +869 this.started = true; +870 var suites = runner.suites(); +871 for (var i = 0; i < suites.length; i++) { +872 var suite = suites[i]; +873 this.suites_.push(this.summarize_(suite)); +874 } +875 }; +876 +877 jasmine.JsApiReporter.prototype.suites = function() { +878 return this.suites_; +879 }; +880 +881 jasmine.JsApiReporter.prototype.summarize_ = function(suiteOrSpec) { +882 var isSuite = suiteOrSpec instanceof jasmine.Suite +883 var summary = { +884 id: suiteOrSpec.id, +885 name: suiteOrSpec.description, +886 type: isSuite ? 'suite' : 'spec', +887 children: [] +888 }; +889 if (isSuite) { +890 var specs = suiteOrSpec.specs(); +891 for (var i = 0; i < specs.length; i++) { +892 summary.children.push(this.summarize_(specs[i])); +893 } +894 } +895 return summary; 896 }; 897 -898 //noinspection JSUnusedLocalSymbols -899 jasmine.JsApiReporter.prototype.reportSuiteResults = function(suite) { +898 jasmine.JsApiReporter.prototype.results = function() { +899 return this.results_; 900 }; 901 -902 //noinspection JSUnusedLocalSymbols -903 jasmine.JsApiReporter.prototype.reportSpecResults = function(spec) { -904 this.results_[spec.id] = { -905 messages: spec.results().getItems(), -906 result: spec.results().failedCount > 0 ? "failed" : "passed" -907 }; -908 }; -909 -910 //noinspection JSUnusedLocalSymbols -911 jasmine.JsApiReporter.prototype.log = function(str) { -912 }; -913 -914 jasmine.Matchers = function(env, actual, results) { -915 this.env = env; -916 this.actual = actual; -917 this.passing_message = 'Passed.'; -918 this.results_ = results || new jasmine.NestedResults(); -919 }; -920 -921 jasmine.Matchers.pp = function(str) { -922 return jasmine.util.htmlEscape(jasmine.pp(str)); -923 }; -924 -925 /** @deprecated */ -926 jasmine.Matchers.prototype.getResults = function() { -927 return this.results_; -928 }; -929 -930 jasmine.Matchers.prototype.results = function() { -931 return this.results_; +902 jasmine.JsApiReporter.prototype.resultsForSpec = function(specId) { +903 return this.results_[specId]; +904 }; +905 +906 //noinspection JSUnusedLocalSymbols +907 jasmine.JsApiReporter.prototype.reportRunnerResults = function(runner) { +908 this.finished = true; +909 }; +910 +911 //noinspection JSUnusedLocalSymbols +912 jasmine.JsApiReporter.prototype.reportSuiteResults = function(suite) { +913 }; +914 +915 //noinspection JSUnusedLocalSymbols +916 jasmine.JsApiReporter.prototype.reportSpecResults = function(spec) { +917 this.results_[spec.id] = { +918 messages: spec.results().getItems(), +919 result: spec.results().failedCount > 0 ? "failed" : "passed" +920 }; +921 }; +922 +923 //noinspection JSUnusedLocalSymbols +924 jasmine.JsApiReporter.prototype.log = function(str) { +925 }; +926 +927 jasmine.Matchers = function(env, actual, results) { +928 this.env = env; +929 this.actual = actual; +930 this.passing_message = 'Passed.'; +931 this.results_ = results || new jasmine.NestedResults(); 932 }; 933 -934 jasmine.Matchers.prototype.report = function(result, failing_message, details) { -935 this.results_.addResult(new jasmine.ExpectationResult(result, result ? this.passing_message : failing_message, details)); -936 return result; -937 }; -938 -939 /** -940 * Matcher that compares the actual to the expected using ===. -941 * -942 * @param expected -943 */ -944 jasmine.Matchers.prototype.toBe = function(expected) { -945 return this.report(this.actual === expected, 'Expected<br /><br />' + jasmine.Matchers.pp(expected) -946 + '<br /><br />to be the same object as<br /><br />' + jasmine.Matchers.pp(this.actual) -947 + '<br />'); -948 }; -949 -950 /** -951 * Matcher that compares the actual to the expected using !== -952 * @param expected -953 */ -954 jasmine.Matchers.prototype.toNotBe = function(expected) { -955 return this.report(this.actual !== expected, 'Expected<br /><br />' + jasmine.Matchers.pp(expected) -956 + '<br /><br />to be a different object from actual, but they were the same.'); -957 }; -958 -959 /** -960 * Matcher that compares the actual to the expected using common sense equality. Handles Objects, Arrays, etc. -961 * -962 * @param expected -963 */ -964 jasmine.Matchers.prototype.toEqual = function(expected) { -965 var mismatchKeys = []; -966 var mismatchValues = []; -967 -968 var formatMismatches = function(name, array) { -969 if (array.length == 0) return ''; -970 var errorOutput = '<br /><br />Different ' + name + ':<br />'; -971 for (var i = 0; i < array.length; i++) { -972 errorOutput += array[i] + '<br />'; -973 } -974 return errorOutput; -975 }; -976 -977 return this.report(this.env.equals_(this.actual, expected, mismatchKeys, mismatchValues), -978 'Expected<br /><br />' + jasmine.Matchers.pp(expected) -979 + '<br /><br />but got<br /><br />' + jasmine.Matchers.pp(this.actual) -980 + '<br />' -981 + formatMismatches('Keys', mismatchKeys) -982 + formatMismatches('Values', mismatchValues), { -983 matcherName: 'toEqual', expected: expected, actual: this.actual -984 }); -985 }; -986 /** @deprecated */ -987 jasmine.Matchers.prototype.should_equal = jasmine.Matchers.prototype.toEqual; -988 -989 /** -990 * Matcher that compares the actual to the expected using the ! of jasmine.Matchers.toEqual -991 * @param expected -992 */ -993 jasmine.Matchers.prototype.toNotEqual = function(expected) { -994 return this.report(!this.env.equals_(this.actual, expected), -995 'Expected ' + jasmine.Matchers.pp(expected) + ' to not equal ' + jasmine.Matchers.pp(this.actual) + ', but it does.'); -996 }; -997 /** @deprecated */ -998 jasmine.Matchers.prototype.should_not_equal = jasmine.Matchers.prototype.toNotEqual; -999 -1000 /** -1001 * Matcher that compares the actual to the expected using a regular expression. Constructs a RegExp, so takes -1002 * a pattern or a String. -1003 * -1004 * @param reg_exp +934 jasmine.Matchers.pp = function(str) { +935 return jasmine.util.htmlEscape(jasmine.pp(str)); +936 }; +937 +938 /** @deprecated */ +939 jasmine.Matchers.prototype.getResults = function() { +940 return this.results_; +941 }; +942 +943 jasmine.Matchers.prototype.results = function() { +944 return this.results_; +945 }; +946 +947 jasmine.Matchers.prototype.report = function(result, failing_message, details) { +948 this.results_.addResult(new jasmine.ExpectationResult(result, result ? this.passing_message : failing_message, details)); +949 return result; +950 }; +951 +952 /** +953 * Matcher that compares the actual to the expected using ===. +954 * +955 * @param expected +956 */ +957 jasmine.Matchers.prototype.toBe = function(expected) { +958 return this.report(this.actual === expected, 'Expected<br /><br />' + jasmine.Matchers.pp(expected) +959 + '<br /><br />to be the same object as<br /><br />' + jasmine.Matchers.pp(this.actual) +960 + '<br />'); +961 }; +962 +963 /** +964 * Matcher that compares the actual to the expected using !== +965 * @param expected +966 */ +967 jasmine.Matchers.prototype.toNotBe = function(expected) { +968 return this.report(this.actual !== expected, 'Expected<br /><br />' + jasmine.Matchers.pp(expected) +969 + '<br /><br />to be a different object from actual, but they were the same.'); +970 }; +971 +972 /** +973 * Matcher that compares the actual to the expected using common sense equality. Handles Objects, Arrays, etc. +974 * +975 * @param expected +976 */ +977 jasmine.Matchers.prototype.toEqual = function(expected) { +978 var mismatchKeys = []; +979 var mismatchValues = []; +980 +981 var formatMismatches = function(name, array) { +982 if (array.length == 0) return ''; +983 var errorOutput = '<br /><br />Different ' + name + ':<br />'; +984 for (var i = 0; i < array.length; i++) { +985 errorOutput += array[i] + '<br />'; +986 } +987 return errorOutput; +988 }; +989 +990 return this.report(this.env.equals_(this.actual, expected, mismatchKeys, mismatchValues), +991 'Expected<br /><br />' + jasmine.Matchers.pp(expected) +992 + '<br /><br />but got<br /><br />' + jasmine.Matchers.pp(this.actual) +993 + '<br />' +994 + formatMismatches('Keys', mismatchKeys) +995 + formatMismatches('Values', mismatchValues), { +996 matcherName: 'toEqual', expected: expected, actual: this.actual +997 }); +998 }; +999 /** @deprecated */ +1000 jasmine.Matchers.prototype.should_equal = jasmine.Matchers.prototype.toEqual; +1001 +1002 /** +1003 * Matcher that compares the actual to the expected using the ! of jasmine.Matchers.toEqual +1004 * @param expected 1005 */ -1006 jasmine.Matchers.prototype.toMatch = function(reg_exp) { -1007 return this.report((new RegExp(reg_exp).test(this.actual)), -1008 'Expected ' + jasmine.Matchers.pp(this.actual) + ' to match ' + reg_exp + '.'); +1006 jasmine.Matchers.prototype.toNotEqual = function(expected) { +1007 return this.report(!this.env.equals_(this.actual, expected), +1008 'Expected ' + jasmine.Matchers.pp(expected) + ' to not equal ' + jasmine.Matchers.pp(this.actual) + ', but it does.'); 1009 }; 1010 /** @deprecated */ -1011 jasmine.Matchers.prototype.should_match = jasmine.Matchers.prototype.toMatch; +1011 jasmine.Matchers.prototype.should_not_equal = jasmine.Matchers.prototype.toNotEqual; 1012 1013 /** -1014 * Matcher that compares the actual to the expected using the boolean inverse of jasmine.Matchers.toMatch -1015 * @param reg_exp -1016 */ -1017 jasmine.Matchers.prototype.toNotMatch = function(reg_exp) { -1018 return this.report((!new RegExp(reg_exp).test(this.actual)), -1019 'Expected ' + jasmine.Matchers.pp(this.actual) + ' to not match ' + reg_exp + '.'); -1020 }; -1021 /** @deprecated */ -1022 jasmine.Matchers.prototype.should_not_match = jasmine.Matchers.prototype.toNotMatch; -1023 -1024 /** -1025 * Matcher that compares the acutal to undefined. -1026 */ -1027 jasmine.Matchers.prototype.toBeDefined = function() { -1028 return this.report((this.actual !== undefined), -1029 'Expected a value to be defined but it was undefined.'); -1030 }; -1031 /** @deprecated */ -1032 jasmine.Matchers.prototype.should_be_defined = jasmine.Matchers.prototype.toBeDefined; -1033 -1034 /** -1035 * Matcher that compares the actual to null. -1036 * -1037 */ -1038 jasmine.Matchers.prototype.toBeNull = function() { -1039 return this.report((this.actual === null), -1040 'Expected a value to be null but it was ' + jasmine.Matchers.pp(this.actual) + '.'); -1041 }; -1042 /** @deprecated */ -1043 jasmine.Matchers.prototype.should_be_null = jasmine.Matchers.prototype.toBeNull; -1044 -1045 /** -1046 * Matcher that boolean not-nots the actual. -1047 */ -1048 jasmine.Matchers.prototype.toBeTruthy = function() { -1049 return this.report(!!this.actual, -1050 'Expected a value to be truthy but it was ' + jasmine.Matchers.pp(this.actual) + '.'); -1051 }; -1052 /** @deprecated */ -1053 jasmine.Matchers.prototype.should_be_truthy = jasmine.Matchers.prototype.toBeTruthy; -1054 -1055 /** -1056 * Matcher that boolean nots the actual. -1057 */ -1058 jasmine.Matchers.prototype.toBeFalsy = function() { -1059 return this.report(!this.actual, -1060 'Expected a value to be falsy but it was ' + jasmine.Matchers.pp(this.actual) + '.'); -1061 }; -1062 /** @deprecated */ -1063 jasmine.Matchers.prototype.should_be_falsy = jasmine.Matchers.prototype.toBeFalsy; -1064 -1065 /** -1066 * Matcher that checks to see if the acutal, a Jasmine spy, was called. -1067 */ -1068 jasmine.Matchers.prototype.wasCalled = function() { -1069 if (!this.actual || !this.actual.isSpy) { -1070 return this.report(false, 'Expected a spy, but got ' + jasmine.Matchers.pp(this.actual) + '.'); -1071 } -1072 if (arguments.length > 0) { -1073 return this.report(false, 'wasCalled matcher does not take arguments'); -1074 } -1075 return this.report((this.actual.wasCalled), -1076 'Expected spy "' + this.actual.identity + '" to have been called, but it was not.'); -1077 }; -1078 /** @deprecated */ -1079 jasmine.Matchers.prototype.was_called = jasmine.Matchers.prototype.wasCalled; -1080 -1081 /** -1082 * Matcher that checks to see if the acutal, a Jasmine spy, was not called. -1083 */ -1084 jasmine.Matchers.prototype.wasNotCalled = function() { -1085 if (!this.actual || !this.actual.isSpy) { -1086 return this.report(false, 'Expected a spy, but got ' + jasmine.Matchers.pp(this.actual) + '.'); +1014 * Matcher that compares the actual to the expected using a regular expression. Constructs a RegExp, so takes +1015 * a pattern or a String. +1016 * +1017 * @param reg_exp +1018 */ +1019 jasmine.Matchers.prototype.toMatch = function(reg_exp) { +1020 return this.report((new RegExp(reg_exp).test(this.actual)), +1021 'Expected ' + jasmine.Matchers.pp(this.actual) + ' to match ' + reg_exp + '.'); +1022 }; +1023 /** @deprecated */ +1024 jasmine.Matchers.prototype.should_match = jasmine.Matchers.prototype.toMatch; +1025 +1026 /** +1027 * Matcher that compares the actual to the expected using the boolean inverse of jasmine.Matchers.toMatch +1028 * @param reg_exp +1029 */ +1030 jasmine.Matchers.prototype.toNotMatch = function(reg_exp) { +1031 return this.report((!new RegExp(reg_exp).test(this.actual)), +1032 'Expected ' + jasmine.Matchers.pp(this.actual) + ' to not match ' + reg_exp + '.'); +1033 }; +1034 /** @deprecated */ +1035 jasmine.Matchers.prototype.should_not_match = jasmine.Matchers.prototype.toNotMatch; +1036 +1037 /** +1038 * Matcher that compares the acutal to undefined. +1039 */ +1040 jasmine.Matchers.prototype.toBeDefined = function() { +1041 return this.report((this.actual !== undefined), +1042 'Expected a value to be defined but it was undefined.'); +1043 }; +1044 /** @deprecated */ +1045 jasmine.Matchers.prototype.should_be_defined = jasmine.Matchers.prototype.toBeDefined; +1046 +1047 /** +1048 * Matcher that compares the actual to null. +1049 * +1050 */ +1051 jasmine.Matchers.prototype.toBeNull = function() { +1052 return this.report((this.actual === null), +1053 'Expected a value to be null but it was ' + jasmine.Matchers.pp(this.actual) + '.'); +1054 }; +1055 /** @deprecated */ +1056 jasmine.Matchers.prototype.should_be_null = jasmine.Matchers.prototype.toBeNull; +1057 +1058 /** +1059 * Matcher that boolean not-nots the actual. +1060 */ +1061 jasmine.Matchers.prototype.toBeTruthy = function() { +1062 return this.report(!!this.actual, +1063 'Expected a value to be truthy but it was ' + jasmine.Matchers.pp(this.actual) + '.'); +1064 }; +1065 /** @deprecated */ +1066 jasmine.Matchers.prototype.should_be_truthy = jasmine.Matchers.prototype.toBeTruthy; +1067 +1068 /** +1069 * Matcher that boolean nots the actual. +1070 */ +1071 jasmine.Matchers.prototype.toBeFalsy = function() { +1072 return this.report(!this.actual, +1073 'Expected a value to be falsy but it was ' + jasmine.Matchers.pp(this.actual) + '.'); +1074 }; +1075 /** @deprecated */ +1076 jasmine.Matchers.prototype.should_be_falsy = jasmine.Matchers.prototype.toBeFalsy; +1077 +1078 /** +1079 * Matcher that checks to see if the acutal, a Jasmine spy, was called. +1080 */ +1081 jasmine.Matchers.prototype.wasCalled = function() { +1082 if (!this.actual || !this.actual.isSpy) { +1083 return this.report(false, 'Expected a spy, but got ' + jasmine.Matchers.pp(this.actual) + '.'); +1084 } +1085 if (arguments.length > 0) { +1086 return this.report(false, 'wasCalled matcher does not take arguments'); 1087 } -1088 return this.report((!this.actual.wasCalled), -1089 'Expected spy "' + this.actual.identity + '" to not have been called, but it was.'); +1088 return this.report((this.actual.wasCalled), +1089 'Expected spy "' + this.actual.identity + '" to have been called, but it was not.'); 1090 }; 1091 /** @deprecated */ -1092 jasmine.Matchers.prototype.was_not_called = jasmine.Matchers.prototype.wasNotCalled; +1092 jasmine.Matchers.prototype.was_called = jasmine.Matchers.prototype.wasCalled; 1093 1094 /** -1095 * Matcher that checks to see if the acutal, a Jasmine spy, was called with a set of parameters. -1096 * -1097 * @example -1098 * -1099 */ -1100 jasmine.Matchers.prototype.wasCalledWith = function() { -1101 if (!this.actual || !this.actual.isSpy) { -1102 return this.report(false, 'Expected a spy, but got ' + jasmine.Matchers.pp(this.actual) + '.', { -1103 matcherName: 'wasCalledWith' -1104 }); -1105 } +1095 * Matcher that checks to see if the acutal, a Jasmine spy, was not called. +1096 */ +1097 jasmine.Matchers.prototype.wasNotCalled = function() { +1098 if (!this.actual || !this.actual.isSpy) { +1099 return this.report(false, 'Expected a spy, but got ' + jasmine.Matchers.pp(this.actual) + '.'); +1100 } +1101 return this.report((!this.actual.wasCalled), +1102 'Expected spy "' + this.actual.identity + '" to not have been called, but it was.'); +1103 }; +1104 /** @deprecated */ +1105 jasmine.Matchers.prototype.was_not_called = jasmine.Matchers.prototype.wasNotCalled; 1106 -1107 var args = jasmine.util.argsToArray(arguments); -1108 -1109 return this.report(this.env.contains_(this.actual.argsForCall, args), -1110 'Expected ' + jasmine.Matchers.pp(this.actual.argsForCall) + ' to contain ' + jasmine.Matchers.pp(args) + ', but it does not.', { -1111 matcherName: 'wasCalledWith', expected: args, actual: this.actual.argsForCall -1112 }); -1113 }; -1114 -1115 /** -1116 * Matcher that checks that the expected item is an element in the actual Array. -1117 * -1118 * @param {Object} item -1119 */ -1120 jasmine.Matchers.prototype.toContain = function(item) { -1121 return this.report(this.env.contains_(this.actual, item), -1122 'Expected ' + jasmine.Matchers.pp(this.actual) + ' to contain ' + jasmine.Matchers.pp(item) + ', but it does not.', { -1123 matcherName: 'toContain', expected: item, actual: this.actual -1124 }); -1125 }; -1126 -1127 /** -1128 * Matcher that checks that the expected item is NOT an element in the actual Array. -1129 * -1130 * @param {Object} item -1131 */ -1132 jasmine.Matchers.prototype.toNotContain = function(item) { -1133 return this.report(!this.env.contains_(this.actual, item), -1134 'Expected ' + jasmine.Matchers.pp(this.actual) + ' not to contain ' + jasmine.Matchers.pp(item) + ', but it does.'); -1135 }; -1136 -1137 jasmine.Matchers.prototype.toBeLessThan = function(expected) { -1138 return this.report(this.actual < expected, -1139 'Expected ' + jasmine.Matchers.pp(this.actual) + ' to be less than ' + jasmine.Matchers.pp(expected) + ', but it was not.'); -1140 }; -1141 -1142 jasmine.Matchers.prototype.toBeGreaterThan = function(expected) { -1143 return this.report(this.actual > expected, -1144 'Expected ' + jasmine.Matchers.pp(this.actual) + ' to be greater than ' + jasmine.Matchers.pp(expected) + ', but it was not.'); -1145 }; -1146 -1147 /** -1148 * Matcher that checks that the expected exception was thrown by the actual. -1149 * -1150 * @param {String} expectedException -1151 */ -1152 jasmine.Matchers.prototype.toThrow = function(expectedException) { -1153 var exception = null; -1154 try { -1155 this.actual(); -1156 } catch (e) { -1157 exception = e; -1158 } -1159 if (expectedException !== undefined) { -1160 if (exception == null) { -1161 return this.report(false, "Expected function to throw " + jasmine.Matchers.pp(expectedException) + ", but it did not."); -1162 } -1163 return this.report( -1164 this.env.equals_( -1165 exception.message || exception, -1166 expectedException.message || expectedException), -1167 "Expected function to throw " + jasmine.Matchers.pp(expectedException) + ", but it threw " + jasmine.Matchers.pp(exception) + "."); -1168 } else { -1169 return this.report(exception != null, "Expected function to throw an exception, but it did not."); -1170 } -1171 }; -1172 -1173 jasmine.Matchers.Any = function(expectedClass) { -1174 this.expectedClass = expectedClass; -1175 }; -1176 -1177 jasmine.Matchers.Any.prototype.matches = function(other) { -1178 if (this.expectedClass == String) { -1179 return typeof other == 'string' || other instanceof String; -1180 } -1181 -1182 if (this.expectedClass == Number) { -1183 return typeof other == 'number' || other instanceof Number; -1184 } +1107 /** +1108 * Matcher that checks to see if the acutal, a Jasmine spy, was called with a set of parameters. +1109 * +1110 * @example +1111 * +1112 */ +1113 jasmine.Matchers.prototype.wasCalledWith = function() { +1114 if (!this.actual || !this.actual.isSpy) { +1115 return this.report(false, 'Expected a spy, but got ' + jasmine.Matchers.pp(this.actual) + '.', { +1116 matcherName: 'wasCalledWith' +1117 }); +1118 } +1119 +1120 var args = jasmine.util.argsToArray(arguments); +1121 +1122 return this.report(this.env.contains_(this.actual.argsForCall, args), +1123 'Expected ' + jasmine.Matchers.pp(this.actual.argsForCall) + ' to contain ' + jasmine.Matchers.pp(args) + ', but it does not.', { +1124 matcherName: 'wasCalledWith', expected: args, actual: this.actual.argsForCall +1125 }); +1126 }; +1127 +1128 /** +1129 * Matcher that checks that the expected item is an element in the actual Array. +1130 * +1131 * @param {Object} item +1132 */ +1133 jasmine.Matchers.prototype.toContain = function(item) { +1134 return this.report(this.env.contains_(this.actual, item), +1135 'Expected ' + jasmine.Matchers.pp(this.actual) + ' to contain ' + jasmine.Matchers.pp(item) + ', but it does not.', { +1136 matcherName: 'toContain', expected: item, actual: this.actual +1137 }); +1138 }; +1139 +1140 /** +1141 * Matcher that checks that the expected item is NOT an element in the actual Array. +1142 * +1143 * @param {Object} item +1144 */ +1145 jasmine.Matchers.prototype.toNotContain = function(item) { +1146 return this.report(!this.env.contains_(this.actual, item), +1147 'Expected ' + jasmine.Matchers.pp(this.actual) + ' not to contain ' + jasmine.Matchers.pp(item) + ', but it does.'); +1148 }; +1149 +1150 jasmine.Matchers.prototype.toBeLessThan = function(expected) { +1151 return this.report(this.actual < expected, +1152 'Expected ' + jasmine.Matchers.pp(this.actual) + ' to be less than ' + jasmine.Matchers.pp(expected) + ', but it was not.'); +1153 }; +1154 +1155 jasmine.Matchers.prototype.toBeGreaterThan = function(expected) { +1156 return this.report(this.actual > expected, +1157 'Expected ' + jasmine.Matchers.pp(this.actual) + ' to be greater than ' + jasmine.Matchers.pp(expected) + ', but it was not.'); +1158 }; +1159 +1160 /** +1161 * Matcher that checks that the expected exception was thrown by the actual. +1162 * +1163 * @param {String} expectedException +1164 */ +1165 jasmine.Matchers.prototype.toThrow = function(expectedException) { +1166 var exception = null; +1167 try { +1168 this.actual(); +1169 } catch (e) { +1170 exception = e; +1171 } +1172 if (expectedException !== undefined) { +1173 if (exception == null) { +1174 return this.report(false, "Expected function to throw " + jasmine.Matchers.pp(expectedException) + ", but it did not."); +1175 } +1176 return this.report( +1177 this.env.equals_( +1178 exception.message || exception, +1179 expectedException.message || expectedException), +1180 "Expected function to throw " + jasmine.Matchers.pp(expectedException) + ", but it threw " + jasmine.Matchers.pp(exception) + "."); +1181 } else { +1182 return this.report(exception != null, "Expected function to throw an exception, but it did not."); +1183 } +1184 }; 1185 -1186 if (this.expectedClass == Function) { -1187 return typeof other == 'function' || other instanceof Function; -1188 } +1186 jasmine.Matchers.Any = function(expectedClass) { +1187 this.expectedClass = expectedClass; +1188 }; 1189 -1190 if (this.expectedClass == Object) { -1191 return typeof other == 'object'; -1192 } -1193 -1194 return other instanceof this.expectedClass; -1195 }; -1196 -1197 jasmine.Matchers.Any.prototype.toString = function() { -1198 return '<jasmine.any(' + this.expectedClass + ')>'; -1199 }; -1200 -1201 /** -1202 * @constructor -1203 */ -1204 jasmine.MultiReporter = function() { -1205 this.subReporters_ = []; -1206 }; -1207 jasmine.util.inherit(jasmine.MultiReporter, jasmine.Reporter); -1208 -1209 jasmine.MultiReporter.prototype.addReporter = function(reporter) { -1210 this.subReporters_.push(reporter); -1211 }; -1212 -1213 (function() { -1214 var functionNames = ["reportRunnerStarting", "reportRunnerResults", "reportSuiteResults", "reportSpecResults", "log"]; -1215 for (var i = 0; i < functionNames.length; i++) { -1216 var functionName = functionNames[i]; -1217 jasmine.MultiReporter.prototype[functionName] = (function(functionName) { -1218 return function() { -1219 for (var j = 0; j < this.subReporters_.length; j++) { -1220 var subReporter = this.subReporters_[j]; -1221 if (subReporter[functionName]) { -1222 subReporter[functionName].apply(subReporter, arguments); -1223 } -1224 } -1225 }; -1226 })(functionName); -1227 } -1228 })(); -1229 /** -1230 * Holds results for a set of Jasmine spec. Allows for the results array to hold another jasmine.NestedResults -1231 * -1232 * @constructor -1233 */ -1234 jasmine.NestedResults = function() { -1235 /** -1236 * The total count of results -1237 */ -1238 this.totalCount = 0; -1239 /** -1240 * Number of passed results -1241 */ -1242 this.passedCount = 0; -1243 /** -1244 * Number of failed results -1245 */ -1246 this.failedCount = 0; -1247 /** -1248 * Was this suite/spec skipped? -1249 */ -1250 this.skipped = false; -1251 /** -1252 * @ignore -1253 */ -1254 this.items_ = []; -1255 }; -1256 -1257 /** -1258 * Roll up the result counts. -1259 * -1260 * @param result -1261 */ -1262 jasmine.NestedResults.prototype.rollupCounts = function(result) { -1263 this.totalCount += result.totalCount; -1264 this.passedCount += result.passedCount; -1265 this.failedCount += result.failedCount; -1266 }; -1267 -1268 /** -1269 * Tracks a result's message. -1270 * @param message -1271 */ -1272 jasmine.NestedResults.prototype.log = function(message) { -1273 this.items_.push(new jasmine.MessageResult(message)); -1274 }; -1275 -1276 /** -1277 * Getter for the results: message & results. -1278 */ -1279 jasmine.NestedResults.prototype.getItems = function() { -1280 return this.items_; -1281 }; -1282 -1283 /** -1284 * Adds a result, tracking counts (total, passed, & failed) -1285 * @param {jasmine.ExpectationResult|jasmine.NestedResults} result -1286 */ -1287 jasmine.NestedResults.prototype.addResult = function(result) { -1288 if (result.type != 'MessageResult') { -1289 if (result.items_) { -1290 this.rollupCounts(result); -1291 } else { -1292 this.totalCount++; -1293 if (result.passed()) { -1294 this.passedCount++; -1295 } else { -1296 this.failedCount++; -1297 } -1298 } -1299 } -1300 this.items_.push(result); -1301 }; -1302 -1303 /** -1304 * @returns {Boolean} True if <b>everything</b> below passed -1305 */ -1306 jasmine.NestedResults.prototype.passed = function() { -1307 return this.passedCount === this.totalCount; -1308 }; -1309 /** -1310 * Base class for pretty printing for expectation results. -1311 */ -1312 jasmine.PrettyPrinter = function() { -1313 this.ppNestLevel_ = 0; +1190 jasmine.Matchers.Any.prototype.matches = function(other) { +1191 if (this.expectedClass == String) { +1192 return typeof other == 'string' || other instanceof String; +1193 } +1194 +1195 if (this.expectedClass == Number) { +1196 return typeof other == 'number' || other instanceof Number; +1197 } +1198 +1199 if (this.expectedClass == Function) { +1200 return typeof other == 'function' || other instanceof Function; +1201 } +1202 +1203 if (this.expectedClass == Object) { +1204 return typeof other == 'object'; +1205 } +1206 +1207 return other instanceof this.expectedClass; +1208 }; +1209 +1210 jasmine.Matchers.Any.prototype.toString = function() { +1211 return '<jasmine.any(' + this.expectedClass + ')>'; +1212 }; +1213 +1214 /** +1215 * @constructor +1216 */ +1217 jasmine.MultiReporter = function() { +1218 this.subReporters_ = []; +1219 }; +1220 jasmine.util.inherit(jasmine.MultiReporter, jasmine.Reporter); +1221 +1222 jasmine.MultiReporter.prototype.addReporter = function(reporter) { +1223 this.subReporters_.push(reporter); +1224 }; +1225 +1226 (function() { +1227 var functionNames = ["reportRunnerStarting", "reportRunnerResults", "reportSuiteResults", "reportSpecResults", "log"]; +1228 for (var i = 0; i < functionNames.length; i++) { +1229 var functionName = functionNames[i]; +1230 jasmine.MultiReporter.prototype[functionName] = (function(functionName) { +1231 return function() { +1232 for (var j = 0; j < this.subReporters_.length; j++) { +1233 var subReporter = this.subReporters_[j]; +1234 if (subReporter[functionName]) { +1235 subReporter[functionName].apply(subReporter, arguments); +1236 } +1237 } +1238 }; +1239 })(functionName); +1240 } +1241 })(); +1242 /** +1243 * Holds results for a set of Jasmine spec. Allows for the results array to hold another jasmine.NestedResults +1244 * +1245 * @constructor +1246 */ +1247 jasmine.NestedResults = function() { +1248 /** +1249 * The total count of results +1250 */ +1251 this.totalCount = 0; +1252 /** +1253 * Number of passed results +1254 */ +1255 this.passedCount = 0; +1256 /** +1257 * Number of failed results +1258 */ +1259 this.failedCount = 0; +1260 /** +1261 * Was this suite/spec skipped? +1262 */ +1263 this.skipped = false; +1264 /** +1265 * @ignore +1266 */ +1267 this.items_ = []; +1268 }; +1269 +1270 /** +1271 * Roll up the result counts. +1272 * +1273 * @param result +1274 */ +1275 jasmine.NestedResults.prototype.rollupCounts = function(result) { +1276 this.totalCount += result.totalCount; +1277 this.passedCount += result.passedCount; +1278 this.failedCount += result.failedCount; +1279 }; +1280 +1281 /** +1282 * Tracks a result's message. +1283 * @param message +1284 */ +1285 jasmine.NestedResults.prototype.log = function(message) { +1286 this.items_.push(new jasmine.MessageResult(message)); +1287 }; +1288 +1289 /** +1290 * Getter for the results: message & results. +1291 */ +1292 jasmine.NestedResults.prototype.getItems = function() { +1293 return this.items_; +1294 }; +1295 +1296 /** +1297 * Adds a result, tracking counts (total, passed, & failed) +1298 * @param {jasmine.ExpectationResult|jasmine.NestedResults} result +1299 */ +1300 jasmine.NestedResults.prototype.addResult = function(result) { +1301 if (result.type != 'MessageResult') { +1302 if (result.items_) { +1303 this.rollupCounts(result); +1304 } else { +1305 this.totalCount++; +1306 if (result.passed()) { +1307 this.passedCount++; +1308 } else { +1309 this.failedCount++; +1310 } +1311 } +1312 } +1313 this.items_.push(result); 1314 }; 1315 1316 /** -1317 * Formats a value in a nice, human-readable string. -1318 * -1319 * @param value -1320 * @returns {String} -1321 */ -1322 jasmine.PrettyPrinter.prototype.format = function(value) { -1323 if (this.ppNestLevel_ > 40) { -1324 // return '(jasmine.pp nested too deeply!)'; -1325 throw new Error('jasmine.PrettyPrinter: format() nested too deeply!'); -1326 } -1327 -1328 this.ppNestLevel_++; -1329 try { -1330 if (value === undefined) { -1331 this.emitScalar('undefined'); -1332 } else if (value === null) { -1333 this.emitScalar('null'); -1334 } else if (value.navigator && value.frames && value.setTimeout) { -1335 this.emitScalar('<window>'); -1336 } else if (value instanceof jasmine.Matchers.Any) { -1337 this.emitScalar(value.toString()); -1338 } else if (typeof value === 'string') { -1339 this.emitString(value); -1340 } else if (typeof value === 'function') { -1341 this.emitScalar('Function'); -1342 } else if (typeof value.nodeType === 'number') { -1343 this.emitScalar('HTMLNode'); -1344 } else if (value instanceof Date) { -1345 this.emitScalar('Date(' + value + ')'); -1346 } else if (value.__Jasmine_been_here_before__) { -1347 this.emitScalar('<circular reference: ' + (jasmine.isArray_(value) ? 'Array' : 'Object') + '>'); -1348 } else if (jasmine.isArray_(value) || typeof value == 'object') { -1349 value.__Jasmine_been_here_before__ = true; -1350 if (jasmine.isArray_(value)) { -1351 this.emitArray(value); -1352 } else { -1353 this.emitObject(value); -1354 } -1355 delete value.__Jasmine_been_here_before__; -1356 } else { -1357 this.emitScalar(value.toString()); -1358 } -1359 } finally { -1360 this.ppNestLevel_--; -1361 } -1362 }; -1363 -1364 jasmine.PrettyPrinter.prototype.iterateObject = function(obj, fn) { -1365 for (var property in obj) { -1366 if (property == '__Jasmine_been_here_before__') continue; -1367 fn(property, obj.__lookupGetter__(property) != null); -1368 } -1369 }; -1370 -1371 jasmine.PrettyPrinter.prototype.emitArray = jasmine.unimplementedMethod_; -1372 jasmine.PrettyPrinter.prototype.emitObject = jasmine.unimplementedMethod_; -1373 jasmine.PrettyPrinter.prototype.emitScalar = jasmine.unimplementedMethod_; -1374 jasmine.PrettyPrinter.prototype.emitString = jasmine.unimplementedMethod_; -1375 -1376 jasmine.StringPrettyPrinter = function() { -1377 jasmine.PrettyPrinter.call(this); -1378 -1379 this.string = ''; -1380 }; -1381 jasmine.util.inherit(jasmine.StringPrettyPrinter, jasmine.PrettyPrinter); -1382 -1383 jasmine.StringPrettyPrinter.prototype.emitScalar = function(value) { -1384 this.append(value); -1385 }; -1386 -1387 jasmine.StringPrettyPrinter.prototype.emitString = function(value) { -1388 this.append("'" + value + "'"); -1389 }; -1390 -1391 jasmine.StringPrettyPrinter.prototype.emitArray = function(array) { -1392 this.append('[ '); -1393 for (var i = 0; i < array.length; i++) { -1394 if (i > 0) { -1395 this.append(', '); -1396 } -1397 this.format(array[i]); -1398 } -1399 this.append(' ]'); -1400 }; -1401 -1402 jasmine.StringPrettyPrinter.prototype.emitObject = function(obj) { -1403 var self = this; -1404 this.append('{ '); -1405 var first = true; -1406 -1407 this.iterateObject(obj, function(property, isGetter) { -1408 if (first) { -1409 first = false; -1410 } else { -1411 self.append(', '); -1412 } -1413 -1414 self.append(property); -1415 self.append(' : '); -1416 if (isGetter) { -1417 self.append('<getter>'); -1418 } else { -1419 self.format(obj[property]); -1420 } -1421 }); -1422 -1423 this.append(' }'); -1424 }; -1425 -1426 jasmine.StringPrettyPrinter.prototype.append = function(value) { -1427 this.string += value; -1428 }; -1429 jasmine.Queue = function(env) { -1430 this.env = env; -1431 this.blocks = []; -1432 this.running = false; -1433 this.index = 0; -1434 this.offset = 0; -1435 }; -1436 -1437 jasmine.Queue.prototype.addBefore = function (block) { -1438 this.blocks.unshift(block); -1439 }; -1440 -1441 jasmine.Queue.prototype.add = function(block) { -1442 this.blocks.push(block); -1443 }; -1444 -1445 jasmine.Queue.prototype.insertNext = function (block) { -1446 this.blocks.splice((this.index + this.offset + 1), 0, block); -1447 this.offset++; +1317 * @returns {Boolean} True if <b>everything</b> below passed +1318 */ +1319 jasmine.NestedResults.prototype.passed = function() { +1320 return this.passedCount === this.totalCount; +1321 }; +1322 /** +1323 * Base class for pretty printing for expectation results. +1324 */ +1325 jasmine.PrettyPrinter = function() { +1326 this.ppNestLevel_ = 0; +1327 }; +1328 +1329 /** +1330 * Formats a value in a nice, human-readable string. +1331 * +1332 * @param value +1333 * @returns {String} +1334 */ +1335 jasmine.PrettyPrinter.prototype.format = function(value) { +1336 if (this.ppNestLevel_ > 40) { +1337 // return '(jasmine.pp nested too deeply!)'; +1338 throw new Error('jasmine.PrettyPrinter: format() nested too deeply!'); +1339 } +1340 +1341 this.ppNestLevel_++; +1342 try { +1343 if (value === undefined) { +1344 this.emitScalar('undefined'); +1345 } else if (value === null) { +1346 this.emitScalar('null'); +1347 } else if (value.navigator && value.frames && value.setTimeout) { +1348 this.emitScalar('<window>'); +1349 } else if (value instanceof jasmine.Matchers.Any) { +1350 this.emitScalar(value.toString()); +1351 } else if (typeof value === 'string') { +1352 this.emitString(value); +1353 } else if (typeof value === 'function') { +1354 this.emitScalar('Function'); +1355 } else if (typeof value.nodeType === 'number') { +1356 this.emitScalar('HTMLNode'); +1357 } else if (value instanceof Date) { +1358 this.emitScalar('Date(' + value + ')'); +1359 } else if (value.__Jasmine_been_here_before__) { +1360 this.emitScalar('<circular reference: ' + (jasmine.isArray_(value) ? 'Array' : 'Object') + '>'); +1361 } else if (jasmine.isArray_(value) || typeof value == 'object') { +1362 value.__Jasmine_been_here_before__ = true; +1363 if (jasmine.isArray_(value)) { +1364 this.emitArray(value); +1365 } else { +1366 this.emitObject(value); +1367 } +1368 delete value.__Jasmine_been_here_before__; +1369 } else { +1370 this.emitScalar(value.toString()); +1371 } +1372 } finally { +1373 this.ppNestLevel_--; +1374 } +1375 }; +1376 +1377 jasmine.PrettyPrinter.prototype.iterateObject = function(obj, fn) { +1378 for (var property in obj) { +1379 if (property == '__Jasmine_been_here_before__') continue; +1380 fn(property, obj.__lookupGetter__(property) != null); +1381 } +1382 }; +1383 +1384 jasmine.PrettyPrinter.prototype.emitArray = jasmine.unimplementedMethod_; +1385 jasmine.PrettyPrinter.prototype.emitObject = jasmine.unimplementedMethod_; +1386 jasmine.PrettyPrinter.prototype.emitScalar = jasmine.unimplementedMethod_; +1387 jasmine.PrettyPrinter.prototype.emitString = jasmine.unimplementedMethod_; +1388 +1389 jasmine.StringPrettyPrinter = function() { +1390 jasmine.PrettyPrinter.call(this); +1391 +1392 this.string = ''; +1393 }; +1394 jasmine.util.inherit(jasmine.StringPrettyPrinter, jasmine.PrettyPrinter); +1395 +1396 jasmine.StringPrettyPrinter.prototype.emitScalar = function(value) { +1397 this.append(value); +1398 }; +1399 +1400 jasmine.StringPrettyPrinter.prototype.emitString = function(value) { +1401 this.append("'" + value + "'"); +1402 }; +1403 +1404 jasmine.StringPrettyPrinter.prototype.emitArray = function(array) { +1405 this.append('[ '); +1406 for (var i = 0; i < array.length; i++) { +1407 if (i > 0) { +1408 this.append(', '); +1409 } +1410 this.format(array[i]); +1411 } +1412 this.append(' ]'); +1413 }; +1414 +1415 jasmine.StringPrettyPrinter.prototype.emitObject = function(obj) { +1416 var self = this; +1417 this.append('{ '); +1418 var first = true; +1419 +1420 this.iterateObject(obj, function(property, isGetter) { +1421 if (first) { +1422 first = false; +1423 } else { +1424 self.append(', '); +1425 } +1426 +1427 self.append(property); +1428 self.append(' : '); +1429 if (isGetter) { +1430 self.append('<getter>'); +1431 } else { +1432 self.format(obj[property]); +1433 } +1434 }); +1435 +1436 this.append(' }'); +1437 }; +1438 +1439 jasmine.StringPrettyPrinter.prototype.append = function(value) { +1440 this.string += value; +1441 }; +1442 jasmine.Queue = function(env) { +1443 this.env = env; +1444 this.blocks = []; +1445 this.running = false; +1446 this.index = 0; +1447 this.offset = 0; 1448 }; 1449 -1450 jasmine.Queue.prototype.start = function(onComplete) { -1451 var self = this; -1452 self.running = true; -1453 self.onComplete = onComplete; -1454 if (self.blocks[0]) { -1455 self.blocks[0].execute(function () { -1456 self._next(); -1457 }); -1458 } else { -1459 self.finish(); -1460 } +1450 jasmine.Queue.prototype.addBefore = function (block) { +1451 this.blocks.unshift(block); +1452 }; +1453 +1454 jasmine.Queue.prototype.add = function(block) { +1455 this.blocks.push(block); +1456 }; +1457 +1458 jasmine.Queue.prototype.insertNext = function (block) { +1459 this.blocks.splice((this.index + this.offset + 1), 0, block); +1460 this.offset++; 1461 }; 1462 -1463 jasmine.Queue.prototype.isRunning = function () { -1464 return this.running; -1465 }; -1466 -1467 jasmine.Queue.prototype._next = function () { -1468 var self = this; -1469 var doNext = function () { -1470 self.offset = 0; -1471 self.index++; -1472 if (self.index < self.blocks.length) { -1473 self.blocks[self.index].execute(function () { -1474 self._next(); -1475 }); -1476 } else { -1477 self.finish(); -1478 } -1479 }; -1480 var now = new Date().getTime(); -1481 if (this.env.updateInterval && now - this.env.lastUpdate > this.env.updateInterval) { -1482 this.env.lastUpdate = now; -1483 this.env.setTimeout(doNext, 0); -1484 } else { -1485 doNext(); -1486 } -1487 -1488 }; -1489 -1490 jasmine.Queue.prototype.finish = function () { -1491 this.running = false; -1492 if (this.onComplete) { -1493 this.onComplete(); -1494 } -1495 }; -1496 -1497 jasmine.Queue.prototype.results = function () { -1498 var results = new jasmine.NestedResults(); -1499 for (var i = 0; i < this.blocks.length; i++) { -1500 if (this.blocks[i].results) { -1501 results.addResult(this.blocks[i].results()); -1502 } -1503 } -1504 return results; -1505 }; -1506 -1507 -1508 /* JasmineReporters.reporter -1509 * Base object that will get called whenever a Spec, Suite, or Runner is done. It is up to -1510 * descendants of this object to do something with the results (see json_reporter.js) -1511 */ -1512 jasmine.Reporters = {}; -1513 -1514 jasmine.Reporters.reporter = function(callbacks) { -1515 var that = { -1516 callbacks: callbacks || {}, -1517 -1518 doCallback: function(callback, results) { -1519 if (callback) { -1520 callback(results); -1521 } -1522 }, -1523 -1524 reportRunnerResults: function(runner) { -1525 that.doCallback(that.callbacks.runnerCallback, runner); -1526 }, -1527 reportSuiteResults: function(suite) { -1528 that.doCallback(that.callbacks.suiteCallback, suite); -1529 }, -1530 reportSpecResults: function(spec) { -1531 that.doCallback(that.callbacks.specCallback, spec); -1532 }, -1533 log: function (str) { -1534 if (console && console.log) console.log(str); -1535 } -1536 }; -1537 -1538 return that; -1539 }; -1540 -1541 /** -1542 * Runner -1543 * -1544 * @constructor -1545 * @param {jasmine.Env} env -1546 */ -1547 jasmine.Runner = function(env) { -1548 var self = this; -1549 self.env = env; -1550 self.queue = new jasmine.Queue(env); -1551 self.suites_ = []; +1463 jasmine.Queue.prototype.start = function(onComplete) { +1464 var self = this; +1465 self.running = true; +1466 self.onComplete = onComplete; +1467 if (self.blocks[0]) { +1468 self.blocks[0].execute(function () { +1469 self._next(); +1470 }); +1471 } else { +1472 self.finish(); +1473 } +1474 }; +1475 +1476 jasmine.Queue.prototype.isRunning = function () { +1477 return this.running; +1478 }; +1479 +1480 jasmine.Queue.prototype._next = function () { +1481 var self = this; +1482 var doNext = function () { +1483 self.offset = 0; +1484 self.index++; +1485 if (self.index < self.blocks.length) { +1486 self.blocks[self.index].execute(function () { +1487 self._next(); +1488 }); +1489 } else { +1490 self.finish(); +1491 } +1492 }; +1493 var now = new Date().getTime(); +1494 if (this.env.updateInterval && now - this.env.lastUpdate > this.env.updateInterval) { +1495 this.env.lastUpdate = now; +1496 this.env.setTimeout(doNext, 0); +1497 } else { +1498 doNext(); +1499 } +1500 +1501 }; +1502 +1503 jasmine.Queue.prototype.finish = function () { +1504 this.running = false; +1505 if (this.onComplete) { +1506 this.onComplete(); +1507 } +1508 }; +1509 +1510 jasmine.Queue.prototype.results = function () { +1511 var results = new jasmine.NestedResults(); +1512 for (var i = 0; i < this.blocks.length; i++) { +1513 if (this.blocks[i].results) { +1514 results.addResult(this.blocks[i].results()); +1515 } +1516 } +1517 return results; +1518 }; +1519 +1520 +1521 /* JasmineReporters.reporter +1522 * Base object that will get called whenever a Spec, Suite, or Runner is done. It is up to +1523 * descendants of this object to do something with the results (see json_reporter.js) +1524 */ +1525 jasmine.Reporters = {}; +1526 +1527 jasmine.Reporters.reporter = function(callbacks) { +1528 var that = { +1529 callbacks: callbacks || {}, +1530 +1531 doCallback: function(callback, results) { +1532 if (callback) { +1533 callback(results); +1534 } +1535 }, +1536 +1537 reportRunnerResults: function(runner) { +1538 that.doCallback(that.callbacks.runnerCallback, runner); +1539 }, +1540 reportSuiteResults: function(suite) { +1541 that.doCallback(that.callbacks.suiteCallback, suite); +1542 }, +1543 reportSpecResults: function(spec) { +1544 that.doCallback(that.callbacks.specCallback, spec); +1545 }, +1546 log: function (str) { +1547 if (console && console.log) console.log(str); +1548 } +1549 }; +1550 +1551 return that; 1552 }; 1553 -1554 jasmine.Runner.prototype.execute = function() { -1555 var self = this; -1556 if (self.env.reporter.reportRunnerStarting) { -1557 self.env.reporter.reportRunnerStarting(this); -1558 } -1559 self.queue.start(function () { -1560 self.finishCallback(); -1561 }); -1562 }; -1563 -1564 jasmine.Runner.prototype.finishCallback = function() { -1565 this.env.reporter.reportRunnerResults(this); -1566 }; -1567 -1568 jasmine.Runner.prototype.addSuite = function(suite) { -1569 this.suites_.push(suite); -1570 }; -1571 -1572 jasmine.Runner.prototype.add = function(block) { -1573 if (block instanceof jasmine.Suite) { -1574 this.addSuite(block); -1575 } -1576 this.queue.add(block); +1554 /** +1555 * Runner +1556 * +1557 * @constructor +1558 * @param {jasmine.Env} env +1559 */ +1560 jasmine.Runner = function(env) { +1561 var self = this; +1562 self.env = env; +1563 self.queue = new jasmine.Queue(env); +1564 self.before_ = []; +1565 self.after_ = []; +1566 self.suites_ = []; +1567 }; +1568 +1569 jasmine.Runner.prototype.execute = function() { +1570 var self = this; +1571 if (self.env.reporter.reportRunnerStarting) { +1572 self.env.reporter.reportRunnerStarting(this); +1573 } +1574 self.queue.start(function () { +1575 self.finishCallback(); +1576 }); 1577 }; 1578 -1579 /** @deprecated */ -1580 jasmine.Runner.prototype.getAllSuites = function() { -1581 return this.suites_; +1579 jasmine.Runner.prototype.beforeEach = function(beforeEachFunction) { +1580 beforeEachFunction.typeName = 'beforeEach'; +1581 this.before_.push(beforeEachFunction); 1582 }; 1583 -1584 -1585 jasmine.Runner.prototype.suites = function() { -1586 return this.suites_; +1584 jasmine.Runner.prototype.afterEach = function(afterEachFunction) { +1585 afterEachFunction.typeName = 'afterEach'; +1586 this.after_.push(afterEachFunction); 1587 }; 1588 -1589 jasmine.Runner.prototype.results = function() { -1590 return this.queue.results(); -1591 }; -1592 -1593 /** @deprecated */ -1594 jasmine.Runner.prototype.getResults = function() { -1595 return this.queue.results(); +1589 +1590 jasmine.Runner.prototype.finishCallback = function() { +1591 this.env.reporter.reportRunnerResults(this); +1592 }; +1593 +1594 jasmine.Runner.prototype.addSuite = function(suite) { +1595 this.suites_.push(suite); 1596 }; -1597 /** -1598 * Internal representation of a Jasmine specification, or test. -1599 * -1600 * @constructor -1601 * @param {jasmine.Env} env -1602 * @param {jasmine.Suite} suite -1603 * @param {String} description -1604 */ -1605 jasmine.Spec = function(env, suite, description) { -1606 if (!env) { -1607 throw new Error('jasmine.Env() required'); -1608 }; -1609 if (!suite) { -1610 throw new Error('jasmine.Suite() required'); -1611 }; -1612 var spec = this; -1613 spec.id = env.nextSpecId ? env.nextSpecId() : null; -1614 spec.env = env; -1615 spec.suite = suite; -1616 spec.description = description; -1617 spec.queue = new jasmine.Queue(env); +1597 +1598 jasmine.Runner.prototype.add = function(block) { +1599 if (block instanceof jasmine.Suite) { +1600 this.addSuite(block); +1601 } +1602 this.queue.add(block); +1603 }; +1604 +1605 /** @deprecated */ +1606 jasmine.Runner.prototype.getAllSuites = function() { +1607 return this.suites_; +1608 }; +1609 +1610 +1611 jasmine.Runner.prototype.suites = function() { +1612 return this.suites_; +1613 }; +1614 +1615 jasmine.Runner.prototype.results = function() { +1616 return this.queue.results(); +1617 }; 1618 -1619 spec.afterCallbacks = []; -1620 spec.spies_ = []; -1621 -1622 spec.results_ = new jasmine.NestedResults(); -1623 spec.results_.description = description; -1624 spec.matchersClass = null; -1625 }; -1626 -1627 jasmine.Spec.prototype.getFullName = function() { -1628 return this.suite.getFullName() + ' ' + this.description + '.'; -1629 }; -1630 -1631 -1632 jasmine.Spec.prototype.results = function() { -1633 return this.results_; -1634 }; -1635 -1636 jasmine.Spec.prototype.log = function(message) { -1637 return this.results_.log(message); -1638 }; -1639 -1640 /** @deprecated */ -1641 jasmine.Spec.prototype.getResults = function() { -1642 return this.results_; -1643 }; -1644 -1645 jasmine.Spec.prototype.runs = function (func) { -1646 var block = new jasmine.Block(this.env, func, this); -1647 this.addToQueue(block); -1648 return this; -1649 }; -1650 -1651 jasmine.Spec.prototype.addToQueue = function (block) { -1652 if (this.queue.isRunning()) { -1653 this.queue.insertNext(block); -1654 } else { -1655 this.queue.add(block); -1656 } +1619 /** @deprecated */ +1620 jasmine.Runner.prototype.getResults = function() { +1621 return this.queue.results(); +1622 }; +1623 /** +1624 * Internal representation of a Jasmine specification, or test. +1625 * +1626 * @constructor +1627 * @param {jasmine.Env} env +1628 * @param {jasmine.Suite} suite +1629 * @param {String} description +1630 */ +1631 jasmine.Spec = function(env, suite, description) { +1632 if (!env) { +1633 throw new Error('jasmine.Env() required'); +1634 } +1635 ; +1636 if (!suite) { +1637 throw new Error('jasmine.Suite() required'); +1638 } +1639 ; +1640 var spec = this; +1641 spec.id = env.nextSpecId ? env.nextSpecId() : null; +1642 spec.env = env; +1643 spec.suite = suite; +1644 spec.description = description; +1645 spec.queue = new jasmine.Queue(env); +1646 +1647 spec.afterCallbacks = []; +1648 spec.spies_ = []; +1649 +1650 spec.results_ = new jasmine.NestedResults(); +1651 spec.results_.description = description; +1652 spec.matchersClass = null; +1653 }; +1654 +1655 jasmine.Spec.prototype.getFullName = function() { +1656 return this.suite.getFullName() + ' ' + this.description + '.'; 1657 }; 1658 -1659 /** -1660 * @private -1661 * @deprecated -1662 */ -1663 jasmine.Spec.prototype.expects_that = function(actual) { -1664 return this.expect(actual); -1665 }; -1666 -1667 jasmine.Spec.prototype.expect = function(actual) { -1668 return new (this.getMatchersClass_())(this.env, actual, this.results_); -1669 }; -1670 -1671 jasmine.Spec.prototype.waits = function(timeout) { -1672 var waitsFunc = new jasmine.WaitsBlock(this.env, timeout, this); -1673 this.addToQueue(waitsFunc); -1674 return this; -1675 }; -1676 -1677 jasmine.Spec.prototype.waitsFor = function(timeout, latchFunction, timeoutMessage) { -1678 var waitsForFunc = new jasmine.WaitsForBlock(this.env, timeout, latchFunction, timeoutMessage, this); -1679 this.addToQueue(waitsForFunc); -1680 return this; -1681 }; -1682 -1683 jasmine.Spec.prototype.fail = function (e) { -1684 this.results_.addResult(new jasmine.ExpectationResult(false, e ? jasmine.util.formatException(e) : null, null)); +1659 +1660 jasmine.Spec.prototype.results = function() { +1661 return this.results_; +1662 }; +1663 +1664 jasmine.Spec.prototype.log = function(message) { +1665 return this.results_.log(message); +1666 }; +1667 +1668 /** @deprecated */ +1669 jasmine.Spec.prototype.getResults = function() { +1670 return this.results_; +1671 }; +1672 +1673 jasmine.Spec.prototype.runs = function (func) { +1674 var block = new jasmine.Block(this.env, func, this); +1675 this.addToQueue(block); +1676 return this; +1677 }; +1678 +1679 jasmine.Spec.prototype.addToQueue = function (block) { +1680 if (this.queue.isRunning()) { +1681 this.queue.insertNext(block); +1682 } else { +1683 this.queue.add(block); +1684 } 1685 }; 1686 -1687 jasmine.Spec.prototype.getMatchersClass_ = function() { -1688 return this.matchersClass || jasmine.Matchers; -1689 }; -1690 -1691 jasmine.Spec.prototype.addMatchers = function(matchersPrototype) { -1692 var parent = this.getMatchersClass_(); -1693 var newMatchersClass = function() { -1694 parent.apply(this, arguments); -1695 }; -1696 jasmine.util.inherit(newMatchersClass, parent); -1697 for (var method in matchersPrototype) { -1698 newMatchersClass.prototype[method] = matchersPrototype[method]; -1699 } -1700 this.matchersClass = newMatchersClass; -1701 }; -1702 -1703 jasmine.Spec.prototype.finishCallback = function() { -1704 this.env.reporter.reportSpecResults(this); -1705 }; -1706 -1707 jasmine.Spec.prototype.finish = function(onComplete) { -1708 this.removeAllSpies(); -1709 this.finishCallback(); -1710 if (onComplete) { -1711 onComplete(); -1712 } +1687 /** +1688 * @private +1689 * @deprecated +1690 */ +1691 jasmine.Spec.prototype.expects_that = function(actual) { +1692 return this.expect(actual); +1693 }; +1694 +1695 jasmine.Spec.prototype.expect = function(actual) { +1696 return new (this.getMatchersClass_())(this.env, actual, this.results_); +1697 }; +1698 +1699 jasmine.Spec.prototype.waits = function(timeout) { +1700 var waitsFunc = new jasmine.WaitsBlock(this.env, timeout, this); +1701 this.addToQueue(waitsFunc); +1702 return this; +1703 }; +1704 +1705 jasmine.Spec.prototype.waitsFor = function(timeout, latchFunction, timeoutMessage) { +1706 var waitsForFunc = new jasmine.WaitsForBlock(this.env, timeout, latchFunction, timeoutMessage, this); +1707 this.addToQueue(waitsForFunc); +1708 return this; +1709 }; +1710 +1711 jasmine.Spec.prototype.fail = function (e) { +1712 this.results_.addResult(new jasmine.ExpectationResult(false, e ? jasmine.util.formatException(e) : null, null)); 1713 }; 1714 -1715 jasmine.Spec.prototype.after = function(doAfter, test) { -1716 -1717 if (this.queue.isRunning()) { -1718 this.queue.add(new jasmine.Block(this.env, doAfter, this)); -1719 } else { -1720 this.afterCallbacks.unshift(doAfter); -1721 } -1722 }; -1723 -1724 jasmine.Spec.prototype.execute = function(onComplete) { -1725 var spec = this; -1726 if (!spec.env.specFilter(spec)) { -1727 spec.results_.skipped = true; -1728 spec.finish(onComplete); -1729 return; -1730 } -1731 this.env.reporter.log('>> Jasmine Running ' + this.suite.description + ' ' + this.description + '...'); -1732 -1733 spec.env.currentSpec = spec; -1734 spec.env.currentlyRunningTests = true; -1735 -1736 spec.addBeforesAndAftersToQueue(); -1737 -1738 spec.queue.start(function () { -1739 spec.finish(onComplete); -1740 }); -1741 spec.env.currentlyRunningTests = false; -1742 }; -1743 -1744 jasmine.Spec.prototype.addBeforesAndAftersToQueue = function() { -1745 for (var suite = this.suite; suite; suite = suite.parentSuite) { -1746 if (suite.beforeQueue) { -1747 for (var i = 0; i < suite.beforeQueue.length; i++) -1748 this.queue.addBefore(new jasmine.Block(this.env, suite.beforeQueue[i], this)); -1749 } -1750 } -1751 for (i = 0; i < this.afterCallbacks.length; i++) { -1752 this.queue.add(new jasmine.Block(this.env, this.afterCallbacks[i], this)); -1753 } -1754 for (suite = this.suite; suite; suite = suite.parentSuite) { -1755 if (suite.afterQueue) { -1756 for (var j = 0; j < suite.afterQueue.length; j++) -1757 this.queue.add(new jasmine.Block(this.env, suite.afterQueue[j], this)); -1758 } -1759 } -1760 }; -1761 -1762 jasmine.Spec.prototype.explodes = function() { -1763 throw 'explodes function should not have been called'; -1764 }; +1715 jasmine.Spec.prototype.getMatchersClass_ = function() { +1716 return this.matchersClass || jasmine.Matchers; +1717 }; +1718 +1719 jasmine.Spec.prototype.addMatchers = function(matchersPrototype) { +1720 var parent = this.getMatchersClass_(); +1721 var newMatchersClass = function() { +1722 parent.apply(this, arguments); +1723 }; +1724 jasmine.util.inherit(newMatchersClass, parent); +1725 for (var method in matchersPrototype) { +1726 newMatchersClass.prototype[method] = matchersPrototype[method]; +1727 } +1728 this.matchersClass = newMatchersClass; +1729 }; +1730 +1731 jasmine.Spec.prototype.finishCallback = function() { +1732 this.env.reporter.reportSpecResults(this); +1733 }; +1734 +1735 jasmine.Spec.prototype.finish = function(onComplete) { +1736 this.removeAllSpies(); +1737 this.finishCallback(); +1738 if (onComplete) { +1739 onComplete(); +1740 } +1741 }; +1742 +1743 jasmine.Spec.prototype.after = function(doAfter, test) { +1744 +1745 if (this.queue.isRunning()) { +1746 this.queue.add(new jasmine.Block(this.env, doAfter, this)); +1747 } else { +1748 this.afterCallbacks.unshift(doAfter); +1749 } +1750 }; +1751 +1752 jasmine.Spec.prototype.execute = function(onComplete) { +1753 var spec = this; +1754 if (!spec.env.specFilter(spec)) { +1755 spec.results_.skipped = true; +1756 spec.finish(onComplete); +1757 return; +1758 } +1759 this.env.reporter.log('>> Jasmine Running ' + this.suite.description + ' ' + this.description + '...'); +1760 +1761 spec.env.currentSpec = spec; +1762 spec.env.currentlyRunningTests = true; +1763 +1764 spec.addBeforesAndAftersToQueue(); 1765 -1766 jasmine.Spec.prototype.spyOn = function(obj, methodName, ignoreMethodDoesntExist) { -1767 if (obj == undefined) { -1768 throw "spyOn could not find an object to spy upon for " + methodName + "()"; -1769 } -1770 -1771 if (!ignoreMethodDoesntExist && obj[methodName] === undefined) { -1772 throw methodName + '() method does not exist'; -1773 } -1774 -1775 if (!ignoreMethodDoesntExist && obj[methodName] && obj[methodName].isSpy) { -1776 throw new Error(methodName + ' has already been spied upon'); -1777 } -1778 -1779 var spyObj = jasmine.createSpy(methodName); -1780 -1781 this.spies_.push(spyObj); -1782 spyObj.baseObj = obj; -1783 spyObj.methodName = methodName; -1784 spyObj.originalValue = obj[methodName]; -1785 -1786 obj[methodName] = spyObj; -1787 -1788 return spyObj; -1789 }; -1790 -1791 jasmine.Spec.prototype.removeAllSpies = function() { -1792 for (var i = 0; i < this.spies_.length; i++) { -1793 var spy = this.spies_[i]; -1794 spy.baseObj[spy.methodName] = spy.originalValue; -1795 } -1796 this.spies_ = []; +1766 spec.queue.start(function () { +1767 spec.finish(onComplete); +1768 }); +1769 spec.env.currentlyRunningTests = false; +1770 }; +1771 +1772 jasmine.Spec.prototype.addBeforesAndAftersToQueue = function() { +1773 var runner = this.env.currentRunner(); +1774 for (var suite = this.suite; suite; suite = suite.parentSuite) { +1775 for (var i = 0; i < suite.before_.length; i++) { +1776 this.queue.addBefore(new jasmine.Block(this.env, suite.before_[i], this)); +1777 } +1778 } +1779 for (var i = 0; i < runner.before_.length; i++) { +1780 this.queue.addBefore(new jasmine.Block(this.env, runner.before_[i], this)); +1781 } +1782 for (i = 0; i < this.afterCallbacks.length; i++) { +1783 this.queue.add(new jasmine.Block(this.env, this.afterCallbacks[i], this)); +1784 } +1785 for (suite = this.suite; suite; suite = suite.parentSuite) { +1786 for (var i = 0; i < suite.after_.length; i++) { +1787 this.queue.add(new jasmine.Block(this.env, suite.after_[i], this)); +1788 } +1789 } +1790 for (var i = 0; i < runner.after_.length; i++) { +1791 this.queue.add(new jasmine.Block(this.env, runner.after_[i], this)); +1792 } +1793 }; +1794 +1795 jasmine.Spec.prototype.explodes = function() { +1796 throw 'explodes function should not have been called'; 1797 }; 1798 -1799 /** -1800 * Internal representation of a Jasmine suite. -1801 * -1802 * @constructor -1803 * @param {jasmine.Env} env -1804 * @param {String} description -1805 * @param {Function} specDefinitions -1806 * @param {jasmine.Suite} parentSuite -1807 */ -1808 jasmine.Suite = function(env, description, specDefinitions, parentSuite) { -1809 var self = this; -1810 self.id = env.nextSuiteId ? env.nextSuiteId() : null; -1811 self.description = description; -1812 self.queue = new jasmine.Queue(env); -1813 self.parentSuite = parentSuite; -1814 self.env = env; -1815 self.beforeQueue = []; -1816 self.afterQueue = []; -1817 self.specs_ = []; -1818 }; -1819 -1820 jasmine.Suite.prototype.getFullName = function() { -1821 var fullName = this.description; -1822 for (var parentSuite = this.parentSuite; parentSuite; parentSuite = parentSuite.parentSuite) { -1823 fullName = parentSuite.description + ' ' + fullName; -1824 } -1825 return fullName; -1826 }; -1827 -1828 jasmine.Suite.prototype.finish = function(onComplete) { -1829 this.env.reporter.reportSuiteResults(this); -1830 this.finished = true; -1831 if (typeof(onComplete) == 'function') { -1832 onComplete(); -1833 } -1834 }; -1835 -1836 jasmine.Suite.prototype.beforeEach = function(beforeEachFunction) { -1837 beforeEachFunction.typeName = 'beforeEach'; -1838 this.beforeQueue.push(beforeEachFunction); -1839 }; -1840 -1841 jasmine.Suite.prototype.afterEach = function(afterEachFunction) { -1842 afterEachFunction.typeName = 'afterEach'; -1843 this.afterQueue.push(afterEachFunction); -1844 }; -1845 -1846 /** @deprecated */ -1847 jasmine.Suite.prototype.getResults = function() { -1848 return this.queue.results(); -1849 }; -1850 -1851 jasmine.Suite.prototype.results = function() { -1852 return this.queue.results(); -1853 }; -1854 -1855 jasmine.Suite.prototype.add = function(block) { -1856 if (block instanceof jasmine.Suite) { -1857 this.env.currentRunner.addSuite(block); -1858 } else { -1859 this.specs_.push(block); -1860 } -1861 this.queue.add(block); -1862 }; -1863 -1864 /** @deprecated */ -1865 jasmine.Suite.prototype.specCount = function() { -1866 return this.specs_.length; +1799 jasmine.Spec.prototype.spyOn = function(obj, methodName, ignoreMethodDoesntExist) { +1800 if (obj == undefined) { +1801 throw "spyOn could not find an object to spy upon for " + methodName + "()"; +1802 } +1803 +1804 if (!ignoreMethodDoesntExist && obj[methodName] === undefined) { +1805 throw methodName + '() method does not exist'; +1806 } +1807 +1808 if (!ignoreMethodDoesntExist && obj[methodName] && obj[methodName].isSpy) { +1809 throw new Error(methodName + ' has already been spied upon'); +1810 } +1811 +1812 var spyObj = jasmine.createSpy(methodName); +1813 +1814 this.spies_.push(spyObj); +1815 spyObj.baseObj = obj; +1816 spyObj.methodName = methodName; +1817 spyObj.originalValue = obj[methodName]; +1818 +1819 obj[methodName] = spyObj; +1820 +1821 return spyObj; +1822 }; +1823 +1824 jasmine.Spec.prototype.removeAllSpies = function() { +1825 for (var i = 0; i < this.spies_.length; i++) { +1826 var spy = this.spies_[i]; +1827 spy.baseObj[spy.methodName] = spy.originalValue; +1828 } +1829 this.spies_ = []; +1830 }; +1831 +1832 /** +1833 * Internal representation of a Jasmine suite. +1834 * +1835 * @constructor +1836 * @param {jasmine.Env} env +1837 * @param {String} description +1838 * @param {Function} specDefinitions +1839 * @param {jasmine.Suite} parentSuite +1840 */ +1841 jasmine.Suite = function(env, description, specDefinitions, parentSuite) { +1842 var self = this; +1843 self.id = env.nextSuiteId ? env.nextSuiteId() : null; +1844 self.description = description; +1845 self.queue = new jasmine.Queue(env); +1846 self.parentSuite = parentSuite; +1847 self.env = env; +1848 self.before_ = []; +1849 self.after_ = []; +1850 self.specs_ = []; +1851 }; +1852 +1853 jasmine.Suite.prototype.getFullName = function() { +1854 var fullName = this.description; +1855 for (var parentSuite = this.parentSuite; parentSuite; parentSuite = parentSuite.parentSuite) { +1856 fullName = parentSuite.description + ' ' + fullName; +1857 } +1858 return fullName; +1859 }; +1860 +1861 jasmine.Suite.prototype.finish = function(onComplete) { +1862 this.env.reporter.reportSuiteResults(this); +1863 this.finished = true; +1864 if (typeof(onComplete) == 'function') { +1865 onComplete(); +1866 } 1867 }; 1868 -1869 jasmine.Suite.prototype.specs = function() { -1870 return this.specs_; -1871 }; -1872 -1873 jasmine.Suite.prototype.execute = function(onComplete) { -1874 var self = this; -1875 this.queue.start(function () { -1876 self.finish(onComplete); -1877 }); -1878 }; -1879 jasmine.WaitsBlock = function(env, timeout, spec) { -1880 this.timeout = timeout; -1881 jasmine.Block.call(this, env, null, spec); +1869 jasmine.Suite.prototype.beforeEach = function(beforeEachFunction) { +1870 beforeEachFunction.typeName = 'beforeEach'; +1871 this.before_.push(beforeEachFunction); +1872 }; +1873 +1874 jasmine.Suite.prototype.afterEach = function(afterEachFunction) { +1875 afterEachFunction.typeName = 'afterEach'; +1876 this.after_.push(afterEachFunction); +1877 }; +1878 +1879 /** @deprecated */ +1880 jasmine.Suite.prototype.getResults = function() { +1881 return this.queue.results(); 1882 }; 1883 -1884 jasmine.util.inherit(jasmine.WaitsBlock, jasmine.Block); -1885 -1886 jasmine.WaitsBlock.prototype.execute = function (onComplete) { -1887 this.env.reporter.log('>> Jasmine waiting for ' + this.timeout + ' ms...'); -1888 this.env.setTimeout(function () { -1889 onComplete(); -1890 }, this.timeout); -1891 }; -1892 jasmine.WaitsForBlock = function(env, timeout, latchFunction, message, spec) { -1893 this.timeout = timeout; -1894 this.latchFunction = latchFunction; -1895 this.message = message; -1896 this.totalTimeSpentWaitingForLatch = 0; -1897 jasmine.Block.call(this, env, null, spec); -1898 }; -1899 -1900 jasmine.util.inherit(jasmine.WaitsForBlock, jasmine.Block); +1884 jasmine.Suite.prototype.results = function() { +1885 return this.queue.results(); +1886 }; +1887 +1888 jasmine.Suite.prototype.add = function(block) { +1889 if (block instanceof jasmine.Suite) { +1890 this.env.currentRunner().addSuite(block); +1891 } else { +1892 this.specs_.push(block); +1893 } +1894 this.queue.add(block); +1895 }; +1896 +1897 /** @deprecated */ +1898 jasmine.Suite.prototype.specCount = function() { +1899 return this.specs_.length; +1900 }; 1901 -1902 jasmine.WaitsForBlock.TIMEOUT_INCREMENT = 100; -1903 -1904 jasmine.WaitsForBlock.prototype.execute = function (onComplete) { -1905 var self = this; -1906 self.env.reporter.log('>> Jasmine waiting for ' + (self.message || 'something to happen')); -1907 var latchFunctionResult; -1908 try { -1909 latchFunctionResult = self.latchFunction.apply(self.spec); -1910 } catch (e) { -1911 self.spec.fail(e); -1912 onComplete(); -1913 return; -1914 } -1915 -1916 if (latchFunctionResult) { -1917 onComplete(); -1918 } else if (self.totalTimeSpentWaitingForLatch >= self.timeout) { -1919 var message = 'timed out after ' + self.timeout + ' msec waiting for ' + (self.message || 'something to happen'); -1920 self.spec.fail({ -1921 name: 'timeout', -1922 message: message -1923 }); -1924 self.spec._next(); -1925 } else { -1926 self.totalTimeSpentWaitingForLatch += jasmine.WaitsForBlock.TIMEOUT_INCREMENT; -1927 self.env.setTimeout(function () { self.execute(onComplete); }, jasmine.WaitsForBlock.TIMEOUT_INCREMENT); -1928 } -1929 }; -1930 // Mock setTimeout, clearTimeout -1931 // Contributed by Pivotal Computer Systems, www.pivotalsf.com -1932 -1933 jasmine.FakeTimer = function() { -1934 this.reset(); -1935 -1936 var self = this; -1937 self.setTimeout = function(funcToCall, millis) { -1938 self.timeoutsMade++; -1939 self.scheduleFunction(self.timeoutsMade, funcToCall, millis, false); -1940 return self.timeoutsMade; -1941 }; -1942 -1943 self.setInterval = function(funcToCall, millis) { -1944 self.timeoutsMade++; -1945 self.scheduleFunction(self.timeoutsMade, funcToCall, millis, true); -1946 return self.timeoutsMade; -1947 }; -1948 -1949 self.clearTimeout = function(timeoutKey) { -1950 self.scheduledFunctions[timeoutKey] = undefined; -1951 }; -1952 -1953 self.clearInterval = function(timeoutKey) { -1954 self.scheduledFunctions[timeoutKey] = undefined; -1955 }; -1956 -1957 }; -1958 -1959 jasmine.FakeTimer.prototype.reset = function() { -1960 this.timeoutsMade = 0; -1961 this.scheduledFunctions = {}; -1962 this.nowMillis = 0; -1963 }; -1964 -1965 jasmine.FakeTimer.prototype.tick = function(millis) { -1966 var oldMillis = this.nowMillis; -1967 var newMillis = oldMillis + millis; -1968 this.runFunctionsWithinRange(oldMillis, newMillis); -1969 this.nowMillis = newMillis; -1970 }; -1971 -1972 jasmine.FakeTimer.prototype.runFunctionsWithinRange = function(oldMillis, nowMillis) { -1973 var scheduledFunc; -1974 var funcsToRun = []; -1975 for (var timeoutKey in this.scheduledFunctions) { -1976 scheduledFunc = this.scheduledFunctions[timeoutKey]; -1977 if (scheduledFunc != undefined && -1978 scheduledFunc.runAtMillis >= oldMillis && -1979 scheduledFunc.runAtMillis <= nowMillis) { -1980 funcsToRun.push(scheduledFunc); -1981 this.scheduledFunctions[timeoutKey] = undefined; -1982 } -1983 } -1984 -1985 if (funcsToRun.length > 0) { -1986 funcsToRun.sort(function(a, b) { -1987 return a.runAtMillis - b.runAtMillis; -1988 }); -1989 for (var i = 0; i < funcsToRun.length; ++i) { -1990 try { -1991 var funcToRun = funcsToRun[i]; -1992 this.nowMillis = funcToRun.runAtMillis; -1993 funcToRun.funcToCall(); -1994 if (funcToRun.recurring) { -1995 this.scheduleFunction(funcToRun.timeoutKey, -1996 funcToRun.funcToCall, -1997 funcToRun.millis, -1998 true); -1999 } -2000 } catch(e) { -2001 } -2002 } -2003 this.runFunctionsWithinRange(oldMillis, nowMillis); -2004 } -2005 }; -2006 -2007 jasmine.FakeTimer.prototype.scheduleFunction = function(timeoutKey, funcToCall, millis, recurring) { -2008 this.scheduledFunctions[timeoutKey] = { -2009 runAtMillis: this.nowMillis + millis, -2010 funcToCall: funcToCall, -2011 recurring: recurring, -2012 timeoutKey: timeoutKey, -2013 millis: millis -2014 }; -2015 }; -2016 +1902 jasmine.Suite.prototype.specs = function() { +1903 return this.specs_; +1904 }; +1905 +1906 jasmine.Suite.prototype.execute = function(onComplete) { +1907 var self = this; +1908 this.queue.start(function () { +1909 self.finish(onComplete); +1910 }); +1911 }; +1912 jasmine.WaitsBlock = function(env, timeout, spec) { +1913 this.timeout = timeout; +1914 jasmine.Block.call(this, env, null, spec); +1915 }; +1916 +1917 jasmine.util.inherit(jasmine.WaitsBlock, jasmine.Block); +1918 +1919 jasmine.WaitsBlock.prototype.execute = function (onComplete) { +1920 this.env.reporter.log('>> Jasmine waiting for ' + this.timeout + ' ms...'); +1921 this.env.setTimeout(function () { +1922 onComplete(); +1923 }, this.timeout); +1924 }; +1925 jasmine.WaitsForBlock = function(env, timeout, latchFunction, message, spec) { +1926 this.timeout = timeout; +1927 this.latchFunction = latchFunction; +1928 this.message = message; +1929 this.totalTimeSpentWaitingForLatch = 0; +1930 jasmine.Block.call(this, env, null, spec); +1931 }; +1932 +1933 jasmine.util.inherit(jasmine.WaitsForBlock, jasmine.Block); +1934 +1935 jasmine.WaitsForBlock.TIMEOUT_INCREMENT = 100; +1936 +1937 jasmine.WaitsForBlock.prototype.execute = function (onComplete) { +1938 var self = this; +1939 self.env.reporter.log('>> Jasmine waiting for ' + (self.message || 'something to happen')); +1940 var latchFunctionResult; +1941 try { +1942 latchFunctionResult = self.latchFunction.apply(self.spec); +1943 } catch (e) { +1944 self.spec.fail(e); +1945 onComplete(); +1946 return; +1947 } +1948 +1949 if (latchFunctionResult) { +1950 onComplete(); +1951 } else if (self.totalTimeSpentWaitingForLatch >= self.timeout) { +1952 var message = 'timed out after ' + self.timeout + ' msec waiting for ' + (self.message || 'something to happen'); +1953 self.spec.fail({ +1954 name: 'timeout', +1955 message: message +1956 }); +1957 self.spec._next(); +1958 } else { +1959 self.totalTimeSpentWaitingForLatch += jasmine.WaitsForBlock.TIMEOUT_INCREMENT; +1960 self.env.setTimeout(function () { self.execute(onComplete); }, jasmine.WaitsForBlock.TIMEOUT_INCREMENT); +1961 } +1962 }; +1963 // Mock setTimeout, clearTimeout +1964 // Contributed by Pivotal Computer Systems, www.pivotalsf.com +1965 +1966 jasmine.FakeTimer = function() { +1967 this.reset(); +1968 +1969 var self = this; +1970 self.setTimeout = function(funcToCall, millis) { +1971 self.timeoutsMade++; +1972 self.scheduleFunction(self.timeoutsMade, funcToCall, millis, false); +1973 return self.timeoutsMade; +1974 }; +1975 +1976 self.setInterval = function(funcToCall, millis) { +1977 self.timeoutsMade++; +1978 self.scheduleFunction(self.timeoutsMade, funcToCall, millis, true); +1979 return self.timeoutsMade; +1980 }; +1981 +1982 self.clearTimeout = function(timeoutKey) { +1983 self.scheduledFunctions[timeoutKey] = undefined; +1984 }; +1985 +1986 self.clearInterval = function(timeoutKey) { +1987 self.scheduledFunctions[timeoutKey] = undefined; +1988 }; +1989 +1990 }; +1991 +1992 jasmine.FakeTimer.prototype.reset = function() { +1993 this.timeoutsMade = 0; +1994 this.scheduledFunctions = {}; +1995 this.nowMillis = 0; +1996 }; +1997 +1998 jasmine.FakeTimer.prototype.tick = function(millis) { +1999 var oldMillis = this.nowMillis; +2000 var newMillis = oldMillis + millis; +2001 this.runFunctionsWithinRange(oldMillis, newMillis); +2002 this.nowMillis = newMillis; +2003 }; +2004 +2005 jasmine.FakeTimer.prototype.runFunctionsWithinRange = function(oldMillis, nowMillis) { +2006 var scheduledFunc; +2007 var funcsToRun = []; +2008 for (var timeoutKey in this.scheduledFunctions) { +2009 scheduledFunc = this.scheduledFunctions[timeoutKey]; +2010 if (scheduledFunc != undefined && +2011 scheduledFunc.runAtMillis >= oldMillis && +2012 scheduledFunc.runAtMillis <= nowMillis) { +2013 funcsToRun.push(scheduledFunc); +2014 this.scheduledFunctions[timeoutKey] = undefined; +2015 } +2016 } 2017 -2018 jasmine.Clock = { -2019 defaultFakeTimer: new jasmine.FakeTimer(), -2020 -2021 reset: function() { -2022 jasmine.Clock.assertInstalled(); -2023 jasmine.Clock.defaultFakeTimer.reset(); -2024 }, -2025 -2026 tick: function(millis) { -2027 jasmine.Clock.assertInstalled(); -2028 jasmine.Clock.defaultFakeTimer.tick(millis); -2029 }, -2030 -2031 runFunctionsWithinRange: function(oldMillis, nowMillis) { -2032 jasmine.Clock.defaultFakeTimer.runFunctionsWithinRange(oldMillis, nowMillis); -2033 }, -2034 -2035 scheduleFunction: function(timeoutKey, funcToCall, millis, recurring) { -2036 jasmine.Clock.defaultFakeTimer.scheduleFunction(timeoutKey, funcToCall, millis, recurring); -2037 }, -2038 -2039 useMock: function() { -2040 var spec = jasmine.getEnv().currentSpec; -2041 spec.after(jasmine.Clock.uninstallMock); -2042 -2043 jasmine.Clock.installMock(); -2044 }, -2045 -2046 installMock: function() { -2047 jasmine.Clock.installed = jasmine.Clock.defaultFakeTimer; -2048 }, +2018 if (funcsToRun.length > 0) { +2019 funcsToRun.sort(function(a, b) { +2020 return a.runAtMillis - b.runAtMillis; +2021 }); +2022 for (var i = 0; i < funcsToRun.length; ++i) { +2023 try { +2024 var funcToRun = funcsToRun[i]; +2025 this.nowMillis = funcToRun.runAtMillis; +2026 funcToRun.funcToCall(); +2027 if (funcToRun.recurring) { +2028 this.scheduleFunction(funcToRun.timeoutKey, +2029 funcToRun.funcToCall, +2030 funcToRun.millis, +2031 true); +2032 } +2033 } catch(e) { +2034 } +2035 } +2036 this.runFunctionsWithinRange(oldMillis, nowMillis); +2037 } +2038 }; +2039 +2040 jasmine.FakeTimer.prototype.scheduleFunction = function(timeoutKey, funcToCall, millis, recurring) { +2041 this.scheduledFunctions[timeoutKey] = { +2042 runAtMillis: this.nowMillis + millis, +2043 funcToCall: funcToCall, +2044 recurring: recurring, +2045 timeoutKey: timeoutKey, +2046 millis: millis +2047 }; +2048 }; 2049 -2050 uninstallMock: function() { -2051 jasmine.Clock.assertInstalled(); -2052 jasmine.Clock.installed = jasmine.Clock.real; -2053 }, -2054 -2055 real: { -2056 setTimeout: window.setTimeout, -2057 clearTimeout: window.clearTimeout, -2058 setInterval: window.setInterval, -2059 clearInterval: window.clearInterval -2060 }, -2061 -2062 assertInstalled: function() { -2063 if (jasmine.Clock.installed != jasmine.Clock.defaultFakeTimer) { -2064 throw new Error("Mock clock is not installed, use jasmine.Clock.useMock()"); -2065 } -2066 }, +2050 +2051 jasmine.Clock = { +2052 defaultFakeTimer: new jasmine.FakeTimer(), +2053 +2054 reset: function() { +2055 jasmine.Clock.assertInstalled(); +2056 jasmine.Clock.defaultFakeTimer.reset(); +2057 }, +2058 +2059 tick: function(millis) { +2060 jasmine.Clock.assertInstalled(); +2061 jasmine.Clock.defaultFakeTimer.tick(millis); +2062 }, +2063 +2064 runFunctionsWithinRange: function(oldMillis, nowMillis) { +2065 jasmine.Clock.defaultFakeTimer.runFunctionsWithinRange(oldMillis, nowMillis); +2066 }, 2067 -2068 installed: null -2069 }; -2070 jasmine.Clock.installed = jasmine.Clock.real; +2068 scheduleFunction: function(timeoutKey, funcToCall, millis, recurring) { +2069 jasmine.Clock.defaultFakeTimer.scheduleFunction(timeoutKey, funcToCall, millis, recurring); +2070 }, 2071 -2072 window.setTimeout = function(funcToCall, millis) { -2073 return jasmine.Clock.installed.setTimeout.apply(this, arguments); -2074 }; +2072 useMock: function() { +2073 var spec = jasmine.getEnv().currentSpec; +2074 spec.after(jasmine.Clock.uninstallMock); 2075 -2076 window.setInterval = function(funcToCall, millis) { -2077 return jasmine.Clock.installed.setInterval.apply(this, arguments); -2078 }; -2079 -2080 window.clearTimeout = function(timeoutKey) { -2081 return jasmine.Clock.installed.clearTimeout.apply(this, arguments); -2082 }; -2083 -2084 window.clearInterval = function(timeoutKey) { -2085 return jasmine.Clock.installed.clearInterval.apply(this, arguments); -2086 }; +2076 jasmine.Clock.installMock(); +2077 }, +2078 +2079 installMock: function() { +2080 jasmine.Clock.installed = jasmine.Clock.defaultFakeTimer; +2081 }, +2082 +2083 uninstallMock: function() { +2084 jasmine.Clock.assertInstalled(); +2085 jasmine.Clock.installed = jasmine.Clock.real; +2086 }, 2087 -2088 \ No newline at end of file +2088 real: { +2089 setTimeout: window.setTimeout, +2090 clearTimeout: window.clearTimeout, +2091 setInterval: window.setInterval, +2092 clearInterval: window.clearInterval +2093 }, +2094 +2095 assertInstalled: function() { +2096 if (jasmine.Clock.installed != jasmine.Clock.defaultFakeTimer) { +2097 throw new Error("Mock clock is not installed, use jasmine.Clock.useMock()"); +2098 } +2099 }, +2100 +2101 installed: null +2102 }; +2103 jasmine.Clock.installed = jasmine.Clock.real; +2104 +2105 window.setTimeout = function(funcToCall, millis) { +2106 return jasmine.Clock.installed.setTimeout.apply(this, arguments); +2107 }; +2108 +2109 window.setInterval = function(funcToCall, millis) { +2110 return jasmine.Clock.installed.setInterval.apply(this, arguments); +2111 }; +2112 +2113 window.clearTimeout = function(timeoutKey) { +2114 return jasmine.Clock.installed.clearTimeout.apply(this, arguments); +2115 }; +2116 +2117 window.clearInterval = function(timeoutKey) { +2118 return jasmine.Clock.installed.clearInterval.apply(this, arguments); +2119 }; +2120 +2121 \ No newline at end of file diff --git a/lib/jasmine-0.9.0.js b/lib/jasmine-0.9.0.js index bae34045..8dcfb8c7 100644 --- a/lib/jasmine-0.9.0.js +++ b/lib/jasmine-0.9.0.js @@ -517,7 +517,7 @@ jasmine.version_= { "major": 0, "minor": 9, "build": 0, - "revision": 1254161532 + "revision": 1254180093 }; /** * @namespace @@ -586,7 +586,7 @@ jasmine.util.argsToArray = function(args) { jasmine.Env = function() { this.currentSpec = null; this.currentSuite = null; - this.currentRunner = new jasmine.Runner(this); + this.currentRunner_ = new jasmine.Runner(this); this.currentlyRunningTests = false; this.reporter = new jasmine.MultiReporter(); @@ -644,7 +644,7 @@ jasmine.Env.prototype.addReporter = function(reporter) { }; jasmine.Env.prototype.execute = function() { - this.currentRunner.execute(); + this.currentRunner_.execute(); }; jasmine.Env.prototype.describe = function(description, specDefinitions) { @@ -654,7 +654,7 @@ jasmine.Env.prototype.describe = function(description, specDefinitions) { if (parentSuite) { parentSuite.add(suite); } else { - this.currentRunner.add(suite); + this.currentRunner_.add(suite); } this.currentSuite = suite; @@ -667,11 +667,24 @@ jasmine.Env.prototype.describe = function(description, specDefinitions) { }; jasmine.Env.prototype.beforeEach = function(beforeEachFunction) { - this.currentSuite.beforeEach(beforeEachFunction); + if (this.currentSuite) { + this.currentSuite.beforeEach(beforeEachFunction); + } else { + this.currentRunner_.beforeEach(beforeEachFunction); + } +}; + +jasmine.Env.prototype.currentRunner = function () { + return this.currentRunner_; }; jasmine.Env.prototype.afterEach = function(afterEachFunction) { - this.currentSuite.afterEach(afterEachFunction); + if (this.currentSuite) { + this.currentSuite.afterEach(afterEachFunction); + } else { + this.currentRunner_.afterEach(afterEachFunction); + } + }; jasmine.Env.prototype.xdescribe = function(desc, specDefinitions) { @@ -1548,6 +1561,8 @@ jasmine.Runner = function(env) { var self = this; self.env = env; self.queue = new jasmine.Queue(env); + self.before_ = []; + self.after_ = []; self.suites_ = []; }; @@ -1561,6 +1576,17 @@ jasmine.Runner.prototype.execute = function() { }); }; +jasmine.Runner.prototype.beforeEach = function(beforeEachFunction) { + beforeEachFunction.typeName = 'beforeEach'; + this.before_.push(beforeEachFunction); +}; + +jasmine.Runner.prototype.afterEach = function(afterEachFunction) { + afterEachFunction.typeName = 'afterEach'; + this.after_.push(afterEachFunction); +}; + + jasmine.Runner.prototype.finishCallback = function() { this.env.reporter.reportRunnerResults(this); }; @@ -1605,10 +1631,12 @@ jasmine.Runner.prototype.getResults = function() { jasmine.Spec = function(env, suite, description) { if (!env) { throw new Error('jasmine.Env() required'); - }; + } + ; if (!suite) { throw new Error('jasmine.Suite() required'); - }; + } + ; var spec = this; spec.id = env.nextSpecId ? env.nextSpecId() : null; spec.env = env; @@ -1714,10 +1742,10 @@ jasmine.Spec.prototype.finish = function(onComplete) { jasmine.Spec.prototype.after = function(doAfter, test) { - if (this.queue.isRunning()) { + if (this.queue.isRunning()) { this.queue.add(new jasmine.Block(this.env, doAfter, this)); } else { - this.afterCallbacks.unshift(doAfter); + this.afterCallbacks.unshift(doAfter); } }; @@ -1742,21 +1770,26 @@ jasmine.Spec.prototype.execute = function(onComplete) { }; jasmine.Spec.prototype.addBeforesAndAftersToQueue = function() { + var runner = this.env.currentRunner(); for (var suite = this.suite; suite; suite = suite.parentSuite) { - if (suite.beforeQueue) { - for (var i = 0; i < suite.beforeQueue.length; i++) - this.queue.addBefore(new jasmine.Block(this.env, suite.beforeQueue[i], this)); + for (var i = 0; i < suite.before_.length; i++) { + this.queue.addBefore(new jasmine.Block(this.env, suite.before_[i], this)); } } + for (var i = 0; i < runner.before_.length; i++) { + this.queue.addBefore(new jasmine.Block(this.env, runner.before_[i], this)); + } for (i = 0; i < this.afterCallbacks.length; i++) { this.queue.add(new jasmine.Block(this.env, this.afterCallbacks[i], this)); } for (suite = this.suite; suite; suite = suite.parentSuite) { - if (suite.afterQueue) { - for (var j = 0; j < suite.afterQueue.length; j++) - this.queue.add(new jasmine.Block(this.env, suite.afterQueue[j], this)); + for (var i = 0; i < suite.after_.length; i++) { + this.queue.add(new jasmine.Block(this.env, suite.after_[i], this)); } } + for (var i = 0; i < runner.after_.length; i++) { + this.queue.add(new jasmine.Block(this.env, runner.after_[i], this)); + } }; jasmine.Spec.prototype.explodes = function() { @@ -1812,8 +1845,8 @@ jasmine.Suite = function(env, description, specDefinitions, parentSuite) { self.queue = new jasmine.Queue(env); self.parentSuite = parentSuite; self.env = env; - self.beforeQueue = []; - self.afterQueue = []; + self.before_ = []; + self.after_ = []; self.specs_ = []; }; @@ -1835,12 +1868,12 @@ jasmine.Suite.prototype.finish = function(onComplete) { jasmine.Suite.prototype.beforeEach = function(beforeEachFunction) { beforeEachFunction.typeName = 'beforeEach'; - this.beforeQueue.push(beforeEachFunction); + this.before_.push(beforeEachFunction); }; jasmine.Suite.prototype.afterEach = function(afterEachFunction) { afterEachFunction.typeName = 'afterEach'; - this.afterQueue.push(afterEachFunction); + this.after_.push(afterEachFunction); }; /** @deprecated */ @@ -1854,7 +1887,7 @@ jasmine.Suite.prototype.results = function() { jasmine.Suite.prototype.add = function(block) { if (block instanceof jasmine.Suite) { - this.env.currentRunner.addSuite(block); + this.env.currentRunner().addSuite(block); } else { this.specs_.push(block); } diff --git a/spec/suites/ExceptionsSpec.js b/spec/suites/ExceptionsSpec.js index f53814d1..04f4917d 100644 --- a/spec/suites/ExceptionsSpec.js +++ b/spec/suites/ExceptionsSpec.js @@ -72,7 +72,7 @@ describe('Exceptions:', function() { }); }); - var runner = env.currentRunner; + var runner = env.currentRunner(); suite.execute(); fakeTimer.tick(2500); diff --git a/spec/suites/ReporterSpec.js b/spec/suites/ReporterSpec.js index ad4e4984..95ba01dd 100644 --- a/spec/suites/ReporterSpec.js +++ b/spec/suites/ReporterSpec.js @@ -49,7 +49,7 @@ describe('jasmine.Reporter', function() { runnerCallback: runnerCallback }); - var runner = env.currentRunner; + var runner = env.currentRunner(); runner.execute(); expect(foo).toEqual(3); // 'foo was expected to be 3, was ' + foo); diff --git a/spec/suites/RunnerSpec.js b/spec/suites/RunnerSpec.js index e193d071..b2e48437 100644 --- a/spec/suites/RunnerSpec.js +++ b/spec/suites/RunnerSpec.js @@ -12,6 +12,70 @@ describe('RunnerTest', function() { env.clearInterval = fakeTimer.clearInterval; }); + describe('beforeEach', function() { + it('should run before each spec for all suites', function () { + var foo; + env.beforeEach(function () { + foo = 0; + }); + + env.describe('suite 1', function () { + env.it('test 1-1', function() { + foo++; + this.expect(foo).toEqual(1); + }); + env.it('test 1-2', function() { + foo++; + this.expect(foo).toEqual(1); + }); + }); + + env.describe('suite 2', function () { + env.it('test 2-1', function() { + foo++; + this.expect(foo).toEqual(1); + }); + }); + + env.currentRunner().execute(); + + var runnerResults = env.currentRunner().results(); + expect(runnerResults.totalCount).toEqual(3); + expect(runnerResults.passedCount).toEqual(3); + }); + }); + + describe('afterEach', function() { + it('should run after each spec for all suites', function () { + var foo = 3; + env.afterEach(function () { + foo = foo - 1; + }); + + env.describe('suite 1', function () { + env.it('test 1-1', function() { + this.expect(foo).toEqual(3); + }); + env.it('test 1-2', function() { + this.expect(foo).toEqual(2); + }); + }); + + env.describe('suite 2', function () { + env.it('test 2-1', function() { + this.expect(foo).toEqual(1); + }); + }); + + env.currentRunner().execute(); + + var runnerResults = env.currentRunner().results(); + expect(runnerResults.totalCount).toEqual(3); + expect(runnerResults.passedCount).toEqual(3); + }); + }); + + it('should run child suites and specs and generate results when execute is called', function() { env.describe('one suite description', function () { env.it('should be a test', function() { @@ -29,9 +93,9 @@ describe('RunnerTest', function() { }); }); - env.currentRunner.execute(); + env.currentRunner().execute(); - var runnerResults = env.currentRunner.results(); + var runnerResults = env.currentRunner().results(); expect(runnerResults.totalCount).toEqual(2); expect(runnerResults.passedCount).toEqual(1); expect(runnerResults.failedCount).toEqual(1); @@ -55,9 +119,9 @@ describe('RunnerTest', function() { }); }); - env.currentRunner.execute(); + env.currentRunner().execute(); - var runnerResults = env.currentRunner.results(); + var runnerResults = env.currentRunner().results(); expect(runnerResults.totalCount).toEqual(1); expect(runnerResults.passedCount).toEqual(0); expect(runnerResults.failedCount).toEqual(1); @@ -80,9 +144,9 @@ describe('RunnerTest', function() { }); }); - env.currentRunner.execute(); - - var results = env.currentRunner.results(); + env.currentRunner().execute(); + + var results = env.currentRunner().results(); expect(results.totalCount).toEqual(2); expect(results.passedCount).toEqual(1); expect(results.failedCount).toEqual(1); @@ -113,16 +177,16 @@ describe('RunnerTest', function() { }); }); - env.currentRunner.execute(); + env.currentRunner().execute(); expect(fakeReporter.reportRunnerResults).wasNotCalled(); fakeTimer.tick(200); //This blows up the JSApiReporter. //expect(fakeReporter.reportRunnerResults).wasCalledWith(env.currentRunner); expect(fakeReporter.reportRunnerResults).wasCalled(); - expect(fakeReporter.reportRunnerResults.mostRecentCall.args[0].results()).toEqual(env.currentRunner.results()); + expect(fakeReporter.reportRunnerResults.mostRecentCall.args[0].results()).toEqual(env.currentRunner().results()); }); - + }); it("should report when the tests start running", function() { @@ -139,18 +203,19 @@ describe('RunnerTest', function() { var reportedRunner = fakeReporter.reportRunnerStarting.mostRecentCall.args[0]; expect(reportedRunner.arbitraryVariable).toEqual('foo'); expect(runner.queue.start).wasCalled(); - + }); it("should return a flat array of all suites, including nested suites", function() { var suite1, suite2; suite1 = env.describe("spec 1", function() { - suite2 = env.describe("nested spec", function() {}); + suite2 = env.describe("nested spec", function() { + }); }); - document.runner = env.currentRunner; + document.runner = env.currentRunner(); - var suites = env.currentRunner.suites(); + var suites = env.currentRunner().suites(); var suiteDescriptions = []; for (var i = 0; i < suites.length; i++) { suiteDescriptions.push(suites[i].getFullName()); diff --git a/spec/suites/SpecRunningSpec.js b/spec/suites/SpecRunningSpec.js index e360e07e..e35ad328 100644 --- a/spec/suites/SpecRunningSpec.js +++ b/spec/suites/SpecRunningSpec.js @@ -864,6 +864,14 @@ describe("jasmine spec running", function () { it("testNestedDescribes", function() { var actions = []; + env.beforeEach(function () { + actions.push('runner beforeEach'); + }); + + env.afterEach(function () { + actions.push('runner afterEach'); + }); + env.describe('Something', function() { env.beforeEach(function() { actions.push('outer beforeEach'); @@ -914,27 +922,35 @@ describe("jasmine spec running", function () { var expected = [ + "runner beforeEach", "outer beforeEach", "outer it 1", "outer afterEach", + "runner afterEach", + "runner beforeEach", "outer beforeEach", "inner 1 beforeEach", "inner 1 it", "inner 1 afterEach", "outer afterEach", + "runner afterEach", + "runner beforeEach", "outer beforeEach", "outer it 2", "outer afterEach", + "runner afterEach", + "runner beforeEach", "outer beforeEach", "inner 2 beforeEach", "inner 2 it", "inner 2 afterEach", - "outer afterEach" + "outer afterEach", + "runner afterEach" ]; - expect(env.equals_(actions, expected)).toEqual(true); + expect(actions).toEqual(expected); }); it("builds up nested names", function() { @@ -963,7 +979,8 @@ describe("jasmine spec running", function () { }); }); - env.describe('EmptySuite', function() {}); + env.describe('EmptySuite', function() { + }); env.describe('NonEmptySuite2', function() { env.it('should pass', function() { @@ -973,7 +990,7 @@ describe("jasmine spec running", function () { env.execute(); - var runnerResults = env.currentRunner.results(); + var runnerResults = env.currentRunner_.results(); expect(runnerResults.totalCount).toEqual(3); expect(runnerResults.passedCount).toEqual(3); expect(runnerResults.failedCount).toEqual(0); diff --git a/src/Env.js b/src/Env.js index d5185203..60842eca 100644 --- a/src/Env.js +++ b/src/Env.js @@ -6,7 +6,7 @@ jasmine.Env = function() { this.currentSpec = null; this.currentSuite = null; - this.currentRunner = new jasmine.Runner(this); + this.currentRunner_ = new jasmine.Runner(this); this.currentlyRunningTests = false; this.reporter = new jasmine.MultiReporter(); @@ -64,7 +64,7 @@ jasmine.Env.prototype.addReporter = function(reporter) { }; jasmine.Env.prototype.execute = function() { - this.currentRunner.execute(); + this.currentRunner_.execute(); }; jasmine.Env.prototype.describe = function(description, specDefinitions) { @@ -74,7 +74,7 @@ jasmine.Env.prototype.describe = function(description, specDefinitions) { if (parentSuite) { parentSuite.add(suite); } else { - this.currentRunner.add(suite); + this.currentRunner_.add(suite); } this.currentSuite = suite; @@ -87,11 +87,24 @@ jasmine.Env.prototype.describe = function(description, specDefinitions) { }; jasmine.Env.prototype.beforeEach = function(beforeEachFunction) { - this.currentSuite.beforeEach(beforeEachFunction); + if (this.currentSuite) { + this.currentSuite.beforeEach(beforeEachFunction); + } else { + this.currentRunner_.beforeEach(beforeEachFunction); + } +}; + +jasmine.Env.prototype.currentRunner = function () { + return this.currentRunner_; }; jasmine.Env.prototype.afterEach = function(afterEachFunction) { - this.currentSuite.afterEach(afterEachFunction); + if (this.currentSuite) { + this.currentSuite.afterEach(afterEachFunction); + } else { + this.currentRunner_.afterEach(afterEachFunction); + } + }; jasmine.Env.prototype.xdescribe = function(desc, specDefinitions) { diff --git a/src/Runner.js b/src/Runner.js index 5c53599f..727c8bfa 100644 --- a/src/Runner.js +++ b/src/Runner.js @@ -8,6 +8,8 @@ jasmine.Runner = function(env) { var self = this; self.env = env; self.queue = new jasmine.Queue(env); + self.before_ = []; + self.after_ = []; self.suites_ = []; }; @@ -21,6 +23,17 @@ jasmine.Runner.prototype.execute = function() { }); }; +jasmine.Runner.prototype.beforeEach = function(beforeEachFunction) { + beforeEachFunction.typeName = 'beforeEach'; + this.before_.push(beforeEachFunction); +}; + +jasmine.Runner.prototype.afterEach = function(afterEachFunction) { + afterEachFunction.typeName = 'afterEach'; + this.after_.push(afterEachFunction); +}; + + jasmine.Runner.prototype.finishCallback = function() { this.env.reporter.reportRunnerResults(this); }; diff --git a/src/Spec.js b/src/Spec.js index 964f9cb9..5ae6b602 100644 --- a/src/Spec.js +++ b/src/Spec.js @@ -9,10 +9,12 @@ jasmine.Spec = function(env, suite, description) { if (!env) { throw new Error('jasmine.Env() required'); - }; + } + ; if (!suite) { throw new Error('jasmine.Suite() required'); - }; + } + ; var spec = this; spec.id = env.nextSpecId ? env.nextSpecId() : null; spec.env = env; @@ -118,10 +120,10 @@ jasmine.Spec.prototype.finish = function(onComplete) { jasmine.Spec.prototype.after = function(doAfter, test) { - if (this.queue.isRunning()) { + if (this.queue.isRunning()) { this.queue.add(new jasmine.Block(this.env, doAfter, this)); } else { - this.afterCallbacks.unshift(doAfter); + this.afterCallbacks.unshift(doAfter); } }; @@ -146,21 +148,26 @@ jasmine.Spec.prototype.execute = function(onComplete) { }; jasmine.Spec.prototype.addBeforesAndAftersToQueue = function() { + var runner = this.env.currentRunner(); for (var suite = this.suite; suite; suite = suite.parentSuite) { - if (suite.beforeQueue) { - for (var i = 0; i < suite.beforeQueue.length; i++) - this.queue.addBefore(new jasmine.Block(this.env, suite.beforeQueue[i], this)); + for (var i = 0; i < suite.before_.length; i++) { + this.queue.addBefore(new jasmine.Block(this.env, suite.before_[i], this)); } } + for (var i = 0; i < runner.before_.length; i++) { + this.queue.addBefore(new jasmine.Block(this.env, runner.before_[i], this)); + } for (i = 0; i < this.afterCallbacks.length; i++) { this.queue.add(new jasmine.Block(this.env, this.afterCallbacks[i], this)); } for (suite = this.suite; suite; suite = suite.parentSuite) { - if (suite.afterQueue) { - for (var j = 0; j < suite.afterQueue.length; j++) - this.queue.add(new jasmine.Block(this.env, suite.afterQueue[j], this)); + for (var i = 0; i < suite.after_.length; i++) { + this.queue.add(new jasmine.Block(this.env, suite.after_[i], this)); } } + for (var i = 0; i < runner.after_.length; i++) { + this.queue.add(new jasmine.Block(this.env, runner.after_[i], this)); + } }; jasmine.Spec.prototype.explodes = function() { diff --git a/src/Suite.js b/src/Suite.js index fadcc1ad..e1e5ea18 100644 --- a/src/Suite.js +++ b/src/Suite.js @@ -14,8 +14,8 @@ jasmine.Suite = function(env, description, specDefinitions, parentSuite) { self.queue = new jasmine.Queue(env); self.parentSuite = parentSuite; self.env = env; - self.beforeQueue = []; - self.afterQueue = []; + self.before_ = []; + self.after_ = []; self.specs_ = []; }; @@ -37,12 +37,12 @@ jasmine.Suite.prototype.finish = function(onComplete) { jasmine.Suite.prototype.beforeEach = function(beforeEachFunction) { beforeEachFunction.typeName = 'beforeEach'; - this.beforeQueue.push(beforeEachFunction); + this.before_.push(beforeEachFunction); }; jasmine.Suite.prototype.afterEach = function(afterEachFunction) { afterEachFunction.typeName = 'afterEach'; - this.afterQueue.push(afterEachFunction); + this.after_.push(afterEachFunction); }; /** @deprecated */ @@ -56,7 +56,7 @@ jasmine.Suite.prototype.results = function() { jasmine.Suite.prototype.add = function(block) { if (block instanceof jasmine.Suite) { - this.env.currentRunner.addSuite(block); + this.env.currentRunner().addSuite(block); } else { this.specs_.push(block); }