How to unit-test behavior under "version"?
Andrey Zherikov
andrey.zherikov at gmail.com
Wed Mar 30 14:45:12 UTC 2022
On Wednesday, 30 March 2022 at 04:15:24 UTC, Paul Backus wrote:
> 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.
This is an option when you have big difference between foo and
not-foo behavior. In my case they are 90% the same so I think
I'll go this way:
```d
void fImpl(bool fooMode)()
{
static if(fooMode)
writeln("foo");
else
writeln("no foo");
}
version(foo)
alias f = fImpl!true;
else
alias f = fImpl!false;
unittest {
// test fImpl!true
}
unittest {
// test fImpl!false
}
```
More information about the Digitalmars-d-learn
mailing list