From b67d2a265d8c2f2b4fd302b03c1d809cab072f71 Mon Sep 17 00:00:00 2001 From: Ryan Dy & Rajan Agaskar Date: Thu, 29 Oct 2009 17:03:24 -0700 Subject: [PATCH] major matcher refactor --- 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.Matchers.html | 611 +-- 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 | 34 +- doc/symbols/jasmine.util.html | 2 +- doc/symbols/src/lib_jasmine-0.10.0.js.html | 4247 ++++++++++---------- lib/jasmine-0.10.0.js | 435 +- lib/jasmine.css | 12 +- spec/suites/ExceptionsSpec.js | 6 +- spec/suites/MatchersSpec.js | 508 ++- spec/suites/NestedResultsSpec.js | 27 +- spec/suites/SpecRunningSpec.js | 1 - spec/suites/TrivialReporterSpec.js | 50 +- spec/suites/UtilSpec.js | 23 + src/Env.js | 6 +- src/Matchers.js | 394 +- src/Spec.js | 12 +- src/base.js | 18 +- src/util.js | 5 + 30 files changed, 3319 insertions(+), 3098 deletions(-) create mode 100644 spec/suites/UtilSpec.js diff --git a/doc/files.html b/doc/files.html index c214ba9..510186a 100644 --- a/doc/files.html +++ b/doc/files.html @@ -260,7 +260,7 @@ ul.inheritsList
- Documentation generated by JsDoc Toolkit 2.1.0 on Tue Oct 27 2009 21:17:03 GMT-0800 (PST) + Documentation generated by JsDoc Toolkit 2.1.0 on Thu Oct 29 2009 16:58:08 GMT-0700 (PDT)
\ No newline at end of file diff --git a/doc/index.html b/doc/index.html index ef9ba6e..b1e631d 100644 --- a/doc/index.html +++ b/doc/index.html @@ -308,7 +308,7 @@ ul.inheritsList
- Documentation generated by JsDoc Toolkit 2.1.0 on Tue Oct 27 2009 21:17:03 GMT-0800 (PST) + Documentation generated by JsDoc Toolkit 2.1.0 on Thu Oct 29 2009 16:58:08 GMT-0700 (PDT)
\ No newline at end of file diff --git a/doc/symbols/_global_.html b/doc/symbols/_global_.html index ced27c2..01ae363 100644 --- a/doc/symbols/_global_.html +++ b/doc/symbols/_global_.html @@ -910,7 +910,7 @@ A convenience method that allows existing specs to be disabled temporarily durin
- Documentation generated by JsDoc Toolkit 2.1.0 on Tue Oct 27 2009 21:17:02 GMT-0800 (PST) + Documentation generated by JsDoc Toolkit 2.1.0 on Thu Oct 29 2009 16:58:08 GMT-0700 (PDT)
diff --git a/doc/symbols/jasmine.Block.html b/doc/symbols/jasmine.Block.html index dc46048..76656a8 100644 --- a/doc/symbols/jasmine.Block.html +++ b/doc/symbols/jasmine.Block.html @@ -341,7 +341,7 @@ ul.inheritsList
- Documentation generated by JsDoc Toolkit 2.1.0 on Tue Oct 27 2009 21:17:02 GMT-0800 (PST) + Documentation generated by JsDoc Toolkit 2.1.0 on Thu Oct 29 2009 16:58:08 GMT-0700 (PDT)
diff --git a/doc/symbols/jasmine.Env.html b/doc/symbols/jasmine.Env.html index 80009df..6ec9913 100644 --- a/doc/symbols/jasmine.Env.html +++ b/doc/symbols/jasmine.Env.html @@ -509,7 +509,7 @@ ul.inheritsList
- Documentation generated by JsDoc Toolkit 2.1.0 on Tue Oct 27 2009 21:17:03 GMT-0800 (PST) + Documentation generated by JsDoc Toolkit 2.1.0 on Thu Oct 29 2009 16:58:08 GMT-0700 (PDT)
diff --git a/doc/symbols/jasmine.JsApiReporter.html b/doc/symbols/jasmine.JsApiReporter.html index 20c5cda..321b133 100644 --- a/doc/symbols/jasmine.JsApiReporter.html +++ b/doc/symbols/jasmine.JsApiReporter.html @@ -318,7 +318,7 @@ ul.inheritsList
- Documentation generated by JsDoc Toolkit 2.1.0 on Tue Oct 27 2009 21:17:03 GMT-0800 (PST) + Documentation generated by JsDoc Toolkit 2.1.0 on Thu Oct 29 2009 16:58:08 GMT-0700 (PDT)
diff --git a/doc/symbols/jasmine.Matchers.html b/doc/symbols/jasmine.Matchers.html index a38cc07..ee476e2 100644 --- a/doc/symbols/jasmine.Matchers.html +++ b/doc/symbols/jasmine.Matchers.html @@ -282,149 +282,14 @@ ul.inheritsList -   + <static>   -
toBe(expected) +
jasmine.Matchers.addMatcher(expected, options)
Matcher that compares the actual to the expected using ===.
- -   - - -
Matcher that compares the acutal to undefined.
- - - - -   - - -
Matcher that boolean nots the actual.
- - - - -   - - -
Matcher that compares the actual to null.
- - - - -   - - -
Matcher that boolean not-nots the actual.
- - - - -   - -
toContain(item) -
-
Matcher that checks that the expected item is an element in the actual Array.
- - - - -   - -
toEqual(expected) -
-
Matcher that compares the actual to the expected using common sense equality.
- - - - -   - -
toMatch(reg_exp) -
-
Matcher that compares the actual to the expected using a regular expression.
- - - - -   - -
toNotBe(expected) -
-
Matcher that compares the actual to the expected using !==
- - - - -   - -
toNotContain(item) -
-
Matcher that checks that the expected item is NOT an element in the actual Array.
- - - - -   - -
toNotEqual(expected) -
-
Matcher that compares the actual to the expected using the ! of jasmine.Matchers.toEqual
- - - - -   - -
toNotMatch(reg_exp) -
-
Matcher that compares the actual to the expected using the boolean inverse of jasmine.Matchers.toMatch
- - - - -   - -
toThrow(expectedException) -
-
Matcher that checks that the expected exception was thrown by the actual.
- - - - -   - - -
Matcher that checks to see if the acutal, a Jasmine spy, was called.
- - - - -   - - -
Matcher that checks to see if the acutal, a Jasmine spy, was called with a set of parameters.
- - - - -   - - -
Matcher that checks to see if the acutal, a Jasmine spy, was not called.
- - - @@ -496,11 +361,11 @@ ul.inheritsList Method Detail
- -
+ +
<static> - toBe(expected) + jasmine.Matchers.addMatcher(expected, options)
@@ -521,138 +386,8 @@ ul.inheritsList
- - - - - - - - - -
- - -
- - - toBeDefined() - -
-
- Matcher that compares the acutal to undefined. - - -
- - - - - - - - - - - -
- - -
- - - toBeFalsy() - -
-
- Matcher that boolean nots the actual. - - -
- - - - - - - - - - - -
- - -
- - - toBeNull() - -
-
- Matcher that compares the actual to null. - - -
- - - - - - - - - - - -
- - -
- - - toBeTruthy() - -
-
- Matcher that boolean not-nots the actual. - - -
- - - - - - - - - - - -
- - -
- - - toContain(item) - -
-
- Matcher that checks that the expected item is an element in the actual Array. - - -
- - - - -
-
Parameters:
-
- {Object} item + options
@@ -666,338 +401,6 @@ ul.inheritsList -
- - -
- - - toEqual(expected) - -
-
- Matcher that compares the actual to the expected using common sense equality. Handles Objects, Arrays, etc. - - -
- - - - -
-
Parameters:
- -
- expected - -
-
- -
- - - - - - - - -
- - -
- - - toMatch(reg_exp) - -
-
- Matcher that compares the actual to the expected using a regular expression. Constructs a RegExp, so takes -a pattern or a String. - - -
- - - - -
-
Parameters:
- -
- reg_exp - -
-
- -
- - - - - - - - -
- - -
- - - toNotBe(expected) - -
-
- Matcher that compares the actual to the expected using !== - - -
- - - - -
-
Parameters:
- -
- expected - -
-
- -
- - - - - - - - -
- - -
- - - toNotContain(item) - -
-
- Matcher that checks that the expected item is NOT an element in the actual Array. - - -
- - - - -
-
Parameters:
- -
- {Object} item - -
-
- -
- - - - - - - - -
- - -
- - - toNotEqual(expected) - -
-
- Matcher that compares the actual to the expected using the ! of jasmine.Matchers.toEqual - - -
- - - - -
-
Parameters:
- -
- expected - -
-
- -
- - - - - - - - -
- - -
- - - toNotMatch(reg_exp) - -
-
- Matcher that compares the actual to the expected using the boolean inverse of jasmine.Matchers.toMatch - - -
- - - - -
-
Parameters:
- -
- reg_exp - -
-
- -
- - - - - - - - -
- - -
- - - toThrow(expectedException) - -
-
- Matcher that checks that the expected exception was thrown by the actual. - - -
- - - - -
-
Parameters:
- -
- {String} expectedException - -
-
- -
- - - - - - - - -
- - -
- - - wasCalled() - -
-
- Matcher that checks to see if the acutal, a Jasmine spy, was called. - - -
- - - - - - - - - - - -
- - -
- - - wasCalledWith() - -
-
- Matcher that checks to see if the acutal, a Jasmine spy, was called with a set of parameters. - - -
- - - -

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

'" + (b[property] ? jasmine.util.htmlEscape(b[property].toString()) : b[property]) + "'

in expected, but was

'" + (a[property] ? jasmine.util.htmlEscape(a[property].toString()) : a[property]) + "'

