miniblog.

← Back to all posts
26
Assertions are a surprisingly nuanced design space. In a test, if I assert `x < y`, I really want to see the values of x and y when it fails. Do you define an API for every possible predicate (Python's assertLess, expect.js) or try to support the native syntax (c.f. pytest)?

6 Comments

@wilfredh I generally find the native syntax much more pleasant than using API calls. C++'s Catch test framework supports this through expression templates. I think they hardcoded certain operators, like `==`, to support it. In languages with macros (e.g. Rust or List dialects), it is also possible to support the same kind of things with native syntax.
@lesley @wilfredh I don't think it's expression templates. Those require you to control the source types. I copied my design from doctest and I think they did it from catch. The trick is to overload an operator that binds stronger than ==. So assert(a == b) becomes "dummy op a == b" which is parsed as (dummy op a) == b. Now dummy is a test framework type that holds "a" and overloads == and the other tests. Whenever such an test is executed, you have both values for your real assertion.