Blame testsuite/test-buffer-output-stream.c

Packit a7d494
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; coding: utf-8 -*- */
Packit a7d494
/* test-buffer-output-stream.c
Packit a7d494
 * This file is part of GtkSourceView
Packit a7d494
 *
Packit a7d494
 * Copyright (C) 2010 - Ignacio Casal Quinteiro
Packit a7d494
 * Copyright (C) 2014 - Sébastien Wilmet
Packit a7d494
 *
Packit a7d494
 * GtkSourceView is free software; you can redistribute it and/or
Packit a7d494
 * modify it under the terms of the GNU Lesser General Public
Packit a7d494
 * License as published by the Free Software Foundation; either
Packit a7d494
 * version 2.1 of the License, or (at your option) any later version.
Packit a7d494
 *
Packit a7d494
 * GtkSourceView is distributed in the hope that it will be useful,
Packit a7d494
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit a7d494
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit a7d494
 * Lesser General Public License for more details.
Packit a7d494
 *
Packit a7d494
 * You should have received a copy of the GNU Lesser General Public
Packit a7d494
 * License along with this library; if not, write to the Free Software
Packit a7d494
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
Packit a7d494
 */
Packit a7d494
Packit a7d494
#include <glib.h>
Packit a7d494
#include <glib/gprintf.h>
Packit a7d494
#include <gtk/gtk.h>
Packit a7d494
#include <string.h>
Packit a7d494
#include <gtksourceview/gtksource.h>
Packit a7d494
#include "gtksourceview/gtksourcebufferoutputstream.h"
Packit a7d494
Packit a7d494
static void
Packit a7d494
test_consecutive_write (const gchar          *inbuf,
Packit a7d494
			const gchar          *outbuf,
Packit a7d494
			gsize                 write_chunk_len,
Packit a7d494
			GtkSourceNewlineType  newline_type)
