[svn-commits] dvossel: branch dvossel/test_api r235217 - in /team/dvossel/test_api: include...

SVN commits to the Digium repositories svn-commits at lists.digium.com
Tue Dec 15 16:13:41 CST 2009


Author: dvossel
Date: Tue Dec 15 16:13:39 2009
New Revision: 235217

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=235217
Log:
additional documentation, addition of test_cat_cmp function

Modified:
    team/dvossel/test_api/include/asterisk/test.h
    team/dvossel/test_api/main/test.c

Modified: team/dvossel/test_api/include/asterisk/test.h
URL: http://svnview.digium.com/svn/asterisk/team/dvossel/test_api/include/asterisk/test.h?view=diff&rev=235217&r1=235216&r2=235217
==============================================================================
--- team/dvossel/test_api/include/asterisk/test.h (original)
+++ team/dvossel/test_api/include/asterisk/test.h Tue Dec 15 16:13:39 2009
@@ -28,12 +28,22 @@
    using the AST_TEST_DEFINE macro.
 
 AST_TEST_DEFINE(sample_test_cb, \\The first argument is the name of the callback function
-{                               \\* The second argument is the function's body
+{                               \\The second argument is the function's body *
 	\test code
 	.
 	.
 	.
-	return res ? AST_RESULT_FAIL : AST_RESULT_PASS;
+
+	                            \\ the following is just some example logic
+	
+	if (fail) {
+		ast_str_set(error_str, 0 , "an error occured because....");
+		res = AST_RESULT_FAIL;
+	} else {
+		res = AST_RESULT_PASS
+	}
+                                \\ result must be of type enum ast_test_result_state
+	return res;
 })
 
 Every callback function is passed a string buffer which allows

Modified: team/dvossel/test_api/main/test.c
URL: http://svnview.digium.com/svn/asterisk/team/dvossel/test_api/main/test.c?view=diff&rev=235217&r1=235216&r2=235217
==============================================================================
--- team/dvossel/test_api/main/test.c (original)
+++ team/dvossel/test_api/main/test.c Tue Dec 15 16:13:39 2009
@@ -22,7 +22,6 @@
  *
  * \author David Vossel <dvossel at digium.com>
  */
-
 
 #include "asterisk.h"
 
@@ -41,18 +40,21 @@
 #define DEFAULT_XML_PATH "asterisk_test_result.xml"
 #define DEFAULT_TXT_PATH "asterisk_test_result.txt"
 
+/*! This array corrisponds to the values defined in the ast_test_result_state enum */
 static const char *test_result2str[] = {
 	"NOT RUN",
 	"PASS",
 	"FAIL",
 };
 
+/*! represents all the test result data for a single ast_test object */
 struct ast_test_result {
 	enum ast_test_result_state state; /*! current test result state */
 	struct ast_str *error;            /*! optional error str to describe error result */
 	int time;                         /*! time in ms test took */
 };
 
+/*! holds all the information pertaining to a single defined test */
 struct ast_test {
 	char *name;          /*! name of test, unique to catagory */
 	char *catagory;      /*! test catagory */
@@ -63,6 +65,7 @@
 	AST_LIST_ENTRY(ast_test) entry;
 };
 
+/*! global structure containing both total and last test execution results */
 static struct ast_test_execute_results {
 	int total_tests;  /* total number of tests, reguardless if they have been executed or not */
 	int total_passed; /* total number of executed tests passed */
@@ -81,6 +84,7 @@
 static struct ast_test *test_free(struct ast_test *test);
 static int test_insert(struct ast_test *test);
 static struct ast_test *test_remove(ast_test_cb_t *cb);
+static int test_cat_cmp(const char *cat1, const char *cat2);
 
 int ast_test_register(const char *name, const char *catagory, const char *summary, const char *description, ast_test_cb_t *cb)
 {
@@ -120,7 +124,12 @@
 	return 0;
 }
 
-/*! \brief executes a single test, storing the results in the test->result structure. */
+/*! 
+ * \brief executes a single test, storing the results in the test->result structure.
+ *
+ * \note The last_results structure which contains global statistics about test execution
+ * must be updated when using this function. See use in ast_test_execute().
+ */
 static void __test_execute(struct ast_test *test)
 {
 	struct timeval begin;
@@ -129,7 +138,7 @@
 	ast_str_reset(test->result.error);
 	/* get start time */
 	begin = ast_tvnow();
-	/* the callback gets the pointer to the pointer of our error buf */
+	/* the callback gets the pointer to the pointer of the error buf */
 	test->result.state = test->cb(&test->result.error);
 	/* record the total time the test took */
 	test->result.time = ast_tvdiff_ms(ast_tvnow(), begin);
@@ -165,7 +174,6 @@
 	fprintf(f,   "Result:            %s\n", test_result2str[test->result.state]);
 	fprintf(f,   "Error Description: %s\n", S_OR(ast_str_buffer(test->result.error), "NA"));
 	fprintf(f,   "Time:              %d\n", test->result.time);
-
 }
 
 int ast_test_execute(const char *name, const char *catagory, struct ast_cli_args *a)
@@ -176,18 +184,22 @@
 	int execute = 0;
 	int res = 0;
 
+	if (!ast_strlen_zero(catagory)) {
+		if (!ast_strlen_zero(name)) {
+			mode = 2;
+		} else {
+			mode = 1;
+		}
+	}
+
 	AST_LIST_LOCK(&tests);
 	/* clear previous execution results */
 	memset(&last_results, 0, sizeof(last_results));
-	if (!ast_strlen_zero(catagory) && ast_strlen_zero(name)) {
-		mode = 1;
-	} else if (!ast_strlen_zero(catagory) && !ast_strlen_zero(name)) {
-		mode = 2;
-	}
 	AST_LIST_TRAVERSE_SAFE_BEGIN(&tests, test, entry) {
+
 		execute = 0;
 		if ((!mode) ||
-			((mode == 1) && !(strcmp(test->catagory, catagory))) ||
+			((mode == 1) && !(test_cat_cmp(test->catagory, catagory))) ||
 			((mode == 2) && !(strcmp(test->catagory, catagory)) && !(strcmp(test->name, name)))) {
 
 			execute = 1;
@@ -198,6 +210,7 @@
 				ast_cli(a->fd, "START  %s/%s \n", test->catagory, test->name);
 			}
 
+			/* execute the test and save results */
 			__test_execute(test);
 
 			/* update execution specific counts here */
@@ -209,12 +222,22 @@
 			}
 
 			if (a) {
-				term_color(result_buf, test_result2str[test->result.state], (test->result.state == AST_TEST_FAIL) ? COLOR_RED : COLOR_GREEN, 0, sizeof(result_buf));
-				ast_cli(a->fd, "END    %s/%s Time: %dms Result: %s %s\n", test->catagory, test->name, test->result.time, result_buf, ast_str_buffer(test->result.error));
+				term_color(result_buf,
+					test_result2str[test->result.state],
+					(test->result.state == AST_TEST_FAIL) ? COLOR_RED : COLOR_GREEN,
+					0,
+					sizeof(result_buf));
+				ast_cli(a->fd, "END    %s/%s Time: %dms Result: %s %s\n",
+					test->catagory,
+					test->name,
+					test->result.time,
+					result_buf,
+					ast_str_buffer(test->result.error));
 			}
 		}
 
-		/* update total counts as well during this iteration */
+		/* update total counts as well during this iteration
+		 * even if the current test did not execute this time */
 		last_results.total_time += test->result.time;
 		last_results.total_tests++;
 		if (test->result.state != AST_TEST_NOT_RUN) {
@@ -402,6 +425,31 @@
 }
 
 /*!
+ * \brief compares two test catagories to determine if cat1 resides in cat2
+ *
+ * \return 0 if true
+ */
+
+static int test_cat_cmp(const char *cat1, const char *cat2)
+{
+	int len1 = 0;
+	int len2 = 0;
+
+	if (!cat1 || !cat2) {
+		return -1;
+	}
+
+	len1 = strlen(cat1);
+	len2 = strlen(cat2);
+
+	if (len2 > len1) {
+		return -1;
+	}
+
+	return strncmp(cat1, cat2, len2) ? 1 : 0;
+}
+
+/*!
  * \brief allocates an ast_test object.
  */
 static struct ast_test *test_alloc(const char *name, const char *catagory, const char *summary, const char *description, ast_test_cb_t *cb)
@@ -439,7 +487,7 @@
 		e->command = "test show registered";
 
 		e->usage =
-			"Usage: test execute can be used in three ways.\n"
+			"Usage: 'test show registered' can be used in three ways.\n"
 			"       1. 'test show registered all' shows all registered tests\n"
 			"       2. 'test show registered catagory [test catagory]' shows all tests in the given\n"
 			"          catagory.\n"
@@ -455,14 +503,16 @@
 		}
 		return NULL;
 	case CLI_HANDLER:
