How to unit-test behavior under "version"?

Paul Backus snarwin at gmail.com
Wed Mar 30 04:15:24 UTC 2022


On Tuesday, 29 March 2022 at 19:55:52 UTC, 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");
> }
> ```

Probably the easiest way is to split it into two functions

```d
void f()
{
     version (foo)
         fVersionFoo();
     else
         fDefault();
}

void fVersionFoo()
{
     /* ... */
}

void fDefault()
{
     /* ... */
}
```

Then you can write separate unit tests for both `fVersionFoo` and 
`fDefault`, and they will both be tested regardless of what 
settings you build with.


More information about the Digitalmars-d-learn mailing list