Problem with toContain and regular expressions #199

Closed
alightgoesout opened this Issue Mar 6, 2012 · 3 comments

Projects

None yet

3 participants

@alightgoesout

With an array of regular expression, it seems that toContain is always true.

This test case fails:

it("is crazy", function()
{
    var array = [];
    var p1 = /1/i;
    var p2 = /2/i;
    array.push(p1);
    array.push(p2);
    array.splice(0,1);
    expect(array).toContain(p2);
    expect(array).not.toContain(p1);
    expect(array.length).toBe(1);
});

The error message says Error: Expected [ /2/i ] not to contain /1/i.. If .not is removed, the test case pass.

@yopefonic

I took a little closer look into the case and it seems that the regex is the main issue here, matching objects and other variables seems to be fine. Also note that the regex issue extends to other matchers like toEqual.

what happens is that the content of the array is identified as an object and is compared in that way. problem is the structure of a RegExp object is different from what the jasmine.Env.compareObjects_ expects and fails to do a compare that is reliable.

I propose adding a check to jasmine.Env.equals_ to do if (a instanceof RegExp && b instanceof RegExp). there should be no other side effects as this would only apply for both a and b being a RegExp and comparing an object with the RegExp does give back a reliable false.

I'm still not a 100% sure how to compare the regexp properly but it is likely that I will turn it into a string like it is done in data comparison. Working on it!

@yopefonic yopefonic added a commit to yopefonic/jasmine that referenced this issue Oct 27, 2012
@yopefonic yopefonic resolving issue that was identified via jasmine/jasmine#199 where Reg…
…Exp objects were not properly compared resulting in non-matching RegExp objects to always return true. a patch to jasmine.Env.equals_ adds an extra step for RexExp objects to be compared.
d65bdc7
@yopefonic

@alightgoesout pull request has been accepted and is merged into master. Issue can be closed

@ragaskar
Collaborator

Thanks!

@ragaskar ragaskar closed this Oct 30, 2012
@ragaskar ragaskar pushed a commit that referenced this issue Dec 3, 2012
@yopefonic yopefonic resolving issue that was identified via jasmine/jasmine#199 where Reg…
…Exp objects were not properly compared resulting in non-matching RegExp objects to always return true. a patch to jasmine.Env.equals_ adds an extra step for RexExp objects to be compared.
b95c43a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment