mirror of
https://github.com/ascii-boxes/boxes.git
synced 2025-06-20 01:37:52 +02:00
Add three more unit tests for regex_replace()
This commit is contained in:
parent
ab28ad8c3a
commit
b07af10931
@ -67,7 +67,7 @@ uint32_t *regex_replace(pcre2_code *search, char *replace, uint32_t *input, cons
|
|||||||
{
|
{
|
||||||
PCRE2_SPTR replacement = u32_strconv_from_arg(replace, config_encoding);
|
PCRE2_SPTR replacement = u32_strconv_from_arg(replace, config_encoding);
|
||||||
if (replacement == NULL) {
|
if (replacement == NULL) {
|
||||||
fprintf(stderr, "Failed to convert replacement string to UTF-32 - \"%s\"\n", replace);
|
bx_fprintf(stderr, "Failed to convert replacement string to UTF-32 - \"%s\"\n", replace);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -112,7 +112,7 @@ uint32_t *regex_replace(pcre2_code *search, char *replace, uint32_t *input, cons
|
|||||||
PCRE2_UCHAR buffer[256];
|
PCRE2_UCHAR buffer[256];
|
||||||
pcre2_get_error_message(pcre2_rc, buffer, sizeof(buffer));
|
pcre2_get_error_message(pcre2_rc, buffer, sizeof(buffer));
|
||||||
/* buffer will normally contain "invalid replacement string" */
|
/* buffer will normally contain "invalid replacement string" */
|
||||||
fprintf(stderr, "Error substituting \"%s\": %s\n", replace, u32_strconv_to_output(buffer));
|
bx_fprintf(stderr, "Error substituting \"%s\": %s\n", replace, u32_strconv_to_output(buffer));
|
||||||
BFREE(output);
|
BFREE(output);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
16
utest/main.c
16
utest/main.c
@ -33,6 +33,15 @@
|
|||||||
#include "regulex_test.h"
|
#include "regulex_test.h"
|
||||||
|
|
||||||
|
|
||||||
|
static int beforeTest(void** state) {
|
||||||
|
(void) state; /* unused */
|
||||||
|
|
||||||
|
collect_reset();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
setup_mocks();
|
setup_mocks();
|
||||||
@ -45,8 +54,11 @@ int main(void)
|
|||||||
};
|
};
|
||||||
|
|
||||||
const struct CMUnitTest regulex_tests[] = {
|
const struct CMUnitTest regulex_tests[] = {
|
||||||
cmocka_unit_test(test_compile_pattern_error),
|
cmocka_unit_test_setup(test_compile_pattern_error, beforeTest),
|
||||||
cmocka_unit_test(test_compile_pattern_empty)
|
cmocka_unit_test_setup(test_compile_pattern_empty, beforeTest),
|
||||||
|
cmocka_unit_test_setup(test_regex_replace_invalid_utf, beforeTest),
|
||||||
|
cmocka_unit_test_setup(test_regex_replace_buffer_resize, beforeTest),
|
||||||
|
cmocka_unit_test_setup(test_regex_replace_error, beforeTest)
|
||||||
};
|
};
|
||||||
|
|
||||||
int num_failed = 0;
|
int num_failed = 0;
|
||||||
|
@ -27,10 +27,13 @@
|
|||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <setjmp.h>
|
#include <setjmp.h>
|
||||||
#include <cmocka.h>
|
#include <cmocka.h>
|
||||||
|
#include <uniconv.h>
|
||||||
|
#include <string.h>
|
||||||
#include "global_mock.h"
|
#include "global_mock.h"
|
||||||
#include "regulex.h"
|
#include "regulex.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void test_compile_pattern_empty(void **state)
|
void test_compile_pattern_empty(void **state)
|
||||||
{
|
{
|
||||||
(void) state; /* unused */
|
(void) state; /* unused */
|
||||||
@ -40,6 +43,7 @@ void test_compile_pattern_empty(void **state)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void test_compile_pattern_error(void **state)
|
void test_compile_pattern_error(void **state)
|
||||||
{
|
{
|
||||||
(void) state; /* unused */
|
(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: */
|
/*EOF*/ /* vim: set cindent sw=4: */
|
||||||
|
@ -29,6 +29,10 @@
|
|||||||
void test_compile_pattern_empty(void **state);
|
void test_compile_pattern_empty(void **state);
|
||||||
void test_compile_pattern_error(void **state);
|
void test_compile_pattern_error(void **state);
|
||||||
|
|
||||||
|
void test_regex_replace_invalid_utf(void **state);
|
||||||
|
void test_regex_replace_buffer_resize(void **state);
|
||||||
|
void test_regex_replace_error(void **state);
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user