[svn-commits] dvossel: branch dvossel/test_api r234333 - /team/dvossel/test_api/main/test.c

SVN commits to the Digium repositories svn-commits at lists.digium.com
Thu Dec 10 15:41:58 CST 2009


Author: dvossel
Date: Thu Dec 10 15:41:55 2009
New Revision: 234333

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=234333
Log:
addition of 'test show results' cli commands

Modified:
    team/dvossel/test_api/main/test.c

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=234333&r1=234332&r2=234333
==============================================================================
--- team/dvossel/test_api/main/test.c (original)
+++ team/dvossel/test_api/main/test.c Thu Dec 10 15:41:55 2009
@@ -43,27 +43,24 @@
 };
 
 static const char *test_result2str[] = {
-	"Not Run",
-	"Pass",
-	"Fail",
+	"NOT RUN",
+	"PASS",
+	"FAIL",
 };
 
 struct ast_test_result {
-	/*! current test result state */
-	enum ast_test_result_state state;
-	/*! optional error buf to describe error results */
-	char error[64];
+	enum ast_test_result_state state; /*! current test result state */
+	char error[64];                   /*! optional error buf to describe error results */
+	int time;                         /*! time in ms test took */
 };
 
 struct ast_test {
-	char *name;
-	char *catagory;
-	char *summary;
-	char *description;
-	ast_test_cb_t *cb;
-
-	/*! stores the last ran result */
-	struct ast_test_result result;
+	char *name;          /*! name of test, unique to catagory */
+	char *catagory;      /*! test catagory */
+	char *summary;       /*! optional short summary of test */
+	char *description;   /*! optional brief detailed description of test */
+	struct ast_test_result result;  /*! stores the last ran result */
+	ast_test_cb_t *cb;   /*! test call back function */
 	AST_LIST_ENTRY(ast_test) entry;
 };
 
@@ -82,120 +79,8 @@
 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);
