www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 13291] New: unittest should be allowed inside template

https://issues.dlang.org/show_bug.cgi?id=13291

          Issue ID: 13291
           Summary: unittest should be allowed inside template functions
           Product: D
           Version: D2
          Hardware: x86
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: DMD
          Assignee: nobody puremagic.com
          Reporter: andrei erdani.com

Currently unittests may lie at top level or inside structs, classes, and
templates. One great outcome (not attainable by library-based approaches such
as gtest for C++) is that a parameterized class/struct/template will
instantiate each unittest for each instantiation, leading to excellent
testability of generic code.

Currently parameterized functions cannot benefit of this - there's no way to
instantiate and fire a unittest for each instantiation of a function template.
We should add this to the core language.

Proposal is to allow a unittest before the body of a function, like this:

void fun(T)(T arg)
{
   ... stuff ...
}

becomes

void fun(T)(T arg)
unittest
{
    ... unittest for each T instantiated ...
}
body
{
    ... stuff ...
}

The unittest has access to T but not arg. In functions with in/out contracts,
unittest may appear only before them.

--
Aug 13 2014