in actual.
"); + mismatchValues.push("'" + property + "' was '" + (b[property] ? jasmine.util.htmlEscape(b[property].toString()) : b[property]) + "' in expected, but was '" + (a[property] ? jasmine.util.htmlEscape(a[property].toString()) : a[property]) + "' in actual."); } } @@ -972,23 +983,23 @@ jasmine.JsApiReporter.prototype.summarizeResult_ = function(result){ * @param actual * @param {jasmine.NestedResults} results */ -jasmine.Matchers = function(env, actual, results) { +jasmine.Matchers = function(env, actual, spec) { this.env = env; this.actual = actual; - this.passing_message = 'Passed.'; - this.results_ = results || new jasmine.NestedResults(); + this.spec = spec; }; jasmine.Matchers.pp = function(str) { return jasmine.util.htmlEscape(jasmine.pp(str)); }; -jasmine.Matchers.prototype.results = function() { - return this.results_; -}; - jasmine.Matchers.prototype.report = function(result, failing_message, details) { - this.results_.addResult(new jasmine.ExpectationResult(result, result ? this.passing_message : failing_message, details)); + var expectationResult = new jasmine.ExpectationResult({ + passed: result, + message: failing_message, + details: details + }); + this.spec.addMatcherResult(expectationResult); return result; }; @@ -997,57 +1008,85 @@ jasmine.Matchers.prototype.report = function(result, failing_message, details) { * * @param expected */ -jasmine.Matchers.prototype.toBe = function(expected) { - return this.report(this.actual === expected, 'Expected

' + jasmine.Matchers.pp(expected) - + '

to be the same object as

' + jasmine.Matchers.pp(this.actual) - + '
'); + + +jasmine.Matchers.addMatcher = function(matcherName, options) { + jasmine.Matchers.prototype[matcherName] = function () { + jasmine.util.extend(this, options); + var expected = jasmine.util.argsToArray(arguments); + var args = [this.actual].concat(expected); + var result = options.test.apply(this, args); + var message; + if (!result) { + message = options.message.apply(this, args); + } + var expectationResult = new jasmine.ExpectationResult({ + matcherName: matcherName, + passed: result, + expected: expected, + actual: this.actual, + message: message + }); + this.spec.addMatcherResult(expectationResult); + return result; + }; }; + /** - * Matcher that compares the actual to the expected using !== + * toBe: compares the actual to the expected using === * @param expected */ -jasmine.Matchers.prototype.toNotBe = function(expected) { - return this.report(this.actual !== expected, 'Expected

' + jasmine.Matchers.pp(expected) - + '

to be a different object from actual, but they were the same.'); -}; + +jasmine.Matchers.addMatcher('toBe', { + test: function (actual, expected) { + return actual === expected; + }, + message: function(actual, expected) { + return "Expected " + jasmine.pp(actual) + " to be " + jasmine.pp(expected); + } +}); /** - * Matcher that compares the actual to the expected using common sense equality. Handles Objects, Arrays, etc. + * toNotBe: compares the actual to the expected using !== + * @param expected + */ +jasmine.Matchers.addMatcher('toNotBe', { + test: function (actual, expected) { + return actual !== expected; + }, + message: function(actual, expected) { + return "Expected " + jasmine.pp(actual) + " to not be " + jasmine.pp(expected); + } +}); + +/** + * toEqual: compares the actual to the expected using common sense equality. Handles Objects, Arrays, etc. * * @param expected */ -jasmine.Matchers.prototype.toEqual = function(expected) { - var mismatchKeys = []; - var mismatchValues = []; - var formatMismatches = function(name, array) { - if (array.length == 0) return ''; - var errorOutput = '

Different ' + name + ':
'; - for (var i = 0; i < array.length; i++) { - errorOutput += array[i] + '
'; - } - return errorOutput; - }; - - return this.report(this.env.equals_(this.actual, expected, mismatchKeys, mismatchValues), - 'Expected

' + jasmine.Matchers.pp(expected) - + '

but got

