[gtk+/treeview-refactor] Add unit tests for textiter.



commit a2360f979a4c00707088d17b65ed964461aa38c4
Author: Ignacio Casal Quinteiro <icq gnome org>
Date:   Sat Oct 30 18:19:59 2010 +0200

    Add unit tests for textiter.

 gtk/tests/Makefile.am |    4 +
 gtk/tests/textiter.c  |  179 +++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 183 insertions(+), 0 deletions(-)
---
diff --git a/gtk/tests/Makefile.am b/gtk/tests/Makefile.am
index c492ce6..e97a30f 100644
--- a/gtk/tests/Makefile.am
+++ b/gtk/tests/Makefile.am
@@ -80,6 +80,10 @@ TEST_PROGS			+= textbuffer
 textbuffer_SOURCES		 = textbuffer.c pixbuf-init.c
 textbuffer_LDADD		 = $(progs_ldadd)
 
+TEST_PROGS			+= textiter
+textiter_SOURCES		 = textiter.c
+textiter_LDADD			 = $(progs_ldadd)
+
 TEST_PROGS			+= filtermodel
 filtermodel_SOURCES		 = filtermodel.c
 filtermodel_LDADD		 = $(progs_ldadd)
diff --git a/gtk/tests/textiter.c b/gtk/tests/textiter.c
new file mode 100644
index 0000000..9418fbd
--- /dev/null
+++ b/gtk/tests/textiter.c
@@ -0,0 +1,179 @@
+/* GTK - The GIMP Toolkit
+ * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#include "config.h"
+#include <stdio.h>
+#include <string.h>
+
+#include <gtk/gtk.h>
+
+static void
+test_empty_search ()
+{
+  GtkTextBuffer *buffer;
+  GtkTextIter it, s, e;
+  gboolean res;
+
+  buffer = gtk_text_buffer_new (NULL);
+  gtk_text_buffer_set_text (buffer, "This is some foo text", -1);
+
+  /* search from start forward */
+  gtk_text_buffer_get_start_iter (buffer, &it);
+  res = gtk_text_iter_forward_search (&it, "", 0, &s, &e, NULL);
+  g_assert (res);
+  g_assert_cmpint (gtk_text_iter_get_offset (&s), ==, gtk_text_iter_get_offset (&e));
+  g_assert_cmpint (gtk_text_iter_get_offset (&s), ==, 1);
+
+  /* search from end backward */
+  gtk_text_buffer_get_end_iter (buffer, &it);
+  res = gtk_text_iter_backward_search (&it, "", 0, &s, &e, NULL);
+  g_assert (res);
+  g_assert_cmpint (gtk_text_iter_get_offset (&s), ==, gtk_text_iter_get_offset (&e));
+  g_assert_cmpint (gtk_text_iter_get_offset (&s), ==, 20);
+}
+
+static void
+check_found_forward (const gchar *haystack,
+                     const gchar *needle,
+                     GtkTextSearchFlags flags,
+                     int expected_start,
+                     int expected_end,
+                     const gchar *expected_string)
+{
+  GtkTextBuffer *buffer;
+  GtkTextIter i, s, e;
+  gboolean res;
+  gchar *text;
+
+  buffer = gtk_text_buffer_new (NULL);
+
+  gtk_text_buffer_set_text (buffer, haystack, -1);
+
+  /* TODO: add test with limit before, after and in the middle
+     of expected start and end */
+
+  /* search from start forward */
+  gtk_text_buffer_get_start_iter (buffer, &i);
+  res = gtk_text_iter_forward_search (&i, needle, flags, &s, &e, NULL);
+  g_assert (res);
+  g_assert_cmpint (expected_start, ==, gtk_text_iter_get_offset (&s));
+  g_assert_cmpint (expected_end, ==, gtk_text_iter_get_offset (&e));
+  text = gtk_text_iter_get_text (&s, &e);
+  g_assert_cmpstr (expected_string, ==, text);
+  g_free (text);
+
+  g_object_unref (buffer);
+}
+
+static void
+check_found_backward (const gchar *haystack,
+                      const gchar *needle,
+                      GtkTextSearchFlags flags,
+                      int expected_start,
+                      int expected_end,
+                      const gchar *expected_string)
+{
+  GtkTextBuffer *buffer;
+  GtkTextIter i, s, e;
+  gboolean res;
+  gchar *text;
+
+  buffer = gtk_text_buffer_new (NULL);
+
+  gtk_text_buffer_set_text (buffer, haystack, -1);
+
+  /* search from end backward */
+  gtk_text_buffer_get_end_iter (buffer, &i);
+  res = gtk_text_iter_backward_search (&i, needle, flags, &s, &e, NULL);
+  g_assert (res);
+  g_assert_cmpint (expected_start, ==, gtk_text_iter_get_offset (&s));
+  g_assert_cmpint (expected_end, ==, gtk_text_iter_get_offset (&e));
+  text = gtk_text_iter_get_text (&s, &e);
+  g_assert_cmpstr (expected_string, ==, text);
+  g_free (text);
+
+  g_object_unref (buffer);
+}
+
+static void
+check_not_found (const gchar *haystack,
+                 const gchar *needle,
+                 GtkTextSearchFlags flags)
+{
+  GtkTextBuffer *buffer;
+  GtkTextIter i, s, e;
+  gboolean res;
+
+  buffer = gtk_text_buffer_new (NULL);
+
+  gtk_text_buffer_set_text (buffer, haystack, -1);
+
+  /* search from start forward */
+  gtk_text_buffer_get_start_iter (buffer, &i);
+  res = gtk_text_iter_forward_search (&i, needle, flags, &s, &e, NULL);
+  g_assert (res == FALSE);
+
+  /* search from end backward */
+  gtk_text_buffer_get_end_iter (buffer, &i);
+  res = gtk_text_iter_backward_search (&i, needle, flags, &s, &e, NULL);
+  g_assert (res == FALSE);
+
+  g_object_unref (buffer);
+}
+
+static void
+test_search (void)
+{
+  /* simple match */
+  check_found_forward ("This is some foo text", "foo", 0, 13, 16, "foo");
+  check_found_backward ("This is some foo text", "foo", 0, 13, 16, "foo");
+  check_not_found ("This is some foo text", "Foo", 0);
+
+  /* different matches for forward and backward */
+  check_found_forward ("This is some foo foo text", "foo", 0, 13, 16, "foo");
+  check_found_backward ("This is some foo foo text", "foo", 0, 17, 20, "foo");
+
+  /* new lines in the haystack */
+  check_found_forward ("This is some\nfoo text", "foo", 0, 13, 16, "foo");
+  check_found_backward ("This is some\nfoo text", "foo", 0, 13, 16, "foo");
+  check_found_forward ("This is some foo\nfoo text", "foo", 0, 13, 16, "foo");
+  check_found_backward ("This is some foo\nfoo text", "foo", 0, 17, 20, "foo")
+  check_not_found ("This is some\nfoo text", "Foo", 0);
+
+  /* end of buffer */
+  check_found_forward ("This is some\ntext foo", "foo", 0, 18, 21, "foo");
+  check_found_backward ("This is some\ntext foo", "foo", 0, 18, 21, "foo");
+  check_not_found ("This is some\ntext foo", "Foo", 0);
+
+  /* multiple lines in the needle */
+  check_found_forward ("This is some foo\nfoo text", "foo\nfoo", 0, 13, 20, "foo\nfoo");
+  check_found_backward ("This is some foo\nfoo text", "foo\nfoo", 0, 13, 20, "foo\nfoo");
+  check_not_found ("This is some foo\nfoo text", "Foo\nfoo", 0);
+}
+
+int
+main (int argc, char** argv)
+{
+  gtk_test_init (&argc, &argv);
+
+  g_test_add_func ("/TextIter/Search Empty", test_empty_search);
+  g_test_add_func ("/TextIter/Search", test_search);
+
+  return g_test_run();
+}



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