busterjs / referee-combinators

Combinators goes assertion
0 stars 1 forks source link

assert.structure #4

Open johlrogge opened 11 years ago

johlrogge commented 11 years ago

I have added an assert.structure

It is basically a syntactic sugar on assertion combinators. At the moment it treats all functions as if they are explicit assertions in the structure and generates combinators.assert.equals(expected) for everything else.

example:

var ca = referee.combinators.assert;
ca.structure({name:'the name',
                   enabled:ca.isTrue(),
                   post:{subject:'hello structure'}
});

is roughly equivalent to:

function(actual) {
  ca.attr('name', ca.equals('the name'))(actual);
  ca.attr('enabled', ca.isTrue()))(actual);
  ca.attr('post', ca.attr('subject', ca.equals('hello structure'))))(actual);
}

The function(actual)should of course be replaced with an all combinator etc but they do not exist yet.

I think that this kind of sugaring will be very useful for testing with promises. From my own experience so far it would be really useful to be able to look inside resolved values this way without returning a different resolved value. That allows you to look into promise chains in the same way you can do with _.tap but also readable.

meisl commented 11 years ago

Hey, just wanted to say that structure is really cool and most probably going to be very useful. I didn't see it just from attr, but now I do.

Only the name... you know ;) but that can wait. I'm thinking "structural match 2.0" (or "up to 11" if you like)...?

It's the sugar that makes the cake :)

meisl commented 11 years ago

Re what makes the cake: ever been invited by "organic rads" for coffee and cake? I was just recently, so I mean it!!

johlrogge commented 11 years ago

Thanks. I really wanted to get assert.structure in there because it sort of gives a good example of the vision. But i didn't want to start in that end since the cool stuff is actually how the assertions are combined underneath.

johlrogge commented 11 years ago

I have now tested the failure messages of assert structure. Atm they are fail fast which is probably not a bad thing. It makes messages easier. Combining asserts really becomes a breeze with the new promise based raw assertions.