diff --git a/lib/jasmine-core/jasmine.js b/lib/jasmine-core/jasmine.js index 551b5a78..3fdbeacc 100644 --- a/lib/jasmine-core/jasmine.js +++ b/lib/jasmine-core/jasmine.js @@ -85,6 +85,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$; @@ -282,6 +287,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. @@ -1723,6 +1760,74 @@ getJasmineRequireObj().ArrayWithExactContents = function(j$) { return ArrayWithExactContents; }; +getJasmineRequireObj().Empty = function (j$) { + + function Empty() {} + + Empty.prototype.asymmetricMatch = function (other) { + if (j$.isString_(other) || j$.isArray_(other) || j$.isTypedArray_(other)) { + return other.length === 0; + } + + if (j$.isMap(other) || j$.isSet(other)) { + return other.size === 0; + } + + if (j$.isObject_(other)) { + return Object.keys(other).length === 0; + } + return false; + }; + + Empty.prototype.jasmineToString = function () { + return ''; + }; + + return Empty; +}; + +getJasmineRequireObj().Falsy = function(j$) { + + function Falsy() {} + + Falsy.prototype.asymmetricMatch = function(other) { + return !other; + }; + + Falsy.prototype.jasmineToString = function() { + return ''; + }; + + return Falsy; +}; + +getJasmineRequireObj().NotEmpty = function (j$) { + + function NotEmpty() {} + + NotEmpty.prototype.asymmetricMatch = function (other) { + if (j$.isString_(other) || j$.isArray_(other) || j$.isTypedArray_(other)) { + return other.length !== 0; + } + + if (j$.isMap(other) || j$.isSet(other)) { + return other.size !== 0; + } + + if (j$.isObject_(other)) { + return Object.keys(other).length !== 0; + } + + return false; + }; + + NotEmpty.prototype.jasmineToString = function () { + return ''; + }; + + return NotEmpty; +}; + getJasmineRequireObj().ObjectContaining = function(j$) { function ObjectContaining(sample) { @@ -1794,6 +1899,21 @@ getJasmineRequireObj().StringMatching = function(j$) { return StringMatching; }; +getJasmineRequireObj().Truthy = function(j$) { + + function Truthy() {} + + Truthy.prototype.asymmetricMatch = function(other) { + return !!other; + }; + + Truthy.prototype.jasmineToString = function() { + return ''; + }; + + return Truthy; +}; + getJasmineRequireObj().CallTracker = function(j$) { /** diff --git a/spec/core/asymmetric_equality/EmptySpec.js b/spec/core/asymmetric_equality/EmptySpec.js new file mode 100644 index 00000000..fbfe4823 --- /dev/null +++ b/spec/core/asymmetric_equality/EmptySpec.js @@ -0,0 +1,44 @@ +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); + }); + + it("matches an empty map", function () { + jasmine.getEnv().requireFunctioningMaps(); + var empty = new jasmineUnderTest.Empty(); + + expect(empty.asymmetricMatch(new Map())).toBe(true); + }); + + it("matches an empty map", function () { + jasmine.getEnv().requireFunctioningSets(); + var empty = new jasmineUnderTest.Empty(); + + expect(empty.asymmetricMatch(new Set())).toBe(true); + }); + + it("matches an empty typed array", function() { + jasmine.getEnv().requireFunctioningTypedArrays(); + var empty = new jasmineUnderTest.Empty(); + + expect(empty.asymmetricMatch(new Int16Array())).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..0ae6443e --- /dev/null +++ b/spec/core/asymmetric_equality/NotEmptySpec.js @@ -0,0 +1,48 @@ +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); + }); + + it("matches a non empty map", function () { + jasmine.getEnv().requireFunctioningMaps(); + 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); + }); + + it("matches a non empty map", function () { + jasmine.getEnv().requireFunctioningMaps(); + var notEmpty = new jasmineUnderTest.NotEmpty(); + var filledSet = new Set([1,2,2,2,2,34]); + + expect(notEmpty.asymmetricMatch(filledSet)).toBe(true); + }); + + it("matches an empty typed array", function() { + jasmine.getEnv().requireFunctioningTypedArrays(); + var notEmpty = new jasmineUnderTest.NotEmpty(); + + expect(notEmpty.asymmetricMatch(new Int16Array([1,2,3]))).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..224c0f5f --- /dev/null +++ b/src/core/asymmetric_equality/Empty.js @@ -0,0 +1,25 @@ +getJasmineRequireObj().Empty = function (j$) { + + function Empty() {} + + Empty.prototype.asymmetricMatch = function (other) { + if (j$.isString_(other) || j$.isArray_(other) || j$.isTypedArray_(other)) { + return other.length === 0; + } + + if (j$.isMap(other) || j$.isSet(other)) { + return other.size === 0; + } + + if (j$.isObject_(other)) { + 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..242e0af3 --- /dev/null +++ b/src/core/asymmetric_equality/Falsy.js @@ -0,0 +1,14 @@ +getJasmineRequireObj().Falsy = function(j$) { + + function Falsy() {} + + Falsy.prototype.asymmetricMatch = function(other) { + return !other; + }; + + 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..3c0368cf --- /dev/null +++ b/src/core/asymmetric_equality/NotEmpty.js @@ -0,0 +1,26 @@ +getJasmineRequireObj().NotEmpty = function (j$) { + + function NotEmpty() {} + + NotEmpty.prototype.asymmetricMatch = function (other) { + if (j$.isString_(other) || j$.isArray_(other) || j$.isTypedArray_(other)) { + return other.length !== 0; + } + + if (j$.isMap(other) || j$.isSet(other)) { + return other.size !== 0; + } + + if (j$.isObject_(other)) { + 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..a852fdaf --- /dev/null +++ b/src/core/asymmetric_equality/Truthy.js @@ -0,0 +1,14 @@ +getJasmineRequireObj().Truthy = function(j$) { + + function Truthy() {} + + Truthy.prototype.asymmetricMatch = function(other) { + return !!other; + }; + + Truthy.prototype.jasmineToString = function() { + return ''; + }; + + return Truthy; +}; diff --git a/src/core/base.js b/src/core/base.js index 1f0f97d0..d01e6bfe 100644 --- a/src/core/base.js +++ b/src/core/base.js @@ -148,6 +148,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 fbc099df..b6cfd7b0 100644 --- a/src/core/requireCore.js +++ b/src/core/requireCore.js @@ -63,6 +63,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$;