The evolution of easy
Ensuring objects behave accordingly has been the primary goal of easyb; yet, initial attempts to utilize JBehave’s ensure framework seemed a bit too formal, man– the ensureThat chaining works naturally in Java (i.e. ensureThat(value, eq(false))); however, in Groovy it seems forced (i.e. ungroovy, baby).
Accordingly, easyb is in the process of introducing a new syntax for ensuring hip behavior via an ensure closure, that attempts to make object verification more natural. For example, taking the action of ensuring some value is false looks like:
ensure(value) {
isFalse
}
or you could do:
ensure(value) {
isEqualTo(false)
}
In fact, you can drop the ()’s too:
ensure(value) {
isEqualTo false
}
As a side note, we want to provide a more simplified call, such as is false, however, is is defined magically in Groovy on all objects, so we’re working through this feature.
That’s a fairly simple example (how often is verification as easy as checking for false, man?).
Imagine you have a copasetic Person object (a plain old Java object, that is) that’s returned upon a find call or something and you’d like to ensure the Person object isn’t null and that a property, firstName, is some value (say “Kristen”).
Using the new easyb ensure syntax, it looks like this:
ensure(person){
isNotNull
and
contains(firstName:"Kristen")
}
Note, the person object is a Java object– it looks like this:
public class Person {
private String firstName;
private int age;
public Person(String firstName, int age){
this.firstName = firstName;
this.age = age;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
//...the rest omitted
}
If you wanted to ensure the age property was another value, you can add it to the map as follows:
ensure(person){
contains([firstName:"Kristen", age:11])
}
The ensure clause can also evaluate expressions. If it’s your bag, and you don’t require a more rich verification, you can simply write stuff like:
ensure(value == 1)
Taking the first example (the top of this entry) where some value was supposed to be false, then becomes even easier:
ensure(!value)
Of course, the expression can be anything that resolves to a boolean.
ensure("this".equals("this") && "this" instanceof String)
and
ensure(!"this".equals("that"))
easyb is all about easy BDD, baby– the framework is evolving nicely and an updated release is imminent, which will formally include the new ensure syntax along with some more emphasis on stories and scenarios. Of course, if it’s your bag, baby, you can grab the source right now (apparently anonymous check-outs aren’t working so you’ll need to create a quick account on QualityLabs.org though)!
Thursday 06 Dec 2007 | Andy | Developer Testing, Groovy
Update! Based upon a suggestion by my friend Andres Almiray,
hashas now been mapped tocontains; consequently, if you find thathasreads more copsetically, you can use it!For instance:
can be rewritten, if you’d like, to:
In fact, I think I prefer
hasnow!Umm … this is called assert() in C.
I will admit that you have a more inclusive version, but I can do the same by just writing a C function that does nothing more than the asserts:
assert_foo( &foo );
void assert_foo( FOO *foo )
{
assert( foo );
assert( foo->type === TYPE_FOO ); // good practice
assert( foo->field_1 == 5 );
assert( strcmp( foo->string, “bar” ) == 0 );
// etc
}
You can even write functions that match ‘has’ (left as excercize of reader).