[Issue 12473] New: Allow version specification for unittests

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed Mar 26 06:47:41 PDT 2014


https://d.puremagic.com/issues/show_bug.cgi?id=12473

           Summary: Allow version specification for unittests
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: coffimplib
        AssignedTo: nobody at puremagic.com
        ReportedBy: andrej.mitrovich at gmail.com


--- Comment #0 from Andrej Mitrovic <andrej.mitrovich at gmail.com> 2014-03-26 14:47:36 CET ---
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.

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list