Add tests for coverage the bug.
authorSlava Zanko <slavazanko@gmail.com>
Fri, 15 Mar 2013 15:59:54 +0000 (15 18:59 +0300)
committerAndrew Borodin <aborodin@vmail.ru>
Thu, 28 Mar 2013 12:50:28 +0000 (28 16:50 +0400)
Signed-off-by: Slava Zanko <slavazanko@gmail.com>
configure.ac
tests/src/Makefile.am
tests/src/editor/Makefile.am [new file with mode: 0644]
tests/src/editor/editcmd__edit_complete_word_cmd.c [new file with mode: 0644]
tests/src/editor/mc.charsets [new file with mode: 0644]
tests/src/editor/test-data.txt.in [new file with mode: 0644]

index 6e2bc48..ed56749 100644 (file)
@@ -648,6 +648,8 @@ tests/lib/vfs/Makefile
 tests/lib/widget/Makefile
 tests/src/Makefile
 tests/src/filemanager/Makefile
+tests/src/editor/Makefile
+tests/src/editor/test-data.txt
 ])
 fi
 
index d428850..92b045c 100644 (file)
@@ -1,4 +1,4 @@
-SUBDIRS = . filemanager
+SUBDIRS = . editor filemanager
 
 AM_CPPFLAGS = \
        $(GLIB_CFLAGS) \
