[asterisk-commits] dvossel: branch dvossel/test_api r234669 - in /team/dvossel/test_api: include...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Mon Dec 14 11:29:50 CST 2009


Author: dvossel
Date: Mon Dec 14 11:29:46 2009
New Revision: 234669

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=234669
Log:
changes callback param from static buffer to ast_str *

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=234669&r1=234668&r2=234669
==============================================================================
--- team/dvossel/test_api/include/asterisk/test.h (original)
+++ team/dvossel/test_api/include/asterisk/test.h Mon Dec 14 11:29:46 2009
@@ -64,7 +64,7 @@
 
 /*! Macros used for defining and registering a test */
 #ifdef TEST_FRAMEWORK
-#define AST_TEST_DEFINE(hdr, body) int hdr(char *errbuf, int len); int hdr(char *errbuf, int len) body
+#define AST_TEST_DEFINE(hdr, body) int hdr(struct ast_str **error_str); int hdr(struct ast_str **error_str) 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 */
@@ -77,16 +77,16 @@
 #ifdef TEST_FRAMEWORK
 
 #include "asterisk/cli.h"
+#include "asterisk/strings.h"
 
 /*!
  * \brief Generic test callback function
  *
- * \param error buffer for failure results
- * \param length of error buffer
+ * \param error buffer string for failure results
  *
  * \return AST_TEST_PASS for pass, AST_TEST_FAIL for failure
  */
-typedef int (ast_test_cb_t)(char *errbuf, int len);
+typedef int (ast_test_cb_t)(struct ast_str **error_str);
 
 /*!
  * \brief Initializes test framework.
@@ -94,6 +94,15 @@
  * \return 0 on success, -1 on failure.
  */
 int ast_test_init(void);
+
+/*!
+ * \brief unregisters a test with the test framework
+ *
+ * \param test callback function (required)
+ *
+ * \return 0 for pass, -1 for failure
+ */
+int ast_test_unregister(ast_test_cb_t *cb);
 
 /*!
  * \brief registers a test with the test framework
@@ -107,15 +116,6 @@
  * \return 0 for pass, -1 for failure
  */
 int ast_test_register(const char *name, const char *catagory, const char *summary, const char *description, ast_test_cb_t *cb);
-
-/*!
- * \brief unregisters a test with the test framework
- *
- * \param test callback function (required)
- *
- * \return 0 for pass, -1 for failure
- */
-int ast_test_unregister(ast_test_cb_t *cb);
 
 /*!
  * \brief Runs test suit

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=234669&r1=234668&r2=234669
==============================================================================
--- team/dvossel/test_api/main/test.c (original)
+++ team/dvossel/test_api/main/test.c Mon Dec 14 11:29:46 2009
@@ -55,7 +55,7 @@
 
 struct ast_test_result {
 	enum ast_test_result_state state; /*! current test result state */
-	char error[64];                   /*! optional error buf to describe error results */
+	struct ast_str *error;             /*! optional error str to describe error result */
 	int time;                         /*! time in ms test took */
 };
 
@@ -131,9 +131,13 @@
 {
 	struct timeval begin;
 
-	memset(test->result.error, 0, ARRAY_LEN(test->result.error));
+	/* clear any previous error results before starting */
+	ast_str_reset(test->result.error);
+	/* get start time */
 	begin = ast_tvnow();
-	test->result.state = test->cb(test->result.error, ARRAY_LEN(test->result.error));
+	/* the callback gets the pointer to the pointer of our 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);
 }
 
@@ -149,7 +153,7 @@
 	fprintf(f, "<summary>%s</summary>\n", S_OR(test->summary, "NA"));
 	fprintf(f, "<description>%s</description>\n", S_OR(test->description, "NA"));
 	fprintf(f, "<result>%s</result>\n", test_result2str[test->result.state]);
-	fprintf(f, "<error>%s</error>\n", S_OR(test->result.error, "NA"));
+	fprintf(f, "<error>%s</error>\n", S_OR(ast_str_buffer(test->result.error), "NA"));
 	fprintf(f, "<time>%d</time>\n", test->result.time);
 	fprintf(f, "</test>\n");
 }
@@ -165,7 +169,7 @@
 	fprintf(f, "Summary: %s\n", S_OR(test->summary, "NA"));
 	fprintf(f, "Description: %s\n", S_OR(test->description, "NA"));
 	fprintf(f, "Result: %s\n", test_result2str[test->result.state]);
-	fprintf(f, "Error Description: %s\n", S_OR(test->result.error, "NA"));
+	fprintf(f, "Error Description: %s\n", S_OR(ast_str_buffer(test->result.error), "NA"));
 	fprintf(f, "Time: %d\n", test->result.time);
 
 }
@@ -213,9 +217,8 @@
 
 			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, test->result.error);
+				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 */
@@ -398,6 +401,7 @@
 	ast_free(test->catagory);
 	ast_free(test->summary);
 	ast_free(test->description);
+	ast_free(test->result.error);
 	ast_free(test);
 
 	return NULL;
@@ -417,7 +421,8 @@
 	if (!(test->name = ast_strdup(name)) ||
 		 !(test->catagory = ast_strdup(catagory)) ||
 		 !(test->summary = ast_strdup(summary)) ||
-		 !(test->description = ast_strdup(description))) {
+		 !(test->description = ast_strdup(description)) ||
+		 !(test->result.error = ast_str_create(128))) {
 
 		return test_free(test);
 	}
@@ -579,13 +584,17 @@
 			if (test->result.state != AST_TEST_NOT_RUN) {
 				test->result.state == AST_TEST_FAIL ? failed++ : passed++;
 				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));
+					/* give our results pretty colors */
+					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) : "");
+						(test->result.state == AST_TEST_FAIL) ? S_OR(ast_str_buffer(test->result.error), "Not Avaliable") : "");
 				}
 			}
 		}
@@ -652,6 +661,7 @@
 AST_TEST_DEFINE(sample_test1,
 {
 	sleep(1);
+	ast_str_set(error_str, 0, "FAKE ERRORS\n");
 	return AST_TEST_FAIL;
 })
 AST_TEST_DEFINE(sample_test2,{return AST_TEST_PASS;}) //todohere remove




More information about the asterisk-commits mailing list