Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module opmix.ut;
- struct UT {}
- mixin template UTInit(string modname) {
- version(UT) {
- import std.traits;
- template isUnitTest(alias f) {
- bool helper() {
- foreach(attr; __traits(getAttributes, f))
- static if(is(attr == UT))
- return true;
- return false;
- }
- enum bool isUnitTest = helper;
- }
- static this() {
- import std.typecons;
- import std.string;
- import std.algorithm;
- mixin("alias mod = " ~ modname ~ ";");
- foreach(name; __traits(allMembers, mod)) {
- mixin ("alias symbol = " ~ name ~ ";");
- static if (isSomeFunction!symbol) {
- static if(name.startsWith("test")) {
- static if(isUnitTest!symbol) {
- testFunctions[fullyQualifiedName!symbol] = &symbol;
- }
- }
- }
- }
- }
- }
- }
- version(UT) {
- import std.stdio;
- import std.algorithm;
- import std.regex;
- import std.getopt;
- import core.runtime;
- static void function()[string] testFunctions;
- bool unitTester() {
- string[] modules;
- string[] tests;
- string[] args = Runtime.args;
- bool help;
- getopt(args,
- "help|h", &help,
- "module_re|m", &modules,
- "test_re|t", &tests);
- if(help) {
- writeln("
- Test Function based unit tests.
- Supported arguments:
- [module_re|m] one or more regexes to match on module names
- [test_re|m] one or more regexes to match on test function names
- ");
- return false;
- }
- auto delim = regex(r"\.");
- foreach( funcName, func ; testFunctions ) {
- auto parts = split(funcName, delim);
- if(modules.length == 0 || any!(re => match(parts[0], re))(modules)) {
- if(tests.length == 0 || any!(re => match(parts[1], re))(tests)) {
- writeln("Running ", funcName);
- func();
- }
- }
- }
- return true;
- }
- static this() {
- Runtime.moduleUnitTester(&unitTester);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment