Ticket #3694: 3694-0001-Add-test-for-mc_search__hex_translate_to_regex.patch

File 3694-0001-Add-test-for-mc_search__hex_translate_to_regex.patch, 4.6 KB (added by mooffie, 3 years ago)
  • tests/lib/search/Makefile.am

    From 4bc796962034b38ec09f22201a6422e15b83f0c4 Mon Sep 17 00:00:00 2001
    From: Mooffie <mooffie@gmail.com>
    Date: Fri, 23 Sep 2016 14:40:54 +0300
    Subject: [PATCH 1/8] Add test for mc_search__hex_translate_to_regex().
    
    ---
     tests/lib/search/Makefile.am              |   4 ++
     tests/lib/search/hex_translate_to_regex.c | 114 ++++++++++++++++++++++++++++++
     2 files changed, 118 insertions(+)
     create mode 100644 tests/lib/search/hex_translate_to_regex.c
    
    diff --git a/tests/lib/search/Makefile.am b/tests/lib/search/Makefile.am
    index b05bb26..a9e3f0c 100644
    a b LIBS = @CHECK_LIBS@ $(top_builddir)/lib/libmc.la @PCRE_LIBS@ 
    1212TESTS = \ 
    1313        glob_prepare_replace_str \ 
    1414        glob_translate_to_regex \ 
     15        hex_translate_to_regex \ 
    1516        regex_replace_esc_seq \ 
    1617        regex_process_escape_sequence \ 
    1718        translate_replace_glob_to_regex 
    translate_replace_glob_to_regex_SOURCES = \ 
    3233 
    3334glob_translate_to_regex_SOURCES = \ 
    3435        glob_translate_to_regex.c 
     36 
     37hex_translate_to_regex_SOURCES = \ 
     38        hex_translate_to_regex.c 
  • new file tests/lib/search/hex_translate_to_regex.c

    diff --git a/tests/lib/search/hex_translate_to_regex.c b/tests/lib/search/hex_translate_to_regex.c
    new file mode 100644
    index 0000000..34c131c
    - +  
     1/* 
     2   libmc - checks for hex pattern parsing 
     3 
     4   Copyright (C) 2011-2016 
     5   Free Software Foundation, Inc. 
     6 
     7   This file is part of the Midnight Commander. 
     8 
     9   The Midnight Commander is free software: you can redistribute it 
     10   and/or modify it under the terms of the GNU General Public License as 
     11   published by the Free Software Foundation, either version 3 of the License, 
     12   or (at your option) any later version. 
     13 
     14   The Midnight Commander is distributed in the hope that it will be useful, 
     15   but WITHOUT ANY WARRANTY; without even the implied warranty of 
     16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
     17   GNU General Public License for more details. 
     18 
     19   You should have received a copy of the GNU General Public License 
     20   along with this program.  If not, see <http://www.gnu.org/licenses/>. 
     21 */ 
     22 
     23#define TEST_SUITE_NAME "lib/search/hex" 
     24 
     25#include "tests/mctest.h" 
     26 
     27#include "hex.c"                /* for testing static functions */ 
     28 
     29/* --------------------------------------------------------------------------------------------- */ 
     30 
     31/* @DataSource("test_hex_translate_to_regex_ds") */ 
     32/* *INDENT-OFF* */ 
     33static const struct test_hex_translate_to_regex_ds 
     34{ 
     35    const char *input_value; 
     36    const char *expected_result; 
     37} test_hex_translate_to_regex_ds[] = 
     38{ 
     39    { 
     40        /* Simplest case */ 
     41        "12 34", 
     42        "\\x12\\x34" 
     43    }, 
     44    { 
     45        /* Prefixes (0x, 0X) */ 
     46        "0x12 0X34", 
     47        "\\x12\\x34" 
     48    }, 
     49    { 
     50        /* Prefix "0" doesn't signify octal! Numbers are always interpreted in hex. */ 
     51        "012", 
     52        "\\x12" 
     53    }, 
     54    { 
     55        /* Extra whitespace (but not trailing one) */ 
     56        "  12  34", 
     57        "\\x12\\x34" 
     58    }, 
     59    { 
     60        /* Min/max values */ 
     61        "0 ff", 
     62        "\\x00\\xFF" 
     63    }, 
     64}; 
     65/* *INDENT-ON* */ 
     66 
     67/* @Test(dataSource = "test_hex_translate_to_regex_ds") */ 
     68/* *INDENT-OFF* */ 
     69START_PARAMETRIZED_TEST (test_hex_translate_to_regex, test_hex_translate_to_regex_ds) 
     70/* *INDENT-ON* */ 
     71{ 
     72    /* given */ 
     73    GString *tmp = g_string_new (data->input_value); 
     74    GString *dest_str; 
     75 
     76    /* when */ 
     77    dest_str = mc_search__hex_translate_to_regex (tmp); 
     78 
     79    /* then */ 
     80    g_string_free (tmp, TRUE); 
     81 
     82    mctest_assert_str_eq (dest_str->str, data->expected_result); 
     83    g_string_free (dest_str, TRUE); 
     84} 
     85/* *INDENT-OFF* */ 
     86END_PARAMETRIZED_TEST 
     87/* *INDENT-ON* */ 
     88 
     89/* --------------------------------------------------------------------------------------------- */ 
     90 
     91int 
     92main (void) 
     93{ 
     94    int number_failed; 
     95 
     96    Suite *s = suite_create (TEST_SUITE_NAME); 
     97    TCase *tc_core = tcase_create ("Core"); 
     98    SRunner *sr; 
     99 
     100    /* Add new tests here: *************** */ 
     101    mctest_add_parameterized_test (tc_core, test_hex_translate_to_regex, 
     102                                   test_hex_translate_to_regex_ds); 
     103    /* *********************************** */ 
     104 
     105    suite_add_tcase (s, tc_core); 
     106    sr = srunner_create (s); 
     107    srunner_set_log (sr, "hex_translate_to_regex.log"); 
     108    srunner_run_all (sr, CK_ENV); 
     109    number_failed = srunner_ntests_failed (sr); 
     110    srunner_free (sr); 
     111    return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE; 
     112} 
     113 
     114/* --------------------------------------------------------------------------------------------- */