tj / should.js

BDD style assertions for node.js -- test framework agnostic
MIT License
2.75k stars 194 forks source link

Add sparseEql functionality #181

Closed apadmarao closed 10 years ago

apadmarao commented 10 years ago
{foo: undefined}.should.eql({}); // throws AssertionError

Would you support a PR to add a function sparseEql that passes in this case?

This function can be implemented by modifying the cheap equality tests in objEquiv (lib/eql.js). We could filter keys with undefined value, then afterwards check that both objects have the same number of owned properties and do the cheap key test.

btd commented 10 years ago

Nope, eql.js should just check equality and nothing more. Could you describe your use case? For me it looks like you need to use .containEql instead of .eql, but i don't know your case.

apadmarao commented 10 years ago

I often write code like:

function sillyObject(foo, bar) {
  return {
    foo: foo, 
    bar: bar
  };
}

sillyObject('foo', undefined).should.eql({foo: 'foo', bar: undefined})

I would like to have an equality operator that will disregard owned keys, if their value is undefined. This will allow me to write cleaner tests, like:

sillyObject('foo', undefined).should.eql({foo: 'foo'})

I am writing server-side Javascript, and stringify my objects into JSON before sending them to the client, so keys with value undefined are filtered. I do not believe .containEql will work in this case.

btd commented 10 years ago

Why do not use .contactEql there?

sillyObject('foo', undefined).should.containEql({foo: 'foo'})

apadmarao commented 10 years ago

I think .containEql will pass in cases where it should not. I want a method that disregards only keys with value undefined.

{foo: 'foo', bar: undefined}.should.containEql({}) // does not throw, but should 
btd commented 10 years ago

Well, every object contains {} as subobject. Ok, agree there is nothing in should for this. Not sure that it is quite usefull, so PR welcome (only pls just filter properties, do not touch eql.js).

btd commented 10 years ago

Close this one as it does not look useful, if anyone want to make PR for this, you will can always reopen, this. Closed for me means that i will not add this myself.