Skip to content

Commit

Permalink
Added new test framework. Tests go into a .specs directory as separat…
Browse files Browse the repository at this point in the history
…e modules.
  • Loading branch information
wilkie committed May 7, 2010
1 parent 86f0f53 commit 3f67663
Show file tree
Hide file tree
Showing 16 changed files with 430 additions and 304 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ djehuty.lib
*.idb
dspec
runtests
specs/test.d
.specs
13 changes: 5 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,15 @@ DFILES_BINDING = binding/opengl/gl.d binding/opengl/glu.d binding/lua.d
DFILES_INTERFACES = interfaces/container.d
DFILES_MATH = math/random.d math/currency.d math/fixed.d math/integer.d math/common.d math/vector.d math/matrix.d math/mathobject.d
DFILES_OPENGL = opengl/window.d opengl/texture.d opengl/light.d
DFILES_SPECS = specs/test.d
DFILES_TESTING = spec/support.d spec/logic.d spec/itemspecification.d spec/packagespecification.d spec/modulespecification.d spec/specification.d
DFILES_TESTING = spec/support.d spec/logic.d spec/itemspecification.d spec/packagespecification.d spec/modulespecification.d spec/specification.d spec/test.d
DFILES_SYNCH = synch/atomic.d synch/condition.d synch/barrier.d synch/mutex.d synch/semaphore.d synch/thread.d synch/timer.d

DFILES_RSC =
DFILES_SPECS = .specs/runtime/array.d .specs/runtime/apply.d .specs/core/application.d .specs/core/arguments.d .specs/core/date.d .specs/core/exception.d .specs/core/regex.d .specs/core/string.d .specs/core/time.d .specs/core/unicode.d .specs/core/util.d .specs/core/variant.d .specs/data/fibonacci.d .specs/data/heap.d .specs/data/queue.d .specs/data/stack.d .specs/hashes/digest.d .specs/hashes/md5.d .specs/hashes/sha1.d .specs/hashes/sha224.d .specs/hashes/sha256.d .specs/math/random.d

OBJS_CORE = $(DFILES:.d=.o) $(DFILES_RUNTIME:.d=.o) $(DFILES_LOCALES:.d=.o) $(DFILES_RESOURCE:.d=.o) $(DFILES_IO:.d=.o) $(DFILES_SYNCH:.d=.o) $(DFILES_PARSING:.d=.o) $(DFILES_OPENGL:.d=.o) $(DFILES_CUI:.d=.o) $(DFILES_ANALYZING:.d=.o) $(DFILES_SCRIPTING:.d=.o) $(DFILES_BINDING:.d=.o) $(DFILES_SPECS:.d=.o) $(DFILES_TESTING:.d=.o) $(DFILES_MATH:.d=.o) $(DFILES_GRAPHICS:.d=.o) $(DFILES_HASHES:.d=.o) $(DFILES_RSC:.d=.o) $(DFILES_NETWORKING:.d=.o) $(DFILES_INTERFACES:.d=.o) $(DFILES_DATA:.d=.o) $(DFILES_CONSOLE:.d=.o) $(DFILES_BINARY_CODECS:.d=.o) $(DFILES_CODEC:.d=.o) $(DFILES_IMAGE_CODECS:.d=.o) $(DFILES_AUDIO_CODECS:.d=.o) $(DFILES_CORE:.d=.o) $(DFILES_GUI:.d=.o) $(DFILES_PARSERS:.d=.o)
SOURCES = $(DFILES_SPECS) $(DFILES) $(DFILES_RUNTIME) $(DFILES_LOCALES) $(DFILES_RESOURCE) $(DFILES_IO) $(DFILES_SYNCH) $(DFILES_PARSING) $(DFILES_OPENGL) $(DFILES_CUI) $(DFILES_ANALYZING) $(DFILES_SCRIPTING) $(DFILES_BINDING) $(DFILES_TESTING) $(DFILES_MATH) $(DFILES_GRAPHICS) $(DFILES_HASHES) $(DFILES_RSC) $(DFILES_NETWORKING) $(DFILES_INTERFACES) $(DFILES_DATA) $(DFILES_CONSOLE) $(DFILES_BINARY_CODECS) $(DFILES_CODEC) $(DFILES_IMAGE_CODECS) $(DFILES_AUDIO_CODECS) $(DFILES_CORE) $(DFILES_GUI) $(DFILES_PARSERS)

