]> granicus.if.org Git - postgis/commitdiff
Allow cu_tester to accept parameters to run individual tests or suites, from Jeff...
authorPaul Ramsey <pramsey@cleverelephant.ca>
Tue, 23 Feb 2010 19:51:41 +0000 (19:51 +0000)
committerPaul Ramsey <pramsey@cleverelephant.ca>
Tue, 23 Feb 2010 19:51:41 +0000 (19:51 +0000)
git-svn-id: http://svn.osgeo.org/postgis/trunk@5323 b70326c6-7e19-0410-871a-916f4a2858ee

liblwgeom/cunit/cu_tester.c

index 4c65e22b8086115be5882e3cc8104353dcf1c93a..b0de2f78fc56a45afb2f56452a2843d47f1d0890 100644 (file)
@@ -12,6 +12,7 @@
 
 #include <stdio.h>
 #include <string.h>
+#include <dlfcn.h>
 #include "CUnit/Basic.h"
 #include "liblwgeom.h"
 #include "cu_tester.h"
@@ -46,6 +47,26 @@ cu_error_msg_reset()
        memset(cu_error_msg, '\0', MAX_CUNIT_ERROR_LENGTH);
 }
 
+/*CU_named_suite *
+cu_create_suite(const char *name, CU_InitializeFunc init_func, CU_CleanupFunc clean_func) {
+       CU_pSuite pSuite = CU_add_suite(name, init_func, clean_func);
+       if (NULL == pSuite)
+       {
+               CU_cleanup_registry();
+               return NULL;
+       }
+       num_suites++;
+}
+void cu_add_named_test(CU_named_suite *nSuite, const char *name, CU_TestFunc test_func) {
+       strcpy(nSuite->tests[nSuite->num_tests].name, name);
+       nSuite->tests[nSuite->num_tests].test_func = CU_add_test(nSuite->cu_suite, name, test_func);
+       if (NULL == nSuite->tests[nSuite->num_tests].test_func) {
+               CU_cleanup_registry();
+               return NULL;
+       }
+       nSuite->num_tests++;
+}*/
+       
 /*
 ** Set up liblwgeom to run in stand-alone mode using the
 ** usual system memory handling functions.
@@ -64,8 +85,18 @@ void lwgeom_init_allocators(void)
 ** Returns a CUE_SUCCESS on successful running, another
 ** CUnit error code on failure.
 */
-int main()
+int main(int argc, char *argv[])
 {
+       int index;
+       char *suite_name;
+       CU_pSuite suite_to_run;
+       char *test_name;
+       CU_pTest test_to_run;
+       void *dl_handle;
+       CU_ErrorCode errCode;
+       CU_pTestRegistry registry;
+       int num_run;
+       int num_failed;
 
        /* initialize the CUnit test registry */
        if (CUE_SUCCESS != CU_initialize_registry())
@@ -150,8 +181,75 @@ int main()
 
        /* Run all tests using the CUnit Basic interface */
        CU_basic_set_mode(CU_BRM_VERBOSE);
-       CU_basic_run_tests();
+       if (argc <= 1) {
+               CU_basic_run_tests();
+       } else {
+               /* NOTE: The cunit functions used here (CU_get_registry, CU_get_suite_by_name, and CU_get_test_by_name) are
+                *       listed with the following warning: "Internal CUnit system functions.  Should not be routinely called by users."
+                *       However, there didn't seem to be any other way to get tests by name, so we're calling them. */
+               registry = CU_get_registry();
+               for (index = 1; index < argc; index++) {
+                       suite_name = argv[index];
+                       test_name = NULL;
+                       suite_to_run = CU_get_suite_by_name(suite_name, registry);
+                       if (NULL == suite_to_run) {
+                               /* See if it's a test name instead of a suite name. */
+                               suite_to_run = registry->pSuite;
+                               while (suite_to_run != NULL) {
+                                       test_to_run = CU_get_test_by_name(suite_name, suite_to_run);
+                                       if (test_to_run != NULL) {
+                                               /* It was a test name. */
+                                               test_name = suite_name;
+                                               suite_name = suite_to_run->pName;
+                                               break;
+                                       }
+                                       suite_to_run = suite_to_run->pNext;
+                               }
+                       }
+                       if (suite_to_run == NULL) {
+                               printf("\n'%s' does not appear to be either a suite name or a test name.\n\n", suite_name);
+                       } else {
+                               if (test_name != NULL) {
+                                       /* Run only this test. */
+                                       printf("\nRunning test '%s' in suite '%s'.\n", test_name, suite_name);
+                                       /* This should be CU_basic_run_test, but that method is broken, see:
+                                        *     https://sourceforge.net/tracker/?func=detail&aid=2851925&group_id=32992&atid=407088
+                                        * This one doesn't output anything for success, so we have to do it manually. */
+                                       errCode = CU_run_test(suite_to_run, test_to_run);
+                                       if (errCode != CUE_SUCCESS) {
+                                               printf("    Error attempting to run tests: %d.  See CUError.h for error code list.\n", errCode);
+                                       } else {
+                                               num_run = CU_get_number_of_asserts();
+                                               num_failed = CU_get_number_of_failures();
+                                               printf("\n    %s - asserts - %3d passed, %3d failed, %3d total.\n\n",
+                                                       (0 == num_failed ? "PASSED" : "FAILED"), (num_run - num_failed), num_failed, num_run);
+                                       }
+                               } else {
+                                       /* Run all the tests in the suite. */
+                                       printf("\nRunning all tests in suite '%s'.\n", suite_name);
+                                       /* This should be CU_basic_run_suite, but that method is broken, see:
+                                        *     https://sourceforge.net/tracker/?func=detail&aid=2851925&group_id=32992&atid=407088
+                                        * This one doesn't output anything for success, so we have to do it manually. */
+                                       errCode = CU_run_suite(suite_to_run);
+                                       if (errCode != CUE_SUCCESS) {
+                                               printf("    Error attempting to run tests: %d.  See CUError.h for error code list.\n", errCode);
+                                       } else {
+                                               num_run = CU_get_number_of_tests_run();
+                                               num_failed = CU_get_number_of_tests_failed();
+                                               printf("\n    %s -   tests - %3d passed, %3d failed, %3d total.\n",
+                                                       (0 == num_failed ? "PASSED" : "FAILED"), (num_run - num_failed), num_failed, num_run);
+                                               num_run = CU_get_number_of_asserts();
+                                               num_failed = CU_get_number_of_failures();
+                                               printf("           - asserts - %3d passed, %3d failed, %3d total.\n\n",
+                                                       (num_run - num_failed), num_failed, num_run);
+                                       }
+                               }
+                       }
+               }
+               /* Presumably if the CU_basic_run_[test|suite] functions worked, we wouldn't have to do this. */
+               CU_basic_show_failures(CU_get_failure_list());
+               printf("\n\n"); /* basic_show_failures leaves off line breaks. */
+       }
        CU_cleanup_registry();
-
        return CU_get_error();
 }