' + jasmine.Matchers.pp(this.actual) - + '
' - + formatMismatches('Keys', mismatchKeys) - + formatMismatches('Values', mismatchValues), { - matcherName: 'toEqual', expected: expected, actual: this.actual - }); -}; +jasmine.Matchers.addMatcher('toEqual', { + test: function (actual, expected) { + return this.env.equals_(actual, expected); + }, + message: function(actual, expected) { + return "Expected " + jasmine.pp(actual) + " to equal " + jasmine.pp(expected); + } +}); /** - * Matcher that compares the actual to the expected using the ! of jasmine.Matchers.toEqual + * toNotEqual: compares the actual to the expected using the ! of jasmine.Matchers.toEqual * @param expected */ -jasmine.Matchers.prototype.toNotEqual = function(expected) { - return this.report(!this.env.equals_(this.actual, expected), - 'Expected ' + jasmine.Matchers.pp(expected) + ' to not equal ' + jasmine.Matchers.pp(this.actual) + ', but it does.'); -}; +jasmine.Matchers.addMatcher('toNotEqual', { + test: function (actual, expected) { + return !this.env.equals_(actual, expected); + }, + message: function(actual, expected) { + return "Expected " + jasmine.pp(actual) + " to not equal " + jasmine.pp(expected); + } +}); /** * Matcher that compares the actual to the expected using a regular expression. Constructs a RegExp, so takes @@ -1055,77 +1094,166 @@ jasmine.Matchers.prototype.toNotEqual = function(expected) { * * @param reg_exp */ -jasmine.Matchers.prototype.toMatch = function(reg_exp) { - return this.report((new RegExp(reg_exp).test(this.actual)), - 'Expected ' + jasmine.Matchers.pp(this.actual) + ' to match ' + reg_exp + '.'); -}; +jasmine.Matchers.addMatcher('toMatch', { + test: function(actual, expected) { + return new RegExp(expected).test(actual); + }, + message: function(actual, expected) { + return actual + " does not match the regular expression " + new RegExp(expected).toString(); + } +}); /** * Matcher that compares the actual to the expected using the boolean inverse of jasmine.Matchers.toMatch * @param reg_exp */ -jasmine.Matchers.prototype.toNotMatch = function(reg_exp) { - return this.report((!new RegExp(reg_exp).test(this.actual)), - 'Expected ' + jasmine.Matchers.pp(this.actual) + ' to not match ' + reg_exp + '.'); -}; + +jasmine.Matchers.addMatcher('toNotMatch', { + test: function(actual, expected) { + return !(new RegExp(expected).test(actual)); + }, + message: function(actual, expected) { + return actual + " should not match " + new RegExp(expected).toString(); + } +}); /** * Matcher that compares the acutal to undefined. */ -jasmine.Matchers.prototype.toBeDefined = function() { - return this.report((this.actual !== undefined), - 'Expected a value to be defined but it was undefined.'); -}; + +jasmine.Matchers.addMatcher('toBeDefined', { + test: function(actual) { + return (actual !== undefined); + }, + message: function() { + return 'Expected actual to not be undefined.'; + } +}); + +/** + * Matcher that compares the acutal to undefined. + */ + +jasmine.Matchers.addMatcher('toBeUndefined', { + test: function(actual) { + return (actual === undefined); + }, + message: function(actual) { + return 'Expected ' + jasmine.pp(actual) + ' to be undefined.'; + } +}); /** * Matcher that compares the actual to null. * */ -jasmine.Matchers.prototype.toBeNull = function() { - return this.report((this.actual === null), - 'Expected a value to be null but it was ' + jasmine.Matchers.pp(this.actual) + '.'); -}; +jasmine.Matchers.addMatcher('toBeNull', { + test: function(actual) { + return (actual === null); + }, + message: function(actual) { + return 'Expected ' + jasmine.pp(actual) + ' to be null.'; + } +}); /** * Matcher that boolean not-nots the actual. */ -jasmine.Matchers.prototype.toBeTruthy = function() { - return this.report(!!this.actual, - 'Expected a value to be truthy but it was ' + jasmine.Matchers.pp(this.actual) + '.'); -}; +jasmine.Matchers.addMatcher('toBeTruthy', { + test: function(actual) { + return !!actual; + }, + message: function() { + return 'Expected actual to be truthy'; + } +}); + /** * Matcher that boolean nots the actual. */ -jasmine.Matchers.prototype.toBeFalsy = function() { - return this.report(!this.actual, - 'Expected a value to be falsy but it was ' + jasmine.Matchers.pp(this.actual) + '.'); -}; +jasmine.Matchers.addMatcher('toBeFalsy', { + test: function(actual) { + return !actual; + }, + message: function(actual) { + return 'Expected ' + jasmine.pp(actual) + ' to be falsy'; + } +}); /** * Matcher that checks to see if the acutal, a Jasmine spy, was called. */ -jasmine.Matchers.prototype.wasCalled = function() { - if (!this.actual || !this.actual.isSpy) { - return this.report(false, 'Expected a spy, but got ' + jasmine.Matchers.pp(this.actual) + '.'); + +jasmine.Matchers.addMatcher('wasCalled', { + getActual_: function() { + var args = jasmine.util.argsToArray(arguments); + if (args.length > 1) { + throw(new Error('wasCalled does not take arguments, use wasCalledWith')); + } + return args.splice(0, 1)[0]; + }, + test: function() { + var actual = this.getActual_.apply(this, arguments); + if (!actual || !actual.isSpy) { + return false; + } + return actual.wasCalled; + }, + message: function() { + var actual = this.getActual_.apply(this, arguments); + if (!actual || !actual.isSpy) { + return 'Actual is not a spy.'; + } + return "Expected spy " + actual.identity + " to have been called."; } - if (arguments.length > 0) { - return this.report(false, 'wasCalled matcher does not take arguments'); - } - return this.report((this.actual.wasCalled), - 'Expected spy "' + this.actual.identity + '" to have been called, but it was not.'); -}; +}); /** * Matcher that checks to see if the acutal, a Jasmine spy, was not called. */ -jasmine.Matchers.prototype.wasNotCalled = function() { - if (!this.actual || !this.actual.isSpy) { - return this.report(false, 'Expected a spy, but got ' + jasmine.Matchers.pp(this.actual) + '.'); +jasmine.Matchers.addMatcher('wasNotCalled', { + getActual_: function() { + var args = jasmine.util.argsToArray(arguments); + return args.splice(0, 1)[0]; + }, + test: function() { + var actual = this.getActual_.apply(this, arguments); + if (!actual || !actual.isSpy) { + return false; + } + return !actual.wasCalled; + }, + message: function() { + var actual = this.getActual_.apply(this, arguments); + if (!actual || !actual.isSpy) { + return 'Actual is not a spy.'; + } + return "Expected spy " + actual.identity + " to not have been called."; } - return this.report((!this.actual.wasCalled), - 'Expected spy "' + this.actual.identity + '" to not have been called, but it was.'); -}; +}); + +jasmine.Matchers.addMatcher('wasCalledWith', { + test: function() { + var args = jasmine.util.argsToArray(arguments); + var actual = args.splice(0, 1)[0]; + if (!actual || !actual.isSpy) { + return false; + } + return this.env.contains_(actual.argsForCall, args); + }, + message: function() { + var args = jasmine.util.argsToArray(arguments); + var actual = args.splice(0, 1)[0]; + var message; + if (!actual || !actual.isSpy) { + message = 'Actual is not a spy'; + } else { + message = "Expected spy to have been called with " + jasmine.pp(args) + " but was called with " + actual.argsForCall; + } + return message; + } +}); /** * Matcher that checks to see if the acutal, a Jasmine spy, was called with a set of parameters. @@ -1133,78 +1261,89 @@ jasmine.Matchers.prototype.wasNotCalled = function() { * @example * */ -jasmine.Matchers.prototype.wasCalledWith = function() { - if (!this.actual || !this.actual.isSpy) { - return this.report(false, 'Expected a spy, but got ' + jasmine.Matchers.pp(this.actual) + '.', { - matcherName: 'wasCalledWith' - }); - } - - var args = jasmine.util.argsToArray(arguments); - - return this.report(this.env.contains_(this.actual.argsForCall, args), - 'Expected ' + jasmine.Matchers.pp(this.actual.argsForCall) + ' to contain ' + jasmine.Matchers.pp(args) + ', but it does not.', { - matcherName: 'wasCalledWith', expected: args, actual: this.actual.argsForCall - }); -}; /** * Matcher that checks that the expected item is an element in the actual Array. * * @param {Object} item */ -jasmine.Matchers.prototype.toContain = function(item) { - return this.report(this.env.contains_(this.actual, item), - 'Expected ' + jasmine.Matchers.pp(this.actual) + ' to contain ' + jasmine.Matchers.pp(item) + ', but it does not.', { - matcherName: 'toContain', expected: item, actual: this.actual - }); -}; + +jasmine.Matchers.addMatcher('toContain', { + test: function(actual, expected) { + return this.env.contains_(actual, expected); + }, + message: function(actual, expected) { + return 'Expected ' + jasmine.pp(actual) + ' to contain ' + jasmine.pp(expected); + } +}); /** * Matcher that checks that the expected item is NOT an element in the actual Array. * * @param {Object} item */ -jasmine.Matchers.prototype.toNotContain = function(item) { - return this.report(!this.env.contains_(this.actual, item), - 'Expected ' + jasmine.Matchers.pp(this.actual) + ' not to contain ' + jasmine.Matchers.pp(item) + ', but it does.'); -}; +jasmine.Matchers.addMatcher('toNotContain', { + test: function(actual, expected) { + return !this.env.contains_(actual, expected); + }, + message: function(actual, expected) { + return 'Expected ' + jasmine.pp(actual) + ' to not contain ' + jasmine.pp(expected); + } +}); -jasmine.Matchers.prototype.toBeLessThan = function(expected) { - return this.report(this.actual < expected, - 'Expected ' + jasmine.Matchers.pp(this.actual) + ' to be less than ' + jasmine.Matchers.pp(expected) + ', but it was not.'); -}; +jasmine.Matchers.addMatcher('toBeLessThan', { + test: function(actual, expected) { + return actual < expected; + }, + message: function(actual, expected) { + return 'Expected ' + jasmine.pp(actual) + ' to be less than ' + jasmine.pp(expected); + } +}); -jasmine.Matchers.prototype.toBeGreaterThan = function(expected) { - return this.report(this.actual > expected, - 'Expected ' + jasmine.Matchers.pp(this.actual) + ' to be greater than ' + jasmine.Matchers.pp(expected) + ', but it was not.'); -}; +jasmine.Matchers.addMatcher('toBeGreaterThan', { + test: function(actual, expected) { + return actual > expected; + }, + message: function(actual, expected) { + return 'Expected ' + jasmine.pp(actual) + ' to be greater than ' + jasmine.pp(expected); + } +}); /** * Matcher that checks that the expected exception was thrown by the actual. * * @param {String} expectedException */ -jasmine.Matchers.prototype.toThrow = function(expectedException) { - var exception = null; - try { - this.actual(); - } catch (e) { - exception = e; - } - if (expectedException !== undefined) { - if (exception == null) { - return this.report(false, "Expected function to throw " + jasmine.Matchers.pp(expectedException) + ", but it did not."); +jasmine.Matchers.addMatcher('toThrow', { + getException_: function(actual, expected) { + var exception; + if (typeof actual != 'function') { + throw new Error('Actual is not a function'); + } + try { + actual(); + } catch (e) { + exception = e; + } + return exception; + }, + test: function(actual, expected) { + var result = false; + var exception = this.getException_(actual, expected); + if (exception) { + result = (expected === undefined || this.env.equals_(exception.message || exception, expected.message || expected)); + } + return result; + }, + message: function(actual, expected) { + var exception = this.getException_(actual, expected); + if (exception && (expected === undefined || !this.env.equals_(exception.message || exception, expected.message || expected))) { + return ["Expected function to throw", expected.message || expected, ", but it threw", exception.message || exception ].join(' '); + } else { + return "Expected function to throw an exception."; } - return this.report( - this.env.equals_( - exception.message || exception, - expectedException.message || expectedException), - "Expected function to throw " + jasmine.Matchers.pp(expectedException) + ", but it threw " + jasmine.Matchers.pp(exception) + "."); - } else { - return this.report(exception != null, "Expected function to throw an exception, but it did not."); } -}; +}); jasmine.Matchers.Any = function(expectedClass) { this.expectedClass = expectedClass; @@ -1716,8 +1855,12 @@ jasmine.Spec.prototype.addToQueue = function (block) { } }; +jasmine.Spec.prototype.addMatcherResult = function(result) { + this.results_.addResult(result); +}; + jasmine.Spec.prototype.expect = function(actual) { - return new (this.getMatchersClass_())(this.env, actual, this.results_); + return new (this.getMatchersClass_())(this.env, actual, this); }; jasmine.Spec.prototype.waits = function(timeout) { @@ -1733,7 +1876,11 @@ jasmine.Spec.prototype.waitsFor = function(timeout, latchFunction, timeoutMessag }; jasmine.Spec.prototype.fail = function (e) { - this.results_.addResult(new jasmine.ExpectationResult(false, e ? jasmine.util.formatException(e) : null, null)); + var expectationResult = new jasmine.ExpectationResult({ + passed: false, + message: e ? jasmine.util.formatException(e) : 'Exception' + }); + this.results_.addResult(expectationResult); }; jasmine.Spec.prototype.getMatchersClass_ = function() { diff --git a/lib/jasmine.css b/lib/jasmine.css index 7d8b3e2..61ea145 100644 --- a/lib/jasmine.css +++ b/lib/jasmine.css @@ -58,6 +58,16 @@ body .run_spec { /*white-space: pre;*/ /*}*/ +.resultMessage span.result { + display: block; + line-height: 2em; + color: black; +} + +.resultMessage .mismatch { + color: black; +} + .stackTrace { white-space: pre; font-size: .8em; @@ -73,4 +83,4 @@ body .run_spec { #jasmine_content { position:fixed; left: 100%; -} \ No newline at end of file +} diff --git a/spec/suites/ExceptionsSpec.js b/spec/suites/ExceptionsSpec.js index 0be6a6c..5494b1c 100644 --- a/spec/suites/ExceptionsSpec.js +++ b/spec/suites/ExceptionsSpec.js @@ -6,7 +6,7 @@ describe('Exceptions:', function() { env.updateInterval = 0; }); - it('jasmine.formatException formats Firefox exception maessages as expected', function() { + it('jasmine.formatException formats Firefox exception messages as expected', function() { var sampleFirefoxException = { fileName: 'foo.js', line: '1978', @@ -19,7 +19,7 @@ describe('Exceptions:', function() { expect(jasmine.util.formatException(sampleFirefoxException)).toEqual(expected); }); - it('jasmine.formatException formats Webkit exception maessages as expected', function() { + it('jasmine.formatException formats Webkit exception messages as expected', function() { var sampleWebkitException = { sourceURL: 'foo.js', lineNumber: '1978', @@ -81,7 +81,7 @@ describe('Exceptions:', function() { var specResults = suiteResults.getItems(); expect(suiteResults.passed()).toEqual(false); - + // expect(specResults.length).toEqual(5); expect(specResults[0].passed()).toMatch(false); var blockResults = specResults[0].getItems(); diff --git a/spec/suites/MatchersSpec.js b/spec/suites/MatchersSpec.js index 1682983..f7bae7f 100644 --- a/spec/suites/MatchersSpec.js +++ b/spec/suites/MatchersSpec.js @@ -1,20 +1,14 @@ describe("jasmine.Matchers", function() { - var env; - + var env, mockSpec; + beforeEach(function() { env = new jasmine.Env(); env.updateInterval = 0; + mockSpec = jasmine.createSpyObj('spec', ['addMatcherResult']); }); - - function match(value) { - return new jasmine.Matchers(env, value); - } - function detailsFor(actual, matcherName, matcherArgs) { - var matcher = match(actual); - matcher[matcherName].apply(matcher, matcherArgs); - expect(matcher.results().getItems().length).toEqual(1); - return matcher.results().getItems()[0].details; + function match(value) { + return new jasmine.Matchers(env, value, mockSpec); } it("toEqual with primitives, objects, dates, html nodes, etc.", function() { @@ -22,8 +16,12 @@ describe("jasmine.Matchers", function() { expect(match({foo:'bar'}).toEqual(null)).toEqual(false); - var functionA = function() { return 'hi'; }; - var functionB = function() { return 'hi'; }; + var functionA = function() { + return 'hi'; + }; + var functionB = function() { + return 'hi'; + }; expect(match({foo:functionA}).toEqual({foo:functionB})).toEqual(false); expect(match({foo:functionA}).toEqual({foo:functionA})).toEqual(true); @@ -49,6 +47,34 @@ describe("jasmine.Matchers", function() { expect((match(['a', 'b']).toEqual(['a', 'b', undefined]))).toEqual(false); }); + it("toEqual to build an Expectation Result", function() { + var matcher = match('a'); + matcher.toEqual('b'); + + var result = mockSpec.addMatcherResult.mostRecentCall.args[0]; + + expect(result.matcherName).toEqual("toEqual"); + expect(result.passed()).toEqual(false); + expect(result.message).toMatch("a"); + expect(result.message).toMatch("b"); + expect(result.expected).toEqual(["b"]); + expect(result.actual).toEqual("a"); + }); + + it("toNotEqual to build an Expectation Result", function() { + var matcher = match('a'); + matcher.toNotEqual('a'); + + var result = mockSpec.addMatcherResult.mostRecentCall.args[0]; + + expect(result.matcherName).toEqual("toNotEqual"); + expect(result.passed()).toEqual(false); + expect(result.message).toMatch(/a/); + expect(result.message).toMatch(/not/); + expect(result.expected).toEqual(["a"]); + expect(result.actual).toEqual("a"); + }); + it('toBe should return true only if the expected and actual items === each other', function() { var a = {}; var b = {}; @@ -62,6 +88,35 @@ describe("jasmine.Matchers", function() { expect((match(a).toNotBe(c))).toEqual(false); }); + it("toBe to build an ExpectationResult", function() { + var expected = 'b'; + var actual = 'a'; + var matcher = match(actual); + matcher.toBe(expected); + + var result = mockSpec.addMatcherResult.mostRecentCall.args[0]; + + expect(result.matcherName).toEqual("toBe"); + expect(result.passed()).toEqual(false); + expect(result.message).toMatch(jasmine.pp(actual)); + expect(result.message).toMatch(jasmine.pp(expected)); + expect(result.expected).toEqual([expected]); + expect(result.actual).toEqual(actual); + }); + + it("toNotBe to build an ExpectationResult", function() { + var str = 'a'; + var matcher = match(str); + matcher.toNotBe(str); + + var result = mockSpec.addMatcherResult.mostRecentCall.args[0]; + + expect(result.matcherName).toEqual("toNotBe"); + expect(result.passed()).toEqual(false); + expect(result.message).toMatch(str); + expect(result.expected).toEqual([str]); + expect(result.actual).toEqual(str); + }); it("toMatch and #toNotMatch should perform regular expression matching on strings", function() { expect((match('foobarbel').toMatch(/bar/))).toEqual(true); @@ -77,17 +132,116 @@ describe("jasmine.Matchers", function() { expect((match('foobazbel').toNotMatch("bar"))).toEqual(true); }); + it("toMatch w/ RegExp to build an ExpectationResult", function() { + var matcher = match('a'); + matcher.toMatch(/b/); + + var result = mockSpec.addMatcherResult.mostRecentCall.args[0]; + + expect(result.matcherName).toEqual("toMatch"); + expect(result.passed()).toEqual(false); + expect(result.message).toEqual("a does not match the regular expression /b/"); + expect(result.expected.toString()).toEqual("/b/"); + expect(result.actual).toEqual("a"); + }); + + it("toMatch w/ String to build an ExpectationResult", function() { + var matcher = match('a'); + matcher.toMatch("b"); + + var result = mockSpec.addMatcherResult.mostRecentCall.args[0]; + + expect(result.matcherName).toEqual("toMatch"); + expect(result.passed()).toEqual(false); + expect(result.message).toEqual("a does not match the regular expression /b/"); + expect(result.expected.toString()).toEqual("b"); + expect(result.actual).toEqual("a"); + }); + + it("toNotMatch w/ RegExp to build an ExpectationResult", function() { + var matcher = match('a'); + matcher.toNotMatch(/a/); + + var result = mockSpec.addMatcherResult.mostRecentCall.args[0]; + + expect(result.matcherName).toEqual("toNotMatch"); + expect(result.passed()).toEqual(false); + expect(result.message).toMatch("/a/"); + expect(result.message).toMatch("not"); + expect(result.expected.toString()).toEqual("/a/"); + expect(result.actual).toEqual("a"); + }); + + it("toNotMatch w/ String to build an ExpectationResult", function() { + var matcher = match('a'); + matcher.toNotMatch('a'); + + var result = mockSpec.addMatcherResult.mostRecentCall.args[0]; + + expect(result.matcherName).toEqual("toNotMatch"); + expect(result.passed()).toEqual(false); + expect(result.message).toMatch("/a/"); + expect(result.message).toMatch("not"); + expect(result.expected.toString()).toEqual('a'); + expect(result.actual).toEqual("a"); + }); + it("toBeDefined", function() { expect(match('foo').toBeDefined()).toEqual(true); expect(match(undefined).toBeDefined()).toEqual(false); }); + it("toBeDefined to build an ExpectationResult", function() { + var matcher = match(undefined); + matcher.toBeDefined(); + + var result = mockSpec.addMatcherResult.mostRecentCall.args[0]; + + expect(result.matcherName).toEqual("toBeDefined"); + expect(result.passed()).toEqual(false); + expect(result.message).toEqual('Expected actual to not be undefined.'); + expect(result.actual).toEqual(undefined); + }); + + it("toBeUndefined", function() { + expect(match('foo').toBeUndefined()).toEqual(false); + expect(match(undefined).toBeUndefined()).toEqual(true); + }); + it("toBeNull", function() { expect(match(null).toBeNull()).toEqual(true); expect(match(undefined).toBeNull()).toEqual(false); expect(match("foo").toBeNull()).toEqual(false); }); + it("toBeNull w/ String to build an ExpectationResult", function() { + var actual = 'a'; + var matcher = match(actual); + matcher.toBeNull(); + + var result = mockSpec.addMatcherResult.mostRecentCall.args[0]; + + expect(result.matcherName).toEqual("toBeNull"); + expect(result.passed()).toEqual(false); + expect(result.message).toMatch(jasmine.pp(actual)); + expect(result.message).toMatch('null'); + expect(result.actual).toEqual(actual); + }); + + it("toBeNull w/ Object to build an ExpectationResult", function() { + var actual = {a: 'b'}; + var matcher = match(actual); + matcher.toBeNull(); + + var result = mockSpec.addMatcherResult.mostRecentCall.args[0]; + + expect(result.matcherName).toEqual("toBeNull"); + expect(result.passed()).toEqual(false); + expect(result.message).toMatch(jasmine.pp(actual)); + expect(result.message).toMatch('null'); + expect(result.actual).toEqual(actual); + }); + it("toBeFalsy", function() { expect(match(false).toBeFalsy()).toEqual(true); expect(match(true).toBeFalsy()).toEqual(false); @@ -96,6 +250,20 @@ describe("jasmine.Matchers", function() { expect(match("").toBeFalsy()).toEqual(true); }); + it("toBeFalsy to build an ExpectationResult", function() { + var actual = 'a'; + var matcher = match(actual); + matcher.toBeFalsy(); + + var result = mockSpec.addMatcherResult.mostRecentCall.args[0]; + + expect(result.matcherName).toEqual("toBeFalsy"); + expect(result.passed()).toEqual(false); + expect(result.message).toMatch(jasmine.pp(actual)); + expect(result.message).toMatch('falsy'); + expect(result.actual).toEqual(actual); + }); + it("toBeTruthy", function() { expect(match(false).toBeTruthy()).toEqual(false); expect(match(true).toBeTruthy()).toEqual(true); @@ -107,6 +275,18 @@ describe("jasmine.Matchers", function() { expect(match({foo: 1}).toBeTruthy()).toEqual(true); }); + it("toBeTruthy to build an ExpectationResult", function() { + var matcher = match(false); + matcher.toBeTruthy(); + + var result = mockSpec.addMatcherResult.mostRecentCall.args[0]; + + expect(result.matcherName).toEqual("toBeTruthy"); + expect(result.passed()).toEqual(false); + expect(result.message).toEqual("Expected actual to be truthy"); + expect(result.actual).toEqual(false); + }); + it("toEqual", function() { expect(match(undefined).toEqual(undefined)).toEqual(true); expect(match({foo:'bar'}).toEqual({foo:'bar'})).toEqual(true); @@ -122,10 +302,13 @@ describe("jasmine.Matchers", function() { expect(match("foo").toEqual(jasmine.any(Object))).toEqual(false); expect(match({someObj:'foo'}).toEqual(jasmine.any(Object))).toEqual(true); expect(match({someObj:'foo'}).toEqual(jasmine.any(Function))).toEqual(false); - expect(match(function() {}).toEqual(jasmine.any(Object))).toEqual(false); + expect(match(function() { + }).toEqual(jasmine.any(Object))).toEqual(false); expect(match(["foo", "goo"]).toEqual(["foo", jasmine.any(String)])).toEqual(true); - expect(match(function() {}).toEqual(jasmine.any(Function))).toEqual(true); - expect(match(["a", function() {}]).toEqual(["a", jasmine.any(Function)])).toEqual(true); + expect(match(function() { + }).toEqual(jasmine.any(Function))).toEqual(true); + expect(match(["a", function() { + }]).toEqual(["a", jasmine.any(Function)])).toEqual(true); }); it("toEqual handles circular objects ok", function() { @@ -154,7 +337,7 @@ describe("jasmine.Matchers", function() { it("toContain and toNotContain", function() { expect(match('ABC').toContain('A')).toEqual(true); expect(match('ABC').toContain('X')).toEqual(false); - + expect(match(['A', 'B', 'C']).toContain('A')).toEqual(true); expect(match(['A', 'B', 'C']).toContain('F')).toEqual(false); expect(match(['A', 'B', 'C']).toNotContain('F')).toEqual(true); @@ -162,10 +345,40 @@ describe("jasmine.Matchers", function() { expect(match(['A', {some:'object'}, 'C']).toContain({some:'object'})).toEqual(true); expect(match(['A', {some:'object'}, 'C']).toContain({some:'other object'})).toEqual(false); + }); - expect(detailsFor('abc', 'toContain', ['x'])).toEqual({ - matcherName: 'toContain', expected: 'x', actual: 'abc' - }); + it("toContain to build an ExpectationResult", function() { + var actual = ['a','b','c']; + var matcher = match(actual); + var expected = 'x'; + matcher.toContain(expected); + + var result = mockSpec.addMatcherResult.mostRecentCall.args[0]; + + expect(result.matcherName).toEqual("toContain"); + expect(result.passed()).toEqual(false); + expect(result.message).toMatch(jasmine.pp(actual)); + expect(result.message).toMatch('contain'); + expect(result.message).toMatch(jasmine.pp(expected)); + expect(result.actual).toEqual(actual); + expect(result.expected).toEqual([expected]); + }); + + it("toNotContain to build an ExpectationResult", function() { + var actual = ['a','b','c']; + var matcher = match(actual); + var expected = 'b'; + matcher.toNotContain(expected); + + var result = mockSpec.addMatcherResult.mostRecentCall.args[0]; + + expect(result.matcherName).toEqual("toNotContain"); + expect(result.passed()).toEqual(false); + expect(result.message).toMatch(jasmine.pp(actual)); + expect(result.message).toMatch('not contain'); + expect(result.message).toMatch(jasmine.pp(expected)); + expect(result.actual).toEqual(actual); + expect(result.expected).toEqual([expected]); }); it("toBeLessThan should pass if actual is less than expected", function() { @@ -174,141 +387,178 @@ describe("jasmine.Matchers", function() { expect(match(37).toBeLessThan(37)).toEqual(false); }); + it("toBeLessThan to build an ExpectationResult", function() { + var actual = 3; + var matcher = match(actual); + var expected = 1; + matcher.toBeLessThan(expected); + + var result = mockSpec.addMatcherResult.mostRecentCall.args[0]; + + expect(result.matcherName).toEqual("toBeLessThan"); + expect(result.passed()).toEqual(false); + expect(result.message).toMatch(jasmine.pp(actual) + ' to be less than'); + expect(result.message).toMatch(jasmine.pp(expected)); + expect(result.actual).toEqual(actual); + expect(result.expected).toEqual([expected]); + }); + it("toBeGreaterThan should pass if actual is greater than expected", function() { expect(match(37).toBeGreaterThan(42)).toEqual(false); expect(match(37).toBeGreaterThan(-42)).toEqual(true); expect(match(37).toBeGreaterThan(37)).toEqual(false); }); + it("toBeGreaterThan to build an ExpectationResult", function() { + var actual = 1; + var matcher = match(actual); + var expected = 3; + matcher.toBeGreaterThan(expected); + + var result = mockSpec.addMatcherResult.mostRecentCall.args[0]; + + expect(result.matcherName).toEqual("toBeGreaterThan"); + expect(result.passed()).toEqual(false); + expect(result.message).toMatch(jasmine.pp(actual) + ' to be greater than'); + expect(result.message).toMatch(jasmine.pp(expected)); + expect(result.actual).toEqual(actual); + expect(result.expected).toEqual([expected]); + }); + it("toThrow", function() { var expected = new jasmine.Matchers(env, function() { throw new Error("Fake Error"); - }); + }, mockSpec); expect(expected.toThrow()).toEqual(true); expect(expected.toThrow("Fake Error")).toEqual(true); expect(expected.toThrow(new Error("Fake Error"))).toEqual(true); + expect(expected.toThrow("Other Error")).toEqual(false); + var result = mockSpec.addMatcherResult.mostRecentCall.args[0]; + expect(result.message).toMatch("Other Error"); + expect(expected.toThrow(new Error("Other Error"))).toEqual(false); + result = mockSpec.addMatcherResult.mostRecentCall.args[0]; + expect(result.message).toMatch("Other Error"); + + var exception; + try { + (function (){ + new jasmine.Matchers(env, 'not-a-function', mockSpec).toThrow(); + })(); + } catch (e) { + exception = e; + }; + expect(exception).toBeDefined(); + expect(exception.message).toEqual('Actual is not a function'); + + + expect(match(function() { + }).toThrow()).toEqual(false); + result = mockSpec.addMatcherResult.mostRecentCall.args[0]; + expect(result.message).toEqual('Expected function to throw an exception.'); + - expect(match(function() {}).toThrow()).toEqual(false); }); - it("wasCalled, wasNotCalled, wasCalledWith", function() { - var currentSuite; - var spec; - currentSuite = env.describe('default current suite', function() { - spec = env.it(); + var spyMatch = function(value, spec) { + return new jasmine.Matchers(env, value, spec); + } + + describe("wasCalled, wasNotCalled, wasCalledWith", function() { + var TestClass; + beforeEach(function() { + TestClass = { someFunction: function() { + } }; + }); + + it('should always show an error if the actual is not a spy', function () { + expect(match(TestClass.someFunction).wasCalled()).toEqual(false); + var result = mockSpec.addMatcherResult.mostRecentCall.args[0]; + expect(result.message).toEqual("Actual is not a spy."); + + expect(match(TestClass.someFunction).wasNotCalled()).toEqual(false); + result = mockSpec.addMatcherResult.mostRecentCall.args[0]; + expect(result.message).toEqual("Actual is not a spy."); }); - var TestClass = { someFunction: function() { - } }; + it("should work for spys", function() { + TestClass.someFunction = jasmine.createSpy("My spy"); - var expected; - expect(match(TestClass.someFunction).wasCalled()).toEqual(false); - expect(match(TestClass.someFunction).wasNotCalled()).toEqual(false); + expect(match(TestClass.someFunction).wasCalled()).toEqual(false); + expect(match(TestClass.someFunction).wasNotCalled()).toEqual(true); - spec.spyOn(TestClass, 'someFunction'); + TestClass.someFunction(); + expect(match(TestClass.someFunction).wasCalled()).toEqual(true); + expect(function () { match(TestClass.someFunction).wasCalled('some arg');}).toThrow('wasCalled does not take arguments, use wasCalledWith'); + expect(match(TestClass.someFunction).wasNotCalled()).toEqual(false); - expect(match(TestClass.someFunction).wasCalled()).toEqual(false); - expect(match(TestClass.someFunction).wasNotCalled()).toEqual(true); + TestClass.someFunction('a', 'b', 'c'); + expect(match(TestClass.someFunction).wasCalledWith('a', 'b', 'c')).toEqual(true); + var expected = match(TestClass.someFunction); + expect(expected.wasCalledWith('c', 'b', 'a')).toEqual(false); + result = mockSpec.addMatcherResult.mostRecentCall.args[0]; + expect(result.passed()).toEqual(false); - TestClass.someFunction(); - expect(match(TestClass.someFunction).wasCalled()).toEqual(true); - expect(match(TestClass.someFunction).wasCalled('some arg')).toEqual(false); - expect(match(TestClass.someFunction).wasNotCalled()).toEqual(false); + TestClass.someFunction.reset(); + TestClass.someFunction('a', 'b', 'c'); + TestClass.someFunction('d', 'e', 'f'); + expect(expected.wasCalledWith('a', 'b', 'c')).toEqual(true); + expect(expected.wasCalledWith('d', 'e', 'f')).toEqual(true); + expect(expected.wasCalledWith('x', 'y', 'z')).toEqual(false); + }); + }); - TestClass.someFunction('a', 'b', 'c'); - expect(match(TestClass.someFunction).wasCalledWith('a', 'b', 'c')).toEqual(true); - - expected = match(TestClass.someFunction); - expect(expected.wasCalledWith('c', 'b', 'a')).toEqual(false); - expect(expected.results().getItems()[0].passed()).toEqual(false); - - TestClass.someFunction.reset(); - TestClass.someFunction('a', 'b', 'c'); - TestClass.someFunction('d', 'e', 'f'); - expect(expected.wasCalledWith('a', 'b', 'c')).toEqual(true); - expect(expected.wasCalledWith('d', 'e', 'f')).toEqual(true); - expect(expected.wasCalledWith('x', 'y', 'z')).toEqual(false); - - expect(detailsFor(TestClass.someFunction, 'wasCalledWith', ['x', 'y', 'z'])).toEqual({ - matcherName: 'wasCalledWith', expected: ['x', 'y', 'z'], actual: TestClass.someFunction.argsForCall + describe("wasCalledWith to build an ExpectationResult", function () { + var TestClass; + beforeEach(function() { + var currentSuite; + var spec; + currentSuite = env.describe('default current suite', function() { + spec = env.it(); + }, spec); + TestClass = { someFunction: function(a, b) { + } }; + spec.spyOn(TestClass, 'someFunction'); }); + it("should handle case of actual not being a spy", function() { + var matcher = match(); + matcher.wasCalledWith('a', 'b'); + + var result = mockSpec.addMatcherResult.mostRecentCall.args[0]; + + expect(result.matcherName).toEqual("wasCalledWith"); + expect(result.passed()).toEqual(false); + expect(result.message).toEqual("Actual is not a spy"); + expect(result.actual).toEqual(undefined); + expect(result.expected).toEqual(['a','b']); + + matcher = match('foo'); + matcher.wasCalledWith('a', 'b'); + + result = mockSpec.addMatcherResult.mostRecentCall.args[0]; + + expect(result.matcherName).toEqual("wasCalledWith"); + expect(result.passed()).toEqual(false); + expect(result.message).toEqual("Actual is not a spy"); + expect(result.actual).toEqual('foo'); + expect(result.expected).toEqual(['a','b']); + }); + + it("should should handle the case of a spy", function() { + TestClass.someFunction('a', 'c'); + var matcher = match(TestClass.someFunction); + matcher.wasCalledWith('a', 'b'); + + var result = mockSpec.addMatcherResult.mostRecentCall.args[0]; + expect(result.matcherName).toEqual("wasCalledWith"); + expect(result.passed()).toEqual(false); + expect(result.message).toMatch("['a', 'b']"); + expect(result.message).toMatch("['a', 'c']"); + expect(result.actual).toEqual(TestClass.someFunction); + expect(result.expected).toEqual(['a','b']); + }); }); - - it("should report mismatches in some nice way", function() { - var results = new jasmine.NestedResults(); - var expected = new jasmine.Matchers(env, true, results); - expected.toEqual(true); - expected.toEqual(false); - - expect(results.getItems().length).toEqual(2); - - expect(results.getItems()[0].passed()).toEqual(true); - - expect(results.getItems()[1].passed()).toEqual(false); - - results = new jasmine.NestedResults(); - expected = new jasmine.Matchers(env, false, results); - expected.toEqual(true); - - var expectedMessage = 'Expected