-		if ((a->argc < 4) || ((a->argc == 4) && strcmp(a->argv[3], "all")) || (a->argc > 7)) {
+		if ((a->argc < 4) || (a->argc == 6) || (a->argc > 7) ||
+			((a->argc == 4) && strcmp(a->argv[3], "all")) ||
+			((a->argc == 7) && strcmp(a->argv[5], "name"))) {
 			return CLI_SHOWUSAGE;
 		}
 		ast_cli(a->fd, FORMAT, "Name", "Catagory", "Summary", "Test Result");
 		AST_LIST_LOCK(&tests);
 		AST_LIST_TRAVERSE_SAFE_BEGIN(&tests, test, entry) {
 			if ((a->argc == 4) ||
-				 ((a->argc == 5) && !strcmp(test->catagory, a->argv[4])) ||
+				 ((a->argc == 5) && !test_cat_cmp(test->catagory, a->argv[4])) ||
 				 ((a->argc == 7) && !strcmp(test->catagory, a->argv[4]) && !strcmp(test->name, a->argv[6]))) {
 
 				ast_cli(a->fd, FORMAT, test->name, test->catagory, test->summary, test_result2str[test->result.state]);
@@ -601,7 +651,6 @@
 	default:
 		return NULL;
 	}
-
 	return CLI_SUCCESS;
 }
 
@@ -614,7 +663,7 @@
 	case CLI_INIT:
 		e->command = "test generate results";
 		e->usage =
-			"Usage: test generate results\n"
+			"Usage: 'test generate results'\n"
 			"       Generates test results in either xml or txt format. An optional \n"
 			"       file path may be provided to specify the location of the xml or\n"
 			"       txt file\n"
@@ -657,7 +706,7 @@
 	AST_CLI_DEFINE(test_cli_show_registered,           "show registered tests"),
 	AST_CLI_DEFINE(test_cli_execute_registered,        "execute registered tests"),
 	AST_CLI_DEFINE(test_cli_show_results,              "show last test results"),
-	AST_CLI_DEFINE(test_cli_generate_results,          "generate test results"),
+	AST_CLI_DEFINE(test_cli_generate_results,          "generate test results to file"),
 };
 
 int ast_test_init()
@@ -665,6 +714,9 @@
 	/* Register cli commands */
 	ast_cli_register_multiple(test_cli, ARRAY_LEN(test_cli));
 
+	/* in the future this function could be used to register functions not
+	 * defined within a module */
+
 	return 0;
 }
 #endif




More information about the svn-commits mailing list