| 1 | /* |
| 2 | libmc - checks search functions |
| 3 | |
| 4 | Copyright (C) 2022 |
| 5 | Free Software Foundation, Inc. |
| 6 | |
| 7 | Written by: |
| 8 | Steef Boerrigter <sxmboer@gmail.com>, 2022 |
| 9 | |
| 10 | This file is part of the Midnight Commander. |
| 11 | |
| 12 | The Midnight Commander is free software: you can redistribute it |
| 13 | and/or modify it under the terms of the GNU General Public License as |
| 14 | published by the Free Software Foundation, either version 3 of the License, |
| 15 | or (at your option) any later version. |
| 16 | |
| 17 | The Midnight Commander is distributed in the hope that it will be useful, |
| 18 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | GNU General Public License for more details. |
| 21 | |
| 22 | You should have received a copy of the GNU General Public License |
| 23 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 24 | */ |
| 25 | |
| 26 | #define TEST_SUITE_NAME "lib/search/search" |
| 27 | |
| 28 | #include "tests/mctest.h" |
| 29 | |
| 30 | #include "search.c" /* for testing static functions */ |
| 31 | |
| 32 | /* --------------------------------------------------------------------------------------------- */ |
| 33 | |
| 34 | /* @DataSource("test_search_str_ds") */ |
| 35 | /* *INDENT-OFF* */ |
| 36 | static const struct test_search_str_ds |
| 37 | { |
| 38 | const mc_search_type_t type; |
| 39 | const char *buffer; |
| 40 | const char *search_str; |
| 41 | const gboolean retval; |
| 42 | } test_search_str_ds[] = |
| 43 | { |
| 44 | { /* 1. */ |
| 45 | MC_SEARCH_T_NORMAL, |
| 46 | "abcdefgh", |
| 47 | "nope", |
| 48 | FALSE |
| 49 | }, |
| 50 | { /* 0. */ |
| 51 | MC_SEARCH_T_NORMAL, |
| 52 | "abcdefgh", |
| 53 | "def", |
| 54 | TRUE |
| 55 | }, |
| 56 | { /* 1. */ |
| 57 | MC_SEARCH_T_NORMAL, |
| 58 | "abcdefgh", |
| 59 | "z", |
| 60 | FALSE |
| 61 | }, |
| 62 | { /* 2. */ |
| 63 | MC_SEARCH_T_NORMAL, |
| 64 | "abcd \tefgh", |
| 65 | "\t ", |
| 66 | TRUE |
| 67 | }, |
| 68 | { /* 3. multiline */ |
| 69 | MC_SEARCH_T_NORMAL, |
| 70 | "abcd\ne\nfgh", |
| 71 | "\ne\nf", |
| 72 | TRUE |
| 73 | }, |
| 74 | { /* 4. */ |
| 75 | MC_SEARCH_T_NORMAL, |
| 76 | "abcd\nefgh", |
| 77 | "d\ne", |
| 78 | TRUE |
| 79 | }, |
| 80 | { /* 5. mc-4.8.28 fails this edge condition */ |
| 81 | MC_SEARCH_T_NORMAL, |
| 82 | "abcdefgh\n", |
| 83 | "\n\n", |
| 84 | FALSE |
| 85 | }, |
| 86 | { /* 6. regex including newline */ |
| 87 | MC_SEARCH_T_REGEX, |
| 88 | "abcd\nefgh", |
| 89 | "abc[c-e]\nef", |
| 90 | TRUE |
| 91 | }, |
| 92 | { /* 6. regex's newline support */ |
| 93 | MC_SEARCH_T_REGEX, |
| 94 | "abcd\nefgh", |
| 95 | "abc[c-e]\\nef", |
| 96 | TRUE |
| 97 | }, |
| 98 | }; |
| 99 | /* *INDENT-ON* */ |
| 100 | |
| 101 | /* @Test(dataSource = "test_search_str_ds") */ |
| 102 | /* *INDENT-OFF* */ |
| 103 | START_PARAMETRIZED_TEST (test_search_str, test_search_str_ds) |
| 104 | /* *INDENT-ON* */ |
| 105 | { |
| 106 | /* given */ |
| 107 | mc_search_t *s; |
| 108 | gboolean retval; |
| 109 | |
| 110 | s = mc_search_new (data->buffer, NULL); |
| 111 | s->is_case_sensitive = FALSE; |
| 112 | s->search_type = data->type; |
| 113 | |
| 114 | /* when */ |
| 115 | retval = mc_search (data->search_str, DEFAULT_CHARSET, data->buffer, s->search_type); |
| 116 | |
| 117 | /* then */ |
| 118 | if (data->retval) |
| 119 | { |
| 120 | mctest_assert_true (retval); |
| 121 | } |
| 122 | else |
| 123 | { |
| 124 | mctest_assert_false (retval); |
| 125 | } |
| 126 | |
| 127 | mc_search_free (s); |
| 128 | } |
| 129 | /* *INDENT-OFF* */ |
| 130 | END_PARAMETRIZED_TEST |
| 131 | /* *INDENT-ON* */ |
| 132 | |
| 133 | /* --------------------------------------------------------------------------------------------- */ |
| 134 | |
| 135 | int |
| 136 | main (void) |
| 137 | { |
| 138 | TCase *tc_core; |
| 139 | |
| 140 | tc_core = tcase_create ("Core"); |
| 141 | |
| 142 | /* Add new tests here: *************** */ |
| 143 | mctest_add_parameterized_test (tc_core, test_search_str, test_search_str_ds); |
| 144 | /* *********************************** */ |
| 145 | |
| 146 | return mctest_run_all (tc_core); |
| 147 | } |
| 148 | |
| 149 | /* --------------------------------------------------------------------------------------------- */ |