true

but got

false
'; - expect(results.getItems()[0].message).toEqual(expectedMessage); - - results = new jasmine.NestedResults(); - expected = new jasmine.Matchers(env, null, results); - expected.toEqual('not null'); - - expectedMessage = 'Expected

\'not null\'

but got

null
'; - expect(results.getItems()[0].message).toEqual(expectedMessage); - - results = new jasmine.NestedResults(); - expected = new jasmine.Matchers(env, undefined, results); - expected.toEqual('not undefined'); - - expectedMessage = 'Expected

\'not undefined\'

but got

undefined
'; - expect(results.getItems()[0].message).toEqual(expectedMessage); - - - results = new jasmine.NestedResults(); - expected = new jasmine.Matchers(env, {foo:'one',baz:'two', more: 'blah'}, results); - expected.toEqual({foo:'one', bar: 'three &', baz: '2'}); - - expectedMessage = - "Expected

{ foo : 'one', bar : '<b>three</b> &', baz : '2' }

but got

{ foo : 'one', baz : 'two', more : 'blah' }
" + - "

Different Keys:
" + - "expected has key 'bar', but missing from actual.
" + - "expected missing key 'more', but present in actual.
" + - "

Different Values:
" + - "'bar' was

'<b>three</b> &'

in expected, but was

