예제 #1
0
TEST(String, Replace)
{
    regex_t regex;
    char *result;

    /* basic replace */
    POINTERS_EQUAL(NULL, string_replace (NULL, NULL, NULL));
    POINTERS_EQUAL(NULL, string_replace ("string", NULL, NULL));
    POINTERS_EQUAL(NULL, string_replace (NULL, "search", NULL));
    POINTERS_EQUAL(NULL, string_replace (NULL, NULL, "replace"));
    POINTERS_EQUAL(NULL, string_replace ("string", "search", NULL));
    POINTERS_EQUAL(NULL, string_replace ("string", NULL, "replace"));
    POINTERS_EQUAL(NULL, string_replace (NULL, "search", "replace"));

    STRCMP_EQUAL("test abc def", string_replace("test abc def", "xyz", "xxx"));
    STRCMP_EQUAL("test xxx def", string_replace("test abc def", "abc", "xxx"));
    STRCMP_EQUAL("xxx test xxx def xxx",
                 string_replace("abc test abc def abc", "abc", "xxx"));

    /* replace with regex */
    WEE_REPLACE_REGEX(-1, NULL, NULL, NULL, NULL, '$', NULL);
    WEE_REPLACE_REGEX(0, NULL, NULL, "", NULL, '$', NULL);
    WEE_REPLACE_REGEX(0, "string", "string", "", NULL, '$', NULL);
    WEE_REPLACE_REGEX(0, "test abc def", "test abc def", "xyz", "xxx", '$', NULL);
    WEE_REPLACE_REGEX(0, "test xxx def", "test abc def", "abc", "xxx", '$', NULL);
    WEE_REPLACE_REGEX(0, "foo", "test foo", "^(test +)(.*)", "$2", '$', NULL);
    WEE_REPLACE_REGEX(0, "test / ***", "test foo", "^(test +)(.*)", "$1/ $.*2", '$', NULL);
    WEE_REPLACE_REGEX(0, "%%%", "test foo", "^(test +)(.*)", "$.%+", '$', NULL);

    /* replace with a callback */
    /* TODO: write tests for string_replace_with_callback */
}
예제 #2
0
TEST(String, ReplaceRegex)
{
    regex_t regex;
    char *result;

    WEE_REPLACE_REGEX(-1, NULL, NULL, NULL, NULL, '$', NULL);
    WEE_REPLACE_REGEX(0, NULL, NULL, "", NULL, '$', NULL);
    WEE_REPLACE_REGEX(0, "string", "string", "", NULL, '$', NULL);
    WEE_REPLACE_REGEX(0, "test abc def", "test abc def",
                      "xyz", "xxx", '$', NULL);
    WEE_REPLACE_REGEX(0, "test xxx def", "test abc def",
                      "abc", "xxx", '$', NULL);
    WEE_REPLACE_REGEX(0, "foo", "test foo", "^(test +)(.*)", "$2", '$', NULL);
    WEE_REPLACE_REGEX(0, "test / ***", "test foo",
                      "^(test +)(.*)", "$1/ $.*2", '$', NULL);
    WEE_REPLACE_REGEX(0, "%%%", "test foo",
                      "^(test +)(.*)", "$.%+", '$', NULL);
}