How to unit-test behavior under "version"?
Steven Schveighoffer
schveiguy at gmail.com
Tue Mar 29 20:20:46 UTC 2022
On 3/29/22 3:55 PM, Andrey Zherikov wrote:
> I have a function below (just an example). What's the recommended way to
> unit-test it for all `version` cases?
> ```d
> void f()
> {
> import std.stdio: writeln;
>
> version(foo)
> writeln("foo");
> else
> writeln("no foo");
> }
> ```
So keep in mind that versions are *module-wide*, and usually
*compilation-wide*. Which means that you won't be testing multiple
version configurations in the same build.
First, it's hard to know how to properly solve this. Many version
conditions either enable or disable a function, or they might make
changes that shouldn't affect the outcome, but just utilize different
mechanisms to accomplish the same result.
In the case where the version will enable or disable the whole function,
you would enable the unittest based on that.
In the case where the version affects implementation details, you
shouldn't change your unittest at all. Just compile it both ways, and it
should work the same.
In the case where version is going to *affect the results of the
function*, as yours does above, then what I would do is repeat the
version tree as above, putting an individual unittest in each branch.
But you could potentially do it inside the unittest itself. I just find
that a bit convoluted.
-Steve
More information about the Digitalmars-d-learn
mailing list