'undefined'

in actual.

" + - "'baz' was

'2'

in expected, but was

'two'

in actual.

"; - var actualMessage = results.getItems()[0].message; - expect(actualMessage).toEqual(expectedMessage); - - - results = new jasmine.NestedResults(); - expected = new jasmine.Matchers(env, true, results); - expected.toEqual(true); - - expect(results.getItems()[0].message).toEqual('Passed.'); - - - expected = new jasmine.Matchers(env, [1, 2, 3], results); - results.getItems().length = 0; - expected.toEqual([1, 2, 3]); - expect(results.getItems()[0].passed()).toEqual(true); - - expected = new jasmine.Matchers(env, [1, 2, 3], results); - results.getItems().length = 0; - expected.toEqual([{}, {}, {}]); - expect(results.getItems()[0].passed()).toEqual(false); - - expected = new jasmine.Matchers(env, [{}, {}, {}], results); - results.getItems().length = 0; - expected.toEqual([1, 2, 3]); - expect(results.getItems()[0].passed()).toEqual(false); - }); - }); diff --git a/spec/suites/NestedResultsSpec.js b/spec/suites/NestedResultsSpec.js index e460d7a..e4bc919 100644 --- a/spec/suites/NestedResultsSpec.js +++ b/spec/suites/NestedResultsSpec.js @@ -3,14 +3,18 @@ describe('jasmine.NestedResults', function() { // Leaf case var results = new jasmine.NestedResults(); - results.addResult(new jasmine.ExpectationResult(true,'Passed.')); + results.addResult(new jasmine.ExpectationResult({ + matcherName: "foo", passed: true, message: 'Passed.', actual: 'bar', expected: 'bar'} + )); expect(results.getItems().length).toEqual(1); expect(results.totalCount).toEqual(1); expect(results.passedCount).toEqual(1); expect(results.failedCount).toEqual(0); - results.addResult(new jasmine.ExpectationResult(false, 'FAIL.')); + results.addResult(new jasmine.ExpectationResult({ + matcherName: "baz", passed: false, message: 'FAIL.', actual: "corge", expected: "quux" + })); expect(results.getItems().length).toEqual(2); expect(results.totalCount).toEqual(2); @@ -21,12 +25,21 @@ describe('jasmine.NestedResults', function() { it('should roll up counts for nested results', function() { // Branch case var leafResultsOne = new jasmine.NestedResults(); - leafResultsOne.addResult(new jasmine.ExpectationResult( true, '')); - leafResultsOne.addResult(new jasmine.ExpectationResult( false, '')); + leafResultsOne.addResult(new jasmine.ExpectationResult({ + matcherName: "toSomething", passed: true, message: 'message', actual: '', expected:'' + })); + + leafResultsOne.addResult(new jasmine.ExpectationResult({ + matcherName: "toSomethingElse", passed: false, message: 'message', actual: 'a', expected: 'b' + })); var leafResultsTwo = new jasmine.NestedResults(); - leafResultsTwo.addResult(new jasmine.ExpectationResult( true, '')); - leafResultsTwo.addResult(new jasmine.ExpectationResult( false, '')); + leafResultsTwo.addResult(new jasmine.ExpectationResult({ + matcherName: "toSomething", passed: true, message: 'message', actual: '', expected: '' + })); + leafResultsTwo.addResult(new jasmine.ExpectationResult({ + matcherName: "toSomethineElse", passed: false, message: 'message', actual: 'c', expected: 'd' + })); var branchResults = new jasmine.NestedResults(); branchResults.addResult(leafResultsOne); @@ -38,4 +51,4 @@ describe('jasmine.NestedResults', function() { expect(branchResults.failedCount).toEqual(2); }); -}); \ No newline at end of file +}); diff --git a/spec/suites/SpecRunningSpec.js b/spec/suites/SpecRunningSpec.js index b00955b..7282a13 100644 --- a/spec/suites/SpecRunningSpec.js +++ b/spec/suites/SpecRunningSpec.js @@ -37,7 +37,6 @@ describe("jasmine spec running", function () { expect(it4.id).toEqual(4); }); - it("should build up some objects with results we can inspect", function() { var specWithNoBody, specWithExpectation, specWithFailingExpectations, specWithMultipleExpectations; diff --git a/spec/suites/TrivialReporterSpec.js b/spec/suites/TrivialReporterSpec.js index eae27fe..1660c51 100644 --- a/spec/suites/TrivialReporterSpec.js +++ b/spec/suites/TrivialReporterSpec.js @@ -76,4 +76,52 @@ describe("TrivialReporter", function() { }); }); -}); \ No newline at end of file + + describe("failure messages (integration)", function () { + var spec, results, expectationResult; + + beforeEach(function() { + results = { + passed: function() { + return false; + }, + getItems: function() { + }}; + + spec = { + suite: { + getFullName: function() { + return "suite 1"; + } + }, + getFullName: function() { + return "foo"; + }, + results: function() { + return results; + } + }; + + trivialReporter = new jasmine.TrivialReporter({ body: body }); + trivialReporter.reportRunnerStarting({ + suites: function() { + return [ new jasmine.Suite({}, "suite 1", null, null) ]; + } + }); + }); + + it("should add the failure message to the DOM (non-toEquals matchers)", function() { + expectationResult = new jasmine.ExpectationResult({ + matcherName: "toBeNull", passed: false, message: "Expected 'a' to be null, but it was not" + }); + + spyOn(results, 'getItems').andReturn([expectationResult]); + + trivialReporter.reportSpecResults(spec); + + var divs = body.getElementsByTagName("div"); + expect(divs[3].innerHTML).toEqual("Expected 'a' to be null, but it was not"); + }); + }); + +}); diff --git a/spec/suites/UtilSpec.js b/spec/suites/UtilSpec.js new file mode 100644 index 0000000..6731034 --- /dev/null +++ b/spec/suites/UtilSpec.js @@ -0,0 +1,23 @@ +describe("jasmine.util", function() { + describe("extend", function () { + it("should add properies to a destination object ", function() { + var destination = {baz: 'baz'}; + jasmine.util.extend(destination, { + foo: 'foo', bar: 'bar' + }); + expect(destination).toEqual({foo: 'foo', bar: 'bar', baz: 'baz'}); + }); + + it("should replace properies that already exist on a destination object", function() { + var destination = {foo: 'foo'}; + jasmine.util.extend(destination, { + foo: 'bar' + }); + expect(destination).toEqual({foo: 'bar'}); + jasmine.util.extend(destination, { + foo: null + }); + expect(destination).toEqual({foo: null}); + }); + }); +}); diff --git a/src/Env.js b/src/Env.js index eb90f40..bdbf761 100644 --- a/src/Env.js +++ b/src/Env.js @@ -145,18 +145,18 @@ jasmine.Env.prototype.compareObjects_ = function(a, b, mismatchKeys, mismatchVal for (var property in b) { if (!hasKey(a, property) && hasKey(b, property)) { - mismatchKeys.push("expected has key '" + property + "', but missing from actual."); + mismatchKeys.push("expected has key '" + property + "', but missing from actual."); } } for (property in a) { if (!hasKey(b, property) && hasKey(a, property)) { - mismatchKeys.push("expected missing key '" + property + "', but present in actual."); + mismatchKeys.push("expected missing key '" + property + "', but present in actual."); } } for (property in b) { if (property == '__Jasmine_been_here_before__') continue; if (!this.equals_(a[property], b[property], mismatchKeys, mismatchValues)) { - mismatchValues.push("'" + property + "' was

