Comment #0 by andrej.mitrovich — 2014-03-26T06:47:36Z
Currently when we want to unittest a library but only under specific conditions we have to use version blocks. Here's the reason why:
-----
module libfoo;
import std.stdio : File;
///
void someFunc(File file) { }
// test someFunc
unittest
{
import core.runtime : Runtime;
string rootPath = Runtime.args[1]; // runtime argument expected!
import std.path : buildPath;
auto file = File(rootPath.buildPath("test_file.dat"));
someFunc(file);
}
-----
Here's the problem with this: If the user of this library also compiles with -unittest and uses RDMD (to test his client application), the above will fail because the library expects a specific runtime argument for the path of the library so it can run its test-suite on some files in the repository.
As a workaround we can use version blocks:
-----
module libfoo;
import std.stdio : File;
///
void someFunc(File file) { }
// test someFunc
version (TestLibFoo) // added
unittest
{
import core.runtime : Runtime;
string rootPath = Runtime.args[1]; // runtime argument expected!
import std.path : buildPath;
auto file = File(rootPath.buildPath("test_file.dat"));
someFunc(file);
}
-----
Now when the user builds with -unittest he won't have failures, the library's unittests are only ran when -version=TestLibFoo.
But I would like a slightly less verbose workaround, by allowing versioned unittests. In other words:
-----
import std.stdio : File;
///
void someFunc(File file) { }
// test someFunc only if -unittest=TestLibFoo was set
unittest (TestLibFoo)
{
import core.runtime : Runtime;
string rootPath = Runtime.args[1];
import std.path : buildPath;
auto file = File(rootPath.buildPath("test_file.dat"));
someFunc(file);
}
-----
The unittest block would be enabled if -unittest=TestLibFoo was set.
I think this would make a simple additive change to the language, and it would make it obvious *what* a switch enables. In other words, you can tell at a glance that -unittest=PngImage enables a unittest block, rather than to have to encode this information in a version switch via -version=TestPngImage.
Comment #1 by dlang-bugzilla — 2014-03-26T06:50:52Z
Comment #2 by andrej.mitrovich — 2014-03-26T06:51:40Z
There is one bit of ambiguity with the design though, what to do about version(unittest) blocks. E.g.:
-----
version (??)
void testFoo()
{
}
unittest(TestFoo)
{
testFoo();
}
-----
What should the first version declaration look like? Maybe:
-----
version (unittest(TestFoo))
void testFoo()
{
}
unittest(TestFoo)
{
testFoo();
}
-----
Comment #3 by dlang-bugzilla — 2014-03-26T06:53:17Z
Oops, never mind my comment - didn't understand this issue fully.
Comment #4 by andrej.mitrovich — 2014-03-26T06:54:05Z
(In reply to comment #1)
> version(unittest) ?
The issue is these blocks are run if -unittest is set when the client compiles his own codebase that happens to use this library, but these blocks should only be ran when the library is built on its own (because it may require specific runtime arguments).
Comment #5 by andrej.mitrovich — 2014-03-26T06:54:25Z
(In reply to comment #3)
> Oops, never mind my comment - didn't understand this issue fully.
I may have underspecified it. :)
Comment #6 by robert.schadek — 2024-12-13T18:18:49Z