[gedit] Add test for checking all encodings



commit b6a28db8047f32e2c33239f4b50bfe6e16b6b030
Author: Ignacio Casal Quinteiro <icq gnome org>
Date:   Tue Jan 12 15:29:20 2010 +0100

    Add test for checking all encodings

 gedit/gedit-gio-document-loader.c |    6 +
 gedit/gedit-tab.c                 |    3 +
 tests/smart-converter.c           |  233 ++++++++++++++----------------------
 3 files changed, 100 insertions(+), 142 deletions(-)
---
diff --git a/gedit/gedit-gio-document-loader.c b/gedit/gedit-gio-document-loader.c
index 33b8eb3..092eb0b 100644
--- a/gedit/gedit-gio-document-loader.c
+++ b/gedit/gedit-gio-document-loader.c
@@ -40,6 +40,7 @@
 
 #include "gedit-gio-document-loader.h"
 #include "gedit-smart-charset-converter.h"
+#include "gedit-convert.h"
 #include "gedit-prefs-manager.h"
 #include "gedit-debug.h"
 #include "gedit-utils.h"
@@ -304,10 +305,12 @@ async_read_cb (GInputStream *stream,
 {
 	gedit_debug (DEBUG_LOADER);
 	GeditGioDocumentLoader *gvloader;
+	GeditDocumentLoader *loader;
 	gssize bytes_read;
 	GError *error = NULL;
 
 	gvloader = async->loader;
+	loader = GEDIT_DOCUMENT_LOADER (gvloader);
 
 	/* manually check cancelled state */
 	if (g_cancellable_is_cancelled (async->cancellable))
@@ -349,6 +352,8 @@ async_read_cb (GInputStream *stream,
 		GEDIT_DOCUMENT_LOADER (gvloader)->auto_detected_encoding =
 			gedit_smart_charset_converter_get_guessed (gvloader->priv->converter);
 
+		loader->auto_detected_encoding = gedit_smart_charset_converter_get_guessed (gvloader->priv->converter);
+
 		/* Check if we needed some fallback char, if so, check if there was
 		   a previous error and if not set a fallback used error */
 		if ((gedit_smart_charset_converter_get_num_fallbacks (gvloader->priv->converter) != 0) &&
@@ -362,6 +367,7 @@ async_read_cb (GInputStream *stream,
 		}
 
 		end_append_text_to_document (GEDIT_DOCUMENT_LOADER (gvloader));
+
 		remote_load_completed_or_failed (gvloader, async);
 
 		return;
diff --git a/gedit/gedit-tab.c b/gedit/gedit-tab.c
index b642ecd..de5325e 100644
--- a/gedit/gedit-tab.c
+++ b/gedit/gedit-tab.c
@@ -981,6 +981,9 @@ document_loaded (GeditDocument *document,
 			g_return_if_fail ((error->domain == G_CONVERT_ERROR) ||
 			      		  (error->domain == GEDIT_CONVERT_ERROR));
 
+			/* FIXME: Check for GEDIT_CONVERT_ERROR_FALLBACK_USED
+			  and set the right message area */
+
 			// TODO: different error messages if tab->priv->state == GEDIT_TAB_STATE_REVERTING?
 			// note that while reverting encoding should be ok, so this is unlikely to happen
 			emsg = gedit_conversion_error_while_loading_message_area_new (
diff --git a/tests/smart-converter.c b/tests/smart-converter.c
index 8b63e54..96d2c45 100644
--- a/tests/smart-converter.c
+++ b/tests/smart-converter.c
@@ -29,38 +29,6 @@
 
 #define TEXT_TO_CONVERT "this is some text to make the tests"
 
-static gchar *
-get_text_with_encoding (const gchar *text,
-			const GeditEncoding *encoding)
-{
-	GCharsetConverter *converter;
-	GConverterResult res;
-	gchar *conv_text;
-	gsize read, written;
-	GError *err = NULL;
-
-	converter = g_charset_converter_new (gedit_encoding_get_charset (encoding),
-					     "UTF-8",
-					     NULL);
-
-	conv_text = g_malloc (200);
-
-	res = g_converter_convert (G_CONVERTER (converter),
-				   text,
-				   strlen (text),
-				   conv_text,
-				   200,
-				   G_CONVERTER_INPUT_AT_END,
-				   &read,
-				   &written,
-				   &err);
-
-	g_assert (err == NULL);
-	conv_text[written] = '\0';
-
-	return conv_text;
-}
-
 static GSList *
 get_all_encodings ()
 {
@@ -85,136 +53,123 @@ get_all_encodings ()
 
 static void
 do_test (const gchar *test_in,
+         const gchar *enc,
          GSList      *encodings,
          gsize        nread,
          const gchar *test_out)
 {
 	GeditSmartCharsetConverter *converter;
-	gchar *out;
-	gsize bytes_read;
-	gsize bytes_written;
+	gchar *out, *out_aux;
+	gsize bytes_read, bytes_read_aux;
+	gsize bytes_written, bytes_written_aux;
+	GConverterResult res;
 	GError *err;
 
+	if (enc != NULL)
+	{
+		encodings = NULL;
+		encodings = g_slist_prepend (encodings, (gpointer)gedit_encoding_get_from_charset (enc));
+	}
+
 	converter = gedit_smart_charset_converter_new (encodings);
 
 	out = g_malloc (200);
+	out_aux = g_malloc (200);
 	err = NULL;
+	bytes_read_aux = 0;
+	bytes_written_aux = 0;
 
-	g_converter_convert (G_CONVERTER (converter),
-	                     test_in,
-	                     nread,
-	                     out,
-	                     200,
-	                     G_CONVERTER_INPUT_AT_END,
-	                     &bytes_read,
-	                     &bytes_written,
-	                     &err);
-
-	g_assert (err == NULL);
-	out[bytes_written] = '\0';
+	do
+	{
+		res = g_converter_convert (G_CONVERTER (converter),
+		                           test_in + bytes_read_aux,
+		                           nread,
+		                           out_aux,
+		                           200,
+		                           G_CONVERTER_INPUT_AT_END,
+		                           &bytes_read,
+		                           &bytes_written,
+		                           &err);
+		memcpy (out + bytes_written_aux, out_aux, bytes_written);
+		bytes_read_aux += bytes_read;
+		bytes_written_aux += bytes_written;
+		nread -= bytes_read;
+	} while (res != G_CONVERTER_FINISHED && res != G_CONVERTER_ERROR);
+
+	g_assert_no_error (err);
+	out[bytes_written_aux] = '\0';
 
 	g_assert_cmpstr (out, ==, test_out);
 }
 
-static void
-do_test_with_error (const gchar *test_in,
-                    GSList      *encodings,
-                    gint         error_code)
+static void
+do_test_roundtrip (const char *str, const char *charset)
 {
-	GeditSmartCharsetConverter *converter;
-	gchar *out;
-	gsize len;
-	gsize bytes_read;
-	gsize bytes_written;
+	gsize len;
+	gchar *buf, *p;
+	GInputStream *in, *tmp;
+	GCharsetConverter *c1;
+	GeditSmartCharsetConverter *c2;
+	gsize n, tot;
 	GError *err;
+	GSList *enc = NULL;
 
-	converter = gedit_smart_charset_converter_new (encodings);
+	len = strlen(str);
+	buf = g_new0 (char, len);
 
-	out = g_malloc (200);
-	len = strlen (test_in);
-	err = NULL;
+	in = g_memory_input_stream_new_from_data (str, -1, NULL);
 
-	g_converter_convert (G_CONVERTER (converter),
-	                     test_in,
-	                     len,
-	                     out,
-	                     200,
-	                     G_CONVERTER_INPUT_AT_END,
-	                     &bytes_read,
-	                     &bytes_written,
-	                     &err);
-
-	g_assert (err->code == error_code);
-}
-#if 0
-static void
-do_test_few_memory (const gchar *test_in,
-                    const gchar *test_out,
-                    GeditNewlineConverterNewlineType type,
-                    gint memory) /* less than 200 */
-{
-  GeditConverter *converter;
-  GConverterResult res;
-  gchar *out;
-  gchar *out_aux;
-  gsize len;
-  gsize bytes_read;
-  gsize bytes_read_aux;
-  gsize bytes_written;
-  gsize bytes_written_aux;
-  GError *err;
-  gint i;
-
-  converter = gedit_newline_converter_new ();
-  gedit_newline_converter_set_newline_type (converter, type);
-
-  out = g_malloc (200);
-  out_aux = g_malloc (memory);
-  len = strlen (test_in);
-  err = NULL;
-  i = 0;
-  bytes_read_aux = 0;
-  bytes_written_aux = 0;
-
-  do
-    {
-      res = g_converter_convert (G_CONVERTER (converter),
-                                 test_in + bytes_read_aux,
-                                 len,
-                                 out_aux,
-                                 memory,
-                                 G_CONVERTER_INPUT_AT_END,
-                                 &bytes_read,
-                                 &bytes_written,
-                                 &err);
-      memcpy (out + bytes_written_aux, out_aux, bytes_written);
-      bytes_read_aux += bytes_read;
-      bytes_written_aux += bytes_written;
-      len -= bytes_read;
-    } while (res != G_CONVERTER_FINISHED && res != G_CONVERTER_ERROR);
-
-  g_assert (err == NULL);
-  out[bytes_written_aux] = '\0';
-  g_assert_cmpstr (out, ==, test_out);
+	c1 = g_charset_converter_new (charset, "UTF-8", NULL);
+
+	tmp = in;
+	in = g_converter_input_stream_new (in, G_CONVERTER (c1));
+	g_object_unref (tmp);
+	g_object_unref (c1);
+
+	enc = g_slist_prepend (enc, (gpointer)gedit_encoding_get_from_charset (charset));
+	c2 = gedit_smart_charset_converter_new (enc);
+	g_slist_free (enc);
+
+	tmp = in;
+	in = g_converter_input_stream_new (in, G_CONVERTER (c2));
+	g_object_unref (tmp);
+	g_object_unref (c2);
+
+	tot = 0;
+	p = buf;
+	n = len;
+	while (TRUE)
+	{
+		gssize res;
+
+		err = NULL;
+		res = g_input_stream_read (in, p, n, NULL, &err);
+		g_assert_no_error (err);
+		if (res == 0)
+		break;
+
+		p += res;
+		n -= res;
+		tot += res;
+	}
+
+	g_assert_cmpint (tot, ==, len);
+	g_assert_cmpstr (str, ==, buf);
+
+	g_free (buf);
+	g_object_unref (in);
 }
-#endif
 
 static void
 test_utf8_utf8 ()
 {
-	GSList *encs = NULL;
-
-	encs = g_slist_prepend (encs, (gpointer)gedit_encoding_get_utf8 ());
-
-	do_test (TEXT_TO_CONVERT, encs, strlen (TEXT_TO_CONVERT), TEXT_TO_CONVERT);
+	do_test (TEXT_TO_CONVERT, "UTF-8", NULL, strlen (TEXT_TO_CONVERT), TEXT_TO_CONVERT);
 
-	do_test ("foobar\xc3\xa8\xc3\xa8\xc3\xa8zzzzzz", encs, 18, "foobar\xc3\xa8\xc3\xa8\xc3\xa8zzzzzz");
-	do_test ("foobar\xc3\xa8\xc3\xa8\xc3\xa8zzzzzz", encs, 9, "foobar\xc3\xa8\xc3");
+	do_test ("foobar\xc3\xa8\xc3\xa8\xc3\xa8zzzzzz", "UTF-8", NULL, 18, "foobar\xc3\xa8\xc3\xa8\xc3\xa8zzzzzz");
+	do_test ("foobar\xc3\xa8\xc3\xa8\xc3\xa8zzzzzz", "UTF-8", NULL, 9, "foobar\xc3\xa8\xc3");
 
 	/* FIXME: Use the utf8 stream for a fallback? */
 	//do_test_with_error ("\xef\xbf\xbezzzzzz", encs, G_IO_ERROR_FAILED);
-
-	g_slist_free (encs);
 }
 
 static void
@@ -228,16 +183,10 @@ test_xxx_xxx ()
 	   are done ok */
 	for (l = encs; l != NULL; l = g_slist_next (l))
 	{
-		gchar *text;
-		GSList *test_enc = NULL;
-
-		text = get_text_with_encoding (TEXT_TO_CONVERT, (const GeditEncoding *)l->data);
-		test_enc = g_slist_prepend (test_enc, l->data);
-
-		//do_test (text, test_enc, TEXT_TO_CONVERT);
-		g_slist_free (test_enc);
-		g_free (text);
+		do_test_roundtrip (TEXT_TO_CONVERT, gedit_encoding_get_charset ((const GeditEncoding *)l->data));
 	}
+
+	g_slist_free (encs);
 }
 
 int main (int   argc,
@@ -247,7 +196,7 @@ int main (int   argc,
 	g_test_init (&argc, &argv, NULL);
 
 	g_test_add_func ("/smart-converter/utf8-utf8", test_utf8_utf8);
-	//g_test_add_func ("/smart-converter/xxx-xxx", test_xxx_xxx);
+	g_test_add_func ("/smart-converter/xxx-xxx", test_xxx_xxx);
 
 	return g_test_run ();
 }



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]