Closed linkrope closed 6 years ago
Is this even possible, language wise?
As foo(1).should
operates on the return value of foo(1)
, I don't think it would be possible to capture the exception.
This is how it's done:
auto should(T)(lazy T testData) {
...
}
No exception is thrown until testData
is evaluated.
To postpone the evaluation further, should
should wrap testData
in a delegate.
I had the same idea a while ago and I don't remember why it did not work... I'll look into it again :)
@linkrope this would be nice to have but the value is passed in the should function... I think it would be possible to do it, but it needs a lot of refactoring to work.
another way of implementing is to write the assert like this:
foo(1).shouldThrowException!Exception;
where shouldThrowException to be something like this:
auto shouldThrowException(lazy value, string file = __FILE__, size_t line = __line__) {
return ({ value }).should.throwException!(E, file, line);
}
Then, you can even abbreviate the function name:
shouldThrow!SomeException
reads better than shouldThrowException!SomeException
.
In theory, you loose the orthogonality: for each should
you have should.not
available. In practice, I've never seen should.not.throwException
in a real unit test.
In the long run, the refactoring with some kind of "design by introspection" would help. "If it makes sense, it works" is much better than having to look up whether the function you need is implemented.
I can write
But I cannot just write
Instead, I have to write it "ugly"