Advertisement
Guest User

Untitled

a guest
Oct 25th, 2013
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 1.99 KB | None | 0 0
  1. module opmix.ut;
  2.  
  3. struct UT {}
  4.  
  5. mixin template UTInit(string modname) {
  6.   version(UT) {
  7.     import std.traits;
  8.  
  9.     template isUnitTest(alias f) {
  10.       bool helper() {
  11.         foreach(attr; __traits(getAttributes, f))
  12.           static if(is(attr == UT))
  13.             return true;
  14.         return false;
  15.       }
  16.       enum bool isUnitTest = helper;
  17.     }
  18.    
  19.     static this() {
  20.       import std.typecons;
  21.       import std.string;
  22.       import std.algorithm;
  23.  
  24.       mixin("alias mod = " ~ modname ~ ";");
  25.       foreach(name; __traits(allMembers, mod)) {
  26.         mixin ("alias symbol = " ~ name ~ ";");
  27.         static if (isSomeFunction!symbol) {
  28.           static if(name.startsWith("test")) {
  29.             static if(isUnitTest!symbol) {
  30.               testFunctions[fullyQualifiedName!symbol] = &symbol;
  31.             }
  32.           }
  33.         }
  34.       }
  35.     }
  36.   }
  37. }
  38.  
  39. version(UT) {
  40.   import std.stdio;
  41.   import std.algorithm;
  42.   import std.regex;
  43.   import std.getopt;
  44.   import core.runtime;
  45.  
  46.   static void function()[string] testFunctions;
  47.  
  48.   bool unitTester() {
  49.     string[] modules;
  50.     string[] tests;
  51.     string[] args = Runtime.args;
  52.     bool help;
  53.  
  54.     getopt(args,
  55.            "help|h", &help,
  56.            "module_re|m", &modules,
  57.            "test_re|t", &tests);
  58.  
  59.     if(help) {
  60.       writeln("
  61.  Test Function based unit tests.
  62.  
  63.  Supported arguments:
  64.    [module_re|m] one or more regexes to match on module names
  65.    [test_re|m] one or more regexes to match on test function names
  66. ");
  67.       return false;
  68.     }
  69.  
  70.     auto delim = regex(r"\.");
  71.     foreach( funcName, func ; testFunctions ) {
  72.       auto parts = split(funcName, delim);
  73.       if(modules.length == 0 || any!(re => match(parts[0], re))(modules)) {
  74.         if(tests.length == 0 || any!(re => match(parts[1], re))(tests)) {
  75.           writeln("Running ", funcName);
  76.           func();
  77.         }
  78.       }
  79.     }
  80.     return true;
  81.   }
  82.  
  83.   static this() {
  84.      Runtime.moduleUnitTester(&unitTester);
  85.   }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement