[asterisk-commits] file: branch file/sorcery r378205 - in /team/file/sorcery: include/asterisk/ ...
SVN commits to the Asterisk project
asterisk-commits at lists.digium.com
Wed Dec 26 09:53:04 CST 2012
Author: file
Date: Wed Dec 26 09:52:59 2012
New Revision: 378205
URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=378205
Log:
Improve existing tests, add a few more, and fix some bugs uncovered by the tests.
Modified:
team/file/sorcery/include/asterisk/sorcery.h
team/file/sorcery/main/sorcery.c
team/file/sorcery/tests/test_sorcery.c
Modified: team/file/sorcery/include/asterisk/sorcery.h
URL: http://svnview.digium.com/svn/asterisk/team/file/sorcery/include/asterisk/sorcery.h?view=diff&rev=378205&r1=378204&r2=378205
==============================================================================
--- team/file/sorcery/include/asterisk/sorcery.h (original)
+++ team/file/sorcery/include/asterisk/sorcery.h Wed Dec 26 09:52:59 2012
@@ -274,7 +274,7 @@
* \retval -1 failure
*/
#define ast_sorcery_object_field_register(sorcery, type, name, default_val, opt_type, flags, ...) \
- __ast_sorcery_object_field_register(sorcery, type, name, default_val, opt_type, NULL, NULL, flags, VA_NARGS(__VA_ARGS__), __VA_ARGS__);
+ __ast_sorcery_object_field_register(sorcery, type, name, default_val, opt_type, NULL, NULL, flags, VA_NARGS(__VA_ARGS__), __VA_ARGS__)
/*!
* \brief Register a field within an object with custom handlers
Modified: team/file/sorcery/main/sorcery.c
URL: http://svnview.digium.com/svn/asterisk/team/file/sorcery/main/sorcery.c?view=diff&rev=378205&r1=378204&r2=378205
==============================================================================
--- team/file/sorcery/main/sorcery.c (original)
+++ team/file/sorcery/main/sorcery.c Wed Dec 26 09:52:59 2012
@@ -414,7 +414,7 @@
struct ast_variable *mapping;
int res = 0;
- if (!config || (config == CONFIG_STATUS_FILEMISSING) || (config = CONFIG_STATUS_FILEINVALID)) {
+ if (!config || (config == CONFIG_STATUS_FILEMISSING) || (config == CONFIG_STATUS_FILEINVALID)) {
return -1;
}
@@ -434,7 +434,7 @@
ast_config_destroy(config);
- return 0;
+ return res;
}
int ast_sorcery_apply_default(struct ast_sorcery *sorcery, const char *type, const char *name, const char *data)
@@ -476,7 +476,7 @@
RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
va_list args;
- if (!object_type) {
+ if (!object_type || !object_type->type) {
return -1;
}
Modified: team/file/sorcery/tests/test_sorcery.c
URL: http://svnview.digium.com/svn/asterisk/team/file/sorcery/tests/test_sorcery.c?view=diff&rev=378205&r1=378204&r2=378205
==============================================================================
--- team/file/sorcery/tests/test_sorcery.c (original)
+++ team/file/sorcery/tests/test_sorcery.c Wed Dec 26 09:52:59 2012
@@ -168,7 +168,7 @@
}
if (!(sorcery = ast_sorcery_open())) {
- ast_test_status_update(test, "Failed to open sorcery with NULL name\n");
+ ast_test_status_update(test, "Failed to open sorcery structure\n");
return AST_TEST_FAIL;
}
@@ -190,6 +190,44 @@
return AST_TEST_PASS;
}
+AST_TEST_DEFINE(apply_config)
+{
+ struct ast_flags flags = { CONFIG_FLAG_NOCACHE };
+ struct ast_config *config;
+ RAII_VAR(struct ast_sorcery *, sorcery, NULL, ast_sorcery_unref);
+
+ switch (cmd) {
+ case TEST_INIT:
+ info->name = "apply_config";
+ info->category = "/main/sorcery/";
+ info->summary = "sorcery object mapping configuration unit test";
+ info->description =
+ "Test configured object mapping in sorcery";
+ return AST_TEST_NOT_RUN;
+ case TEST_EXECUTE:
+ break;
+ }
+
+ if (!(config = ast_config_load2("sorcery.conf", "test_sorcery", flags))) {
+ ast_test_status_update(test, "Sorcery configuration file not present - skipping apply_config test\n");
+ return AST_TEST_NOT_RUN;
+ }
+
+ ast_config_destroy(config);
+
+ if (!(sorcery = ast_sorcery_open())) {
+ ast_test_status_update(test, "Failed to open sorcery structure\n");
+ return AST_TEST_FAIL;
+ }
+
+ if (ast_sorcery_apply_config(sorcery, "test_sorcery")) {
+ ast_test_status_update(test, "Failed to apply configured object mappings\n");
+ return AST_TEST_FAIL;
+ }
+
+ return AST_TEST_PASS;
+}
+
AST_TEST_DEFINE(object_register)
{
RAII_VAR(struct ast_sorcery *, sorcery, NULL, ast_sorcery_unref);
@@ -207,7 +245,7 @@
}
if (!(sorcery = ast_sorcery_open())) {
- ast_test_status_update(test, "Failed to open sorcery with NULL name\n");
+ ast_test_status_update(test, "Failed to open structure\n");
return AST_TEST_FAIL;
}
@@ -224,6 +262,84 @@
return AST_TEST_PASS;
}
+AST_TEST_DEFINE(object_register_without_mapping)
+{
+ RAII_VAR(struct ast_sorcery *, sorcery, NULL, ast_sorcery_unref);
+
+ switch (cmd) {
+ case TEST_INIT:
+ info->name = "object_register_without_mapping";
+ info->category = "/main/sorcery/";
+ info->summary = "sorcery object type registration (without mapping) unit test";
+ info->description =
+ "Test object type registration when no mapping exists in sorcery";
+ return AST_TEST_NOT_RUN;
+ case TEST_EXECUTE:
+ break;
+ }
+
+ if (!(sorcery = ast_sorcery_open())) {
+ ast_test_status_update(test, "Failed to open sorcery structure\n");
+ return AST_TEST_FAIL;
+ }
+
+ if (!ast_sorcery_object_register(sorcery, "test", &test_object, NULL, NULL)) {
+ ast_test_status_update(test, "Registered object type when no object mapping exists\n");
+ return AST_TEST_FAIL;
+ }
+
+ return AST_TEST_PASS;
+}
+
+AST_TEST_DEFINE(object_field_register)
+{
+ RAII_VAR(struct ast_sorcery *, sorcery, NULL, ast_sorcery_unref);
+
+ switch (cmd) {
+ case TEST_INIT:
+ info->name = "object_field_register";
+ info->category = "/main/sorcery/";
+ info->summary = "sorcery object field registration unit test";
+ info->description =
+ "Test object field registration in sorcery with a provided id";
+ return AST_TEST_NOT_RUN;
+ case TEST_EXECUTE:
+ break;
+ }
+
+ if (!(sorcery = ast_sorcery_open())) {
+ ast_test_status_update(test, "Failed to open sorcery structure\n");
+ return AST_TEST_FAIL;
+ }
+
+ if (!ast_sorcery_object_field_register(sorcery, "test", "bob", "5", OPT_UINT_T, PARSE_DEFAULT, FLDSET(struct test_sorcery_object, bob))) {
+ ast_test_status_update(test, "Registered an object field successfully when no mappings or object types exist\n");
+ return AST_TEST_FAIL;
+ }
+
+ if (ast_sorcery_apply_default(sorcery, "test", "memory", NULL)) {
+ ast_test_status_update(test, "Failed to set a known wizard as a default\n");
+ return AST_TEST_FAIL;
+ }
+
+ if (!ast_sorcery_object_field_register(sorcery, "test", "bob", "5", OPT_UINT_T, PARSE_DEFAULT, FLDSET(struct test_sorcery_object, bob))) {
+ ast_test_status_update(test, "Registered an object field successfully when object type does not exist\n");
+ return AST_TEST_FAIL;
+ }
+
+ if (ast_sorcery_object_register(sorcery, "test", &test_object, NULL, NULL)) {
+ ast_test_status_update(test, "Failed to register object type\n");
+ return AST_TEST_FAIL;
+ }
+
+ if (ast_sorcery_object_field_register(sorcery, "test", "bob", "5", OPT_UINT_T, PARSE_DEFAULT, FLDSET(struct test_sorcery_object, bob))) {
+ ast_test_status_update(test, "Could not successfully register object field when mapping and object type exists\n");
+ return AST_TEST_FAIL;
+ }
+
+ return AST_TEST_PASS;
+}
+
AST_TEST_DEFINE(object_alloc_with_id)
{
int res = AST_TEST_PASS;
@@ -243,7 +359,7 @@
}
if (!(sorcery = ast_sorcery_open())) {
- ast_test_status_update(test, "Failed to open sorcery with NULL name\n");
+ ast_test_status_update(test, "Failed to open sorcery structure\n");
return AST_TEST_FAIL;
}
@@ -299,7 +415,7 @@
}
if (!(sorcery = ast_sorcery_open())) {
- ast_test_status_update(test, "Failed to open sorcery with NULL name\n");
+ ast_test_status_update(test, "Failed to open sorcery structure\n");
return AST_TEST_FAIL;
}
@@ -348,7 +464,7 @@
}
if (!(sorcery = ast_sorcery_open())) {
- ast_test_status_update(test, "Failed to open sorcery with NULL name\n");
+ ast_test_status_update(test, "Failed to open sorcery structure\n");
return AST_TEST_FAIL;
}
@@ -409,7 +525,7 @@
}
if (!(sorcery = ast_sorcery_open())) {
- ast_test_status_update(test, "Failed to open sorcery with NULL name\n");
+ ast_test_status_update(test, "Failed to open sorcery structure\n");
return AST_TEST_FAIL;
}
@@ -483,7 +599,7 @@
}
if (!(sorcery = ast_sorcery_open())) {
- ast_test_status_update(test, "Failed to open sorcery with NULL name\n");
+ ast_test_status_update(test, "Failed to open sorcery structure\n");
return AST_TEST_FAIL;
}
@@ -550,7 +666,7 @@
}
if (!(sorcery = ast_sorcery_open())) {
- ast_test_status_update(test, "Failed to open sorcery with NULL name\n");
+ ast_test_status_update(test, "Failed to open sorcery structure\n");
return AST_TEST_FAIL;
}
@@ -605,7 +721,7 @@
}
if (!(sorcery = ast_sorcery_open())) {
- ast_test_status_update(test, "Failed to open sorcery with NULL name\n");
+ ast_test_status_update(test, "Failed to open sorcery structure\n");
return AST_TEST_FAIL;
}
@@ -777,7 +893,7 @@
}
if (!(sorcery = ast_sorcery_open())) {
- ast_test_status_update(test, "Failed to open sorcery with NULL name\n");
+ ast_test_status_update(test, "Failed to open sorcery structure\n");
return AST_TEST_FAIL;
}
@@ -825,7 +941,7 @@
}
if (!(sorcery = ast_sorcery_open())) {
- ast_test_status_update(test, "Failed to open sorcery with NULL name\n");
+ ast_test_status_update(test, "Failed to open sorcery structure\n");
return AST_TEST_FAIL;
}
@@ -851,9 +967,24 @@
ao2_cleanup(obj);
+ if (!(obj = ast_sorcery_alloc(sorcery, "test", "blah2"))) {
+ ast_test_status_update(test, "Failed to allocate second instance of a known object type\n");
+ return AST_TEST_FAIL;
+ }
+
+ if (ast_sorcery_create(sorcery, obj)) {
+ ast_test_status_update(test, "Failed to create second object using in-memory wizard\n");
+ return AST_TEST_FAIL;
+ }
+
+ ao2_cleanup(obj);
+
if (!(obj = ast_sorcery_retrieve(sorcery, "test", AST_RETRIEVE_FLAG_DEFAULT, AST_RETRIEVE_PARAM_ID, "blah", AST_RETRIEVE_PARAM_END))) {
ast_test_status_update(test, "Failed to retrieve properly created object using id of 'blah'\n");
return AST_TEST_FAIL;
+ } else if (strcmp(ast_sorcery_object_get_id(obj), "blah")) {
+ ast_test_status_update(test, "Retrieved object does not have correct id\n");
+ return AST_TEST_FAIL;
}
return AST_TEST_PASS;
@@ -877,7 +1008,7 @@
}
if (!(sorcery = ast_sorcery_open())) {
- ast_test_status_update(test, "Failed to open sorcery with NULL name\n");
+ ast_test_status_update(test, "Failed to open sorcery structure\n");
return AST_TEST_FAIL;
}
@@ -942,7 +1073,7 @@
}
if (!(sorcery = ast_sorcery_open())) {
- ast_test_status_update(test, "Failed to open sorcery with NULL name\n");
+ ast_test_status_update(test, "Failed to open sorcery structure\n");
return AST_TEST_FAIL;
}
@@ -966,37 +1097,118 @@
return AST_TEST_FAIL;
}
+ ao2_cleanup(obj);
+
+ if (!(obj = ast_sorcery_alloc(sorcery, "test", "blah2"))) {
+ ast_test_status_update(test, "Failed to allocate second instance of a known object type\n");
+ return AST_TEST_FAIL;
+ }
+
+ if (ast_sorcery_create(sorcery, obj)) {
+ ast_test_status_update(test, "Failed to create second object using in-memory wizard\n");
+ return AST_TEST_FAIL;
+ }
+
if (!(objects = ast_sorcery_retrieve(sorcery, "test", AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, AST_RETRIEVE_PARAM_END))) {
ast_test_status_update(test, "Failed to retrieve a container of all objects\n");
return AST_TEST_FAIL;
+ } else if (ao2_container_count(objects) != 2) {
+ ast_test_status_update(test, "Received a container with no objects in it when there should be some\n");
+ return AST_TEST_FAIL;
+ }
+
+ return AST_TEST_PASS;
+}
+
+AST_TEST_DEFINE(object_retrieve_multiple_field)
+{
+ RAII_VAR(struct ast_sorcery *, sorcery, NULL, ast_sorcery_unref);
+ RAII_VAR(struct test_sorcery_object *, obj, NULL, ao2_cleanup);
+ RAII_VAR(struct ao2_container *, objects, NULL, ao2_cleanup);
+
+ switch (cmd) {
+ case TEST_INIT:
+ info->name = "object_retrieve_multiple_field";
+ info->category = "/main/sorcery/";
+ info->summary = "sorcery multiple object retrieval unit test";
+ info->description =
+ "Test multiple object retrieval in sorcery using fields";
+ return AST_TEST_NOT_RUN;
+ case TEST_EXECUTE:
+ break;
+ }
+
+ if (!(sorcery = ast_sorcery_open())) {
+ ast_test_status_update(test, "Failed to open sorcery structure\n");
+ return AST_TEST_FAIL;
+ }
+
+ if (ast_sorcery_apply_default(sorcery, "test", "memory", NULL)) {
+ ast_test_status_update(test, "Failed to set a known wizard as a default\n");
+ return AST_TEST_FAIL;
+ }
+
+ if (ast_sorcery_object_register(sorcery, "test", &test_object, NULL, NULL)) {
+ ast_test_status_update(test, "Failed to register object type\n");
+ return AST_TEST_FAIL;
+ }
+
+ ast_sorcery_object_field_register(sorcery, "test", "bob", "5", OPT_UINT_T, PARSE_DEFAULT, FLDSET(struct test_sorcery_object, bob));
+ ast_sorcery_object_field_register(sorcery, "test", "joe", "10", OPT_UINT_T, PARSE_DEFAULT, FLDSET(struct test_sorcery_object, joe));
+
+ if (!(obj = ast_sorcery_alloc(sorcery, "test", "blah"))) {
+ ast_test_status_update(test, "Failed to allocate a known object type\n");
+ return AST_TEST_FAIL;
+ }
+
+ obj->joe = 6;
+
+ if (ast_sorcery_create(sorcery, obj)) {
+ ast_test_status_update(test, "Failed to create object using in-memory wizard\n");
+ return AST_TEST_FAIL;
+ }
+
+ if (!(objects = ast_sorcery_retrieve(sorcery, "test", AST_RETRIEVE_FLAG_MULTIPLE, AST_RETRIEVE_PARAM_FIELD, "joe", "6", AST_RETRIEVE_PARAM_END))) {
+ ast_test_status_update(test, "Failed to retrieve a container of all objects\n");
+ return AST_TEST_FAIL;
} else if (!ao2_container_count(objects)) {
ast_test_status_update(test, "Received a container with no objects in it when there should be some\n");
return AST_TEST_FAIL;
}
- return AST_TEST_PASS;
-}
-
-AST_TEST_DEFINE(object_retrieve_multiple_field)
+ ao2_cleanup(objects);
+
+ if (!(objects = ast_sorcery_retrieve(sorcery, "test", AST_RETRIEVE_FLAG_MULTIPLE, AST_RETRIEVE_PARAM_FIELD, "joe", "7", AST_RETRIEVE_PARAM_END))) {
+ ast_test_status_update(test, "Failed to retrieve an empty container when retrieving multiple\n");
+ return AST_TEST_FAIL;
+ } else if (ao2_container_count(objects)) {
+ ast_test_status_update(test, "Received a container with objects when there should be none in it\n");
+ return AST_TEST_FAIL;
+ }
+
+ return AST_TEST_PASS;
+}
+
+AST_TEST_DEFINE(object_update)
{
RAII_VAR(struct ast_sorcery *, sorcery, NULL, ast_sorcery_unref);
RAII_VAR(struct test_sorcery_object *, obj, NULL, ao2_cleanup);
- RAII_VAR(struct ao2_container *, objects, NULL, ao2_cleanup);
-
- switch (cmd) {
- case TEST_INIT:
- info->name = "object_retrieve_multiple_field";
- info->category = "/main/sorcery/";
- info->summary = "sorcery multiple object retrieval unit test";
- info->description =
- "Test multiple object retrieval in sorcery using fields";
- return AST_TEST_NOT_RUN;
- case TEST_EXECUTE:
- break;
- }
-
- if (!(sorcery = ast_sorcery_open())) {
- ast_test_status_update(test, "Failed to open sorcery with NULL name\n");
+ RAII_VAR(struct test_sorcery_object *, obj2, NULL, ao2_cleanup);
+
+ switch (cmd) {
+ case TEST_INIT:
+ info->name = "object_update";
+ info->category = "/main/sorcery/";
+ info->summary = "sorcery object update unit test";
+ info->description =
+ "Test object updating in sorcery";
+ return AST_TEST_NOT_RUN;
+ case TEST_EXECUTE:
+ break;
+ }
+
+ if (!(sorcery = ast_sorcery_open())) {
+ ast_test_status_update(test, "Failed to open sorcery structure\n");
return AST_TEST_FAIL;
}
@@ -1018,80 +1230,11 @@
return AST_TEST_FAIL;
}
- obj->joe = 6;
-
if (ast_sorcery_create(sorcery, obj)) {
ast_test_status_update(test, "Failed to create object using in-memory wizard\n");
return AST_TEST_FAIL;
}
- if (!(objects = ast_sorcery_retrieve(sorcery, "test", AST_RETRIEVE_FLAG_MULTIPLE, AST_RETRIEVE_PARAM_FIELD, "joe", "6", AST_RETRIEVE_PARAM_END))) {
- ast_test_status_update(test, "Failed to retrieve a container of all objects\n");
- return AST_TEST_FAIL;
- } else if (!ao2_container_count(objects)) {
- ast_test_status_update(test, "Received a container with no objects in it when there should be some\n");
- return AST_TEST_FAIL;
- }
-
- ao2_cleanup(objects);
-
- if (!(objects = ast_sorcery_retrieve(sorcery, "test", AST_RETRIEVE_FLAG_MULTIPLE, AST_RETRIEVE_PARAM_FIELD, "joe", "7", AST_RETRIEVE_PARAM_END))) {
- ast_test_status_update(test, "Failed to retrieve an empty container when retrieving multiple\n");
- return AST_TEST_FAIL;
- } else if (ao2_container_count(objects)) {
- ast_test_status_update(test, "Received a container with objects when there should be none in it\n");
- return AST_TEST_FAIL;
- }
-
- return AST_TEST_PASS;
-}
-
-AST_TEST_DEFINE(object_update)
-{
- RAII_VAR(struct ast_sorcery *, sorcery, NULL, ast_sorcery_unref);
- RAII_VAR(struct test_sorcery_object *, obj, NULL, ao2_cleanup);
- RAII_VAR(struct test_sorcery_object *, obj2, NULL, ao2_cleanup);
-
- switch (cmd) {
- case TEST_INIT:
- info->name = "object_update";
- info->category = "/main/sorcery/";
- info->summary = "sorcery object update unit test";
- info->description =
- "Test object updating in sorcery";
- return AST_TEST_NOT_RUN;
- case TEST_EXECUTE:
- break;
- }
-
- if (!(sorcery = ast_sorcery_open())) {
- ast_test_status_update(test, "Failed to open sorcery with NULL name\n");
- return AST_TEST_FAIL;
- }
-
- if (ast_sorcery_apply_default(sorcery, "test", "memory", NULL)) {
- ast_test_status_update(test, "Failed to set a known wizard as a default\n");
- return AST_TEST_FAIL;
- }
-
- if (ast_sorcery_object_register(sorcery, "test", &test_object, NULL, NULL)) {
- ast_test_status_update(test, "Failed to register object type\n");
- return AST_TEST_FAIL;
- }
-
- ast_sorcery_object_field_register(sorcery, "test", "bob", "5", OPT_UINT_T, PARSE_DEFAULT, FLDSET(struct test_sorcery_object, bob));
- ast_sorcery_object_field_register(sorcery, "test", "joe", "10", OPT_UINT_T, PARSE_DEFAULT, FLDSET(struct test_sorcery_object, joe));
-
- if (!(obj = ast_sorcery_alloc(sorcery, "test", "blah"))) {
- ast_test_status_update(test, "Failed to allocate a known object type\n");
- return AST_TEST_FAIL;
- }
-
- if (ast_sorcery_create(sorcery, obj)) {
- ast_test_status_update(test, "Failed to create object using in-memory wizard\n");
- return AST_TEST_FAIL;
- }
-
if (!(obj2 = ast_sorcery_copy(sorcery, obj))) {
ast_test_status_update(test, "Failed to allocate a known object type for updating\n");
return AST_TEST_FAIL;
@@ -1133,7 +1276,7 @@
}
if (!(sorcery = ast_sorcery_open())) {
- ast_test_status_update(test, "Failed to open sorcery with NULL name\n");
+ ast_test_status_update(test, "Failed to open sorcery structure\n");
return AST_TEST_FAIL;
}
@@ -1177,7 +1320,10 @@
AST_TEST_UNREGISTER(wizard_registration);
AST_TEST_UNREGISTER(open);
AST_TEST_UNREGISTER(apply_default);
+ AST_TEST_UNREGISTER(apply_config);
AST_TEST_UNREGISTER(object_register);
+ AST_TEST_UNREGISTER(object_register_without_mapping);
+ AST_TEST_UNREGISTER(object_field_register);
AST_TEST_UNREGISTER(object_alloc_with_id);
AST_TEST_UNREGISTER(object_alloc_without_id);
AST_TEST_UNREGISTER(object_copy);
@@ -1202,7 +1348,10 @@
AST_TEST_REGISTER(wizard_registration);
AST_TEST_REGISTER(open);
AST_TEST_REGISTER(apply_default);
+ AST_TEST_REGISTER(apply_config);
AST_TEST_REGISTER(object_register);
+ AST_TEST_REGISTER(object_register_without_mapping);
+ AST_TEST_REGISTER(object_field_register);
AST_TEST_REGISTER(object_alloc_with_id);
AST_TEST_REGISTER(object_alloc_without_id);
AST_TEST_REGISTER(object_copy);
More information about the asterisk-commits
mailing list