Skip to content
This repository has been archived by the owner on Oct 12, 2022. It is now read-only.

Commit

Permalink
Merge pull request #447 from dawgfoto/SharedRuntime_7
Browse files Browse the repository at this point in the history
unittests with shared library
  • Loading branch information
MartinNowak committed Mar 16, 2013
2 parents 73c4d2c + 374144a commit 33c2630
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 10 deletions.
2 changes: 2 additions & 0 deletions mak/MANIFEST
Expand Up @@ -13,6 +13,8 @@ MANIFEST=\
\
src\object_.d \
src\object.di \
src\test_runner.d \
src\unittest.d \
\
src\core\atomic.d \
src\core\bitop.d \
Expand Down
49 changes: 39 additions & 10 deletions posix.mak
Expand Up @@ -35,7 +35,21 @@ DOCDIR=doc
IMPDIR=import

MODEL=32
override PIC:=$(if $(PIC),-fPIC,)
# default to SHARED on some platforms
ifeq (linux,$(OS))
ifeq (64,$(MODEL))
SHARED:=1
endif
endif
override PIC:=$(if $(or $(PIC), $(SHARED)),-fPIC,)

ifeq (osx,$(OS))
DOTDLL:=.dylib
DOTLIB:=.a
else
DOTDLL:=.so
DOTLIB:=.a
endif

DFLAGS=-m$(MODEL) -O -release -inline -w -Isrc -Iimport -property $(PIC)
UDFLAGS=-m$(MODEL) -O -release -w -Isrc -Iimport -property $(PIC)
Expand Down Expand Up @@ -147,7 +161,7 @@ $(DRUNTIME): $(OBJS) $(SRCS)

UT_MODULES:=$(patsubst src/%.d,$(OBJDIR)/%,$(SRCS))

unittest : $(UT_MODULES) $(DRUNTIME) $(OBJDIR)/emptymain.d
unittest : $(UT_MODULES)
@echo done

ifeq ($(OS),freebsd)
Expand All @@ -159,20 +173,35 @@ endif
$(addprefix $(OBJDIR)/,$(DISABLED_TESTS)) :
@echo $@ - disabled

$(OBJDIR)/% : src/%.d $(DRUNTIME) $(OBJDIR)/emptymain.d
@echo Testing $@
$(QUIET)$(DMD) $(UDFLAGS) -version=druntime_unittest -unittest -of$@ $(OBJDIR)/emptymain.d $< -L-Llib -debuglib=$(DRUNTIME_BASE) -defaultlib=$(DRUNTIME_BASE)
ifeq (,$(SHARED))

$(OBJDIR)/test_runner: $(OBJS) $(SRCS) src/test_runner.d
$(DMD) $(UDFLAGS) -version=druntime_unittest -unittest -of$@ src/test_runner.d $(SRCS) $(OBJS) -debuglib= -defaultlib=

else

UT_DRUNTIME:=$(OBJDIR)/lib$(DRUNTIME_BASE)-ut$(DOTDLL)

$(UT_DRUNTIME): $(OBJS) $(SRCS)
$(DMD) $(UDFLAGS) -shared -version=druntime_unittest -unittest -of$@ $(SRCS) $(OBJS) -debuglib= -defaultlib=

$(OBJDIR)/test_runner: $(UT_DRUNTIME) src/test_runner.d
$(DMD) $(UDFLAGS) -of$@ src/test_runner.d -L-L$(OBJDIR) -L-rpath=$(OBJDIR) -debuglib=$(DRUNTIME_BASE)-ut -defaultlib=$(DRUNTIME_BASE)-ut

endif

# macro that returns the module name given the src path
moduleName=$(subst rt.invariant,invariant,$(subst object_,object,$(subst /,.,$(1))))

$(OBJDIR)/% : $(OBJDIR)/test_runner
@mkdir -p $(dir $@)
# make the file very old so it builds and runs again if it fails
@touch -t 197001230123 $@
# run unittest in its own directory
$(QUIET)$(RUN) $@
$(QUIET)$(RUN) $(OBJDIR)/test_runner $(call moduleName,$*)
# succeeded, render the file new again
@touch $@

$(OBJDIR)/emptymain.d :
@mkdir -p $(OBJDIR)
@echo 'void main(){}' >$@

detab:
detab $(MANIFEST)
tolf $(MANIFEST)
Expand Down
32 changes: 32 additions & 0 deletions src/test_runner.d
@@ -0,0 +1,32 @@
import core.runtime;
import core.stdc.stdio;

ModuleInfo* getModuleInfo(string name)
{
foreach (m; ModuleInfo)
if (m.name == name) return m;
assert(0, "module '"~name~"' not found");
}

bool tester()
{
assert(Runtime.args().length == 2);
auto name = Runtime.args()[1];

auto m = getModuleInfo(name);
if (auto fp = m.unitTest)
{
printf("Testing %.*s\n", cast(int)name.length, name.ptr);
fp();
}
return true;
}

shared static this()
{
Runtime.moduleUnitTester = &tester;
}

void main()
{
}

0 comments on commit 33c2630

Please sign in to comment.