From: Rudolf Polzer Date: Wed, 12 Jun 2013 11:52:57 +0000 (+0200) Subject: add a simple (yet unused) unit test framework. X-Git-Tag: xonotic-v0.8.0~389^2 X-Git-Url: https://git.rm.cloudns.org/?a=commitdiff_plain;h=df3016ae09ceb806fa1a87d1422f7ed0bfb98331;p=xonotic%2Fxonotic-data.pk3dir.git add a simple (yet unused) unit test framework. Functions starting with _TEST_ are unit tests, shall call TEST_OK() at the end, and TEST_Check(condition) to test conditions. --- diff --git a/qcsrc/client/progs.src b/qcsrc/client/progs.src index 3b8aa1bac..114f0a5b5 100644 --- a/qcsrc/client/progs.src +++ b/qcsrc/client/progs.src @@ -16,6 +16,7 @@ Defs.qc ../common/teams.qh ../common/util.qh +../common/test.qh ../common/counting.qh ../common/items.qh ../common/explosion_equation.qh @@ -99,6 +100,7 @@ prandom.qc bgmscript.qc noise.qc +../common/test.qc ../common/util.qc ../common/notifications.qc ../common/command/markup.qc diff --git a/qcsrc/common/command/generic.qc b/qcsrc/common/command/generic.qc index 3059916c6..143f3bd78 100644 --- a/qcsrc/common/command/generic.qc +++ b/qcsrc/common/command/generic.qc @@ -525,6 +525,32 @@ void GenericCommand_settemp_restore(float request, float argc) } } +void GenericCommand_runtest(float request, float argc) +{ + switch(request) + { + case CMD_REQUEST_COMMAND: + { + if(argc > 1) + { + float i; + for(i = 1; i < argc; ++i) + TEST_Run(argv(i)); + } + else + TEST_RunAll(); + return; + } + + default: + case CMD_REQUEST_USAGE: + { + print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " [function to run]")); + return; + } + } +} + /* use this when creating a new command, making sure to place it in alphabetical order... also, ** ADD ALL NEW COMMANDS TO commands.cfg WITH PROPER ALIASES IN THE SAME FASHION! void GenericCommand_(float request) @@ -565,6 +591,7 @@ void GenericCommand_(float request) GENERIC_COMMAND("rpn", GenericCommand_rpn(request, arguments, command), "RPN calculator") \ GENERIC_COMMAND("settemp", GenericCommand_settemp(request, arguments), "Temporarily set a value to a cvar which is restored later") \ GENERIC_COMMAND("settemp_restore", GenericCommand_settemp_restore(request, arguments), "Restore all cvars set by settemp command") \ + GENERIC_COMMAND("runtest", GenericCommand_runtest(request, arguments), "Run unit tests") \ /* nothing */ void GenericCommand_macro_help() diff --git a/qcsrc/common/test.qc b/qcsrc/common/test.qc new file mode 100644 index 000000000..15193fd41 --- /dev/null +++ b/qcsrc/common/test.qc @@ -0,0 +1,56 @@ +float TEST_failed; +float TEST_ok; + +void TEST_Fail(string cond) +{ + print(sprintf("Assertion failed: ", cond)); + //backtrace(); + ++TEST_failed; +} + +void TEST_OK() +{ + TEST_ok = TRUE; +} + +float TEST_RunAll() +{ + float f = 0; + float n = numentityfields(); + float i; + for(i = 0; i < n; ++i) + { + string name = entityfieldname(i); + if(substring(name, 0, 6) == "_TEST_") + if(!TEST_Run(substring(name, 6, -1))) + ++f; + } + if(f) + { + print(sprintf("%d tests failed\n", f)); + return 1; + } + else + { + print(sprintf("All tests OK\n", f)); + return 0; + } +} + +float TEST_Run(string s) +{ + print(sprintf("%s: testing...\n", s)); + TEST_failed = TEST_ok = 0; + callfunction(strcat("_TEST_", s)); + if(TEST_failed > 0) + { + print(sprintf("%s: %d items failed.\n", s, TEST_failed)); + return 0; + } + else if(!TEST_ok) + { + print(sprintf("%s: did not complete.\n", s)); + return 0; + } + return 1; +} diff --git a/qcsrc/common/test.qh b/qcsrc/common/test.qh new file mode 100644 index 000000000..ff442cef9 --- /dev/null +++ b/qcsrc/common/test.qh @@ -0,0 +1,7 @@ +#define TEST_Check(cond) do { if(!(cond)) TEST_Fail(#cond); } while(0) + +void TEST_OK(); +void TEST_Fail(string cond); + +float TEST_RunAll(); +float TEST_Run(string test); diff --git a/qcsrc/menu/progs.src b/qcsrc/menu/progs.src index 36905e17a..3036278c1 100644 --- a/qcsrc/menu/progs.src +++ b/qcsrc/menu/progs.src @@ -10,6 +10,7 @@ config.qh ../warpzonelib/mathlib.qh ../common/util.qh +../common/test.qh oo/base.h @@ -37,6 +38,7 @@ oo/implementation.h classes.c ../common/util.qc +../common/test.qc ../common/command/markup.qc ../common/command/rpn.qc ../common/command/generic.qc diff --git a/qcsrc/server/progs.src b/qcsrc/server/progs.src index df5623f05..367e8609d 100644 --- a/qcsrc/server/progs.src +++ b/qcsrc/server/progs.src @@ -15,6 +15,7 @@ sys-post.qh ../common/constants.qh ../common/teams.qh ../common/util.qh +../common/test.qh ../common/counting.qh ../common/items.qh ../common/explosion_equation.qh @@ -256,6 +257,7 @@ mutators/mutator_touchexplode.qc ../warpzonelib/server.qc ../common/animdecide.qc +../common/test.qc ../common/util.qc ../common/notifications.qc