diff --git a/spec/core/asymmetric_equality/AsymmetricEqualitySpec.js b/spec/core/asymmetric_equality/AsymmetricEqualitySpec.js new file mode 100644 index 00000000..e9442b88 --- /dev/null +++ b/spec/core/asymmetric_equality/AsymmetricEqualitySpec.js @@ -0,0 +1,40 @@ +describe("Fuzzy Equalities", function() { + describe('Truthy/Falsy equalities', function() { + it('allows the test to pass', function() { + expect({ + name: "Rex", + id: null, + description: undefined, + age: 42, + tags: [], + }).toEqual({ + name: jasmineUnderTest.truthy(), + id: jasmineUnderTest.falsy(), + description: jasmineUnderTest.falsy(), + age: jasmineUnderTest.truthy(), + tags: jasmineUnderTest.truthy() + }) + }) + }); + + if (typeof Set !== 'undefined') { + describe('empty/notEmpty equalities', function () { + it('allows the test to pass', function () { + var tags = new Set(['blue', 'green']); + expect({ + name: "Rex", + description: '', + age: 42, + tags: tags, + gps_coord: [] + }).toEqual({ + name: jasmineUnderTest.notEmpty(), + description: jasmineUnderTest.empty(), + age: jasmineUnderTest.truthy(), + tags: jasmineUnderTest.notEmpty(), + gps_coord: jasmineUnderTest.empty() + }) + }) + }); + } +}); diff --git a/spec/core/asymmetric_equality/EmptySpec.js b/spec/core/asymmetric_equality/EmptySpec.js new file mode 100644 index 00000000..dd4d0274 --- /dev/null +++ b/spec/core/asymmetric_equality/EmptySpec.js @@ -0,0 +1,39 @@ +describe("Empty", function () { + it("matches an empty object", function () { + var empty = new jasmineUnderTest.Empty(); + + expect(empty.asymmetricMatch({})).toBe(true); + expect(empty.asymmetricMatch({undefined: false})).toBe(false); + }); + + it("matches an empty array", function () { + var empty = new jasmineUnderTest.Empty(); + + expect(empty.asymmetricMatch([])).toBe(true); + expect(empty.asymmetricMatch([1, 12, 3])).toBe(false); + }); + + it("matches an empty string", function () { + var empty = new jasmineUnderTest.Empty(); + + expect(empty.asymmetricMatch("")).toBe(true); + expect(empty.asymmetricMatch('')).toBe(true); + expect(empty.asymmetricMatch('12312')).toBe(false); + }); + + if (typeof Map !== 'undefined') { + it("matches an empty map", function () { + var empty = new jasmineUnderTest.Empty(); + + expect(empty.asymmetricMatch(new Map())).toBe(true); + }); + } + + if (typeof Set !== 'undefined') { + it("matches an empty map", function () { + var empty = new jasmineUnderTest.Empty(); + + expect(empty.asymmetricMatch(new Set())).toBe(true); + }); + } +}); diff --git a/spec/core/asymmetric_equality/FalsySpec.js b/spec/core/asymmetric_equality/FalsySpec.js new file mode 100644 index 00000000..581bedcb --- /dev/null +++ b/spec/core/asymmetric_equality/FalsySpec.js @@ -0,0 +1,38 @@ +describe("Falsy", function() { + it("is true for an empty string", function() { + var falsy = new jasmineUnderTest.Falsy(); + + expect(falsy.asymmetricMatch("")).toBe(true); + expect(falsy.asymmetricMatch('')).toBe(true); + expect(falsy.asymmetricMatch('asdasdad')).toBe(false); + }); + + it("is false for a number that is 0", function() { + var falsy = new jasmineUnderTest.Falsy(Number); + + expect(falsy.asymmetricMatch(1)).toBe(false); + expect(falsy.asymmetricMatch(0)).toBe(true); + expect(falsy.asymmetricMatch(-23)).toBe(false); + expect(falsy.asymmetricMatch(-3.1)).toBe(false); + }); + + it("is true for a null or undefined", function() { + var falsy = new jasmineUnderTest.Falsy(Function); + + expect(falsy.asymmetricMatch(null)).toBe(true); + expect(falsy.asymmetricMatch(undefined )).toBe(true); + }); + + it("is true for NaN", function() { + var falsy = new jasmineUnderTest.Falsy(Object); + + expect(falsy.asymmetricMatch(NaN)).toBe(true); + }); + + it("is true for a false Boolean", function() { + var falsy = new jasmineUnderTest.Falsy(Boolean); + + expect(falsy.asymmetricMatch(false)).toBe(true); + expect(falsy.asymmetricMatch(true)).toBe(false); + }); +}); diff --git a/spec/core/asymmetric_equality/NotEmptySpec.js b/spec/core/asymmetric_equality/NotEmptySpec.js new file mode 100644 index 00000000..5e6810b1 --- /dev/null +++ b/spec/core/asymmetric_equality/NotEmptySpec.js @@ -0,0 +1,43 @@ +describe("NotEmpty", function () { + it("matches a non empty object", function () { + var notEmpty = new jasmineUnderTest.NotEmpty(); + + expect(notEmpty.asymmetricMatch({undefined: false})).toBe(true); + expect(notEmpty.asymmetricMatch({})).toBe(false); + }); + + it("matches a non empty array", function () { + var notEmpty = new jasmineUnderTest.NotEmpty(); + + expect(notEmpty.asymmetricMatch([1, 12, 3])).toBe(true); + expect(notEmpty.asymmetricMatch([])).toBe(false); + }); + + it("matches a non empty string", function () { + var notEmpty = new jasmineUnderTest.NotEmpty(); + + expect(notEmpty.asymmetricMatch('12312')).toBe(true); + expect(notEmpty.asymmetricMatch("")).toBe(false); + expect(notEmpty.asymmetricMatch('')).toBe(false); + }); + + if (typeof Map !== 'undefined') { + it("matches a non empty map", function () { + var notEmpty = new jasmineUnderTest.NotEmpty(); + var fullMap = new Map([['one',1]]); + var emptyMap = new Map(); + + expect(notEmpty.asymmetricMatch(fullMap)).toBe(true); + expect(notEmpty.asymmetricMatch(emptyMap)).toBe(false); + }); + } + + if (typeof Set !== 'undefined') { + it("matches a non empty map", function () { + var notEmpty = new jasmineUnderTest.NotEmpty(); + var filledSet = new Set([1,2,2,2,2,34]); + + expect(notEmpty.asymmetricMatch(filledSet)).toBe(true); + }); + } +}); diff --git a/spec/core/asymmetric_equality/TruthySpec.js b/spec/core/asymmetric_equality/TruthySpec.js new file mode 100644 index 00000000..af1bddee --- /dev/null +++ b/spec/core/asymmetric_equality/TruthySpec.js @@ -0,0 +1,63 @@ +describe("Truthy", function () { + it("is true for a non empty string", function () { + var truthy = new jasmineUnderTest.Truthy(); + + expect(truthy.asymmetricMatch("foo")).toBe(true); + expect(truthy.asymmetricMatch("")).toBe(false); + }); + + it("is true for a number that is not 0", function () { + var truthy = new jasmineUnderTest.Truthy(); + + expect(truthy.asymmetricMatch(1)).toBe(true); + expect(truthy.asymmetricMatch(0)).toBe(false); + expect(truthy.asymmetricMatch(-23)).toBe(true); + expect(truthy.asymmetricMatch(-3.1)).toBe(true); + }); + + it("is true for a function", function () { + var truthy = new jasmineUnderTest.Truthy(); + + expect(truthy.asymmetricMatch(function () { + })).toBe(true); + }); + + it("is true for an Object", function () { + var truthy = new jasmineUnderTest.Truthy(); + + expect(truthy.asymmetricMatch({})).toBe(true); + }); + + it("is true for a truthful Boolean", function () { + var truthy = new jasmineUnderTest.Truthy(); + + expect(truthy.asymmetricMatch(true)).toBe(true); + expect(truthy.asymmetricMatch(false)).toBe(false); + }); + + it("is true for an empty object", function () { + var truthy = new jasmineUnderTest.Truthy(); + + expect(truthy.asymmetricMatch({})).toBe(true); + }); + + it("is true for an empty array", function () { + var truthy = new jasmineUnderTest.Truthy(); + + expect(truthy.asymmetricMatch([])).toBe(true); + }); + + it("is true for a date", function () { + var truthy = new jasmineUnderTest.Truthy(); + + expect(truthy.asymmetricMatch(new Date())).toBe(true); + }); + + it("is true for a infiniti", function () { + var truthy = new jasmineUnderTest.Truthy(); + + expect(truthy.asymmetricMatch(Infinity)).toBe(true); + expect(truthy.asymmetricMatch(-Infinity)).toBe(true); + }); +}); + diff --git a/src/core/asymmetric_equality/Empty.js b/src/core/asymmetric_equality/Empty.js new file mode 100644 index 00000000..e983d195 --- /dev/null +++ b/src/core/asymmetric_equality/Empty.js @@ -0,0 +1,33 @@ +getJasmineRequireObj().Empty = function (j$) { + + function Empty() {} + + Empty.prototype.asymmetricMatch = function (other) { + if (typeof other === 'string') { + return other.length === 0; + } + + if (other instanceof Array) { + return other.length === 0; + } + + if (typeof Map !== 'undefined' && other instanceof Map) { + return other.size === 0; + } + + if (typeof Set !== 'undefined' && other instanceof Set) { + return other.size === 0; + } + + if (other instanceof Object) { + return Object.keys(other).length === 0; + } + return false; + }; + + Empty.prototype.jasmineToString = function () { + return ''; + }; + + return Empty; +}; diff --git a/src/core/asymmetric_equality/Falsy.js b/src/core/asymmetric_equality/Falsy.js new file mode 100644 index 00000000..b713d4cc --- /dev/null +++ b/src/core/asymmetric_equality/Falsy.js @@ -0,0 +1,18 @@ +getJasmineRequireObj().Falsy = function(j$) { + + function Falsy() {} + + Falsy.prototype.asymmetricMatch = function(other) { + if (!other) { + return true; + } + + return false; + }; + + Falsy.prototype.jasmineToString = function() { + return ''; + }; + + return Falsy; +}; diff --git a/src/core/asymmetric_equality/NotEmpty.js b/src/core/asymmetric_equality/NotEmpty.js new file mode 100644 index 00000000..17b764c2 --- /dev/null +++ b/src/core/asymmetric_equality/NotEmpty.js @@ -0,0 +1,34 @@ +getJasmineRequireObj().NotEmpty = function (j$) { + + function NotEmpty() {} + + NotEmpty.prototype.asymmetricMatch = function (other) { + if (typeof other === 'string') { + return other.length !== 0; + } + + if (other instanceof Array) { + return other.length !== 0; + } + + if (typeof Map !== 'undefined' && other instanceof Map) { + return other.size !== 0; + } + + if (typeof Set !== 'undefined' && other instanceof Set) { + return other.size !== 0; + } + + if (other instanceof Object) { + return Object.keys(other).length !== 0; + } + + return false; + }; + + NotEmpty.prototype.jasmineToString = function () { + return ''; + }; + + return NotEmpty; +}; diff --git a/src/core/asymmetric_equality/Truthy.js b/src/core/asymmetric_equality/Truthy.js new file mode 100644 index 00000000..60e9caba --- /dev/null +++ b/src/core/asymmetric_equality/Truthy.js @@ -0,0 +1,18 @@ +getJasmineRequireObj().Truthy = function(j$) { + + function Truthy() {} + + Truthy.prototype.asymmetricMatch = function(other) { + if (other) { + return true; + } + + return false; + }; + + Truthy.prototype.jasmineToString = function() { + return ''; + }; + + return Truthy; +}; diff --git a/src/core/base.js b/src/core/base.js index ea09cb34..a993e9a8 100644 --- a/src/core/base.js +++ b/src/core/base.js @@ -123,6 +123,38 @@ getJasmineRequireObj().base = function(j$, jasmineGlobal) { return new j$.Anything(); }; + /** + * Get a matcher, usable in any {@link matchers|matcher} that uses Jasmine's equality (e.g. {@link matchers#toEqual|toEqual}, {@link matchers#toContain|toContain}, or {@link matchers#toHaveBeenCalledWith|toHaveBeenCalledWith}), + * that will succeed if the actual value being compared is `true` or anything truthy. + * @name jasmine.truthy + * @function + */ + j$.truthy = function() {return new j$.Truthy();}; + + /** + * Get a matcher, usable in any {@link matchers|matcher} that uses Jasmine's equality (e.g. {@link matchers#toEqual|toEqual}, {@link matchers#toContain|toContain}, or {@link matchers#toHaveBeenCalledWith|toHaveBeenCalledWith}), + * that will succeed if the actual value being compared is `null`, `undefined`, `0`, `false` or anything falsey. + * @name jasmine.falsy + * @function + */ + j$.falsy = function() {return new j$.Falsy();}; + + /** + * Get a matcher, usable in any {@link matchers|matcher} that uses Jasmine's equality (e.g. {@link matchers#toEqual|toEqual}, {@link matchers#toContain|toContain}, or {@link matchers#toHaveBeenCalledWith|toHaveBeenCalledWith}), + * that will succeed if the actual value being compared is empty. + * @name jasmine.empty + * @function + */ + j$.empty = function() {return new j$.Empty();}; + + /** + * Get a matcher, usable in any {@link matchers|matcher} that uses Jasmine's equality (e.g. {@link matchers#toEqual|toEqual}, {@link matchers#toContain|toContain}, or {@link matchers#toHaveBeenCalledWith|toHaveBeenCalledWith}), + * that will succeed if the actual value being compared is not empty. + * @name jasmine.notEmpty + * @function + */ + j$.notEmpty = function() {return new j$.NotEmpty();}; + /** * Get a matcher, usable in any {@link matchers|matcher} that uses Jasmine's equality (e.g. {@link matchers#toEqual|toEqual}, {@link matchers#toContain|toContain}, or {@link matchers#toHaveBeenCalledWith|toHaveBeenCalledWith}), * that will succeed if the actual value being compared contains at least the keys and values. diff --git a/src/core/requireCore.js b/src/core/requireCore.js index 44e868e3..9f7d9141 100644 --- a/src/core/requireCore.js +++ b/src/core/requireCore.js @@ -61,6 +61,11 @@ var getJasmineRequireObj = (function (jasmineGlobal) { j$.ObjectPath = jRequire.ObjectPath(j$); j$.GlobalErrors = jRequire.GlobalErrors(j$); + j$.Truthy = jRequire.Truthy(j$); + j$.Falsy = jRequire.Falsy(j$); + j$.Empty = jRequire.Empty(j$); + j$.NotEmpty = jRequire.NotEmpty(j$); + j$.matchers = jRequire.requireMatchers(jRequire, j$); return j$;