]> git.rm.cloudns.org Git - xonotic/xonotic-data.pk3dir.git/commitdiff
add a simple (yet unused) unit test framework.
authorRudolf Polzer <divVerent@xonotic.org>
Wed, 12 Jun 2013 11:52:57 +0000 (13:52 +0200)
committerRudolf Polzer <divVerent@xonotic.org>
Wed, 12 Jun 2013 11:52:57 +0000 (13:52 +0200)
Functions starting with _TEST_ are unit tests, shall call TEST_OK() at the end, and TEST_Check(condition) to test conditions.

qcsrc/client/progs.src
qcsrc/common/command/generic.qc
qcsrc/common/test.qc [new file with mode: 0644]
qcsrc/common/test.qh [new file with mode: 0644]
qcsrc/menu/progs.src
qcsrc/server/progs.src

index 3b8aa1bace9adc8970b782b4388416bda44dadc5..114f0a5b5c77fdd666db5ae4953b653fa40302e5 100644 (file)
@@ -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
index 3059916c61ba4445c9b0da907b187d36a817adc4..143f3bd78c03591919a40be9aebe3d90d479f172 100644 (file)
@@ -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 (file)
index 0000000..15193fd
--- /dev/null
@@ -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 (file)
index 0000000..ff442ce
--- /dev/null
@@ -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);
index 36905e17ad5be250f3fd0838cb4ed118a8850928..3036278c1fcfbe09ed31da9f56795e2be7f8dbe6 100644 (file)
@@ -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
index df5623f0535a42bd3c5b1a4b04baa0b516aa57b8..367e8609ded2741ab10a74503dbf7888a82bd1a6 100644 (file)
@@ -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