summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorGeorge Joseph <george.joseph@fairview5.com>2014-09-18 19:23:39 +0000
committerGeorge Joseph <george.joseph@fairview5.com>2014-09-18 19:23:39 +0000
commitad8ef9175ad92f90d5ad1c8429fc71d791dae42f (patch)
treed2b7961aca136b4db7de7bde117a0a86743efc07 /tests
parentde72f3edbc6385a5ff438484a6b31d5548de1caf (diff)
utils: Create ast_strsep function that ignores separators inside quotes
This function acts like strsep with three exceptions... * The separator is a single character instead of a string. * Separators inside quotes are treated literally instead of like separators. * You can elect to have leading and trailing whitespace and quotes stripped from the result and have '\' sequences unescaped. Like strsep, ast_strsep maintains no internal state and you can call it recursively using different separators on the same storage. Also like strsep, for consistent results, consecutive separators are not collapsed so you may get an empty string as a valid result. Tested by: George Joseph Review: https://reviewboard.asterisk.org/r/3989/ ........ Merged revisions 423476 from http://svn.asterisk.org/svn/asterisk/branches/12 ........ Merged revisions 423478 from http://svn.asterisk.org/svn/asterisk/branches/13 git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@423480 65c4cc65-6c06-0410-ace0-fbb531ad65f3
Diffstat (limited to 'tests')
-rw-r--r--tests/test_strings.c80
1 files changed, 80 insertions, 0 deletions
diff --git a/tests/test_strings.c b/tests/test_strings.c
index 5e5a17d27..127ee789d 100644
--- a/tests/test_strings.c
+++ b/tests/test_strings.c
@@ -310,11 +310,90 @@ AST_TEST_DEFINE(ends_with_test)
return AST_TEST_PASS;
}
+AST_TEST_DEFINE(strsep_test)
+{
+ char *test1, *test2, *test3;
+
+ switch (cmd) {
+ case TEST_INIT:
+ info->name = "strsep";
+ info->category = "/main/strings/";
+ info->summary = "Test ast_strsep";
+ info->description = "Test ast_strsep";
+ return AST_TEST_NOT_RUN;
+ case TEST_EXECUTE:
+ break;
+ }
+
+ test1 = ast_strdupa("ghi=jkl,mno='pqr,stu',abc=def, vwx = yz1 , vwx = yz1 , '"
+ " vwx = yz1 ' , ' vwx , yz1 ',v\"w\"x, '\"x,v\",\"x\"' , \" i\\'m a test\""
+ ", \" i\\'m a, test\", \" i\\'m a, test\", e\\,nd, end\\");
+
+ test2 = ast_strsep(&test1, ',', 0);
+ ast_test_validate(test, 0 == strcmp("ghi=jkl", test2));
+
+ test3 = ast_strsep(&test2, '=', 0);
+ ast_test_validate(test, 0 == strcmp("ghi", test3));
+
+ test3 = ast_strsep(&test2, '=', 0);
+ ast_test_validate(test, 0 == strcmp("jkl", test3));
+
+ test2 = ast_strsep(&test1, ',', 0);
+ ast_test_validate(test, 0 == strcmp("mno='pqr,stu'", test2));
+
+ test3 = ast_strsep(&test2, '=', 0);
+ ast_test_validate(test, 0 == strcmp("mno", test3));
+
+ test3 = ast_strsep(&test2, '=', 0);
+ ast_test_validate(test, 0 == strcmp("'pqr,stu'", test3));
+
+ test2 = ast_strsep(&test1, ',', 0);
+ ast_test_validate(test, 0 == strcmp("abc=def", test2));
+
+ test2 = ast_strsep(&test1, ',', 0);
+ ast_test_validate(test, 0 == strcmp(" vwx = yz1 ", test2));
+
+ test2 = ast_strsep(&test1, ',', AST_STRSEP_TRIM);
+ ast_test_validate(test, 0 == strcmp("vwx = yz1", test2));
+
+ test2 = ast_strsep(&test1, ',', AST_STRSEP_STRIP);
+ ast_test_validate(test, 0 == strcmp(" vwx = yz1 ", test2));
+
+ test2 = ast_strsep(&test1, ',', AST_STRSEP_STRIP | AST_STRSEP_TRIM);
+ ast_test_validate(test, 0 == strcmp("vwx , yz1", test2));
+
+ test2 = ast_strsep(&test1, ',', AST_STRSEP_STRIP | AST_STRSEP_TRIM);
+ ast_test_validate(test, 0 == strcmp("v\"w\"x", test2));
+
+ test2 = ast_strsep(&test1, ',', AST_STRSEP_TRIM);
+ ast_test_validate(test, 0 == strcmp("'\"x,v\",\"x\"'", test2));
+
+ test2 = ast_strsep(&test1, ',', AST_STRSEP_TRIM);
+ ast_test_validate(test, 0 == strcmp("\" i\\'m a test\"", test2));
+
+ test2 = ast_strsep(&test1, ',', AST_STRSEP_TRIM | AST_STRSEP_UNESCAPE);
+ ast_test_validate(test, 0 == strcmp("\" i'm a, test\"", test2));
+
+ test2 = ast_strsep(&test1, ',', AST_STRSEP_ALL);
+ ast_test_validate(test, 0 == strcmp("i'm a, test", test2));
+
+ test2 = ast_strsep(&test1, ',', AST_STRSEP_TRIM | AST_STRSEP_UNESCAPE);
+ ast_test_validate(test, 0 == strcmp("e,nd", test2));
+
+ test2 = ast_strsep(&test1, ',', AST_STRSEP_TRIM | AST_STRSEP_UNESCAPE);
+ ast_test_validate(test, 0 == strcmp("end", test2));
+
+ // nothing failed; we're all good!
+ return AST_TEST_PASS;
+}
+
+
static int unload_module(void)
{
AST_TEST_UNREGISTER(str_test);
AST_TEST_UNREGISTER(begins_with_test);
AST_TEST_UNREGISTER(ends_with_test);
+ AST_TEST_UNREGISTER(strsep_test);
return 0;
}
@@ -323,6 +402,7 @@ static int load_module(void)
AST_TEST_REGISTER(str_test);
AST_TEST_REGISTER(begins_with_test);
AST_TEST_REGISTER(ends_with_test);
+ AST_TEST_REGISTER(strsep_test);
return AST_MODULE_LOAD_SUCCESS;
}