toMatch requires the expected to be a String or RegExp

- Otherwise it was using the `toString` as the RegExp, which is almost
  definitely _not_ what you want.

Fixes #723
This commit is contained in:
slackersoft
2014-12-16 11:50:45 -08:00
parent 0fdb28c6ff
commit 305252f5a8
3 changed files with 18 additions and 2 deletions

View File

@@ -2630,11 +2630,15 @@ getJasmineRequireObj().toHaveBeenCalledWith = function(j$) {
return toHaveBeenCalledWith; return toHaveBeenCalledWith;
}; };
getJasmineRequireObj().toMatch = function() { getJasmineRequireObj().toMatch = function(j$) {
function toMatch() { function toMatch() {
return { return {
compare: function(actual, expected) { compare: function(actual, expected) {
if (!j$.isString_(expected) && !j$.isA_('RegExp', expected)) {
throw new Error('Expected is not a String or a RegExp');
}
var regexp = new RegExp(expected); var regexp = new RegExp(expected);
return { return {

View File

@@ -30,5 +30,13 @@ describe("toMatch", function() {
result = matcher.compare('bar', 'foo'); result = matcher.compare('bar', 'foo');
expect(result.pass).toBe(false); expect(result.pass).toBe(false);
}); });
it("throws an Error when the expected is not a String or RegExp", function() {
var matcher = j$.matchers.toMatch();
expect(function() {
matcher.compare('foo', { bar: 'baz' });
}).toThrowError('Expected is not a String or a RegExp');
});
}); });

View File

@@ -1,8 +1,12 @@
getJasmineRequireObj().toMatch = function() { getJasmineRequireObj().toMatch = function(j$) {
function toMatch() { function toMatch() {
return { return {
compare: function(actual, expected) { compare: function(actual, expected) {
if (!j$.isString_(expected) && !j$.isA_('RegExp', expected)) {
throw new Error('Expected is not a String or a RegExp');
}
var regexp = new RegExp(expected); var regexp = new RegExp(expected);
return { return {