Add asymmetric equality tester to match a string against a regexp
- Also move the asymmetric testers into their own dir for easier locating. [#58120558] Fix #243
This commit is contained in:
@@ -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 '<jasmine.any(' + this.expectedObject + ')>';
|
||||
};
|
||||
|
||||
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 '<jasmine.objectContaining(' + j$.pp(this.sample) + ')>';
|
||||
};
|
||||
|
||||
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 '<jasmine.any(' + this.expectedObject + ')>';
|
||||
};
|
||||
|
||||
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 '<jasmine.objectContaining(' + j$.pp(this.sample) + ')>';
|
||||
};
|
||||
|
||||
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?
|
||||
|
||||
|
||||
21
spec/core/asymmetric_equality/StringMatchingSpec.js
Normal file
21
spec/core/asymmetric_equality/StringMatchingSpec.js
Normal file
@@ -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/);
|
||||
});
|
||||
});
|
||||
16
src/core/asymmetric_equality/StringMatching.js
Normal file
16
src/core/asymmetric_equality/StringMatching.js
Normal file
@@ -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;
|
||||
};
|
||||
@@ -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({
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user