Advertisement
Guest User

Untitled

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