[tepl] File loading: write FileContent unit tests for the fallback mode
- From: Sébastien Wilmet <swilmet src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [tepl] File loading: write FileContent unit tests for the fallback mode
- Date: Sun, 22 Oct 2017 13:03:31 +0000 (UTC)
commit 4326a8f743a49aa7318b0a4db5c7bb4b017e54d4
Author: Sébastien Wilmet <swilmet gnome org>
Date: Sun Oct 22 13:09:02 2017 +0200
File loading: write FileContent unit tests for the fallback mode
tepl/tepl-file-content.c | 21 +++++----
tepl/tepl-file-content.h | 8 +++-
testsuite/Makefile.am | 3 +
testsuite/test-file-content.c | 107 +++++++++++++++++++++++++++++++++++++++++
4 files changed, 129 insertions(+), 10 deletions(-)
---
diff --git a/tepl/tepl-file-content.c b/tepl/tepl-file-content.c
index 66f8a43..d889607 100644
--- a/tepl/tepl-file-content.c
+++ b/tepl/tepl-file-content.c
@@ -164,14 +164,14 @@ determine_encoding_with_uchardet (TeplFileContent *content)
/* Try the candidate encodings one by one, taking the first without conversion
* error.
*/
-static TeplEncoding *
-determine_encoding_with_fallback_mode (TeplFileContent *content)
+TeplEncoding *
+_tepl_file_content_determine_encoding_with_fallback_mode (TeplFileContent *content,
+ GSList *candidate_encodings)
{
- GSList *candidate_encodings;
- GSList *l;
TeplEncoding *encoding = NULL;
+ GSList *l;
- candidate_encodings = tepl_encoding_get_default_candidates ();
+ g_return_val_if_fail (TEPL_IS_FILE_CONTENT (content), NULL);
for (l = candidate_encodings; l != NULL; l = l->next)
{
@@ -184,8 +184,6 @@ determine_encoding_with_fallback_mode (TeplFileContent *content)
}
}
- g_slist_free_full (candidate_encodings, (GDestroyNotify)tepl_encoding_free);
-
return encoding;
}
@@ -203,7 +201,12 @@ _tepl_file_content_determine_encoding (TeplFileContent *content)
if (encoding == NULL)
{
- encoding = determine_encoding_with_fallback_mode (content);
+ GSList *candidate_encodings;
+
+ candidate_encodings = tepl_encoding_get_default_candidates ();
+ encoding = _tepl_file_content_determine_encoding_with_fallback_mode (content,
+ candidate_encodings);
+ g_slist_free_full (candidate_encodings, (GDestroyNotify)tepl_encoding_free);
}
return encoding;
@@ -262,7 +265,7 @@ out:
return success;
}
-/* For the unit tests. */
+/* For unit tests. */
gint64
_tepl_file_content_get_encoding_converter_buffer_size (void)
{
diff --git a/tepl/tepl-file-content.h b/tepl/tepl-file-content.h
index 9afa5f1..ec47cef 100644
--- a/tepl/tepl-file-content.h
+++ b/tepl/tepl-file-content.h
@@ -69,8 +69,14 @@ gboolean _tepl_file_content_convert_to_utf8 (TeplFileContent
*c
gpointer
callback_user_data,
GError **error);
+/* For unit tests */
+
+G_GNUC_INTERNAL
+TeplEncoding * _tepl_file_content_determine_encoding_with_fallback_mode (TeplFileContent
*content,
+ GSList
*candidate_encodings);
+
G_GNUC_INTERNAL
-gint64 _tepl_file_content_get_encoding_converter_buffer_size (void);
+gint64 _tepl_file_content_get_encoding_converter_buffer_size (void);
G_END_DECLS
diff --git a/testsuite/Makefile.am b/testsuite/Makefile.am
index 1057c2d..d003894 100644
--- a/testsuite/Makefile.am
+++ b/testsuite/Makefile.am
@@ -52,6 +52,9 @@ test_encoding_converter_SOURCES = test-encoding-converter.c
UNIT_TEST_PROGS += test-file
test_file_SOURCES = test-file.c
+UNIT_TEST_PROGS += test-file-content
+test_file_content_SOURCES = test-file-content.c
+
UNIT_TEST_PROGS += test-file-loader
test_file_loader_SOURCES = test-file-loader.c
diff --git a/testsuite/test-file-content.c b/testsuite/test-file-content.c
new file mode 100644
index 0000000..db5e76d
--- /dev/null
+++ b/testsuite/test-file-content.c
@@ -0,0 +1,107 @@
+/*
+ * This file is part of Tepl, a text editor library.
+ *
+ * Copyright 2017 - Sébastien Wilmet <swilmet gnome org>
+ *
+ * Tepl is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU Lesser General Public License as published by the
+ * Free Software Foundation; either version 2.1 of the License, or (at your
+ * option) any later version.
+ *
+ * Tepl 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 Lesser General Public
+ * License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <tepl/tepl.h>
+#include "tepl/tepl-file-content.h"
+#include <string.h>
+
+static void
+add_string (TeplFileContent *content,
+ const gchar *str)
+{
+ GBytes *chunk;
+
+ chunk = g_bytes_new (str, strlen (str));
+ _tepl_file_content_add_chunk (content, chunk);
+ g_bytes_unref (chunk);
+}
+
+static void
+check_determine_encoding_fallback (GSList *candidate_encodings,
+ const gchar *str,
+ TeplEncoding *expected_encoding)
+{
+ TeplFileContent *content;
+ TeplEncoding *detected_encoding;
+
+ content = _tepl_file_content_new ();
+
+ if (str != NULL)
+ {
+ add_string (content, str);
+ }
+
+ detected_encoding = _tepl_file_content_determine_encoding_with_fallback_mode (content,
+ candidate_encodings);
+
+ g_assert (tepl_encoding_equals (detected_encoding, expected_encoding));
+
+ g_object_unref (content);
+ tepl_encoding_free (detected_encoding);
+}
+
+static void
+test_determine_encoding_with_fallback_mode (void)
+{
+ GSList *candidate_encodings = NULL;
+ TeplEncoding *utf8_encoding;
+ TeplEncoding *ascii_encoding;
+
+ utf8_encoding = tepl_encoding_new_utf8 ();
+ ascii_encoding = tepl_encoding_new ("ASCII");
+
+ /* UTF-8 -> ASCII */
+ candidate_encodings = g_slist_append (candidate_encodings, utf8_encoding);
+ candidate_encodings = g_slist_append (candidate_encodings, ascii_encoding);
+
+ // An empty/0-bytes file has no GBytes chunks, the list is empty.
+ check_determine_encoding_fallback (candidate_encodings, NULL, utf8_encoding);
+
+ check_determine_encoding_fallback (candidate_encodings, "Wistiti", utf8_encoding);
+ check_determine_encoding_fallback (candidate_encodings, "Wißtiti", utf8_encoding);
+
+ g_slist_free (candidate_encodings);
+ candidate_encodings = NULL;
+
+ /* ASCII -> UTF-8 */
+ candidate_encodings = g_slist_append (candidate_encodings, ascii_encoding);
+ candidate_encodings = g_slist_append (candidate_encodings, utf8_encoding);
+
+ check_determine_encoding_fallback (candidate_encodings, NULL, ascii_encoding);
+ check_determine_encoding_fallback (candidate_encodings, "Wistiti", ascii_encoding);
+ check_determine_encoding_fallback (candidate_encodings, "Wißtiti", utf8_encoding);
+
+ g_slist_free (candidate_encodings);
+ candidate_encodings = NULL;
+
+ tepl_encoding_free (utf8_encoding);
+ tepl_encoding_free (ascii_encoding);
+}
+
+int
+main (int argc,
+ char **argv)
+{
+ g_test_init (&argc, &argv, NULL);
+
+ g_test_add_func ("/FileContent/test_determine_encoding_with_fallback_mode",
+ test_determine_encoding_with_fallback_mode);
+
+ return g_test_run ();
+}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]