'" + (b[property] ? jasmine.util.htmlEscape(b[property].toString()) : b[property]) + "'

in expected, but was

'" + (a[property] ? jasmine.util.htmlEscape(a[property].toString()) : a[property]) + "'

in actual.
"); + mismatchValues.push("'" + property + "' was '" + (b[property] ? jasmine.util.htmlEscape(b[property].toString()) : b[property]) + "' in expected, but was '" + (a[property] ? jasmine.util.htmlEscape(a[property].toString()) : a[property]) + "' in actual."); } } diff --git a/src/Matchers.js b/src/Matchers.js index b119a02..0865217 100644 --- a/src/Matchers.js +++ b/src/Matchers.js @@ -4,23 +4,23 @@ * @param actual * @param {jasmine.NestedResults} results */ -jasmine.Matchers = function(env, actual, results) { +jasmine.Matchers = function(env, actual, spec) { this.env = env; this.actual = actual; - this.passing_message = 'Passed.'; - this.results_ = results || new jasmine.NestedResults(); + this.spec = spec; }; jasmine.Matchers.pp = function(str) { return jasmine.util.htmlEscape(jasmine.pp(str)); }; -jasmine.Matchers.prototype.results = function() { - return this.results_; -}; - jasmine.Matchers.prototype.report = function(result, failing_message, details) { - this.results_.addResult(new jasmine.ExpectationResult(result, result ? this.passing_message : failing_message, details)); + var expectationResult = new jasmine.ExpectationResult({ + passed: result, + message: failing_message, + details: details + }); + this.spec.addMatcherResult(expectationResult); return result; }; @@ -29,57 +29,85 @@ jasmine.Matchers.prototype.report = function(result, failing_message, details) { * * @param expected */ -jasmine.Matchers.prototype.toBe = function(expected) { - return this.report(this.actual === expected, 'Expected

