1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
/*
* document-output-stream.c
* This file is part of gedit
*
* Copyright (C) 2010 - Ignacio Casal Quinteiro
*
* gedit 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 2 of the License, or
* (at your option) any later version.
*
* gedit 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 gedit; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301 USA
*/
#include "gedit-document-output-stream.h"
#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,
GeditDocumentNewlineType newline_type)
{
GeditDocument *doc;
GOutputStream *out;
gsize len;
gssize n, w;
GError *err = NULL;
gchar *b;
GeditDocumentNewlineType type;
doc = gedit_document_new ();
out = gedit_document_output_stream_new (doc);
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);
type = gedit_document_output_stream_detect_newline_type (GEDIT_DOCUMENT_OUTPUT_STREAM (out));
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 ()
{
test_consecutive_write ("", "", 10, GEDIT_DOCUMENT_NEWLINE_TYPE_DEFAULT);
test_consecutive_write ("\r\n", "", 10, GEDIT_DOCUMENT_NEWLINE_TYPE_CR_LF);
test_consecutive_write ("\r", "", 10, GEDIT_DOCUMENT_NEWLINE_TYPE_CR);
test_consecutive_write ("\n", "", 10, GEDIT_DOCUMENT_NEWLINE_TYPE_LF);
}
static void
test_consecutive ()
{
test_consecutive_write ("hello\nhow\nare\nyou", "hello\nhow\nare\nyou", 2,
GEDIT_DOCUMENT_NEWLINE_TYPE_LF);
test_consecutive_write ("hello\rhow\rare\ryou", "hello\rhow\rare\ryou", 2,
GEDIT_DOCUMENT_NEWLINE_TYPE_CR);
test_consecutive_write ("hello\r\nhow\r\nare\r\nyou", "hello\r\nhow\r\nare\r\nyou", 2,
GEDIT_DOCUMENT_NEWLINE_TYPE_CR_LF);
}
static void
test_consecutive_tnewline ()
{
test_consecutive_write ("hello\nhow\nare\nyou\n", "hello\nhow\nare\nyou", 2,
GEDIT_DOCUMENT_NEWLINE_TYPE_LF);
test_consecutive_write ("hello\rhow\rare\ryou\r", "hello\rhow\rare\ryou", 2,
GEDIT_DOCUMENT_NEWLINE_TYPE_CR);
test_consecutive_write ("hello\r\nhow\r\nare\r\nyou\r\n", "hello\r\nhow\r\nare\r\nyou", 2,
GEDIT_DOCUMENT_NEWLINE_TYPE_CR_LF);
}
static void
test_big_char ()
{
test_consecutive_write ("\343\203\200\343\203\200", "\343\203\200\343\203\200", 2,
GEDIT_DOCUMENT_NEWLINE_TYPE_LF);
}
int main (int argc,
char *argv[])
{
g_type_init ();
g_test_init (&argc, &argv, NULL);
gedit_prefs_manager_app_init ();
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 ();
}
|