OBJS_CORE = $(SOURCES:.d=.o)

OBJS_MAC = $(OBJS_CORE) $(DFILES_PLATFORM_MAC:.d=.o) $(OBJC_FILES:.m=.o)

Expand Down Expand Up @@ -139,9 +141,6 @@ ifeq (${MY_ARCH},Darwin)
else
endif

$(DFILES_SPECS):
touch $(DFILES_SPECS)

# initiates the compilation of the main framework
lib:
@echo compiling framework... Target: ${MY_ARCH}
Expand All @@ -158,8 +157,6 @@ else
endif
endif



app: libdeps_xomb

@echo compiling test program...
Expand Down
8 changes: 8 additions & 0 deletions io/directory.d
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ class Directory {
}
}

static Directory openOrCreate(string path) {
Directory ret = open(path);
if (ret is null) {
return Directory.create(path);
}
return ret;
}

static Directory open(string path) {
Directory ret = new Directory;
ret._isRoot = false;
Expand Down
62 changes: 58 additions & 4 deletions runtests.d
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import djehuty;

import io.console;

import specs.test;
import spec.specification;
import spec.itemspecification;
import spec.packagespecification;
import spec.modulespecification;
import spec.test;

import parsing.options;

Expand Down Expand Up @@ -45,13 +49,63 @@ class DjehutyTester : Application {
if (options.modules is null) {
Console.putln();

uint result = Tests.testAll();
if (result > 0) {
exit(1);
// Go through every package
foreach(pack; Specification) {
_testPackage(pack);
}
}
}

private:

void _testPackage(PackageSpecification ps, string prior = "") {
foreach(PackageSpecification pack; ps) {
_testPackage(pack, prior ~ ps.name ~ ".");
}

foreach(ModuleSpecification mod; ps) {
_testModule(mod, prior ~ ps.name);
}
}

void _testModule(ModuleSpecification ms, string packName = "") {
Console.put(packName ~ "." ~ ms.name, " : ");

// Keep track of success over the module
int numFailures;
int numSuccesses;
foreach(item; ms) {
foreach(feature; item) {
auto tester = new Test(item, feature);
tester.run();
if (tester.failures > 0) {
Console.forecolor = Color.Red;
if (numFailures == 0) {
Console.putln("FAILED ");
}
Console.putln(" ".times((packName ~ "." ~ ms.name).length), " : ", item.name, " ", feature);
}
numFailures += tester.failures;
numSuccesses += tester.successes;
}
}
if (numFailures > 0) {
Console.forecolor = Color.Gray;
Console.put(packName ~ "." ~ ms.name, " : ");
Console.forecolor = Color.Red;
Console.put("FAILED ");

Console.forecolor = Color.Gray;
Console.putln(numSuccesses, " / ", numSuccesses+numFailures);
}
else {
Console.forecolor = Color.Green;
Console.put("PASSED ");

Console.forecolor = Color.Gray;
Console.putln("all ", numSuccesses, " tests");
}
}

Opts options;
}
129 changes: 10 additions & 119 deletions spec/itemspecification.d
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ import spec.logic;

import djehuty;

import io.console;

class ItemSpecification {

this(string name) {
Expand All @@ -27,27 +25,22 @@ class ItemSpecification {
return _name;
}

void add(string specification, it delegate() testBody) {
void add(string specification, it function() testBody) {
_tests[specification] = testBody;
}

// Description: This will run all tests.
// Returns: When all tests are successful, it returns true. It returns false otherwise.
bool all() {
bool ret = true;
foreach(test; _tests.values) {
if (!test()) {
ret = false;
}
}
return ret;
}

// Description: This will test a particular item.
// Returns: When the test is successful, it returns true. It returns false otherwise.
bool test(string name) {
if (!(name in _tests)) {
throw new Exception("Unknown Test");
}
it ret = _tests[name]();
return false;
return ret == it.does;
}

size_t length() {
return _tests.length;
}

int opApply(int delegate(ref string) loopBody) {
Expand Down Expand Up @@ -77,108 +70,6 @@ private:

string _name;

it delegate()[string] _tests;
it function()[string] _tests;
}

/*
class Tester {
this(char[] testClass, char[] moduleName, char[] specFile = "") {
currentTest = testClass;
this.specFile = specFile;
}
void run() {
}
void logSubset(char[] subsetName) {
currentRegion = subsetName;
}
void logResult(it result, char[] msg, char[] lineNumber) {
if (result == it.does) {
// success
Console.forecolor = Color.Green;
Console.putln(" OK : (", lineNumber, ") : ", currentTest, " ", msg);
Console.forecolor = Color.Gray;
testsOk++;
classOk++;
}
else {
// fail
Console.forecolor = Color.Red;
Console.putln("FAILED : (", specFile, ":", lineNumber, ")");
Console.putln(" : ", currentTest, " ", msg);
Console.putln();
Console.forecolor = Color.Gray;
testsFailcopter++;
classFail++;
}
}
void finish() {
if (classFail == 0) {
// success
Console.forecolor = Color.Green;
Console.putln(" OK : ", currentTest, " (", classOk, " out of ", classOk + classFail, ")");
Console.forecolor = Color.Gray;
}
else {
Console.forecolor = Color.Red;
Console.putln("FAILED : ", currentTest, " (", classOk, " out of ", classOk + classFail, ")");
Console.putln();
Console.forecolor = Color.Gray;
}
classOk = 0;
classFail = 0;
}
static void done() {
Console.putln("");
Console.putln("Testing Completed");
Console.putln("");
if (testsFailcopter > 0) {
Console.forecolor = Color.Red;
Console.putln(testsFailcopter, " tests FAILED");
Console.forecolor = Color.Gray;
}
else {
Console.forecolor = Color.Green;
Console.putln("All ", testsOk, " tests SUCCEEDED");
Console.forecolor = Color.Gray;
}
Console.putln("");
lastOk = testsOk;
lastFailcopter = testsFailcopter;
testsFailcopter = 0;
testsOk = 0;
}
static uint getSuccessCount() {
return lastOk;
}
static uint getFailureCount() {
return lastFailcopter;
}
private:
static uint testsOk;
static uint testsFailcopter;
static uint lastOk;
static uint lastFailcopter;
uint classFail;
uint classOk;
char[] currentTest;
char[] specFile;
char[] currentRegion;
}
*/
3 changes: 3 additions & 0 deletions spec/logic.d
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ enum it {
does,
doesnt
}

alias void describe;
alias void done;
17 changes: 6 additions & 11 deletions spec/modulespecification.d
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,18 @@ class ModuleSpecification {
return _name;
}

// Description: This function will run all tests associated with this
// module.
bool all() {
bool ret = true;
foreach(test; _tests.values) {
if(!test.all()) {
ret = false;
}
}
return ret;
void add(ItemSpecification item) {
_tests[item.name] = item;
}

// Description: This function will return a class representing the test
// given by the name.
// name: The name of the test.
// Returns: A class that can be used to run the test.
ItemSpecification test(string name) {
ItemSpecification retrieve(string name) {
if (!(name in _tests)) {
return null;
}
return _tests[name];
}

Expand Down
Loading

0 comments on commit 3f67663

Please sign in to comment.