Skip to content

Commit

Permalink
allow importing trial in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Szabo Bogdan committed Sep 23, 2022
1 parent 024fbfc commit 0b763ff
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 86 deletions.
3 changes: 2 additions & 1 deletion dub.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"optional": true
},
"vibe-d:data": "~>0.9.5"
}
},
"versions": ["trial_as_dependency"]
},
{
"name": "unittest",
Expand Down
2 changes: 1 addition & 1 deletion examples/spec/tests/gameoflife/rules.d
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module tests.gameoflife.rules;

import gameoflife.rules;

import trial.discovery.spec;
import trial;
import fluent.asserts;

private alias suite = Spec!({
Expand Down
88 changes: 4 additions & 84 deletions source/package.d
Original file line number Diff line number Diff line change
Expand Up @@ -32,91 +32,11 @@ public import trial.stackresult;
public import trial.step;
public import trial.terminal;

import std.stdio;
import std.meta : Alias;
import std.traits : fullyQualifiedName;
import dub_test_root;

string getModuleFileName(alias m)() {
string location;

static foreach (member; __traits(allMembers, m)) {
static if(__traits(compiles, __traits(getLocation, __traits(getMember, m, member)))) {{
location = __traits(getLocation, __traits(getMember, m, member))[0];
}}
}

return location;
}

auto getModules() {
struct ModuleWithPath {
string name;
string path;
}

ModuleWithPath[] modules;

foreach (m; dub_test_root.allModules) {
static if (__traits(isModule, m)) {
alias module_ = m;
} else {
alias module_ = Alias!(__traits(parent, m));
}

modules ~= ModuleWithPath(fullyQualifiedName!module_, getModuleFileName!(module_));
}

return modules;
}
version(trial_as_dependency) {} else :
version(unittest):

shared static this() {
import core.runtime : Runtime, UnitTestResult;
import std.getopt : getopt;
import core.stdc.stdlib;
import trial.discovery.unit;
import trial.discovery.spec;
import trial.discovery.testclass;

Runtime.extendedModuleUnitTester = function() {
string testName;
string suiteName;
string executor;
string reporters;

auto args = Runtime.args;
args.getopt(
"testName|t", &testName,
"suiteName|s", &suiteName,
"executor|e", &executor,
"reporters|r", &reporters
);

auto settings = Settings();
settings.reporters = ["spec", "result", "stats", "html", "allure", "xunit"];
settings.artifactsLocation = ".trial";
settings.maxThreads = 1;

auto unittestDiscovery = new UnitTestDiscovery();
auto specTestDiscovery = new SpecTestDiscovery();
auto testClassDiscovery = new TestClassDiscovery();

LifeCycleListeners.instance.add(unittestDiscovery);
LifeCycleListeners.instance.add(specTestDiscovery);
LifeCycleListeners.instance.add(testClassDiscovery);

enum allModules = getModules();

static foreach(m; allModules) {
unittestDiscovery.addModule!(m.path, m.name);
specTestDiscovery.addModule!(m.path, m.name);
testClassDiscovery.addModule!(m.path, m.name);
}

setupLifecycle(settings);

runTests(LifeCycleListeners.instance.getTestCases, testName, suiteName);
import dub_test_root;

return UnitTestResult(1, 1, false, false);
};
unittestRuntimeSetup!(dub_test_root.allModules);
}
88 changes: 88 additions & 0 deletions source/runner.d
Original file line number Diff line number Diff line change
Expand Up @@ -459,4 +459,92 @@ void setupSegmentationHandler(bool testRunner)() {
sigaction(SIGBUS, &action, null);
}
}
}

string getModuleFileName(alias m)() {
string location;

static foreach (member; __traits(allMembers, m)) {
static if(__traits(compiles, __traits(getLocation, __traits(getMember, m, member)))) {{
location = __traits(getLocation, __traits(getMember, m, member))[0];
}}
}

return location;
}

auto getModules(allModules...)() {
struct ModuleWithPath {
string name;
string path;
}

ModuleWithPath[] modules;

foreach (m; allModules) {
static if (__traits(isModule, m)) {
alias module_ = m;
} else {
alias module_ = Alias!(__traits(parent, m));
}

auto path = getModuleFileName!(module_);

if(path != "") {
modules ~= ModuleWithPath(fullyQualifiedName!module_, path);
}
}

return modules;
}

void unittestRuntimeSetup(allModules...)() {
import core.runtime : Runtime, UnitTestResult;
import std.getopt : getopt;
import core.stdc.stdlib;
import trial.discovery.unit;
import trial.discovery.spec;
import trial.discovery.testclass;

Runtime.extendedModuleUnitTester = function() {
string testName;
string suiteName;
string executor;
string reporters;

auto args = Runtime.args;
args.getopt(
"testName|t", &testName,
"suiteName|s", &suiteName,
"executor|e", &executor,
"reporters|r", &reporters
);

auto settings = Settings();
settings.reporters = ["spec", "result", "stats", "html", "allure", "xunit"];
settings.artifactsLocation = ".trial";
settings.maxThreads = 1;

auto unittestDiscovery = new UnitTestDiscovery();
auto specTestDiscovery = new SpecTestDiscovery();
auto testClassDiscovery = new TestClassDiscovery();

LifeCycleListeners.instance.add(unittestDiscovery);
LifeCycleListeners.instance.add(specTestDiscovery);
LifeCycleListeners.instance.add(testClassDiscovery);

enum allModulesWithPath = getModules!(allModules);

static foreach(m; allModulesWithPath) {
unittestDiscovery.addModule!(m.path, m.name);
specTestDiscovery.addModule!(m.path, m.name);
testClassDiscovery.addModule!(m.path, m.name);
}

setupLifecycle(settings);

runTests(LifeCycleListeners.instance.getTestCases, testName, suiteName);

return UnitTestResult(1, 1, false, false);
};
}
12 changes: 12 additions & 0 deletions source/setup.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module trial.setup;

version(trial_as_dependency):
version(unittest):

import trial.runner;

shared static this() {
import dub_test_root;

unittestRuntimeSetup!(dub_test_root.allModules);
}

0 comments on commit 0b763ff

Please sign in to comment.