1 /*
2  * document-loader.c
3  * This file is part of pluma
4  *
5  * Copyright (C) 2010 - Jesse van den Kieboom
6  * Copyright (C) 2012-2021 MATE Developers
7  *
8  * pluma is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * pluma is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with pluma; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor,
21  * Boston, MA  02110-1301  USA
22  */
23 
24 #include "pluma-document-loader.h"
25 #include <gio/gio.h>
26 #include <gtk/gtk.h>
27 #include <glib.h>
28 #include <string.h>
29 
30 static gboolean test_completed;
31 
32 typedef struct
33 {
34 	const gchar   *in_buffer;
35 	gint           newline_type;
36 	GFile         *file;
37 } LoaderTestData;
38 
39 static GFile *
create_document(const gchar * filename,const gchar * contents)40 create_document (const gchar *filename,
41                  const gchar *contents)
42 {
43 	GError *error = NULL;
44 
45 	if (!g_file_set_contents (filename, contents, -1, &error))
46 	{
47 		g_assert_no_error (error);
48 	}
49 
50 	return g_file_new_for_path (filename);
51 }
52 
53 static void
delete_document(GFile * location)54 delete_document (GFile *location)
55 {
56 	if (g_file_query_exists (location, NULL))
57 	{
58 		GError *err = NULL;
59 
60 		g_file_delete (location, NULL, &err);
61 		g_assert_no_error (err);
62 	}
63 
64 	test_completed = TRUE;
65 }
66 
67 static void
on_document_loaded(PlumaDocument * document,GError * error,LoaderTestData * data)68 on_document_loaded (PlumaDocument  *document,
69                     GError         *error,
70                     LoaderTestData *data)
71 {
72 	GtkTextIter start, end;
73 
74 	g_assert_no_error (error);
75 
76 	if (data->in_buffer != NULL)
77 	{
78 		gchar *text;
79 
80 		gtk_text_buffer_get_bounds (GTK_TEXT_BUFFER (document), &start, &end);
81 		text = gtk_text_iter_get_slice (&start, &end);
82 
83 		g_assert_cmpstr (text, ==, data->in_buffer);
84 
85 		g_free (text);
86 	}
87 
88 	if (data->newline_type != -1)
89 	{
90 		g_assert_cmpint (pluma_document_get_newline_type (document),
91 		                 ==,
92 		                 data->newline_type);
93 	}
94 
95 	delete_document (data->file);
96 }
97 
98 static void
test_loader(const gchar * filename,const gchar * contents,const gchar * in_buffer,gint newline_type)99 test_loader (const gchar *filename,
100              const gchar *contents,
101              const gchar *in_buffer,
102              gint         newline_type)
103 {
104 	GFile *file;
105 	gchar *uri;
106 	PlumaDocument *document;
107 
108 	file = create_document (filename, contents);
109 
110 	document = pluma_document_new ();
111 
112 	LoaderTestData *data = g_slice_new (LoaderTestData);
113 	data->in_buffer = in_buffer;
114 	data->newline_type = newline_type;
115 	data->file = file;
116 
117 	test_completed = FALSE;
118 
119 	g_signal_connect (document,
120 	                  "loaded",
121 	                  G_CALLBACK (on_document_loaded),
122 	                  data);
123 
124 	uri = g_file_get_uri (file);
125 
126 	pluma_document_load (document, uri, pluma_encoding_get_utf8 (), 0, FALSE);
127 
128 	g_free (uri);
129 
130 	while (!test_completed)
131 	{
132 		g_main_context_iteration (NULL, TRUE);
133 	}
134 
135 	g_slice_free (LoaderTestData, data);
136 	g_object_unref (file);
137 	g_object_unref (document);
138 }
139 
140 static void
test_end_line_stripping()141 test_end_line_stripping ()
142 {
143 	test_loader ("document-loader.txt",
144 	             "hello world\n",
145 	             "hello world",
146 	             -1);
147 
148 	test_loader ("document-loader.txt",
149 	             "hello world",
150 	             "hello world",
151 	             -1);
152 
153 	test_loader ("document-loader.txt",
154 	             "\nhello world",
155 	             "\nhello world",
156 	             -1);
157 
158 	test_loader ("document-loader.txt",
159 	             "\nhello world\n",
160 	             "\nhello world",
161 	             -1);
162 
163 	test_loader ("document-loader.txt",
164 	             "hello world\n\n",
165 	             "hello world\n",
166 	             -1);
167 
168 	test_loader ("document-loader.txt",
169 	             "hello world\r\n",
170 	             "hello world",
171 	             -1);
172 
173 	test_loader ("document-loader.txt",
174 	             "hello world\r\n\r\n",
175 	             "hello world\r\n",
176 	             -1);
177 
178 	test_loader ("document-loader.txt",
179 	             "\n",
180 	             "",
181 	             -1);
182 
183 	test_loader ("document-loader.txt",
184 	             "\r\n",
185 	             "",
186 	             -1);
187 
188 	test_loader ("document-loader.txt",
189 	             "\n\n",
190 	             "\n",
191 	             -1);
192 
193 	test_loader ("document-loader.txt",
194 	             "\r\n\r\n",
195 	             "\r\n",
196 	             -1);
197 }
198 
199 static void
test_end_new_line_detection()200 test_end_new_line_detection ()
201 {
202 	test_loader ("document-loader.txt",
203 	             "hello world\n",
204 	             NULL,
205 	             PLUMA_DOCUMENT_NEWLINE_TYPE_LF);
206 
207 	test_loader ("document-loader.txt",
208 	             "hello world\r\n",
209 	             NULL,
210 	             PLUMA_DOCUMENT_NEWLINE_TYPE_CR_LF);
211 
212 	test_loader ("document-loader.txt",
213 	             "hello world\r",
214 	             NULL,
215 	             PLUMA_DOCUMENT_NEWLINE_TYPE_CR);
216 }
217 
218 static void
test_begin_new_line_detection()219 test_begin_new_line_detection ()
220 {
221 	test_loader ("document-loader.txt",
222 	             "\nhello world",
223 	             NULL,
224 	             PLUMA_DOCUMENT_NEWLINE_TYPE_LF);
225 
226 	test_loader ("document-loader.txt",
227 	             "\r\nhello world",
228 	             NULL,
229 	             PLUMA_DOCUMENT_NEWLINE_TYPE_CR_LF);
230 
231 	test_loader ("document-loader.txt",
232 	             "\rhello world",
233 	             NULL,
234 	             PLUMA_DOCUMENT_NEWLINE_TYPE_CR);
235 }
236 
main(int argc,char * argv[])237 int main (int   argc,
238           char *argv[])
239 {
240 	g_test_init (&argc, &argv, NULL);
241 
242 	g_test_add_func ("/document-loader/end-line-stripping", test_end_line_stripping);
243 	g_test_add_func ("/document-loader/end-new-line-detection", test_end_new_line_detection);
244 	g_test_add_func ("/document-loader/begin-new-line-detection", test_begin_new_line_detection);
245 
246 	return g_test_run ();
247 }
248