' + jasmine.Matchers.pp(expected) - + '

to be the same object as

' + jasmine.Matchers.pp(this.actual) - + '
'); + + +jasmine.Matchers.addMatcher = function(matcherName, options) { + jasmine.Matchers.prototype[matcherName] = function () { + jasmine.util.extend(this, options); + var expected = jasmine.util.argsToArray(arguments); + var args = [this.actual].concat(expected); + var result = options.test.apply(this, args); + var message; + if (!result) { + message = options.message.apply(this, args); + } + var expectationResult = new jasmine.ExpectationResult({ + matcherName: matcherName, + passed: result, + expected: expected, + actual: this.actual, + message: message + }); + this.spec.addMatcherResult(expectationResult); + return result; + }; }; + /** - * Matcher that compares the actual to the expected using !== + * toBe: compares the actual to the expected using === * @param expected */ -jasmine.Matchers.prototype.toNotBe = function(expected) { - return this.report(this.actual !== expected, 'Expected

' + jasmine.Matchers.pp(expected) - + '

to be a different object from actual, but they were the same.'); -}; + +jasmine.Matchers.addMatcher('toBe', { + test: function (actual, expected) { + return actual === expected; + }, + message: function(actual, expected) { + return "Expected " + jasmine.pp(actual) + " to be " + jasmine.pp(expected); + } +}); /** - * Matcher that compares the actual to the expected using common sense equality. Handles Objects, Arrays, etc. + * toNotBe: compares the actual to the expected using !== + * @param expected + */ +jasmine.Matchers.addMatcher('toNotBe', { + test: function (actual, expected) { + return actual !== expected; + }, + message: function(actual, expected) { + return "Expected " + jasmine.pp(actual) + " to not be " + jasmine.pp(expected); + } +}); + +/** + * toEqual: compares the actual to the expected using common sense equality. Handles Objects, Arrays, etc. * * @param expected */ -jasmine.Matchers.prototype.toEqual = function(expected) { - var mismatchKeys = []; - var mismatchValues = []; - var formatMismatches = function(name, array) { - if (array.length == 0) return ''; - var errorOutput = '

Different ' + name + ':
'; - for (var i = 0; i < array.length; i++) { - errorOutput += array[i] + '
'; - } - return errorOutput; - }; - - return this.report(this.env.equals_(this.actual, expected, mismatchKeys, mismatchValues), - 'Expected

' + jasmine.Matchers.pp(expected) - + '

but got

