Skip to content

Commit

Permalink
rework unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ianharrigan committed Sep 2, 2024
1 parent 854d34a commit becbbc7
Show file tree
Hide file tree
Showing 7 changed files with 513 additions and 380 deletions.
99 changes: 99 additions & 0 deletions tests/AssertionTools.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package;

class AssertionTools {
public static function shouldContain(expectedToContain:Dynamic, array:Array<Dynamic>) {
doesContain(expectedToContain, array, true);
}

public static function shouldMatch(expectedToMatch:Dynamic, object:Dynamic) {
doesMatch(expectedToMatch, object, false, true);
}

private static function doesContain(expectedToContain:Dynamic, array:Array<Dynamic>, throwException:Bool):Bool {
var found = false;
for (item in array) {
if (doesMatch(expectedToContain, item, false, false)) {
found = true;
break;
}
}

if (!found) {
if (throwException) {
throw 'not found in array!';
} else {
//trace('not found in array!');
}
}

return found;
}

private static function doesMatch(expectedToMatch:Dynamic, object:Dynamic, strict:Bool, throwException:Bool):Bool {
for (f in Reflect.fields(expectedToMatch)) {
if (!hasField(object, f)) {
if (throwException) {
throw 'couldnt find field "${f}"';
} else {
trace('couldnt find field "${f}"');
}
return false;
}
var objectValue:Dynamic = Reflect.getProperty(object, f);
var expectedValue:Dynamic = Reflect.field(expectedToMatch, f);
switch (Type.typeof(expectedValue)) {
case TObject:
var r = doesMatch(expectedValue, objectValue, strict, throwException);
if (!r) {
return r;
}
case TClass(Array):
var expectedArray:Array<Dynamic> = expectedValue;
var objectArray:Array<Dynamic> = objectValue;
/*
if (expectedArray.length != objectArray.length) {
if (throwException) {
throw 'array fields are not of the same length ${f} (${objectArray.length} != ${expectedArray.length})';
} else {
trace('array fields are not of the same length ${f} (${objectArray.length} != ${expectedArray.length})');
}
return false;
}
*/
for (i in 0...expectedArray.length) {
var expectedItem = expectedArray[i];
var objectItem = objectArray[i];
var r = false;
if (strict) {
r = doesMatch(expectedItem, objectItem, strict, throwException);
} else {
r = doesContain(expectedItem, objectArray, throwException);
}
if (!r) {
return r;
}
}
case _:
if (objectValue != expectedValue) {
if (throwException) {
throw 'field mismatch on "${f}" (${objectValue} != ${expectedValue})';
} else {
//trace('field mismatch on "${f}" (${objectValue} != ${expectedValue})');
}
return false;
}
}
}

return true;
}

private static function hasField(object:Dynamic, field:String) {
if (Reflect.hasField(object, field)) {
return true;
}

var fields = Type.getInstanceFields(Type.getClass(object));
return fields.contains(field);
}
}
5 changes: 3 additions & 2 deletions tests/TestAll.hx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class TestAll {

databaseBackend = Sys.getEnv("DB_CORE_BACKEND");
if (databaseBackend == null) {
databaseBackend = "sqlite";
databaseBackend = "mysql";
}

trace("DB_CORE_BACKEND: " + databaseBackend);
Expand Down Expand Up @@ -75,7 +75,8 @@ class TestAll {
database: name,
host: Sys.getEnv("MYSQL_HOST"),
user: Sys.getEnv("MYSQL_USER"),
pass: Sys.getEnv("MYSQL_PASS")
pass: Sys.getEnv("MYSQL_PASS"),
port: 3306
});
}
}
Loading

0 comments on commit becbbc7

Please sign in to comment.