Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added additional matchers to Jasmine

  • Loading branch information...
commit 78fd99c2a1de318481e97640c75c8c4c7fc14776 0 parents
@JamieMason authored
Showing with 171 additions and 0 deletions.
  1. +33 −0 README.md
  2. +138 −0 jasmine-matchers-1.0.js
33 README.md
@@ -0,0 +1,33 @@
+
+The [Jasmine testing framework](http://pivotal.github.com/jasmine/) from [Pivotal Labs](http://pivotallabs.com/) comes with this [default set of matchers](https://github.com/pivotal/jasmine/wiki/Matchers);
+
+ expect(function(){fn();}).toThrow(e);
+ expect(x).toBe(y);
+ expect(x).toBeDefined();
+ expect(x).toBeFalsy();
+ expect(x).toBeGreaterThan(y);
+ expect(x).toBeLessThan(y);
+ expect(x).toBeNull();
+ expect(x).toBeTruthy();
+ expect(x).toBeUndefined();
+ expect(x).toContain(y);
+ expect(x).toEqual(y);
+ expect(x).toMatch(pattern);
+
+All [Jasmine-Matchers](https://github.com/JamieMason/Jasmine-Matchers) does is add a few more;
+
+ expect(x).toBeArray();
+ expect(x).toBeArrayOfSize(number);
+ expect(x).toBeBoolean();
+ expect(x).toBeCloneOf(object);
+ expect(x).toBeFalse();
+ expect(x).toBeFunction();
+ expect(x).toBeNonEmptyString();
+ expect(x).toBeNumber();
+ expect(x).toBeObject();
+ expect(x).toBeString();
+ expect(x).toBeTrue();
+ expect(x).toThrowError();
+ expect(x).toThrowErrorOfType(y);
+
+Just include a reference to the script after your reference to Jasmine.
138 jasmine-matchers-1.0.js
@@ -0,0 +1,138 @@
+// Copyright © 2012 Jamie Mason, @GotNoSugarBaby, https://github.com/jamiemason
+// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+/*global beforeEach*/
+
+beforeEach(function() {
+
+ function typeOf (actual, type) {
+ return Object.prototype.toString.call(actual) === "[object " + type + "]";
+ }
+
+ this.addMatchers({
+
+ /**
+ * Does not return true if subject is null
+ * @return {Boolean}
+ */
+ toBeObject: function() {
+ return typeOf(this.actual, 'Object');
+ },
+
+ /**
+ * Asserts both Objects have the same public members
+ * @param {Object} candidate
+ * @return {Boolean}
+ */
+ toBeCloneOf: function(candidate) {
+ var sKey;
+ if (!typeOf(this.actual, 'Object') || !typeOf(candidate, 'Object')) {
+ return false;
+ }
+ for (sKey in this.actual) {
+ if (candidate[sKey] !== this.actual[sKey]) {
+ return false;
+ }
+ }
+ return true;
+ },
+
+ /**
+ * @return {Boolean}
+ */
+ toBeArray: function() {
+ return typeOf(this.actual, 'Array');
+ },
+
+ /**
+ * Asserts subject is an Array with a defined number of members
+ * @param {Number} size
+ * @return {Boolean}
+ */
+ toBeArrayOfSize: function(size) {
+ return typeOf(this.actual, 'Array') && this.actual.length === size;
+ },
+
+ /**
+ * @return {Boolean}
+ */
+ toBeString: function() {
+ return typeOf(this.actual, 'String');
+ },
+
+ /**
+ * @return {Boolean}
+ */
+ toBeBoolean: function() {
+ return typeOf(this.actual, 'Boolean');
+ },
+
+ /**
+ * Asserts subject is not only truthy, but a proper Boolean
+ * @return {Boolean}
+ */
+ toBeTrue: function() {
+ return this.actual === true;
+ },
+
+ /**
+ * Asserts subject is not only falsy, but a proper Boolean
+ * @return {Boolean}
+ */
+ toBeFalse: function() {
+ return this.actual === false;
+ },
+
+ /**
+ * @return {Boolean}
+ */
+ toBeNonEmptyString: function() {
+ return typeOf(this.actual, 'String') && this.actual.length > 0;
+ },
+
+ /**
+ * @return {Boolean}
+ */
+ toBeNumber: function() {
+ return !isNaN(parseFloat(this.actual)) && !typeOf(this.actual, 'String');
+ },
+
+ /**
+ * @return {Boolean}
+ */
+ toBeFunction: function() {
+ return typeOf(this.actual, 'Function');
+ },
+
+ /**
+ * Asserts subject throws an Error of any type
+ * @return {Boolean}
+ */
+ toThrowError: function() {
+ var threwError = false;
+ try {
+ this.actual();
+ } catch (e) {
+ threwError = true;
+ }
+ return threwError;
+ },
+
+ /**
+ * Asserts subject throws an Error of a specific type, such as 'TypeError'
+ * @param {String} type
+ * @return {Boolean}
+ */
+ toThrowErrorOfType: function(type) {
+ var threwErrorOfType = false;
+ try {
+ this.actual();
+ } catch (e) {
+ threwErrorOfType = (e.name === type);
+ }
+ return threwErrorOfType;
+ }
+ });
+});
Please sign in to comment.
Something went wrong with that request. Please try again.