' + jasmine.Matchers.pp(this.actual) - + '
' - + formatMismatches('Keys', mismatchKeys) - + formatMismatches('Values', mismatchValues), { - matcherName: 'toEqual', expected: expected, actual: this.actual - }); -}; +jasmine.Matchers.addMatcher('toEqual', { + test: function (actual, expected) { + return this.env.equals_(actual, expected); + }, + message: function(actual, expected) { + return "Expected " + jasmine.pp(actual) + " to equal " + jasmine.pp(expected); + } +}); /** - * Matcher that compares the actual to the expected using the ! of jasmine.Matchers.toEqual + * toNotEqual: compares the actual to the expected using the ! of jasmine.Matchers.toEqual * @param expected */ -jasmine.Matchers.prototype.toNotEqual = function(expected) { - return this.report(!this.env.equals_(this.actual, expected), - 'Expected ' + jasmine.Matchers.pp(expected) + ' to not equal ' + jasmine.Matchers.pp(this.actual) + ', but it does.'); -}; +jasmine.Matchers.addMatcher('toNotEqual', { + test: function (actual, expected) { + return !this.env.equals_(actual, expected); + }, + message: function(actual, expected) { + return "Expected " + jasmine.pp(actual) + " to not equal " + jasmine.pp(expected); + } +}); /** * Matcher that compares the actual to the expected using a regular expression. Constructs a RegExp, so takes @@ -87,77 +115,166 @@ jasmine.Matchers.prototype.toNotEqual = function(expected) { * * @param reg_exp */ -jasmine.Matchers.prototype.toMatch = function(reg_exp) { - return this.report((new RegExp(reg_exp).test(this.actual)), - 'Expected ' + jasmine.Matchers.pp(this.actual) + ' to match ' + reg_exp + '.'); -}; +jasmine.Matchers.addMatcher('toMatch', { + test: function(actual, expected) { + return new RegExp(expected).test(actual); + }, + message: function(actual, expected) { + return actual + " does not match the regular expression " + new RegExp(expected).toString(); + } +}); /** * Matcher that compares the actual to the expected using the boolean inverse of jasmine.Matchers.toMatch * @param reg_exp */ -jasmine.Matchers.prototype.toNotMatch = function(reg_exp) { - return this.report((!new RegExp(reg_exp).test(this.actual)), - 'Expected ' + jasmine.Matchers.pp(this.actual) + ' to not match ' + reg_exp + '.'); -}; + +jasmine.Matchers.addMatcher('toNotMatch', { + test: function(actual, expected) { + return !(new RegExp(expected).test(actual)); + }, + message: function(actual, expected) { + return actual + " should not match " + new RegExp(expected).toString(); + } +}); /** * Matcher that compares the acutal to undefined. */ -jasmine.Matchers.prototype.toBeDefined = function() { - return this.report((this.actual !== undefined), - 'Expected a value to be defined but it was undefined.'); -}; + +jasmine.Matchers.addMatcher('toBeDefined', { + test: function(actual) { + return (actual !== undefined); + }, + message: function() { + return 'Expected actual to not be undefined.'; + } +}); + +/** + * Matcher that compares the acutal to undefined. + */ + +jasmine.Matchers.addMatcher('toBeUndefined', { + test: function(actual) { + return (actual === undefined); + }, + message: function(actual) { + return 'Expected ' + jasmine.pp(actual) + ' to be undefined.'; + } +}); /** * Matcher that compares the actual to null. * */ -jasmine.Matchers.prototype.toBeNull = function() { - return this.report((this.actual === null), - 'Expected a value to be null but it was ' + jasmine.Matchers.pp(this.actual) + '.'); -}; +jasmine.Matchers.addMatcher('toBeNull', { + test: function(actual) { + return (actual === null); + }, + message: function(actual) { + return 'Expected ' + jasmine.pp(actual) + ' to be null.'; + } +}); /** * Matcher that boolean not-nots the actual. */ -jasmine.Matchers.prototype.toBeTruthy = function() { - return this.report(!!this.actual, - 'Expected a value to be truthy but it was ' + jasmine.Matchers.pp(this.actual) + '.'); -}; +jasmine.Matchers.addMatcher('toBeTruthy', { + test: function(actual) { + return !!actual; + }, + message: function() { + return 'Expected actual to be truthy'; + } +}); + /** * Matcher that boolean nots the actual. */ -jasmine.Matchers.prototype.toBeFalsy = function() { - return this.report(!this.actual, - 'Expected a value to be falsy but it was ' + jasmine.Matchers.pp(this.actual) + '.'); -}; +jasmine.Matchers.addMatcher('toBeFalsy', { + test: function(actual) { + return !actual; + }, + message: function(actual) { + return 'Expected ' + jasmine.pp(actual) + ' to be falsy'; + } +}); /** * Matcher that checks to see if the acutal, a Jasmine spy, was called. */ -jasmine.Matchers.prototype.wasCalled = function() { - if (!this.actual || !this.actual.isSpy) { - return this.report(false, 'Expected a spy, but got ' + jasmine.Matchers.pp(this.actual) + '.'); + +jasmine.Matchers.addMatcher('wasCalled', { + getActual_: function() { + var args = jasmine.util.argsToArray(arguments); + if (args.length > 1) { + throw(new Error('wasCalled does not take arguments, use wasCalledWith')); + } + return args.splice(0, 1)[0]; + }, + test: function() { + var actual = this.getActual_.apply(this, arguments); + if (!actual || !actual.isSpy) { + return false; + } + return actual.wasCalled; + }, + message: function() { + var actual = this.getActual_.apply(this, arguments); + if (!actual || !actual.isSpy) { + return 'Actual is not a spy.'; + } + return "Expected spy " + actual.identity + " to have been called."; } - if (arguments.length > 0) { - return this.report(false, 'wasCalled matcher does not take arguments'); - } - return this.report((this.actual.wasCalled), - 'Expected spy "' + this.actual.identity + '" to have been called, but it was not.'); -}; +}); /** * Matcher that checks to see if the acutal, a Jasmine spy, was not called. */ -jasmine.Matchers.prototype.wasNotCalled = function() { - if (!this.actual || !this.actual.isSpy) { - return this.report(false, 'Expected a spy, but got ' + jasmine.Matchers.pp(this.actual) + '.'); +jasmine.Matchers.addMatcher('wasNotCalled', { + getActual_: function() { + var args = jasmine.util.argsToArray(arguments); + return args.splice(0, 1)[0]; + }, + test: function() { + var actual = this.getActual_.apply(this, arguments); + if (!actual || !actual.isSpy) { + return false; + } + return !actual.wasCalled; + }, + message: function() { + var actual = this.getActual_.apply(this, arguments); + if (!actual || !actual.isSpy) { + return 'Actual is not a spy.'; + } + return "Expected spy " + actual.identity + " to not have been called."; } - return this.report((!this.actual.wasCalled), - 'Expected spy "' + this.actual.identity + '" to not have been called, but it was.'); -}; +}); + +jasmine.Matchers.addMatcher('wasCalledWith', { + test: function() { + var args = jasmine.util.argsToArray(arguments); + var actual = args.splice(0, 1)[0]; + if (!actual || !actual.isSpy) { + return false; + } + return this.env.contains_(actual.argsForCall, args); + }, + message: function() { + var args = jasmine.util.argsToArray(arguments); + var actual = args.splice(0, 1)[0]; + var message; + if (!actual || !actual.isSpy) { + message = 'Actual is not a spy'; + } else { + message = "Expected spy to have been called with " + jasmine.pp(args) + " but was called with " + actual.argsForCall; + } + return message; + } +}); /** * Matcher that checks to see if the acutal, a Jasmine spy, was called with a set of parameters. @@ -165,78 +282,89 @@ jasmine.Matchers.prototype.wasNotCalled = function() { * @example * */ -jasmine.Matchers.prototype.wasCalledWith = function() { - if (!this.actual || !this.actual.isSpy) { - return this.report(false, 'Expected a spy, but got ' + jasmine.Matchers.pp(this.actual) + '.', { - matcherName: 'wasCalledWith' - }); - } - - var args = jasmine.util.argsToArray(arguments); - - return this.report(this.env.contains_(this.actual.argsForCall, args), - 'Expected ' + jasmine.Matchers.pp(this.actual.argsForCall) + ' to contain ' + jasmine.Matchers.pp(args) + ', but it does not.', { - matcherName: 'wasCalledWith', expected: args, actual: this.actual.argsForCall - }); -}; /** * Matcher that checks that the expected item is an element in the actual Array. * * @param {Object} item */ -jasmine.Matchers.prototype.toContain = function(item) { - return this.report(this.env.contains_(this.actual, item), - 'Expected ' + jasmine.Matchers.pp(this.actual) + ' to contain ' + jasmine.Matchers.pp(item) + ', but it does not.', { - matcherName: 'toContain', expected: item, actual: this.actual - }); -}; + +jasmine.Matchers.addMatcher('toContain', { + test: function(actual, expected) { + return this.env.contains_(actual, expected); + }, + message: function(actual, expected) { + return 'Expected ' + jasmine.pp(actual) + ' to contain ' + jasmine.pp(expected); + } +}); /** * Matcher that checks that the expected item is NOT an element in the actual Array. * * @param {Object} item */ -jasmine.Matchers.prototype.toNotContain = function(item) { - return this.report(!this.env.contains_(this.actual, item), - 'Expected ' + jasmine.Matchers.pp(this.actual) + ' not to contain ' + jasmine.Matchers.pp(item) + ', but it does.'); -}; +jasmine.Matchers.addMatcher('toNotContain', { + test: function(actual, expected) { + return !this.env.contains_(actual, expected); + }, + message: function(actual, expected) { + return 'Expected ' + jasmine.pp(actual) + ' to not contain ' + jasmine.pp(expected); + } +}); -jasmine.Matchers.prototype.toBeLessThan = function(expected) { - return this.report(this.actual < expected, - 'Expected ' + jasmine.Matchers.pp(this.actual) + ' to be less than ' + jasmine.Matchers.pp(expected) + ', but it was not.'); -}; +jasmine.Matchers.addMatcher('toBeLessThan', { + test: function(actual, expected) { + return actual < expected; + }, + message: function(actual, expected) { + return 'Expected ' + jasmine.pp(actual) + ' to be less than ' + jasmine.pp(expected); + } +}); -jasmine.Matchers.prototype.toBeGreaterThan = function(expected) { - return this.report(this.actual > expected, - 'Expected ' + jasmine.Matchers.pp(this.actual) + ' to be greater than ' + jasmine.Matchers.pp(expected) + ', but it was not.'); -}; +jasmine.Matchers.addMatcher('toBeGreaterThan', { + test: function(actual, expected) { + return actual > expected; + }, + message: function(actual, expected) { + return 'Expected ' + jasmine.pp(actual) + ' to be greater than ' + jasmine.pp(expected); + } +}); /** * Matcher that checks that the expected exception was thrown by the actual. * * @param {String} expectedException */ -jasmine.Matchers.prototype.toThrow = function(expectedException) { - var exception = null; - try { - this.actual(); - } catch (e) { - exception = e; - } - if (expectedException !== undefined) { - if (exception == null) { - return this.report(false, "Expected function to throw " + jasmine.Matchers.pp(expectedException) + ", but it did not."); +jasmine.Matchers.addMatcher('toThrow', { + getException_: function(actual, expected) { + var exception; + if (typeof actual != 'function') { + throw new Error('Actual is not a function'); + } + try { + actual(); + } catch (e) { + exception = e; + } + return exception; + }, + test: function(actual, expected) { + var result = false; + var exception = this.getException_(actual, expected); + if (exception) { + result = (expected === undefined || this.env.equals_(exception.message || exception, expected.message || expected)); + } + return result; + }, + message: function(actual, expected) { + var exception = this.getException_(actual, expected); + if (exception && (expected === undefined || !this.env.equals_(exception.message || exception, expected.message || expected))) { + return ["Expected function to throw", expected.message || expected, ", but it threw", exception.message || exception ].join(' '); + } else { + return "Expected function to throw an exception."; } - return this.report( - this.env.equals_( - exception.message || exception, - expectedException.message || expectedException), - "Expected function to throw " + jasmine.Matchers.pp(expectedException) + ", but it threw " + jasmine.Matchers.pp(exception) + "."); - } else { - return this.report(exception != null, "Expected function to throw an exception, but it did not."); } -}; +}); jasmine.Matchers.Any = function(expectedClass) { this.expectedClass = expectedClass; diff --git a/src/Spec.js b/src/Spec.js index 5e2d3e0..93e8f8d 100644 --- a/src/Spec.js +++ b/src/Spec.js @@ -57,8 +57,12 @@ jasmine.Spec.prototype.addToQueue = function (block) { } }; +jasmine.Spec.prototype.addMatcherResult = function(result) { + this.results_.addResult(result); +}; + jasmine.Spec.prototype.expect = function(actual) { - return new (this.getMatchersClass_())(this.env, actual, this.results_); + return new (this.getMatchersClass_())(this.env, actual, this); }; jasmine.Spec.prototype.waits = function(timeout) { @@ -74,7 +78,11 @@ jasmine.Spec.prototype.waitsFor = function(timeout, latchFunction, timeoutMessag }; jasmine.Spec.prototype.fail = function (e) { - this.results_.addResult(new jasmine.ExpectationResult(false, e ? jasmine.util.formatException(e) : null, null)); + var expectationResult = new jasmine.ExpectationResult({ + passed: false, + message: e ? jasmine.util.formatException(e) : 'Exception' + }); + this.results_.addResult(expectationResult); }; jasmine.Spec.prototype.getMatchersClass_ = function() { diff --git a/src/base.js b/src/base.js index 7448d42..829b278 100755 --- a/src/base.js +++ b/src/base.js @@ -51,12 +51,18 @@ jasmine.MessageResult = function(text) { this.trace = new Error(); // todo: test better }; -jasmine.ExpectationResult = function(passed, message, details) { +jasmine.ExpectationResult = function(params) { this.type = 'ExpectationResult'; - this.passed_ = passed; - this.message = message; - this.details = details; - this.trace = new Error(message); // todo: test better + this.matcherName = params.matcherName; + this.passed_ = params.passed; + this.expected = params.expected; + this.actual = params.actual; + + /** @deprecated */ + this.details = params.details; + + this.message = this.passed_ ? 'Passed.' : params.message; + this.trace = this.passed_ ? '' : new Error(this.message); }; jasmine.ExpectationResult.prototype.passed = function () { @@ -524,4 +530,4 @@ jasmine.include = function(url, opt_global) { return eval(xhr.responseText); } -}; \ No newline at end of file +}; diff --git a/src/util.js b/src/util.js index fd297e0..155b496 100644 --- a/src/util.js +++ b/src/util.js @@ -57,3 +57,8 @@ jasmine.util.argsToArray = function(args) { return arrayOfArgs; }; +jasmine.util.extend = function(destination, source) { + for (var property in source) destination[property] = source[property]; + return destination; +}; +