Added a stringContaining asymmetric equality tester

* Fixes #1923.
This commit is contained in:
Steve Gravrock
2021-09-22 11:19:59 -07:00
parent 00f6708e1f
commit e3c9a59c6c
6 changed files with 110 additions and 0 deletions

View File

@@ -96,6 +96,7 @@ var getJasmineRequireObj = (function(jasmineGlobal) {
j$.SpyRegistry = jRequire.SpyRegistry(j$);
j$.SpyStrategy = jRequire.SpyStrategy(j$);
j$.StringMatching = jRequire.StringMatching(j$);
j$.StringContaining = jRequire.StringContaining(j$);
j$.UserContext = jRequire.UserContext(j$);
j$.Suite = jRequire.Suite(j$);
j$.Timer = jRequire.Timer();
@@ -487,6 +488,17 @@ getJasmineRequireObj().base = function(j$, jasmineGlobal) {
return new j$.StringMatching(expected);
};
/**
* 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 is a `String` that contains the specified `String`.
* @name jasmine.stringContaining
* @function
* @param {String} expected
*/
j$.stringContaining = function(expected) {
return new j$.StringContaining(expected);
};
/**
* 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 is an `Array` that contains at least the elements in the sample.
@@ -2926,6 +2938,31 @@ getJasmineRequireObj().SetContaining = function(j$) {
return SetContaining;
};
getJasmineRequireObj().StringContaining = function(j$) {
function StringContaining(expected) {
if (!j$.isString_(expected)) {
throw new Error('Expected is not a String');
}
this.expected = expected;
}
StringContaining.prototype.asymmetricMatch = function(other) {
if (!j$.isString_(other)) {
// Arrays, etc. don't match no matter what their indexOf returns.
return false;
}
return other.indexOf(this.expected) !== -1;
};
StringContaining.prototype.jasmineToString = function() {
return '<jasmine.stringContaining("' + this.expected + '")>';
};
return StringContaining;
};
getJasmineRequireObj().StringMatching = function(j$) {
function StringMatching(expected) {
if (!j$.isString_(expected) && !j$.isA_('RegExp', expected)) {

View File

@@ -0,0 +1,27 @@
describe('StringContaining', function() {
it('searches for a provided substring when the expected is a String', function() {
var matcher = new jasmineUnderTest.StringContaining('foo');
expect(matcher.asymmetricMatch('barfoobaz')).toBe(true);
expect(matcher.asymmetricMatch('barbaz')).toBe(false);
});
it('raises an Error when the expected is not a String', function() {
expect(function() {
new jasmineUnderTest.StringContaining(/foo/);
}).toThrowError(/not a String/);
});
it('fails when the actual is not a String', function() {
var matcher = new jasmineUnderTest.StringContaining('x');
expect(matcher.asymmetricMatch(['x'])).toBe(false);
});
it("jasmineToString's itself", function() {
var matching = new jasmineUnderTest.StringContaining('foo');
expect(matching.jasmineToString()).toEqual(
'<jasmine.stringContaining("foo")>'
);
});
});

View File

@@ -231,6 +231,16 @@ describe('Asymmetric equality testers (Integration)', function() {
});
});
describe('stringContaining', function() {
verifyPasses(function(env) {
env.expect('foo').toEqual(jasmineUnderTest.stringContaining('o'));
});
verifyFails(function(env) {
env.expect('bar').toEqual(jasmineUnderTest.stringContaining('o'));
});
});
describe('truthy', function() {
verifyPasses(function(env) {
env.expect(true).toEqual(jasmineUnderTest.truthy());

View File

@@ -0,0 +1,24 @@
getJasmineRequireObj().StringContaining = function(j$) {
function StringContaining(expected) {
if (!j$.isString_(expected)) {
throw new Error('Expected is not a String');
}
this.expected = expected;
}
StringContaining.prototype.asymmetricMatch = function(other) {
if (!j$.isString_(other)) {
// Arrays, etc. don't match no matter what their indexOf returns.
return false;
}
return other.indexOf(this.expected) !== -1;
};
StringContaining.prototype.jasmineToString = function() {
return '<jasmine.stringContaining("' + this.expected + '")>';
};
return StringContaining;
};

View File

@@ -319,6 +319,17 @@ getJasmineRequireObj().base = function(j$, jasmineGlobal) {
return new j$.StringMatching(expected);
};
/**
* 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 is a `String` that contains the specified `String`.
* @name jasmine.stringContaining
* @function
* @param {String} expected
*/
j$.stringContaining = function(expected) {
return new j$.StringContaining(expected);
};
/**
* 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 is an `Array` that contains at least the elements in the sample.

View File

@@ -74,6 +74,7 @@ var getJasmineRequireObj = (function(jasmineGlobal) {
j$.SpyRegistry = jRequire.SpyRegistry(j$);
j$.SpyStrategy = jRequire.SpyStrategy(j$);
j$.StringMatching = jRequire.StringMatching(j$);
j$.StringContaining = jRequire.StringContaining(j$);
j$.UserContext = jRequire.UserContext(j$);
j$.Suite = jRequire.Suite(j$);
j$.Timer = jRequire.Timer();