mirror of
https://github.com/MidnightCommander/mc
synced 2025-03-30 11:42:54 +03:00
added tests for mc_search_regex__replace_handle_esc_seq() function
Signed-off-by: Slava Zanko <slavazanko@gmail.com>
This commit is contained in:
parent
6cec807140
commit
3ced63361b
@ -598,6 +598,7 @@ if test x$enable_tests != xno; then
|
||||
AC_CONFIG_FILES([
|
||||
lib/tests/Makefile
|
||||
lib/tests/mcconfig/Makefile
|
||||
lib/tests/search/Makefile
|
||||
lib/tests/vfs/Makefile
|
||||
])
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
SUBDIRS = . mcconfig vfs
|
||||
SUBDIRS = . mcconfig search vfs
|
||||
|
||||
AM_CFLAGS = $(GLIB_CFLAGS) -I$(top_srcdir) @CHECK_CFLAGS@
|
||||
LIBS=@CHECK_LIBS@ $(top_builddir)/lib/libmc.la
|
||||
|
14
lib/tests/search/Makefile.am
Normal file
14
lib/tests/search/Makefile.am
Normal file
@ -0,0 +1,14 @@
|
||||
AM_CFLAGS = -I$(top_srcdir)/lib/search $(GLIB_CFLAGS) -I$(top_srcdir) @CHECK_CFLAGS@
|
||||
LIBS=@CHECK_LIBS@ $(top_builddir)/lib/libmc.la
|
||||
|
||||
TESTS = \
|
||||
regex_replace_esc_seq \
|
||||
regex_process_escape_sequence
|
||||
|
||||
check_PROGRAMS = $(TESTS)
|
||||
|
||||
regex_replace_esc_seq_SOURCES = \
|
||||
regex_replace_esc_seq.c
|
||||
|
||||
regex_process_escape_sequence_SOURCES = \
|
||||
regex_process_escape_sequence.c
|
101
lib/tests/search/regex_process_escape_sequence.c
Normal file
101
lib/tests/search/regex_process_escape_sequence.c
Normal file
@ -0,0 +1,101 @@
|
||||
/* libmc - checks for processing esc sequences in replace string
|
||||
|
||||
Copyright (C) 2011 Free Software Foundation, Inc.
|
||||
|
||||
Written by:
|
||||
Slava Zanko <slavazanko@gmail.com>, 2011
|
||||
|
||||
This program is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Library General Public License
|
||||
as published by the Free Software Foundation; either version 2 of
|
||||
the License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Library General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Library General Public
|
||||
License along with this program; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
#define TEST_SUITE_NAME "lib/search/regex"
|
||||
|
||||
#include <check.h>
|
||||
|
||||
#include "regex.c" /* for testing static functions*/
|
||||
|
||||
/* --------------------------------------------------------------------------------------------- */
|
||||
#define test_helper_valid_data(from, etalon, dest_str, replace_flags) { \
|
||||
dest_str = g_string_new(""); \
|
||||
mc_search_regex__process_escape_sequence (dest_str, from, -1, &replace_flags); \
|
||||
fail_if (strcmp(dest_str->str, etalon), "dest_str(%s) != %s", dest_str->str, etalon); \
|
||||
g_string_free(dest_str, TRUE); \
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------- */
|
||||
|
||||
START_TEST (test_regex_process_escape_sequence_valid)
|
||||
{
|
||||
GString *dest_str;
|
||||
replace_transform_type_t replace_flags = REPLACE_T_NO_TRANSFORM;
|
||||
|
||||
test_helper_valid_data("{101}", "A", dest_str, replace_flags);
|
||||
test_helper_valid_data("x42", "B", dest_str, replace_flags);
|
||||
test_helper_valid_data("x{4344}", "CD", dest_str, replace_flags);
|
||||
|
||||
test_helper_valid_data("n", "\n", dest_str, replace_flags);
|
||||
test_helper_valid_data("t", "\t", dest_str, replace_flags);
|
||||
test_helper_valid_data("v", "\v", dest_str, replace_flags);
|
||||
test_helper_valid_data("b", "\b", dest_str, replace_flags);
|
||||
test_helper_valid_data("r", "\r", dest_str, replace_flags);
|
||||
test_helper_valid_data("f", "\f", dest_str, replace_flags);
|
||||
test_helper_valid_data("a", "\a", dest_str, replace_flags);
|
||||
}
|
||||
END_TEST
|
||||
|
||||
/* --------------------------------------------------------------------------------------------- */
|
||||
|
||||
START_TEST (test_regex_process_escape_sequence_invalid)
|
||||
{
|
||||
GString *dest_str;
|
||||
replace_transform_type_t replace_flags = REPLACE_T_NO_TRANSFORM;
|
||||
|
||||
test_helper_valid_data("{101", "{101", dest_str, replace_flags);
|
||||
test_helper_valid_data("101}", "101}", dest_str, replace_flags);
|
||||
test_helper_valid_data("{ab}", "{ab}", dest_str, replace_flags);
|
||||
test_helper_valid_data("xqw", "xqw", dest_str, replace_flags);
|
||||
test_helper_valid_data("x{41", "x{41", dest_str, replace_flags);
|
||||
test_helper_valid_data("x{qwer}", "x{qwer}", dest_str, replace_flags);
|
||||
test_helper_valid_data("s", "s", dest_str, replace_flags);
|
||||
test_helper_valid_data("Q", "Q", dest_str, replace_flags);
|
||||
test_helper_valid_data("1", "1", dest_str, replace_flags);
|
||||
}
|
||||
END_TEST
|
||||
|
||||
/* --------------------------------------------------------------------------------------------- */
|
||||
|
||||
int
|
||||
main (void)
|
||||
{
|
||||
int number_failed;
|
||||
|
||||
Suite *s = suite_create (TEST_SUITE_NAME);
|
||||
TCase *tc_core = tcase_create ("Core");
|
||||
SRunner *sr;
|
||||
|
||||
/* Add new tests here: *************** */
|
||||
tcase_add_test (tc_core, test_regex_process_escape_sequence_valid);
|
||||
tcase_add_test (tc_core, test_regex_process_escape_sequence_invalid);
|
||||
/* *********************************** */
|
||||
|
||||
suite_add_tcase (s, tc_core);
|
||||
sr = srunner_create (s);
|
||||
srunner_run_all (sr, CK_NORMAL);
|
||||
number_failed = srunner_ntests_failed (sr);
|
||||
srunner_free (sr);
|
||||
return (number_failed == 0) ? 0 : 1;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------- */
|
236
lib/tests/search/regex_replace_esc_seq.c
Normal file
236
lib/tests/search/regex_replace_esc_seq.c
Normal file
@ -0,0 +1,236 @@
|
||||
/* libmc - checks for processing esc sequences in replace string
|
||||
|
||||
Copyright (C) 2011 Free Software Foundation, Inc.
|
||||
|
||||
Written by:
|
||||
Slava Zanko <slavazanko@gmail.com>, 2011
|
||||
|
||||
This program is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Library General Public License
|
||||
as published by the Free Software Foundation; either version 2 of
|
||||
the License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Library General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Library General Public
|
||||
License along with this program; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
#define TEST_SUITE_NAME "lib/search/regex"
|
||||
|
||||
#include <check.h>
|
||||
|
||||
#include "regex.c" /* for testing static functions*/
|
||||
|
||||
/* --------------------------------------------------------------------------------------------- */
|
||||
#define test_helper_check_valid_data( a, b, c, d, e, f, g, h ) \
|
||||
{ \
|
||||
fail_unless( a == b, "ret_value != %s", (b) ? "TRUE": "FALSE" ); \
|
||||
fail_unless( c == d, "skip_len(%d) != %d", c, d ); \
|
||||
if (f!=0) fail_unless( e == f, "ret(%d) != %d", e, f ); \
|
||||
fail_unless( g == h, "next_char('%c':%d) != %d", g, g, h ); \
|
||||
} \
|
||||
|
||||
/* --------------------------------------------------------------------------------------------- */
|
||||
|
||||
START_TEST (test_regex_replace_esc_seq_prepare_valid)
|
||||
{
|
||||
GString *replace_str;
|
||||
gsize skip_len;
|
||||
int ret;
|
||||
char next_char;
|
||||
|
||||
replace_str = g_string_new("bla-bla\\{123}bla-bla\\xabc234 bla-bla\\x{456abcd}bla-bla\\xtre\\n\\t\\v\\b\\r\\f\\a");
|
||||
|
||||
/* \\{123} */
|
||||
skip_len=0;
|
||||
test_helper_check_valid_data(
|
||||
mc_search_regex__replace_handle_esc_seq ( replace_str, 7, &skip_len, &ret, &next_char ), FALSE,
|
||||
skip_len, 6,
|
||||
ret, REPLACE_PREPARE_T_ESCAPE_SEQ,
|
||||
next_char, '{'
|
||||
);
|
||||
|
||||
/* \\xab */
|
||||
skip_len=0;
|
||||
test_helper_check_valid_data(
|
||||
mc_search_regex__replace_handle_esc_seq ( replace_str, 20, &skip_len, &ret, &next_char ), FALSE,
|
||||
skip_len, 4,
|
||||
ret, REPLACE_PREPARE_T_ESCAPE_SEQ,
|
||||
next_char, 'b'
|
||||
);
|
||||
|
||||
/* \\x{456abcd} */
|
||||
skip_len=0;
|
||||
test_helper_check_valid_data(
|
||||
mc_search_regex__replace_handle_esc_seq ( replace_str, 36, &skip_len, &ret, &next_char ), FALSE,
|
||||
skip_len, 11,
|
||||
ret, REPLACE_PREPARE_T_ESCAPE_SEQ,
|
||||
next_char, '{'
|
||||
);
|
||||
|
||||
/* \\xtre */
|
||||
skip_len=0;
|
||||
test_helper_check_valid_data(
|
||||
mc_search_regex__replace_handle_esc_seq ( replace_str, 54, &skip_len, &ret, &next_char ), FALSE,
|
||||
skip_len, 2,
|
||||
ret, REPLACE_PREPARE_T_NOTHING_SPECIAL,
|
||||
next_char, 't'
|
||||
);
|
||||
|
||||
/* \\n */
|
||||
skip_len=0;
|
||||
test_helper_check_valid_data(
|
||||
mc_search_regex__replace_handle_esc_seq ( replace_str, 59, &skip_len, &ret, &next_char ), FALSE,
|
||||
skip_len, 2,
|
||||
ret, REPLACE_PREPARE_T_ESCAPE_SEQ,
|
||||
next_char, 'n'
|
||||
);
|
||||
|
||||
/* \\t */
|
||||
skip_len=0;
|
||||
test_helper_check_valid_data(
|
||||
mc_search_regex__replace_handle_esc_seq ( replace_str, 61, &skip_len, &ret, &next_char ), FALSE,
|
||||
skip_len, 2,
|
||||
ret, REPLACE_PREPARE_T_ESCAPE_SEQ,
|
||||
next_char, 't'
|
||||
);
|
||||
|
||||
/* \\v */
|
||||
skip_len=0;
|
||||
test_helper_check_valid_data(
|
||||
mc_search_regex__replace_handle_esc_seq ( replace_str, 63, &skip_len, &ret, &next_char ), FALSE,
|
||||
skip_len, 2,
|
||||
ret, REPLACE_PREPARE_T_ESCAPE_SEQ,
|
||||
next_char, 'v'
|
||||
);
|
||||
|
||||
/* \\b */
|
||||
skip_len=0;
|
||||
test_helper_check_valid_data(
|
||||
mc_search_regex__replace_handle_esc_seq ( replace_str, 65, &skip_len, &ret, &next_char ), FALSE,
|
||||
skip_len, 2,
|
||||
ret, REPLACE_PREPARE_T_ESCAPE_SEQ,
|
||||
next_char, 'b'
|
||||
);
|
||||
|
||||
/* \\r */
|
||||
skip_len=0;
|
||||
test_helper_check_valid_data(
|
||||
mc_search_regex__replace_handle_esc_seq ( replace_str, 67, &skip_len, &ret, &next_char ), FALSE,
|
||||
skip_len, 2,
|
||||
ret, REPLACE_PREPARE_T_ESCAPE_SEQ,
|
||||
next_char, 'r'
|
||||
);
|
||||
|
||||
/* \\f */
|
||||
skip_len=0;
|
||||
test_helper_check_valid_data(
|
||||
mc_search_regex__replace_handle_esc_seq ( replace_str, 69, &skip_len, &ret, &next_char ), FALSE,
|
||||
skip_len, 2,
|
||||
ret, REPLACE_PREPARE_T_ESCAPE_SEQ,
|
||||
next_char, 'f'
|
||||
);
|
||||
|
||||
/* \\a */
|
||||
skip_len=0;
|
||||
test_helper_check_valid_data(
|
||||
mc_search_regex__replace_handle_esc_seq ( replace_str, 71, &skip_len, &ret, &next_char ), FALSE,
|
||||
skip_len, 2,
|
||||
ret, REPLACE_PREPARE_T_ESCAPE_SEQ,
|
||||
next_char, 'a'
|
||||
);
|
||||
|
||||
g_string_free(replace_str, TRUE);
|
||||
}
|
||||
END_TEST
|
||||
|
||||
/* --------------------------------------------------------------------------------------------- */
|
||||
|
||||
START_TEST (test_regex_replace_esc_seq_prepare_invalid)
|
||||
{
|
||||
|
||||
GString *replace_str;
|
||||
gsize skip_len;
|
||||
int ret;
|
||||
char next_char;
|
||||
|
||||
replace_str = g_string_new("\\{123 \\x{qwerty} \\12} \\x{456a-bcd}bla-bla\\satre");
|
||||
|
||||
/* \\{123 */
|
||||
skip_len=0;
|
||||
test_helper_check_valid_data(
|
||||
mc_search_regex__replace_handle_esc_seq ( replace_str, 0, &skip_len, &ret, &next_char ), TRUE,
|
||||
skip_len, 0,
|
||||
0, 0,
|
||||
next_char, '{'
|
||||
);
|
||||
|
||||
/* \\x{qwerty} */
|
||||
skip_len=0;
|
||||
test_helper_check_valid_data(
|
||||
mc_search_regex__replace_handle_esc_seq ( replace_str, 6, &skip_len, &ret, &next_char ), TRUE,
|
||||
skip_len, 0,
|
||||
0, 0,
|
||||
next_char, 'x'
|
||||
);
|
||||
/* \\12} */
|
||||
skip_len=0;
|
||||
test_helper_check_valid_data(
|
||||
mc_search_regex__replace_handle_esc_seq ( replace_str, 17, &skip_len, &ret, &next_char ), TRUE,
|
||||
skip_len, 0,
|
||||
0, 0,
|
||||
next_char, '1'
|
||||
);
|
||||
|
||||
/* \\x{456a-bcd} */
|
||||
skip_len=0;
|
||||
test_helper_check_valid_data(
|
||||
mc_search_regex__replace_handle_esc_seq ( replace_str, 22, &skip_len, &ret, &next_char ), TRUE,
|
||||
skip_len, 0,
|
||||
0, 0,
|
||||
next_char, 'x'
|
||||
);
|
||||
|
||||
/* \\satre */
|
||||
skip_len=0;
|
||||
test_helper_check_valid_data(
|
||||
mc_search_regex__replace_handle_esc_seq ( replace_str, 41, &skip_len, &ret, &next_char ), TRUE,
|
||||
skip_len, 0,
|
||||
0, 0,
|
||||
next_char, 's'
|
||||
);
|
||||
|
||||
g_string_free(replace_str, TRUE);
|
||||
}
|
||||
END_TEST
|
||||
|
||||
/* --------------------------------------------------------------------------------------------- */
|
||||
|
||||
int
|
||||
main (void)
|
||||
{
|
||||
int number_failed;
|
||||
|
||||
Suite *s = suite_create (TEST_SUITE_NAME);
|
||||
TCase *tc_core = tcase_create ("Core");
|
||||
SRunner *sr;
|
||||
|
||||
/* Add new tests here: *************** */
|
||||
tcase_add_test (tc_core, test_regex_replace_esc_seq_prepare_valid);
|
||||
tcase_add_test (tc_core, test_regex_replace_esc_seq_prepare_invalid);
|
||||
/* *********************************** */
|
||||
|
||||
suite_add_tcase (s, tc_core);
|
||||
sr = srunner_create (s);
|
||||
srunner_run_all (sr, CK_NORMAL);
|
||||
number_failed = srunner_ntests_failed (sr);
|
||||
srunner_free (sr);
|
||||
return (number_failed == 0) ? 0 : 1;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------- */
|
Loading…
x
Reference in New Issue
Block a user