-
-/* CLI commands */
-static char *test_cli_show_registered(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
-{
-#define FORMAT "%-15s %-20s %-30s %-10s\n"
-	struct ast_test *test = NULL;
-	int count = 0;
-	switch (cmd) {
-	case CLI_INIT:
-		e->command = "test show registered";
-		e->usage =
-			"Usage: test show registered [test catagory] [test name] \n"
-			"       Shows registered tests. Has three modes.\n"
-			"       1. If no arguments are provided, all registered tests will be shown\n"
-			"       2. If [test catagory] is provided only tests which fall within that\n"
-			"          catagory will be shown.\n"
-			"       3. If both [test catagory] and [test name] is provided, only the test\n"
-			"          within [test catagory] matching [test name] will be shown \n";
-		return NULL;
-	case CLI_GENERATE:
-		return NULL;
-	case CLI_HANDLER:
-		if (a->argc < 3 || a->argc > 5) {
-			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 == 3) ||
-				 ((a->argc == 4) && !strcmp(test->catagory, a->argv[3])) ||
-				 ((a->argc == 5) && !strcmp(test->catagory, a->argv[3]) && !strcmp(test->name, a->argv[4]))) {
-
-				ast_cli(a->fd, FORMAT, test->name, test->catagory, test->summary, test_result2str[test->result.state]);
-				count ++;
-			}
-		}
-		AST_LIST_TRAVERSE_SAFE_END;
-		AST_LIST_UNLOCK(&tests);
-		ast_cli(a->fd, "%d Registered Tests Matched\n", count);
-	default:
-		return NULL;
-	}
-
-	return CLI_SUCCESS;
-}
-
-static char *test_cli_execute_registered(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
-{
-	switch (cmd) {
-	case CLI_INIT:
-		e->command = "test execute";
-		e->usage =
-			"Usage: test execute [test catagory] [test name]\n"
-			"       Executes registered tests. Has three modes.\n"
-			"       1. If no arguments are provided, all registered tests will execute\n"
-			"       2. If [test catagory] is provided only tests which fall within that\n"
-			"          catagory will be executed.\n"
-			"       3. If both [test catagory] and [test name] is provided, only the test\n"
-			"          within [test catagory] matching [test name] will execute \n";
-		return NULL;
-	case CLI_GENERATE:
-		return NULL;
-	case CLI_HANDLER:
-
-		if (a->argc == 2) { /* run all registered tests */
-			ast_cli(a->fd, "Running all available tests...\n");
-			ast_test_execute(NULL, NULL);
-		} else if (a->argc == 3) { /* run only tests within a catagory */
-			ast_cli(a->fd, "Running all available tests matching catagory %s\n", a->argv[2]);
-			ast_test_execute(NULL, a->argv[2]);
-		} else if (a->argc == 4) { /* run only a single test matching the catagory and name */
-			ast_cli(a->fd, "Running all available tests matching catagory %s and name %s\n", a->argv[2], a->argv[3]);
-			ast_test_execute(a->argv[3], a->argv[2]);
-		} else {
-			return CLI_SHOWUSAGE;
-		}
-
-		if (!last_results.count) {
-			ast_cli(a->fd, "--- No Tests Found! ---\n");
-		}
-		ast_cli(a->fd, "%d Test(s) Executed  %d Passed  %d Failed\n", last_results.count, last_results.passed, last_results.failed);
-	default:
-		return NULL;
-	}
-
-	return CLI_SUCCESS;
-}
-
-
-static struct ast_cli_entry test_cli[] = {
-	AST_CLI_DEFINE(test_cli_show_registered,           "show registered tests"),
-	AST_CLI_DEFINE(test_cli_execute_registered,        "execute registered tests"),
-};
-
-
-/* Sample Test */
-AST_TEST_DEFINE(sample_test1,
-{
-	return AST_TEST_PASS;
-})
-
-int ast_test_init()
-{
-	/* Register cli commands */
-	ast_cli_register_multiple(test_cli, ARRAY_LEN(test_cli));
-
-	/* Register Sample Tests */
-	AST_TEST_REGISTER("sample_test_1", "main/test", "this is sample test 1", "for example purposes", sample_test1);
-
-	/* Unregister Sample Tests */
-	//AST_TEST_UNREGISTER(sample_test1);
-
-	return 0;
-}
+/*static int test_generate_xml(void);*/
+
 
 int ast_test_register(const char *name, const char *catagory, const char *summary, const char *description, ast_test_cb_t *cb)
 {
@@ -391,4 +276,207 @@
 
 	return test;
 }
+
+/* CLI commands */
+static char *test_cli_show_registered(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
+{
+#define FORMAT "%-15s %-20s %-30s %-10s\n"
+	struct ast_test *test = NULL;
+	int count = 0;
+	switch (cmd) {
+	case CLI_INIT:
+		e->command = "test show registered";
+		e->usage =
+			"Usage: test show registered [test catagory] [test name] \n"
+			"       Shows registered tests. Has three modes.\n"
+			"       1. If no arguments are provided, all registered tests will be shown\n"
+			"       2. If [test catagory] is provided only tests which fall within that\n"
+			"          catagory will be shown.\n"
+			"       3. If both [test catagory] and [test name] is provided, only the test\n"
+			"          within [test catagory] matching [test name] will be shown \n";
+		return NULL;
+	case CLI_GENERATE:
+		return NULL;
+	case CLI_HANDLER:
+		if (a->argc < 3 || a->argc > 5) {
+			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 == 3) ||
+				 ((a->argc == 4) && !strcmp(test->catagory, a->argv[3])) ||
+				 ((a->argc == 5) && !strcmp(test->catagory, a->argv[3]) && !strcmp(test->name, a->argv[4]))) {
+
+				ast_cli(a->fd, FORMAT, test->name, test->catagory, test->summary, test_result2str[test->result.state]);
+				count ++;
+			}
+		}
+		AST_LIST_TRAVERSE_SAFE_END;
+		AST_LIST_UNLOCK(&tests);
+		ast_cli(a->fd, "%d Registered Tests Matched\n", count);
+	default:
+		return NULL;
+	}
+
+	return CLI_SUCCESS;
+}
+
+static char *test_cli_execute_registered(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
+{
+	switch (cmd) {
+	case CLI_INIT:
+		e->command = "test execute";
+		e->usage =
+			"Usage: test execute [test catagory] [test name]\n"
+			"       Executes registered tests. Has three modes.\n"
+			"       1. If no arguments are provided, all registered tests will execute\n"
+			"       2. If [test catagory] is provided only tests which fall within that\n"
+			"          catagory will be executed.\n"
+			"       3. If both [test catagory] and [test name] is provided, only the test\n"
+			"          within [test catagory] matching [test name] will execute \n";
+		return NULL;
+	case CLI_GENERATE:
+		return NULL;
+	case CLI_HANDLER:
+
+		if (a->argc == 2) { /* run all registered tests */
+			ast_cli(a->fd, "Running all available tests...\n");
+			ast_test_execute(NULL, NULL);
+		} else if (a->argc == 3) { /* run only tests within a catagory */
+			ast_cli(a->fd, "Running all available tests matching catagory %s\n", a->argv[2]);
+			ast_test_execute(NULL, a->argv[2]);
+		} else if (a->argc == 4) { /* run only a single test matching the catagory and name */
+			ast_cli(a->fd, "Running all available tests matching catagory %s and name %s\n", a->argv[2], a->argv[3]);
+			ast_test_execute(a->argv[3], a->argv[2]);
+		} else {
+			return CLI_SHOWUSAGE;
+		}
+
+		if (!last_results.count) {
+			ast_cli(a->fd, "--- No Tests Found! ---\n");
+		}
+		ast_cli(a->fd, "%d Test(s) Executed  %d Passed  %d Failed\n", last_results.count, last_results.passed, last_results.failed);
+	default:
+		return NULL;
+	}
+
+	return CLI_SUCCESS;
+}
+
+static char *test_cli_show_results_all(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
+{
+#define FORMAT_RES_ALL "%-10s %-15s %-20s %-30s\n"
+	struct ast_test *test = NULL;
+	switch (cmd) {
+	case CLI_INIT:
+		e->command = "test show results all";
+		e->usage =
+			"Usage: test show results\n"
+			"       Displays detailed test results for every test that has been run.\n";
+		return NULL;
+	case CLI_GENERATE:
+		return NULL;
+	case CLI_HANDLER:
+
+		if (a->argc != 4) {
+			return CLI_SHOWUSAGE;
+		}
+
+		ast_cli(a->fd, FORMAT_RES_ALL, "Result", "Name", "Catagory", "Error Description");
+		AST_LIST_LOCK(&tests);
+		AST_LIST_TRAVERSE_SAFE_BEGIN(&tests, test, entry) {
+			if (test->result.state != AST_TEST_NOT_RUN) {
+				ast_cli(a->fd, FORMAT_RES_ALL,
+					test_result2str[test->result.state],
+					test->name,
+					test->catagory,
+					(test->result.state == AST_TEST_FAIL) ? (ast_strlen_zero(test->result.error) ? "Not Avaliable" : test->result.error) : "");
+			}
+		}
+		AST_LIST_TRAVERSE_SAFE_END;
+		AST_LIST_UNLOCK(&tests);
+
+		if (!last_results.count) {
+			ast_cli(a->fd, "--- No Test Results Found ---\n");
+		}
+		ast_cli(a->fd, "%d Test(s) Executed  %d Passed  %d Failed\n", last_results.count, last_results.passed, last_results.failed);
+	default:
+		return NULL;
+	}
+
+	return CLI_SUCCESS;
+}
+
+static char *test_cli_show_results_failed(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
+{
+#define FORMAT_RES_FAIL "%-10s %-15s %-20s %-30s\n"
+	struct ast_test *test = NULL;
+	switch (cmd) {
+	case CLI_INIT:
+		e->command = "test show results failed";
+		e->usage =
+			"Usage: test show results\n"
+			"       Displays detailed test results for every test that has been run.\n";
+		return NULL;
+	case CLI_GENERATE:
+		return NULL;
+	case CLI_HANDLER:
+
+		if (a->argc != 4) {
+			return CLI_SHOWUSAGE;
+		}
+
+		ast_cli(a->fd, FORMAT_RES_FAIL, "Result", "Name", "Catagory", "Error Description");
+		AST_LIST_LOCK(&tests);
+		AST_LIST_TRAVERSE_SAFE_BEGIN(&tests, test, entry) {
+			if (test->result.state == AST_TEST_FAIL) {
+				ast_cli(a->fd, FORMAT_RES_FAIL,
+					test_result2str[test->result.state],
+					test->name,
+					test->catagory,
+					ast_strlen_zero(test->result.error) ? "Not Avaliable" : test->result.error);
+			}
+		}
+		AST_LIST_TRAVERSE_SAFE_END;
+		AST_LIST_UNLOCK(&tests);
+
+		if (!last_results.failed) {
+			ast_cli(a->fd, "--- No failed test results found---\n");
+		}
+		ast_cli(a->fd, "%d Test(s) Executed  %d Passed  %d Failed\n", last_results.count, last_results.passed, last_results.failed);
+	default:
+		return NULL;
+	}
+
+	return CLI_SUCCESS;
+}
+
+static struct ast_cli_entry test_cli[] = {
+	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_all,          "show last test results"),
+	AST_CLI_DEFINE(test_cli_show_results_failed,       "show failed tests"),
+};
+
+
+/* Sample Test */
+AST_TEST_DEFINE(sample_test1,
+{
+	return AST_TEST_FAIL;
+})
+
+int ast_test_init()
+{
+	/* Register cli commands */
+	ast_cli_register_multiple(test_cli, ARRAY_LEN(test_cli));
+
+	/* Register Sample Tests */
+	AST_TEST_REGISTER("sample_test_1", "main/test", "this is sample test 1", "for example purposes", sample_test1);
+
+	/* Unregister Sample Tests */
+	//AST_TEST_UNREGISTER(sample_test1);
+
+	return 0;
+}
 #endif




More information about the svn-commits mailing list