[asterisk-commits] dvossel: branch dvossel/test_api r234374 - /team/dvossel/test_api/main/test.c
SVN commits to the Asterisk project
asterisk-commits at lists.digium.com
Fri Dec 11 10:39:22 CST 2009
Author: dvossel
Date: Fri Dec 11 10:39:18 2009
New Revision: 234374
URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=234374
Log:
added color to cli results. red for FAIL, green for PASS
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=234374&r1=234373&r2=234374
==============================================================================
--- team/dvossel/test_api/main/test.c (original)
+++ team/dvossel/test_api/main/test.c Fri Dec 11 10:39:18 2009
@@ -35,6 +35,7 @@
#include "asterisk/linkedlists.h"
#include "asterisk/utils.h"
#include "asterisk/cli.h"
+#include "asterisk/term.h"
enum ast_test_result_state {
AST_TEST_NOT_RUN = 0,
@@ -132,6 +133,7 @@
int ast_test_execute(const char *name, const char *catagory, struct ast_cli_args *a)
{
+ char result_buf[32] = { 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;
@@ -169,7 +171,8 @@
}
if (a) {
- ast_cli(a->fd, "END %s/%s Time: %dms Result: %s %s\n", test->catagory, test->name, test->result.time, test_result2str[test->result.state], 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, test->result.error);
}
}
@@ -402,45 +405,62 @@
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"
+static char *test_cli_show_results(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
+{
+#define FORMAT_RES_ALL "%s%s %-15s %-20s %-30s\n"
+ static const char * const option1[] = { "all", "failed", "passed", NULL };
+ char result_buf[32] = { 0 };
struct ast_test *test = NULL;
int failed = 0;
int passed = 0;
+ int mode; /* 0 for show all, 1 for show fail, 2 for show passed */
+
switch (cmd) {
case CLI_INIT:
- e->command = "test show results all";
+ e->command = "test show results";
e->usage =
"Usage: test show results\n"
- " Displays detailed test results for every test that has been run.\n";
+ " Displays test results for tests that has been run.\n";
return NULL;
case CLI_GENERATE:
+ if (a->pos == 3) {
+ return ast_cli_complete(a->word, option1, a->n);
+ }
return NULL;
case CLI_HANDLER:
+ /* verify input */
if (a->argc != 4) {
return CLI_SHOWUSAGE;
- }
-
- ast_cli(a->fd, FORMAT_RES_ALL, "Result", "Name", "Catagory", "Error Description");
+ } else if (!strcmp(a->argv[3], "passed")) {
+ mode = 2;
+ } else if (!strcmp(a->argv[3], "failed")) {
+ mode = 1;
+ } else if (!strcmp(a->argv[3], "all")) {
+ mode = 0;
+ } else {
+ 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) {
test->result.state == AST_TEST_FAIL ? failed++ : passed++;
- 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) : "");
+ if (!mode || ((mode == 1) && (test->result.state == AST_TEST_FAIL)) || ((mode == 2) && (test->result.state == AST_TEST_PASS))) {
+ 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, FORMAT_RES_ALL,
+ result_buf,
+ " ",
+ 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", (failed + passed), passed, failed);
default:
return NULL;
@@ -448,59 +468,10 @@
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;
- int failed = 0;
-
- 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) {
- failed++;
- 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) Failed\n", 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"),
+ AST_CLI_DEFINE(test_cli_show_results, "show last test results"),
};
@@ -510,6 +481,7 @@
sleep(1);
return AST_TEST_FAIL;
})
+AST_TEST_DEFINE(sample_test2,{return AST_TEST_PASS;}) //todohere remove
int ast_test_init()
{
@@ -518,6 +490,7 @@
/* Register Sample Tests */
AST_TEST_REGISTER("sample_test_1", "main/test", "this is sample test 1", "for example purposes", sample_test1);
+ AST_TEST_REGISTER("sample_test_2", "main/test", "this is sample test 2", "for example purposes", sample_test2);
/* Unregister Sample Tests */
//AST_TEST_UNREGISTER(sample_test1);
More information about the asterisk-commits
mailing list