Skip to content

Commit

Permalink
Merge pull request #366 from jacob-carlborg/traits
Browse files Browse the repository at this point in the history
Add documentation for isOverrideFunction and getUnitTests traits
  • Loading branch information
9rnsr committed Nov 4, 2013
1 parent 675b16d commit f16a201
Showing 1 changed file with 101 additions and 0 deletions.
101 changes: 101 additions & 0 deletions traits.dd
Expand Up @@ -32,6 +32,7 @@ $(GNAME TraitsKeyword):
$(GBLINK isAbstractFunction)
$(GBLINK isFinalFunction)
$(GBLINK isStaticFunction)
$(GBLINK isOverrideFunction)
$(GBLINK isRef)
$(GBLINK isOut)
$(GBLINK isLazy)
Expand All @@ -43,6 +44,7 @@ $(GNAME TraitsKeyword):
$(GBLINK getProtection)
$(GBLINK getVirtualFunctions)
$(GBLINK getVirtualMethods)
$(GBLINK getUnitTests)
$(GBLINK parent)
$(GBLINK classInstanceSize)
$(GBLINK getVirtualIndex)
Expand Down Expand Up @@ -268,6 +270,32 @@ void main() {
}
---

$(H2 $(GNAME isOverrideFunction))

$(P Takes one argument. If that argument is a function marked with
$(D_KEYWORD override), $(B true) is returned, otherwise $(B false).
)

---
import std.stdio;

class Base {
void foo() { }
}

class Foo : Base
{
override void foo() { }
void bar() { }
}

void main() {
writeln(__traits(isOverrideFunction, Base.foo)); // false
writeln(__traits(isOverrideFunction, Foo.foo)); // true
writeln(__traits(isOverrideFunction, Foo.bar)); // false
}
---

$(H2 $(GNAME isStaticFunction))

$(P Takes one argument. If that argument is a static function,
Expand Down Expand Up @@ -522,6 +550,79 @@ int()
2
)

$(H2 $(GNAME getUnitTests))

$(P
Takes one argument, a symbol of an aggregate (e.g. struct/class/module).
The result is a tuple of all the unit test functions of that aggregate.
The functions returned are like normal nested static functions,
$(DDSUBLINK glossary, ctfe, CTEF) will work and
$(DDSUBLINK attribute, uda, UDA's) will be accessible.
)

$(H4 Note:)

$(P
The -unittest flag needs to be passed to the compiler. If the flag
is not passed $(CODE __traits(getUnitTests)) will always return an
empty tuple.
)

---
module foo;

import core.runtime;
import std.stdio;

struct name { string name; }

class Foo {
unittest {
writeln("foo.Foo.unittest");
}
}

@name("foo") unittest {
writeln("foo.unittest");
}

template Tuple (T...)
{
alias Tuple = T;
}

shared static this()
{
// Override the default unit test runner to do nothing. After that, "main" will
// be called.
Runtime.moduleUnitTester = { return true; };
}

void main() {
writeln("start main");

alias tests = Tuple!(__traits(getUnitTests, foo));
static assert(tests.length == 1);

alias attributes = Tuple!(__traits(getAttributes, tests[0]));
static assert(attributes.length == 1);

foreach (test; tests)
test();

foreach (test; __traits(getUnitTests, Foo))
test();
}
---

$(P By default, the above will print:)

$(CONSOLE
start main
foo.unittest
foo.Foo.unittest
)

$(H2 $(GNAME parent))

$(P Takes a single argument which must evaluate to a symbol.
Expand Down

0 comments on commit f16a201

Please sign in to comment.