Add three more unit tests for regex_replace()

This commit is contained in:
Thomas Jensen
2021-11-01 21:20:37 +01:00
parent ab28ad8c3a
commit b07af10931
4 changed files with 74 additions and 4 deletions

View File

@ -27,10 +27,13 @@
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
#include <uniconv.h>
#include <string.h>
#include "global_mock.h"
#include "regulex.h"
void test_compile_pattern_empty(void **state)
{
(void) state; /* unused */
@ -40,6 +43,7 @@ void test_compile_pattern_empty(void **state)
}
void test_compile_pattern_error(void **state)
{
(void) state; /* unused */
@ -57,4 +61,54 @@ void test_compile_pattern_error(void **state)
}
void test_regex_replace_invalid_utf(void **state)
{
(void) state; /* unused */
const char *input = "input";
assert_null(regex_replace(compile_pattern("search"), NULL, /* NULL is an invalid replacement string*/
u32_strconv_from_encoding(input, "ASCII", iconveh_question_mark), strlen(input), 0));
assert_int_equal(1, collect_err_size);
assert_string_equal("Failed to convert replacement string to UTF-32 - \"(null)\"\n", collect_err[0]);
}
void test_regex_replace_buffer_resize(void **state)
{
(void) state; /* unused */
const char *input = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
uint32_t *actual = regex_replace(compile_pattern("x"), "long_replacement_string_",
u32_strconv_from_encoding(input, "ASCII", iconveh_question_mark), strlen(input), 1);
char *actual_str = u32_strconv_to_encoding(actual, "ASCII", iconveh_question_mark);
const char *expected = "long_replacement_string_long_replacement_string_long_replacement_string_"
"long_replacement_string_long_replacement_string_long_replacement_string_long_replacement_string_"
"long_replacement_string_long_replacement_string_long_replacement_string_long_replacement_string_"
"long_replacement_string_long_replacement_string_long_replacement_string_long_replacement_string_"
"long_replacement_string_long_replacement_string_long_replacement_string_long_replacement_string_"
"long_replacement_string_long_replacement_string_long_replacement_string_long_replacement_string_"
"long_replacement_string_long_replacement_string_long_replacement_string_long_replacement_string_"
"long_replacement_string_long_replacement_string_long_replacement_string_long_replacement_string_"
"long_replacement_string_long_replacement_string_long_replacement_string_long_replacement_string_"
"long_replacement_string_long_replacement_string_long_replacement_string_long_replacement_string_";
assert_string_equal(expected, actual_str);
}
void test_regex_replace_error(void **state)
{
(void) state; /* unused */
const char *input = "xxx";
uint32_t *actual = regex_replace(compile_pattern("x"), "INVALID $2",
u32_strconv_from_encoding(input, "ASCII", iconveh_question_mark), strlen(input), 0);
assert_null(actual);
assert_int_equal(1, collect_err_size);
assert_string_equal("Error substituting \"INVALID $2\": unknown substring\n", collect_err[0]);
}
/*EOF*/ /* vim: set cindent sw=4: */