Packit a7d494
{
Packit a7d494
	GtkSourceBuffer *source_buffer;
Packit a7d494
	GtkSourceBufferOutputStream *out;
Packit a7d494
	gsize len;
Packit a7d494
	gssize n, w;
Packit a7d494
	GError *err = NULL;
Packit a7d494
	gchar *b;
Packit a7d494
	GtkSourceNewlineType type;
Packit a7d494
	GSList *encodings = NULL;
Packit a7d494
Packit a7d494
	source_buffer = gtk_source_buffer_new (NULL);
Packit a7d494
	encodings = g_slist_prepend (encodings, (gpointer)gtk_source_encoding_get_utf8 ());
Packit a7d494
	out = gtk_source_buffer_output_stream_new (source_buffer, encodings, TRUE);
Packit a7d494
Packit a7d494
	n = 0;
Packit a7d494
Packit a7d494
	do
Packit a7d494
	{
Packit a7d494
		len = MIN (write_chunk_len, strlen (inbuf + n));
Packit a7d494
		w = g_output_stream_write (G_OUTPUT_STREAM (out), inbuf + n, len, NULL, &err;;
Packit a7d494
		g_assert_cmpint (w, >=, 0);
Packit a7d494
		g_assert_no_error (err);
Packit a7d494
Packit a7d494
		n += w;
Packit a7d494
	} while (w != 0);
Packit a7d494
Packit a7d494
	g_output_stream_flush (G_OUTPUT_STREAM (out), NULL, &err;;
Packit a7d494
Packit a7d494
	g_assert_no_error (err);
Packit a7d494
Packit a7d494
	type = gtk_source_buffer_output_stream_detect_newline_type (out);
Packit a7d494
	g_assert (type == newline_type);
Packit a7d494
Packit a7d494
	g_output_stream_close (G_OUTPUT_STREAM (out), NULL, &err;;
Packit a7d494
	g_assert_no_error (err);
Packit a7d494
Packit a7d494
	g_object_get (G_OBJECT (source_buffer), "text", &b, NULL);
Packit a7d494
Packit a7d494
	g_assert_cmpstr (outbuf, ==, b);
Packit a7d494
	g_free (b);
Packit a7d494
Packit a7d494
	g_assert (gtk_text_buffer_get_modified (GTK_TEXT_BUFFER (source_buffer)) == FALSE);
Packit a7d494
Packit a7d494
	g_object_unref (source_buffer);
Packit a7d494
	g_object_unref (out);
Packit a7d494
}
Packit a7d494
Packit a7d494
static void
Packit a7d494
test_empty (void)
Packit a7d494
{
Packit a7d494
	test_consecutive_write ("", "", 10, GTK_SOURCE_NEWLINE_TYPE_DEFAULT);
Packit a7d494
	test_consecutive_write ("\r\n", "", 10, GTK_SOURCE_NEWLINE_TYPE_CR_LF);
Packit a7d494
	test_consecutive_write ("\r", "", 10, GTK_SOURCE_NEWLINE_TYPE_CR);
Packit a7d494
	test_consecutive_write ("\n", "", 10, GTK_SOURCE_NEWLINE_TYPE_LF);
Packit a7d494
}
Packit a7d494
Packit a7d494
static void
Packit a7d494
test_consecutive (void)
Packit a7d494
{
Packit a7d494
	test_consecutive_write ("hello\nhow\nare\nyou", "hello\nhow\nare\nyou", 2,
Packit a7d494
				GTK_SOURCE_NEWLINE_TYPE_LF);
Packit a7d494
	test_consecutive_write ("hello\rhow\rare\ryou", "hello\rhow\rare\ryou", 2,
Packit a7d494
				GTK_SOURCE_NEWLINE_TYPE_CR);
Packit a7d494
	test_consecutive_write ("hello\r\nhow\r\nare\r\nyou", "hello\r\nhow\r\nare\r\nyou", 2,
Packit a7d494
				GTK_SOURCE_NEWLINE_TYPE_CR_LF);
Packit a7d494
}
Packit a7d494
Packit a7d494
static void
Packit a7d494
test_consecutive_tnewline (void)
Packit a7d494
{
Packit a7d494
	test_consecutive_write ("hello\nhow\nare\nyou\n", "hello\nhow\nare\nyou", 2,
Packit a7d494
				GTK_SOURCE_NEWLINE_TYPE_LF);
Packit a7d494
	test_consecutive_write ("hello\rhow\rare\ryou\r", "hello\rhow\rare\ryou", 2,
Packit a7d494
				GTK_SOURCE_NEWLINE_TYPE_CR);
Packit a7d494
	test_consecutive_write ("hello\r\nhow\r\nare\r\nyou\r\n", "hello\r\nhow\r\nare\r\nyou", 2,
Packit a7d494
				GTK_SOURCE_NEWLINE_TYPE_CR_LF);
Packit a7d494
}
Packit a7d494
Packit a7d494
static void
Packit a7d494
test_big_char (void)
Packit a7d494
{
Packit a7d494
	test_consecutive_write ("\343\203\200\343\203\200", "\343\203\200\343\203\200", 2,
Packit a7d494
				GTK_SOURCE_NEWLINE_TYPE_LF);
Packit a7d494
}
Packit a7d494
Packit a7d494
static void
Packit a7d494
test_boundary (void)
Packit a7d494
{
Packit a7d494
	GtkSourceBuffer *source_buffer;
Packit a7d494
	GtkSourceBufferOutputStream *out;
Packit a7d494
	gint line_count;
Packit a7d494
	GError *err = NULL;
Packit a7d494
	GSList *encodings = NULL;
Packit a7d494
Packit a7d494
	source_buffer = gtk_source_buffer_new (NULL);
Packit a7d494
	encodings = g_slist_prepend (encodings, (gpointer)gtk_source_encoding_get_utf8 ());
Packit a7d494
	out = gtk_source_buffer_output_stream_new (source_buffer, encodings, TRUE);
Packit a7d494
Packit a7d494
	g_output_stream_write (G_OUTPUT_STREAM (out), "\r", 1, NULL, NULL);
Packit a7d494
	g_output_stream_write (G_OUTPUT_STREAM (out), "\n", 1, NULL, NULL);
Packit a7d494
Packit a7d494
	g_output_stream_flush (G_OUTPUT_STREAM (out), NULL, &err;;
Packit a7d494
	g_assert_no_error (err);
Packit a7d494
Packit a7d494
	line_count = gtk_text_buffer_get_line_count (GTK_TEXT_BUFFER (source_buffer));
Packit a7d494
Packit a7d494
	g_assert_cmpint (line_count, ==, 2);
Packit a7d494
Packit a7d494
	g_output_stream_close (G_OUTPUT_STREAM (out), NULL, &err;;
Packit a7d494
	g_assert_no_error (err);
Packit a7d494
Packit a7d494
	g_object_unref (source_buffer);
Packit a7d494
	g_object_unref (out);
Packit a7d494
}
Packit a7d494
Packit a7d494
#if 0
Packit a7d494
static void
Packit a7d494
test_invalid_utf8 (void)
Packit a7d494
{
Packit a7d494
	test_consecutive_write ("foobar\n\xef\xbf\xbe", "foobar\n\\EF\\BF\\BE", 10,
Packit a7d494
	                        GTK_SOURCE_NEWLINE_TYPE_LF);
Packit a7d494
	test_consecutive_write ("foobar\n\xef\xbf\xbezzzzzz\n", "foobar\n\\EF\\BF\\BEzzzzzz", 10,
Packit a7d494
	                        GTK_SOURCE_NEWLINE_TYPE_LF);
Packit a7d494
	test_consecutive_write ("\xef\xbf\xbezzzzzz\n", "\\EF\\BF\\BEzzzzzz", 10,
Packit a7d494
	                        GTK_SOURCE_NEWLINE_TYPE_LF);
Packit a7d494
}
Packit a7d494
#endif
Packit a7d494
Packit a7d494
/* SMART CONVERSION */
Packit a7d494
Packit a7d494
#define TEXT_TO_CONVERT "this is some text to make the tests"
Packit a7d494
#define TEXT_TO_GUESS "hello \xe6\x96\x87 world"
Packit a7d494
Packit a7d494
static gchar *
Packit a7d494
get_encoded_text (const gchar             *text,
Packit a7d494
		  gint                     nread,
Packit a7d494
		  const GtkSourceEncoding *to,
Packit a7d494
		  const GtkSourceEncoding *from,
Packit a7d494
		  gsize                   *bytes_written_aux,
Packit a7d494
		  gboolean                 care_about_error)
Packit a7d494
{
Packit a7d494
	GCharsetConverter *converter;
Packit a7d494
	gchar *out, *out_aux;
Packit a7d494
	gsize bytes_read, bytes_read_aux;
Packit a7d494
	gsize bytes_written;
Packit a7d494
	GConverterResult res;
Packit a7d494
	GError *err;
Packit a7d494
Packit a7d494
	converter = g_charset_converter_new (gtk_source_encoding_get_charset (to),
Packit a7d494
					     gtk_source_encoding_get_charset (from),
Packit a7d494
					     NULL);
Packit a7d494
Packit a7d494
	out = g_malloc (200);
Packit a7d494
	out_aux = g_malloc (200);
Packit a7d494
	err = NULL;
Packit a7d494
	bytes_read_aux = 0;
Packit a7d494
	*bytes_written_aux = 0;
Packit a7d494
Packit a7d494
	if (nread == -1)
Packit a7d494
	{
Packit a7d494
		nread = strlen (text);
Packit a7d494
	}
Packit a7d494
Packit a7d494
	do
Packit a7d494
	{
Packit a7d494
		res = g_converter_convert (G_CONVERTER (converter),
Packit a7d494
		                           text + bytes_read_aux,
Packit a7d494
		                           nread,
Packit a7d494
		                           out_aux,
Packit a7d494
		                           200,
Packit a7d494
		                           G_CONVERTER_INPUT_AT_END,
Packit a7d494
		                           &bytes_read,
Packit a7d494
		                           &bytes_written,
Packit a7d494
		                           &err;;
Packit a7d494
		memcpy (out + *bytes_written_aux, out_aux, bytes_written);
Packit a7d494
		bytes_read_aux += bytes_read;
Packit a7d494
		*bytes_written_aux += bytes_written;
Packit a7d494
		nread -= bytes_read;
Packit a7d494
	} while (res != G_CONVERTER_FINISHED && res != G_CONVERTER_ERROR);
Packit a7d494
Packit a7d494
	if (care_about_error)
Packit a7d494
	{
Packit a7d494
		g_assert_no_error (err);
Packit a7d494
	}
Packit a7d494
	else if (err)
Packit a7d494
	{
Packit a7d494
		g_printf ("** You don't care, but there was an error: %s", err->message);
Packit a7d494
		return NULL;
Packit a7d494
	}
Packit a7d494
Packit a7d494
	out[*bytes_written_aux] = '\0';
Packit a7d494
Packit a7d494
	if (!g_utf8_validate (out, *bytes_written_aux, NULL) && !care_about_error)
Packit a7d494
	{
Packit a7d494
		if (!care_about_error)
Packit a7d494
		{
Packit a7d494
			return NULL;
Packit a7d494
		}
Packit a7d494
		else
Packit a7d494
		{
Packit a7d494
			g_assert_not_reached ();
Packit a7d494
		}
Packit a7d494
	}
Packit a7d494
Packit a7d494
	return out;
Packit a7d494
}
Packit a7d494
Packit a7d494
static gchar *
Packit a7d494
do_test (const gchar              *inbuf,
Packit a7d494
	 const gchar              *enc,
Packit a7d494
	 GSList                   *encodings,
Packit a7d494
	 gsize                     len,
Packit a7d494
	 gsize                     write_chunk_len,
Packit a7d494
	 const GtkSourceEncoding **guessed)
Packit a7d494
{
Packit a7d494
	GtkSourceBuffer *source_buffer;
Packit a7d494
	GtkSourceBufferOutputStream *out;
Packit a7d494
	GError *err = NULL;
Packit a7d494
	GtkTextIter start, end;
Packit a7d494
	gchar *text;
Packit a7d494
	gsize to_write;
Packit a7d494
	gssize n, w;
Packit a7d494
Packit a7d494
	if (enc != NULL)
Packit a7d494
	{
Packit a7d494
		encodings = NULL;
Packit a7d494
		encodings = g_slist_prepend (encodings, (gpointer)gtk_source_encoding_get_from_charset (enc));
Packit a7d494
	}
Packit a7d494
Packit a7d494
	source_buffer = gtk_source_buffer_new (NULL);
Packit a7d494
	out = gtk_source_buffer_output_stream_new (source_buffer, encodings, TRUE);
Packit a7d494
Packit a7d494
	n = 0;
Packit a7d494
Packit a7d494
	do
Packit a7d494
	{
Packit a7d494
		to_write = MIN (len, write_chunk_len);
Packit a7d494
		w = g_output_stream_write (G_OUTPUT_STREAM (out), inbuf + n, to_write, NULL, &err;;
Packit a7d494
		g_assert_cmpint (w, >=, 0);
Packit a7d494
		g_assert_no_error (err);
Packit a7d494
Packit a7d494
		len -= w;
Packit a7d494
		n += w;
Packit a7d494
	} while (len != 0);
Packit a7d494
Packit a7d494
	g_output_stream_flush (G_OUTPUT_STREAM (out), NULL, &err;;
Packit a7d494
	g_assert_no_error (err);
Packit a7d494
Packit a7d494
	g_output_stream_close (G_OUTPUT_STREAM (out), NULL, &err;;
Packit a7d494
	g_assert_no_error (err);
Packit a7d494
Packit a7d494
	if (guessed != NULL)
Packit a7d494
	{
Packit a7d494
		*guessed = gtk_source_buffer_output_stream_get_guessed (out);
Packit a7d494
	}
Packit a7d494
Packit a7d494
	gtk_text_buffer_get_bounds (GTK_TEXT_BUFFER (source_buffer), &start, &end;;
Packit a7d494
	text = gtk_text_buffer_get_text (GTK_TEXT_BUFFER (source_buffer),
Packit a7d494
	                                 &start,
Packit a7d494
	                                 &end,
Packit a7d494
	                                 FALSE);
Packit a7d494
Packit a7d494
	g_object_unref (source_buffer);
Packit a7d494
	g_object_unref (out);
Packit a7d494
Packit a7d494
	return text;
Packit a7d494
}
Packit a7d494
Packit a7d494
static void
Packit a7d494
test_utf8_utf8 (void)
Packit a7d494
{
Packit a7d494
	gchar *aux;
Packit a7d494
Packit a7d494
	aux = do_test (TEXT_TO_CONVERT, "UTF-8", NULL, strlen (TEXT_TO_CONVERT), strlen (TEXT_TO_CONVERT), NULL);
Packit a7d494
	g_assert_cmpstr (aux, ==, TEXT_TO_CONVERT);
Packit a7d494
	g_free (aux);
Packit a7d494
Packit a7d494
	aux = do_test ("foobar\xc3\xa8\xc3\xa8\xc3\xa8zzzzzz", "UTF-8", NULL, 18, 18, NULL);
Packit a7d494
	g_assert_cmpstr (aux, ==, "foobar\xc3\xa8\xc3\xa8\xc3\xa8zzzzzz");
Packit a7d494
	g_free (aux);
Packit a7d494
Packit a7d494
	/* small chunk */
Packit a7d494
	aux = do_test ("foobar\xc3\xa8\xc3\xa8\xc3\xa8zzzzzz", "UTF-8", NULL, 18, 2, NULL);
Packit a7d494
	g_assert_cmpstr (aux, ==, "foobar\xc3\xa8\xc3\xa8\xc3\xa8zzzzzz");
Packit a7d494
	g_free (aux);
Packit a7d494
}
Packit a7d494
Packit a7d494
static void
Packit a7d494
test_empty_conversion (void)
Packit a7d494
{
Packit a7d494
	const GtkSourceEncoding *guessed;
Packit a7d494
	gchar *out;
Packit a7d494
	GSList *encodings = NULL;
Packit a7d494
Packit a7d494
	/* testing the case of an empty file and list of encodings with no
Packit a7d494
	   utf-8. In this case, the smart converter cannot determine the right
Packit a7d494
	   encoding (because there is no input), but should still default to
Packit a7d494
	   utf-8 for the detection */
Packit a7d494
	encodings = g_slist_prepend (encodings, (gpointer)gtk_source_encoding_get_from_charset ("UTF-16"));
Packit a7d494
	encodings = g_slist_prepend (encodings, (gpointer)gtk_source_encoding_get_from_charset ("ISO-8859-15"));
Packit a7d494
Packit a7d494
	out = do_test ("", NULL, encodings, 0, 0, &guessed);
Packit a7d494
Packit a7d494
	g_assert_cmpstr (out, ==, "");
Packit a7d494
	g_free (out);
Packit a7d494
Packit a7d494
	g_assert (guessed == gtk_source_encoding_get_utf8 ());
Packit a7d494
}
Packit a7d494
Packit a7d494
static void
Packit a7d494
test_guessed (void)
Packit a7d494
{
Packit a7d494
	GSList *encs = NULL;
Packit a7d494
	gchar *aux, *aux2, *fail;
Packit a7d494
	gsize aux_len, fail_len;
Packit a7d494
	const GtkSourceEncoding *guessed;
Packit a7d494
Packit a7d494
	aux = get_encoded_text (TEXT_TO_GUESS, -1,
Packit a7d494
	                        gtk_source_encoding_get_from_charset ("UTF-16"),
Packit a7d494
	                        gtk_source_encoding_get_from_charset ("UTF-8"),
Packit a7d494
	                        &aux_len,
Packit a7d494
	                        TRUE);
Packit a7d494
Packit a7d494
	fail = get_encoded_text (aux, aux_len,
Packit a7d494
	                         gtk_source_encoding_get_from_charset ("UTF-8"),
Packit a7d494
	                         gtk_source_encoding_get_from_charset ("ISO-8859-15"),
Packit a7d494
	                         &fail_len,
Packit a7d494
	                         FALSE);
Packit a7d494
Packit a7d494
	g_assert (fail == NULL);
Packit a7d494
Packit a7d494
	/* ISO-8859-15 should fail */
Packit a7d494
	encs = g_slist_append (encs, (gpointer)gtk_source_encoding_get_from_charset ("ISO-8859-15"));
Packit a7d494
	encs = g_slist_append (encs, (gpointer)gtk_source_encoding_get_from_charset ("UTF-16"));
Packit a7d494
Packit a7d494
	aux2 = do_test (aux, NULL, encs, aux_len, aux_len, &guessed);
Packit a7d494
	g_free (aux);
Packit a7d494
	g_free (aux2);
Packit a7d494
Packit a7d494
	g_assert (guessed == gtk_source_encoding_get_from_charset ("UTF-16"));
Packit a7d494
}
Packit a7d494
Packit a7d494
static void
Packit a7d494
test_utf16_utf8 (void)
Packit a7d494
{
Packit a7d494
	gchar *text, *aux;
Packit a7d494
	gsize aux_len;
Packit a7d494
Packit a7d494
	text = get_encoded_text ("\xe2\xb4\xb2", -1,
Packit a7d494
	                         gtk_source_encoding_get_from_charset ("UTF-16"),
Packit a7d494
	                         gtk_source_encoding_get_from_charset ("UTF-8"),
Packit a7d494
	                         &aux_len,
Packit a7d494
	                         TRUE);
Packit a7d494
Packit a7d494
	aux = do_test (text, "UTF-16", NULL, aux_len, aux_len, NULL);
Packit a7d494
	g_assert_cmpstr (aux, ==, "\xe2\xb4\xb2");
Packit a7d494
	g_free (aux);
Packit a7d494
Packit a7d494
	aux = do_test (text, "UTF-16", NULL, aux_len, 1, NULL);
Packit a7d494
	g_assert_cmpstr (aux, ==, "\xe2\xb4\xb2");
Packit a7d494
	g_free (aux);
Packit a7d494
}
Packit a7d494
Packit a7d494
gint
Packit a7d494
main (gint   argc,
Packit a7d494
      gchar *argv[])
Packit a7d494
{
Packit a7d494
	g_test_init (&argc, &argv, NULL);
Packit a7d494
Packit a7d494
	g_test_add_func ("/buffer-output-stream/empty", test_empty);
Packit a7d494
Packit a7d494
	g_test_add_func ("/buffer-output-stream/consecutive", test_consecutive);
Packit a7d494
	g_test_add_func ("/buffer-output-stream/consecutive_tnewline", test_consecutive_tnewline);
Packit a7d494
	g_test_add_func ("/buffer-output-stream/big-char", test_big_char);
Packit a7d494
	g_test_add_func ("/buffer-output-stream/test-boundary", test_boundary);
Packit a7d494
Packit a7d494
Packit a7d494
	/* This broke after https://bugzilla.gnome.org/show_bug.cgi?id=694669 We
Packit a7d494
	 * need to revisit the test to pick something that is actually invalid
Packit a7d494
	 * utf8.
Packit a7d494
	 */
Packit a7d494
#if 0
Packit a7d494
	g_test_add_func ("/buffer-output-stream/test-invalid-utf8", test_invalid_utf8);
Packit a7d494
#endif
Packit a7d494
	g_test_add_func ("/buffer-output-stream/smart conversion: utf8-utf8", test_utf8_utf8);
Packit a7d494
	g_test_add_func ("/buffer-output-stream/smart conversion: empty", test_empty_conversion);
Packit a7d494
	g_test_add_func ("/buffer-output-stream/smart conversion: guessed", test_guessed);
Packit a7d494
	g_test_add_func ("/buffer-output-stream/smart conversion: utf16-utf8", test_utf16_utf8);
Packit a7d494
Packit a7d494
	return g_test_run ();
Packit a7d494
}