M
martinus
Hello everybody,
I am thinking of writing a replacement / addition for junit's assert
system. I don't like how it is currently done because it is so
inflexible. After playing around a bit, I came up with an API that
looks like this:
public void testInt() {
int num = 4+1;
ensure(num).is(5);
ensure(num).either(4, 5, 6);
ensure(num).between(3, 10);
}
public void testDouble() {
double val = 4 + 0.4;
// an epsilon is mandatory when comparing double
ensure(val).is(4.4, 0.0001);
ensure(val).between(4.3, 4.5);
}
public void testArray() {
int[] a = { 1, 2, 3, 4, 5 };
ensure(a).contains(3);
ensure(a).size().either(5, 6);
ensure(a).contains(2, 3, 4);
ensure(a).contains().either(3, 10, 11);
ensure(a).contains().neither(6, 7, 9);
ensure(a).contains().any(3, 4);
ensure(a).contains().all(3, 4, 5);
ensure(a).isSorted();
ensure(a).isUnique();
}
What do you think about such an API?
Martin
PS: Here is my blog about this
http://martin.ankerl.org/2006/08/02/redesigning-junit-asserts/
I am thinking of writing a replacement / addition for junit's assert
system. I don't like how it is currently done because it is so
inflexible. After playing around a bit, I came up with an API that
looks like this:
public void testInt() {
int num = 4+1;
ensure(num).is(5);
ensure(num).either(4, 5, 6);
ensure(num).between(3, 10);
}
public void testDouble() {
double val = 4 + 0.4;
// an epsilon is mandatory when comparing double
ensure(val).is(4.4, 0.0001);
ensure(val).between(4.3, 4.5);
}
public void testArray() {
int[] a = { 1, 2, 3, 4, 5 };
ensure(a).contains(3);
ensure(a).size().either(5, 6);
ensure(a).contains(2, 3, 4);
ensure(a).contains().either(3, 10, 11);
ensure(a).contains().neither(6, 7, 9);
ensure(a).contains().any(3, 4);
ensure(a).contains().all(3, 4, 5);
ensure(a).isSorted();
ensure(a).isUnique();
}
What do you think about such an API?
Martin
PS: Here is my blog about this
http://martin.ankerl.org/2006/08/02/redesigning-junit-asserts/