diff --git a/tests/src/editor/Makefile.am b/tests/src/editor/Makefile.am
new file mode 100644 (file)
index 0000000..c0ee705
--- /dev/null
@@ -0,0 +1,27 @@
+AM_CPPFLAGS = \
+       -DTEST_SHARE_DIR=\"$(abs_srcdir)\" \
+       $(GLIB_CFLAGS) \
+       -I$(top_srcdir) \
+       @CHECK_CFLAGS@
+
+AM_LDFLAGS = @TESTS_LDFLAGS@
+
+LIBS=@CHECK_LIBS@  \
+       $(top_builddir)/src/libinternal.la \
+       $(top_builddir)/lib/libmc.la
+
+if ENABLE_VFS_SMB
+# this is a hack for linking with own samba library in simple way
+LIBS += $(top_builddir)/src/vfs/smbfs/helpers/libsamba.a
+endif
+
+EXTRA_DIST = mc.charsets test-data.txt.in
+
+TESTS = \
+       editcmd__edit_complete_word_cmd
+
+check_PROGRAMS = $(TESTS)
+
+editcmd__edit_complete_word_cmd_SOURCES = \
+       editcmd__edit_complete_word_cmd.c
+
diff --git a/tests/src/editor/editcmd__edit_complete_word_cmd.c b/tests/src/editor/editcmd__edit_complete_word_cmd.c
new file mode 100644 (file)
index 0000000..51b47e7
--- /dev/null
@@ -0,0 +1,293 @@
+/*
+   src/editor - tests for edit_complete_word_cmd() function
+
+   Copyright (C) 2013
+   The Free Software Foundation, Inc.
+
+   Written by:
+   Slava Zanko <slavazanko@gmail.com>, 2013
+
+   This file is part of the Midnight Commander.
+
+   The Midnight Commander is free software: you can redistribute it
+   and/or modify it under the terms of the GNU General Public License as
+   published by the Free Software Foundation, either version 3 of the License,
+   or (at your option) any later version.
+
+   The Midnight Commander 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 General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#define TEST_SUITE_NAME "/src/editor"
+
+#include "tests/mctest.h"
+
+#include <ctype.h>
+
+#ifdef HAVE_CHARSET
+#include "lib/charsets.h"
+#endif
+#include "lib/strutil.h"
+
+#include "src/vfs/local/local.c"
+#include "src/editor/editwidget.h"
+#include "src/editor/editcmd_dialogs.h"
+
+
+static WEdit *test_edit;
+
+/* --------------------------------------------------------------------------------------------- */
+/* @Mock */
+void
+edit_load_syntax (WEdit * _edit, char ***_pnames, const char *_type)
+{
+    (void) _edit;
+    (void) _pnames;
+    (void) _type;
+}
+
+/* --------------------------------------------------------------------------------------------- */
+
+/* @Mock */
+int
+edit_get_syntax_color (WEdit * _edit, off_t _byte_index)
+{
+    (void) _edit;
+    (void) _byte_index;
+
+    return 0;
+}
+
+/* --------------------------------------------------------------------------------------------- */
+
+/* @Mock */
+gboolean
+edit_load_macro_cmd (WEdit * _edit)
+{
+    (void) _edit;
+
+    return FALSE;
+}
+
+/* --------------------------------------------------------------------------------------------- */
+
+/* @CapturedValue */
+static const WEdit *editcmd_dialog_completion_show__edit;
+/* @CapturedValue */
+static int editcmd_dialog_completion_show__max_len;
+/* @CapturedValue */
+static GString **editcmd_dialog_completion_show__compl;
+/* @CapturedValue */
+static int editcmd_dialog_completion_show__num_compl;
+
+/* @ThenReturnValue */
+static char *editcmd_dialog_completion_show__return_value;
+
+/* @Mock */
+char *
+editcmd_dialog_completion_show (const WEdit * edit, int max_len, GString ** compl, int num_compl)
+{
+
+    editcmd_dialog_completion_show__edit = edit;
+    editcmd_dialog_completion_show__max_len = max_len;
+    editcmd_dialog_completion_show__num_compl = num_compl;
+
+    {
+        int iterator;
+
+        editcmd_dialog_completion_show__compl = g_new0 (GString *, num_compl);
+
+        for (iterator = 0; iterator < editcmd_dialog_completion_show__num_compl; iterator++)
+            editcmd_dialog_completion_show__compl[iterator] =
+                g_string_new_len (compl[iterator]->str, compl[iterator]->len);
+    }
+
+    return editcmd_dialog_completion_show__return_value;
+}
+
+static void
+editcmd_dialog_completion_show__init (void)
+{
+    editcmd_dialog_completion_show__edit = NULL;
+    editcmd_dialog_completion_show__max_len = 0;
+    editcmd_dialog_completion_show__compl = NULL;
+    editcmd_dialog_completion_show__num_compl = 0;
+    editcmd_dialog_completion_show__return_value = NULL;
+}
+
+static void
+editcmd_dialog_completion_show__deinit (void)
+{
+    if (editcmd_dialog_completion_show__compl != NULL)
+    {
+        int iterator;
+
+        for (iterator = 0; iterator < editcmd_dialog_completion_show__num_compl; iterator++)
+            g_string_free (editcmd_dialog_completion_show__compl[iterator], TRUE);
+
+        g_free (editcmd_dialog_completion_show__compl);
+    }
+}
+
+
+/* --------------------------------------------------------------------------------------------- */
+
+/* @Before */
+static void
+my_setup (void)
+{
+    str_init_strings (NULL);
+
+    vfs_init ();
+    init_localfs ();
+    vfs_setup_work_dir ();
+
+#ifdef HAVE_CHARSET
+    mc_global.sysconfig_dir = (char *) TEST_SHARE_DIR;
+    load_codepages_list ();
+#endif /* HAVE_CHARSET */
+
+    test_edit = edit_init (NULL, 0, 0, 24, 80, vfs_path_from_str ("test-data.txt"), 1);
+    editcmd_dialog_completion_show__init ();
+}
+
+/* --------------------------------------------------------------------------------------------- */
+
+/* @After */
+static void
+my_teardown (void)
+{
+    editcmd_dialog_completion_show__deinit ();
+    edit_clean (test_edit);
+    g_free (test_edit);
+
+#ifdef HAVE_CHARSET
+    free_codepages_list ();
+#endif /* HAVE_CHARSET */
+
+    vfs_shut ();
+
+    str_uninit_strings ();
+}
+
+/* --------------------------------------------------------------------------------------------- */
+
+#ifdef HAVE_CHARSET
+/* @DataSource("test_autocomplete_ds") */
+/* *INDENT-OFF* */
+static const struct test_autocomplete_ds
+{
+    off_t input_position;
+    const char *input_system_code_page;
+    const char *input_editor_code_page;
+    const char *input_completed_word;
+
+    int expected_max_len;
+    int expected_compl_word_count;
+    int input_completed_word_start_pos;
+    const char *expected_completed_word;
+} test_autocomplete_ds[] =
+{
+    { /* 0. */
+        111,
+        "KOI8-R",
+        "UTF-8",
+        "эъйцукен",
+
+        16,
+        2,
+        107,
+        "эъйцукен"
+    },
+    { /* 0. */
+        147,
+        "KOI8-R",
+        "UTF-8",
+        "ÜßÊÃÕËÅÎ",
+
+        8,
+        2,
+        145,
+        "ÜßÊÃÕËÅÎ"
+    },
+};
+/* *INDENT-ON* */
+
+/* @Test(dataSource = "test_autocomplete_ds") */
+/* *INDENT-OFF* */
+START_PARAMETRIZED_TEST (test_autocomplete, test_autocomplete_ds)
+/* *INDENT-ON* */
+{
+    /* given */
+    editcmd_dialog_completion_show__return_value = g_strdup (data->input_completed_word);
+    cp_display = data->input_system_code_page;
+    cp_source = data->input_editor_code_page;
+
+    /* when */
+    edit_cursor_move (test_edit, data->input_position);
+    edit_complete_word_cmd (test_edit);
+
+    /* then */
+    mctest_assert_ptr_eq (editcmd_dialog_completion_show__edit, test_edit);
+    mctest_assert_int_eq (editcmd_dialog_completion_show__num_compl,
+                          data->expected_compl_word_count);
+    mctest_assert_int_eq (editcmd_dialog_completion_show__max_len, data->expected_max_len);
+
+    {
+        off_t i = 0;
+        GString *actual_completed_str;
+
+        actual_completed_str = g_string_new ("");
+
+        while (TRUE)
+        {
+            int chr;
+
+            chr = edit_get_byte (test_edit, data->input_completed_word_start_pos + i++);
+            if (isspace (chr))
+                break;
+            g_string_append_c (actual_completed_str, chr);
+        }
+        mctest_assert_str_eq (actual_completed_str->str, data->expected_completed_word);
+        g_string_free (actual_completed_str, TRUE);
+    }
+}
+/* *INDENT-OFF* */
+END_PARAMETRIZED_TEST
+/* *INDENT-ON* */
+#endif /* HAVE_CHARSET */
+
+/* --------------------------------------------------------------------------------------------- */
+
+int
+main (void)
+{
+    int number_failed;
+
+    Suite *s = suite_create (TEST_SUITE_NAME);
+    TCase *tc_core = tcase_create ("Core");
+    SRunner *sr;
+
+    tcase_add_checked_fixture (tc_core, my_setup, my_teardown);
+
+    /* Add new tests here: *************** */
+#ifdef HAVE_CHARSET
+    mctest_add_parameterized_test (tc_core, test_autocomplete, test_autocomplete_ds);
+#endif /* HAVE_CHARSET */
+    /* *********************************** */
+
+    suite_add_tcase (s, tc_core);
+    sr = srunner_create (s);
+    srunner_set_log (sr, "edit_complete_word_cmd.log");
+    srunner_run_all (sr, CK_NORMAL);
+    number_failed = srunner_ntests_failed (sr);
+    srunner_free (sr);
+    return (number_failed == 0) ? 0 : 1;
+}
+
+/* --------------------------------------------------------------------------------------------- */
diff --git a/tests/src/editor/mc.charsets b/tests/src/editor/mc.charsets
new file mode 100644 (file)
index 0000000..7b27d61
--- /dev/null
@@ -0,0 +1,2 @@
+KOI8-R         KOI8-R
+UTF-8          UTF-8
diff --git a/tests/src/editor/test-data.txt.in b/tests/src/editor/test-data.txt.in
new file mode 100644 (file)
index 0000000..1ace292
--- /dev/null
@@ -0,0 +1,17 @@
+****************** START:editcmd__edit_complete_word_cmd.c
+
+--- UTF-8:
+
+эъйцукен
+эъфывапр
+
+эъ
+
+--- KOI8-R:
+
+ÜßÊÃÕËÅÎ
+ÜßÆÙ×ÁÐÒ
+
+Üß
+
+****************** END:editcmd__edit_complete_word_cmd.c