From ccc2a5cf31b95f610ddba9480fb7acdf6b112aee Mon Sep 17 00:00:00 2001 From: "Tristan B. Velloza Kildaire" Date: Thu, 29 Dec 2022 15:55:08 +0200 Subject: [PATCH] Make testing prints and pragmas only enabled under `-ddbg` flag --- README.md | 10 ++++++++- source/libpb.d | 55 ++++++++++++++++++++++++++++++++++++++------------ 2 files changed, 51 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 19c3c3a..999554f 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,6 @@ libpb ## Example usage - ### Server initiation Firstly we create a new PocketBase instance to manage our server: @@ -84,6 +83,15 @@ This is to show off deserialization method `fromJSON(RecordType)(JSONValue jsonI //TODO: object test case, list test case ``` +## Development + +### Unit tests + +To run tests you will want to enable the `pragma`s and `writeln`s. therefore pass the `dbg` flag in as such: + +```bash +dub test -ddbg +``` ## License diff --git a/source/libpb.d b/source/libpb.d index 7f77244..917b1fa 100644 --- a/source/libpb.d +++ b/source/libpb.d @@ -119,14 +119,20 @@ public class PocketBase static foreach(cnt; 0..structTypes.length) { - pragma(msg, structTypes[cnt]); - pragma(msg, structNames[cnt]); - // pragma(msg, structValues[cnt]); + debug(dbg) + { + pragma(msg, structTypes[cnt]); + pragma(msg, structNames[cnt]); + // pragma(msg, structValues[cnt]); + } builtJSON[structNames[cnt]] = structValues[cnt]; } - - writeln(builtJSON.toPrettyString()); + + debug(dbg) + { + writeln(builtJSON.toPrettyString()); + } return builtJSON; } @@ -145,9 +151,12 @@ public class PocketBase static foreach(cnt; 0..structTypes.length) { - pragma(msg, structTypes[cnt]); - pragma(msg, structNames[cnt]); - // pragma(msg, structValues[cnt]); + debug(dbg) + { + pragma(msg, structTypes[cnt]); + pragma(msg, structNames[cnt]); + // pragma(msg, structValues[cnt]); + } //TODO: Add all integral types static if(__traits(isSame, mixin(structTypes[cnt]), int)) @@ -169,23 +178,39 @@ public class PocketBase static if(__traits(isSame, mixin(structTypes[cnt]), string)) { mixin("record."~structNames[cnt]) = jsonIn[structNames[cnt]].str(); - pragma(msg,"record."~structNames[cnt]); + + debug(dbg) + { + pragma(msg,"record."~structNames[cnt]); + } } static if(__traits(isSame, mixin(structTypes[cnt]), JSONValue)) { mixin("record."~structNames[cnt]) = jsonIn[structNames[cnt]]; - pragma(msg,"record."~structNames[cnt]); + + debug(dbg) + { + pragma(msg,"record."~structNames[cnt]); + } } static if(__traits(isSame, mixin(structTypes[cnt]), bool)) { mixin("record."~structNames[cnt]) = jsonIn[structNames[cnt]].boolean(); - pragma(msg,"record."~structNames[cnt]); + + debug(dbg) + { + pragma(msg,"record."~structNames[cnt]); + } } //FIXME: Not sure how to get array support going, very new to meta programming static if(__traits(isSame, mixin(structTypes[cnt]), mixin(structTypes[cnt])[])) { mixin("record."~structNames[cnt]) = jsonIn[structNames[cnt]].boolean(); - pragma(msg,"record."~structNames[cnt]); + + debug(dbg) + { + pragma(msg,"record."~structNames[cnt]); + } } } @@ -252,7 +277,11 @@ unittest Person person = PocketBase.fromJSON!(Person)(json); - writeln(person); + debug(dbg) + { + writeln(person); + } + assert(cmp(person.firstname, "Tristan") == 0); assert(cmp(person.lastname, "Kildaire") == 0);