[svn-commits] eliel: branch group/data_api_gsoc2009 r254403 - in /team/group/data_api_gsoc2...

SVN commits to the Digium repositories svn-commits at lists.digium.com
Wed Mar 24 21:11:54 CDT 2010


Author: eliel
Date: Wed Mar 24 21:11:51 2010
New Revision: 254403

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=254403
Log:
Move the unit testing to the asterisk test API.


Removed:
    team/group/data_api_gsoc2009/tests/test_data.c
Modified:
    team/group/data_api_gsoc2009/main/data.c

Modified: team/group/data_api_gsoc2009/main/data.c
URL: http://svnview.digium.com/svn/asterisk/team/group/data_api_gsoc2009/main/data.c?view=diff&rev=254403&r1=254402&r2=254403
==============================================================================
--- team/group/data_api_gsoc2009/main/data.c (original)
+++ team/group/data_api_gsoc2009/main/data.c Wed Mar 24 21:11:51 2010
@@ -38,6 +38,7 @@
 #include "asterisk/cli.h"
 #include "asterisk/term.h"
 #include "asterisk/manager.h"
+#include "asterisk/test.h"
 
 /*** DOCUMENTATION
 	<manager name="DataGet" language="en_US">
@@ -1343,7 +1344,9 @@
 	}
 
 	/* do not increment the refcount of the returned object. */
-	ao2_ref(current, -1);
+	if (current != node) {
+		ao2_ref(current, -1);
+	}
 
 	return current;
 }
@@ -2706,6 +2709,152 @@
 	return RESULT_SUCCESS;
 }
 
+#ifdef TEST_FRAMEWORK
+
+/*!
+ * \internal
+ * \brief Structure used to test how to add a complete structure,
+ *        and how to compare it.
+ */
+struct test_structure {
+	int a_int;
+	unsigned int b_bool:1;
+	char *c_str;
+	unsigned int a_uint;
+};
+
+/*!
+ * \internal
+ * \brief test_structure mapping.
+ */
+#define DATA_EXPORT_TEST_STRUCTURE(MEMBER)                              \
+	MEMBER(test_structure, a_int, AST_DATA_INTEGER)                 \
+	MEMBER(test_structure, b_bool, AST_DATA_BOOLEAN)                \
+	MEMBER(test_structure, c_str, AST_DATA_STRING)                  \
+	MEMBER(test_structure, a_uint, AST_DATA_UNSIGNED_INTEGER)
+
+AST_DATA_STRUCTURE(test_structure, DATA_EXPORT_TEST_STRUCTURE);
+
+/*!
+ * \internal
+ * \brief Callback implementation.
+ */
+static int test_data_full_provider(const struct ast_data_search *search,
+		struct ast_data *root)
+{
+	struct ast_data *test_structure;
+	struct test_structure local_test_structure = {
+		.a_int = 10,
+		.b_bool = 1,
+		.c_str = "test string",
+		.a_uint = 20
+	};
+
+/*	if (!ast_data_search_cmp_int(search, "test_structure/a_int", local_test_structure.a_int)) {
+		return 0;
+	} */
+
+	test_structure = ast_data_add_node(root, "test_structure");
+	if (!test_structure) {
+		ast_debug(1, "Internal data api error\n");
+		return 0;
+	}
+
+	/* add the complete structure. */
+	ast_data_add_structure(test_structure, test_structure, &local_test_structure);
+
+	return 0;
+}
+
+/*!
+ * \internal
+ * \brief Handler definition for the full provider.
+ */
+static const struct ast_data_handler full_provider = {
+	.version = AST_DATA_HANDLER_VERSION,
+	.get = test_data_full_provider
+};
+
+/*!
+ * \internal
+ * \brief Structure used to define multiple providers at once.
+ */
+static const struct ast_data_entry test_providers[] = {
+	AST_DATA_ENTRY("test/node1/node11/node111", &full_provider)
+};
+
+AST_TEST_DEFINE(test_data_get)
+{
+	struct ast_data *res, *node;
+	struct ast_data_iterator *i;
+	struct ast_data_query query = {
+		.version = AST_DATA_QUERY_VERSION,
+		.path = "test/node1/node11/node111",
+		.search = "node111/test_structure/a_int=10",
+		.filter = "node111/test_structure/a*int"
+	};
+
+	switch (cmd) {
+	case TEST_INIT:
+		info->name = "data_test";
+		info->category = "main/data/";
+		info->summary = "Data API unit test";
+		info->description =
+			"Tests whether data API get implementation works as expected.";
+		return AST_TEST_NOT_RUN;
+	case TEST_EXECUTE:
+		break;
+	}
+
+	ast_data_register_multiple_core(test_providers, ARRAY_LEN(test_providers));
+
+	res = ast_data_get(&query);
+	if (!res) {
+		ast_test_status_update(test, "Unable to get tree.");
+		ast_data_unregister("test/node1/node11/node111");
+		return AST_TEST_FAIL;
+	}
+
+	/* initiate the iterator and check for errors. */
+	i = ast_data_iterator_init(res, "test_structure/");
+	if (!i) {
+		ast_test_status_update(test, "Unable to initiate the iterator.");
+		ast_data_free(res);
+		ast_data_unregister("test/node1/node11/node111");
+		return AST_TEST_FAIL;
+	}
+
+	/* walk the returned nodes. */
+	while ((node = ast_data_iterator_next(i))) {
+		if (!strcmp(ast_data_retrieve_name(node), "a_int")) {
+			if (ast_data_retrieve_int(node, "/") != 10) {
+				ast_data_iterator_end(i);
+				ast_data_free(res);
+				ast_data_unregister("test/node1/node11/node111");
+				return AST_TEST_FAIL;
+			}
+		} else if (!strcmp(ast_data_retrieve_name(node), "a_uint")) {
+			if (ast_data_retrieve_uint(node, "/") != 20) {
+				ast_data_iterator_end(i);
+				ast_data_free(res);
+				ast_data_unregister("test/node1/node11/node111");
+				return AST_TEST_FAIL;
+			}
+		}
+	}
+
+	/* finish the iterator. */
+	ast_data_iterator_end(i);
+
+	ast_data_free(res);
+
+	ast_data_unregister("test/node1/node11/node111");
+
+	return AST_TEST_PASS;
+}
+
+#endif
+
 int ast_data_init(void)
 {
 	int res = 0;
@@ -2721,5 +2870,9 @@
 
 	res |= ast_manager_register_xml("DataGet", 0, manager_data_get);
 
+#ifdef TEST_FRAMEWORK
+	AST_TEST_REGISTER(test_data_get);
+#endif
+
 	return res;
 }




More information about the svn-commits mailing list