[asterisk-commits] dvossel: branch dvossel/test_api r234209 - in /team/dvossel/test_api: include...
SVN commits to the Asterisk project
asterisk-commits at lists.digium.com
Thu Dec 10 12:30:42 CST 2009
Author: dvossel
Date: Thu Dec 10 12:30:39 2009
New Revision: 234209
URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=234209
Log:
update to ast_test_execute(). includes new cli 'test execute' command
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=234209&r1=234208&r2=234209
==============================================================================
--- team/dvossel/test_api/include/asterisk/test.h (original)
+++ team/dvossel/test_api/include/asterisk/test.h Thu Dec 10 12:30:39 2009
@@ -28,7 +28,7 @@
/* Macros used for for test API */
#ifdef AST_TEST_FRAMEWORK
-#define AST_TEST_DEFINE(hdr, body) hdr body
+#define AST_TEST_DEFINE(hdr, body) int hdr(char *errbuf, int len); int hdr(char *errbuf, int len) body
#define AST_TEST_REGISTER(name, cat, sum, des, cb) ast_test_register(name, cat, sum, des, cb)
#define AST_TEST_UNREGISTER(cb) ast_test_unregister(cb)
#else /* else no-op */
@@ -39,6 +39,8 @@
#ifdef AST_TEST_FRAMEWORK
+
+#define AST_TEST_EXECUTE(test) test->result.state = test->cb(test->result.error, ARRAY_LEN(test->result.error))
/*!
* \brief Generic test callback function
@@ -67,7 +69,7 @@
* \param name of test to run (optional)
* \param test catagory to run (optional)
*
- * \return 0 if test(s) ran, 1 if error finding registered test(s)
+ * \return number of tests executed.
*
* \note This function has three modes of operation
* 1. When given a name and catagory that individual test will execute if found.
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=234209&r1=234208&r2=234209
==============================================================================
--- team/dvossel/test_api/main/test.c (original)
+++ team/dvossel/test_api/main/test.c Thu Dec 10 12:30:39 2009
@@ -33,10 +33,17 @@
#include "asterisk/utils.h"
#include "asterisk/cli.h"
+#ifdef AST_TEST_FRAMEWORK
enum ast_test_result_state {
AST_TEST_NOT_RUN = 0,
AST_TEST_PASS = 1,
AST_TEST_FAIL = 2,
+};
+
+static const char *test_result2str[] = {
+ "Not Run",
+ "Pass",
+ "Fail",
};
struct ast_test_result {
@@ -58,7 +65,12 @@
AST_LIST_ENTRY(ast_test) entry;
};
-#ifdef AST_TEST_FRAMEWORK
+static struct ast_test_execute_results {
+ int count; /* number of tests run */
+ int passed; /* number of tests passed */
+ int failed; /* number of tests failed */
+} last_results = { 0 };
+
/*! List of registered test definitions */
static AST_LIST_HEAD_STATIC(tests, ast_test);
#endif
@@ -70,44 +82,81 @@
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 *cur = NULL;
int count = 0;
switch (cmd) {
case CLI_INIT:
- e->command = "tests show registered";
+ e->command = "test show registered";
e->usage =
- "Usage: iax2 show registered\n"
+ "Usage: test show registered\n"
" Shows current registered tests\n";
return NULL;
case CLI_GENERATE:
return NULL;
case CLI_HANDLER:
+ ast_cli(a->fd, FORMAT, "Name", "Catagory", "Summary", "Test Result");
AST_LIST_LOCK(&tests);
AST_LIST_TRAVERSE_SAFE_BEGIN(&tests, cur, entry) {
- ast_cli(a->fd, "Name %s Catagory %s \n", cur->name, cur->catagory);
+ ast_cli(a->fd, FORMAT, cur->name, cur->catagory, cur->summary, test_result2str[cur->result.state]);
count ++;
}
AST_LIST_TRAVERSE_SAFE_END;
AST_LIST_UNLOCK(&tests);
- ast_cli(a->fd, "%d Tests Registered\n", count);
+ ast_cli(a->fd, "%d Test(s) Registered\n", count);
default:
return NULL;
}
}
-/* CLI commands */
+static char *test_cli_execute_registered(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
+{
+//todohere add additional options for tests
+ switch (cmd) {
+ case CLI_INIT:
+ e->command = "test execute";
+ e->usage =
+ "Usage: test execute\n"
+ " Executes registered tests\n";
+ return NULL;
+ case CLI_GENERATE:
+ return NULL;
+ case CLI_HANDLER:
+ ast_test_execute(NULL, NULL);
+ 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;
+ }
+}
+
+
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;
}
@@ -152,8 +201,42 @@
int ast_test_execute(const char *name, const char *catagory)
{
- //todohere
- return 0;
+ struct ast_test *test = NULL;
+ int mode = 0; /* 3 modes, 0 = run all, 1 = only by catagory, 2 = only by name and catagory */
+ int execute = 0;
+
+ last_results.count = last_results.passed = last_results.failed = 0;
+
+ if (!ast_strlen_zero(catagory) && !ast_strlen_zero(name)) {
+ mode = 1;
+ } else if (!ast_strlen_zero(catagory)) {
+ mode = 2;
+ }
+
+ AST_LIST_LOCK(&tests);
+ AST_LIST_TRAVERSE_SAFE_BEGIN(&tests, test, entry) {
+ execute = 0;
+ if ((!mode) ||
+ ((mode == 1) && !(strcmp(test->catagory, catagory))) ||
+ ((mode == 2) && !(strcmp(test->catagory, catagory)) && !(strcmp(test->name, name)))) {
+
+ execute = 1;
+ }
+
+ if (execute) {
+ AST_TEST_EXECUTE(test);
+ last_results.count++;
+ if (test->result.state == AST_TEST_PASS) {
+ last_results.passed++;
+ } else {
+ last_results.failed++;
+ }
+ }
+ }
+ AST_LIST_TRAVERSE_SAFE_END;
+ AST_LIST_UNLOCK(&tests);
+
+ return last_results.count;
}
int ast_test_results(const char *name, const char *catagory, const char *xml_path)
@@ -173,6 +256,7 @@
struct ast_test *cur = NULL;
int res = 0;
int i = 0;
+ int inserted = 0;
/* This is a slow operation that may need to be optimized in the future
* as the test framework expands. At the moment we are doing string
@@ -181,10 +265,12 @@
AST_LIST_TRAVERSE_SAFE_BEGIN(&tests, cur, entry) {
if ((i = strcmp(test->catagory, cur->catagory)) < 0) {
AST_LIST_INSERT_BEFORE_CURRENT(test, entry);
+ inserted = 1;
break;
} else if (!i) { /* same catagory, now insert by name within that catagory*/
if ((i = strcmp(test->name, cur->name)) < 0) {
AST_LIST_INSERT_BEFORE_CURRENT(test, entry);
+ inserted = 1;
break;
} else if (!i) {
/* Error, duplicate found */
@@ -194,6 +280,12 @@
}
}
AST_LIST_TRAVERSE_SAFE_END;
+
+ if (!inserted && !res) {
+ AST_LIST_INSERT_TAIL(&tests, test, entry);
+ inserted = 1;
+ }
+
AST_LIST_UNLOCK(&tests);
return res;
More information about the asterisk-commits
mailing list