diff --git a/lib/jasmine-core/jasmine.js b/lib/jasmine-core/jasmine.js index a088955b..76e084c5 100644 --- a/lib/jasmine-core/jasmine.js +++ b/lib/jasmine-core/jasmine.js @@ -58,6 +58,7 @@ getJasmineRequireObj = (function (jasmineGlobal) { j$.Spec = jRequire.Spec(j$); j$.SpyRegistry = jRequire.SpyRegistry(j$); j$.SpyStrategy = jRequire.SpyStrategy(); + j$.StringMatching = jRequire.StringMatching(j$); j$.Suite = jRequire.Suite(); j$.Timer = jRequire.Timer(); j$.version = jRequire.version(); @@ -935,54 +936,6 @@ getJasmineRequireObj().JsApiReporter = function() { return JsApiReporter; }; -getJasmineRequireObj().Any = function() { - - function Any(expectedObject) { - this.expectedObject = expectedObject; - } - - Any.prototype.asymmetricMatch = function(other) { - if (this.expectedObject == String) { - return typeof other == 'string' || other instanceof String; - } - - if (this.expectedObject == Number) { - return typeof other == 'number' || other instanceof Number; - } - - if (this.expectedObject == Function) { - return typeof other == 'function' || other instanceof Function; - } - - if (this.expectedObject == Object) { - return typeof other == 'object'; - } - - if (this.expectedObject == Boolean) { - return typeof other == 'boolean'; - } - - return other instanceof this.expectedObject; - }; - - Any.prototype.jasmineToString = function() { - return ''; - }; - - return Any; -}; - -getJasmineRequireObj().Anything = function(j$) { - - function Anything() {} - - Anything.prototype.asymmetricMatch = function(other) { - return !j$.util.isUndefined(other) && other !== null; - }; - - return Anything; -}; - getJasmineRequireObj().CallTracker = function() { function CallTracker() { @@ -1559,32 +1512,6 @@ getJasmineRequireObj().MockDate = function() { return MockDate; }; -getJasmineRequireObj().ObjectContaining = function(j$) { - - function ObjectContaining(sample) { - this.sample = sample; - } - - ObjectContaining.prototype.asymmetricMatch = function(other) { - if (typeof(this.sample) !== 'object') { throw new Error('You must provide an object to objectContaining, not \''+this.sample+'\'.'); } - - for (var property in this.sample) { - if (!Object.prototype.hasOwnProperty.call(other, property) || - !j$.matchersUtil.equals(this.sample[property], other[property])) { - return false; - } - } - - return true; - }; - - ObjectContaining.prototype.jasmineToString = function() { - return ''; - }; - - return ObjectContaining; -}; - getJasmineRequireObj().pp = function(j$) { function PrettyPrinter() { @@ -2198,6 +2125,97 @@ getJasmineRequireObj().Timer = function() { return Timer; }; +getJasmineRequireObj().Any = function() { + + function Any(expectedObject) { + this.expectedObject = expectedObject; + } + + Any.prototype.asymmetricMatch = function(other) { + if (this.expectedObject == String) { + return typeof other == 'string' || other instanceof String; + } + + if (this.expectedObject == Number) { + return typeof other == 'number' || other instanceof Number; + } + + if (this.expectedObject == Function) { + return typeof other == 'function' || other instanceof Function; + } + + if (this.expectedObject == Object) { + return typeof other == 'object'; + } + + if (this.expectedObject == Boolean) { + return typeof other == 'boolean'; + } + + return other instanceof this.expectedObject; + }; + + Any.prototype.jasmineToString = function() { + return ''; + }; + + return Any; +}; + +getJasmineRequireObj().Anything = function(j$) { + + function Anything() {} + + Anything.prototype.asymmetricMatch = function(other) { + return !j$.util.isUndefined(other) && other !== null; + }; + + return Anything; +}; + +getJasmineRequireObj().ObjectContaining = function(j$) { + + function ObjectContaining(sample) { + this.sample = sample; + } + + ObjectContaining.prototype.asymmetricMatch = function(other) { + if (typeof(this.sample) !== 'object') { throw new Error('You must provide an object to objectContaining, not \''+this.sample+'\'.'); } + + for (var property in this.sample) { + if (!Object.prototype.hasOwnProperty.call(other, property) || + !j$.matchersUtil.equals(this.sample[property], other[property])) { + return false; + } + } + + return true; + }; + + ObjectContaining.prototype.jasmineToString = function() { + return ''; + }; + + return ObjectContaining; +}; + +getJasmineRequireObj().StringMatching = function(j$) { + + function StringMatching(expected) { + if (!j$.isString_(expected) && !j$.isA_('RegExp', expected)) { + throw new Error('Expected is not a String or a RegExp'); + } + + this.regexp = new RegExp(expected); + } + + StringMatching.prototype.asymmetricMatch = function(other) { + return this.regexp.test(other); + }; + + return StringMatching; +}; + getJasmineRequireObj().matchersUtil = function(j$) { // TODO: what to do about jasmine.pp not being inject? move to JSON.stringify? gut PrettyPrinter? diff --git a/spec/core/AnySpec.js b/spec/core/asymmetric_equality/AnySpec.js similarity index 100% rename from spec/core/AnySpec.js rename to spec/core/asymmetric_equality/AnySpec.js diff --git a/spec/core/AnythingSpec.js b/spec/core/asymmetric_equality/AnythingSpec.js similarity index 100% rename from spec/core/AnythingSpec.js rename to spec/core/asymmetric_equality/AnythingSpec.js diff --git a/spec/core/ObjectContainingSpec.js b/spec/core/asymmetric_equality/ObjectContainingSpec.js similarity index 100% rename from spec/core/ObjectContainingSpec.js rename to spec/core/asymmetric_equality/ObjectContainingSpec.js diff --git a/spec/core/asymmetric_equality/StringMatchingSpec.js b/spec/core/asymmetric_equality/StringMatchingSpec.js new file mode 100644 index 00000000..d7758f90 --- /dev/null +++ b/spec/core/asymmetric_equality/StringMatchingSpec.js @@ -0,0 +1,21 @@ +describe("StringMatching", function() { + it("matches a string against a provided regexp", function() { + var matcher = new j$.StringMatching(/foo/); + + expect(matcher.asymmetricMatch('barfoobaz')).toBe(true); + expect(matcher.asymmetricMatch('barbaz')).toBe(false); + }); + + it("matches a string against provided string", function() { + var matcher = new j$.StringMatching('foo'); + + expect(matcher.asymmetricMatch('barfoobaz')).toBe(true); + expect(matcher.asymmetricMatch('barbaz')).toBe(false); + }); + + it("raises an Error when the expected is not a String or RegExp", function() { + expect(function() { + new j$.StringMatching({}); + }).toThrowError(/not a String or a RegExp/); + }); +}); diff --git a/src/core/Any.js b/src/core/asymmetric_equality/Any.js similarity index 100% rename from src/core/Any.js rename to src/core/asymmetric_equality/Any.js diff --git a/src/core/Anything.js b/src/core/asymmetric_equality/Anything.js similarity index 100% rename from src/core/Anything.js rename to src/core/asymmetric_equality/Anything.js diff --git a/src/core/ObjectContaining.js b/src/core/asymmetric_equality/ObjectContaining.js similarity index 100% rename from src/core/ObjectContaining.js rename to src/core/asymmetric_equality/ObjectContaining.js diff --git a/src/core/asymmetric_equality/StringMatching.js b/src/core/asymmetric_equality/StringMatching.js new file mode 100644 index 00000000..081c8d0e --- /dev/null +++ b/src/core/asymmetric_equality/StringMatching.js @@ -0,0 +1,16 @@ +getJasmineRequireObj().StringMatching = function(j$) { + + function StringMatching(expected) { + if (!j$.isString_(expected) && !j$.isA_('RegExp', expected)) { + throw new Error('Expected is not a String or a RegExp'); + } + + this.regexp = new RegExp(expected); + } + + StringMatching.prototype.asymmetricMatch = function(other) { + return this.regexp.test(other); + }; + + return StringMatching; +}; diff --git a/src/core/base.js b/src/core/base.js index 00f81116..b0b3847c 100644 --- a/src/core/base.js +++ b/src/core/base.js @@ -53,6 +53,10 @@ getJasmineRequireObj().base = function(j$, jasmineGlobal) { return new j$.ObjectContaining(sample); }; + j$.stringMatching = function(expected) { + return new j$.StringMatching(expected); + }; + j$.createSpy = function(name, originalFn) { var spyStrategy = new j$.SpyStrategy({ diff --git a/src/core/requireCore.js b/src/core/requireCore.js index 2dbf43ee..10de5590 100644 --- a/src/core/requireCore.js +++ b/src/core/requireCore.js @@ -36,6 +36,7 @@ getJasmineRequireObj = (function (jasmineGlobal) { j$.Spec = jRequire.Spec(j$); j$.SpyRegistry = jRequire.SpyRegistry(j$); j$.SpyStrategy = jRequire.SpyStrategy(); + j$.StringMatching = jRequire.StringMatching(j$); j$.Suite = jRequire.Suite(); j$.Timer = jRequire.Timer(); j$.version = jRequire.version();