2011-11-07 13:46:58 -06:00
|
|
|
/*
|
|
|
|
* document-output-stream.c
|
2011-11-07 16:52:18 -06:00
|
|
|
* This file is part of pluma
|
2011-11-07 13:46:58 -06:00
|
|
|
*
|
|
|
|
* Copyright (C) 2010 - Ignacio Casal Quinteiro
|
|
|
|
*
|
2011-11-07 16:52:18 -06:00
|
|
|
* pluma is free software; you can redistribute it and/or modify
|
2011-11-07 13:46:58 -06:00
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
2011-11-07 16:52:18 -06:00
|
|
|
* pluma is distributed in the hope that it will be useful,
|
2011-11-07 13:46:58 -06:00
|
|
|
* 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
|
2011-11-07 16:52:18 -06:00
|
|
|
* along with pluma; if not, write to the Free Software
|
2011-11-07 13:46:58 -06:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2011-11-07 16:52:18 -06:00
|
|
|
#include "pluma-document-output-stream.h"
|
2011-11-07 13:46:58 -06:00
|
|
|
#include <gio/gio.h>
|
|
|
|
#include <gtk/gtk.h>
|
|
|
|
#include <glib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
static void
|
|
|
|
test_consecutive_write (const gchar *inbuf,
|
|
|
|
const gchar *outbuf,
|
|
|
|
gsize write_chunk_len,
|
2011-11-07 16:52:18 -06:00
|
|
|
PlumaDocumentNewlineType newline_type)
|
2011-11-07 13:46:58 -06:00
|
|
|
{
|
2011-11-07 16:52:18 -06:00
|
|
|
PlumaDocument *doc;
|
2011-11-07 13:46:58 -06:00
|
|
|
GOutputStream *out;
|
|
|
|
gsize len;
|
|
|
|
gssize n, w;
|
|
|
|
GError *err = NULL;
|
|
|
|
gchar *b;
|
2011-11-07 16:52:18 -06:00
|
|
|
PlumaDocumentNewlineType type;
|
2011-11-07 13:46:58 -06:00
|
|
|
|
2011-11-07 16:52:18 -06:00
|
|
|
doc = pluma_document_new ();
|
|
|
|
out = pluma_document_output_stream_new (doc);
|
2011-11-07 13:46:58 -06:00
|
|
|
|
|
|
|
n = 0;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
len = MIN (write_chunk_len, strlen (inbuf + n));
|
|
|
|
w = g_output_stream_write (out, inbuf + n, len, NULL, &err);
|
|
|
|
g_assert_cmpint (w, >=, 0);
|
|
|
|
g_assert_no_error (err);
|
|
|
|
|
|
|
|
n += w;
|
|
|
|
} while (w != 0);
|
|
|
|
|
|
|
|
g_object_get (G_OBJECT (doc), "text", &b, NULL);
|
|
|
|
|
|
|
|
g_assert_cmpstr (inbuf, ==, b);
|
|
|
|
g_free (b);
|
|
|
|
|
2011-11-07 16:52:18 -06:00
|
|
|
type = pluma_document_output_stream_detect_newline_type (PLUMA_DOCUMENT_OUTPUT_STREAM (out));
|
2011-11-07 13:46:58 -06:00
|
|
|
g_assert (type == newline_type);
|
|
|
|
|
|
|
|
g_output_stream_close (out, NULL, &err);
|
|
|
|
g_assert_no_error (err);
|
|
|
|
|
|
|
|
g_object_get (G_OBJECT (doc), "text", &b, NULL);
|
|
|
|
|
|
|
|
g_assert_cmpstr (outbuf, ==, b);
|
|
|
|
g_free (b);
|
|
|
|
|
|
|
|
g_assert (gtk_text_buffer_get_modified (GTK_TEXT_BUFFER (doc)) == FALSE);
|
|
|
|
|
|
|
|
g_object_unref (doc);
|
|
|
|
g_object_unref (out);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
test_empty ()
|
|
|
|
{
|
2011-11-07 16:52:18 -06:00
|
|
|
test_consecutive_write ("", "", 10, PLUMA_DOCUMENT_NEWLINE_TYPE_DEFAULT);
|
|
|
|
test_consecutive_write ("\r\n", "", 10, PLUMA_DOCUMENT_NEWLINE_TYPE_CR_LF);
|
|
|
|
test_consecutive_write ("\r", "", 10, PLUMA_DOCUMENT_NEWLINE_TYPE_CR);
|
|
|
|
test_consecutive_write ("\n", "", 10, PLUMA_DOCUMENT_NEWLINE_TYPE_LF);
|
2011-11-07 13:46:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
test_consecutive ()
|
|
|
|
{
|
|
|
|
test_consecutive_write ("hello\nhow\nare\nyou", "hello\nhow\nare\nyou", 2,
|
2011-11-07 16:52:18 -06:00
|
|
|
PLUMA_DOCUMENT_NEWLINE_TYPE_LF);
|
2011-11-07 13:46:58 -06:00
|
|
|
test_consecutive_write ("hello\rhow\rare\ryou", "hello\rhow\rare\ryou", 2,
|
2011-11-07 16:52:18 -06:00
|
|
|
PLUMA_DOCUMENT_NEWLINE_TYPE_CR);
|
2011-11-07 13:46:58 -06:00
|
|
|
test_consecutive_write ("hello\r\nhow\r\nare\r\nyou", "hello\r\nhow\r\nare\r\nyou", 2,
|
2011-11-07 16:52:18 -06:00
|
|
|
PLUMA_DOCUMENT_NEWLINE_TYPE_CR_LF);
|
2011-11-07 13:46:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
test_consecutive_tnewline ()
|
|
|
|
{
|
|
|
|
test_consecutive_write ("hello\nhow\nare\nyou\n", "hello\nhow\nare\nyou", 2,
|
2011-11-07 16:52:18 -06:00
|
|
|
PLUMA_DOCUMENT_NEWLINE_TYPE_LF);
|
2011-11-07 13:46:58 -06:00
|
|
|
test_consecutive_write ("hello\rhow\rare\ryou\r", "hello\rhow\rare\ryou", 2,
|
2011-11-07 16:52:18 -06:00
|
|
|
PLUMA_DOCUMENT_NEWLINE_TYPE_CR);
|
2011-11-07 13:46:58 -06:00
|
|
|
test_consecutive_write ("hello\r\nhow\r\nare\r\nyou\r\n", "hello\r\nhow\r\nare\r\nyou", 2,
|
2011-11-07 16:52:18 -06:00
|
|
|
PLUMA_DOCUMENT_NEWLINE_TYPE_CR_LF);
|
2011-11-07 13:46:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
test_big_char ()
|
|
|
|
{
|
|
|
|
test_consecutive_write ("\343\203\200\343\203\200", "\343\203\200\343\203\200", 2,
|
2011-11-07 16:52:18 -06:00
|
|
|
PLUMA_DOCUMENT_NEWLINE_TYPE_LF);
|
2011-11-07 13:46:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
int main (int argc,
|
|
|
|
char *argv[])
|
|
|
|
{
|
2013-11-04 05:46:45 -06:00
|
|
|
#if !GLIB_CHECK_VERSION (2, 36, 0)
|
2011-11-07 13:46:58 -06:00
|
|
|
g_type_init ();
|
2013-11-04 05:46:45 -06:00
|
|
|
#endif
|
2011-11-07 13:46:58 -06:00
|
|
|
g_test_init (&argc, &argv, NULL);
|
|
|
|
|
2011-11-07 16:52:18 -06:00
|
|
|
pluma_prefs_manager_app_init ();
|
2011-11-07 13:46:58 -06:00
|
|
|
|
|
|
|
g_test_add_func ("/document-output-stream/empty", test_empty);
|
|
|
|
|
|
|
|
g_test_add_func ("/document-output-stream/consecutive", test_consecutive);
|
|
|
|
g_test_add_func ("/document-output-stream/consecutive_tnewline", test_consecutive_tnewline);
|
|
|
|
g_test_add_func ("/document-output-stream/big-char", test_big_char);
|
|
|
|
|
|
|
|
return g_test_run ();
|
|
|
|
}
|