From dca71f66c3cf65537017d5add98617a60235e5d7 Mon Sep 17 00:00:00 2001
From: Přemysl Janouch
Date: Sat, 5 Feb 2011 19:47:07 +0100
Subject: Fix undoing, create a unit test for history.
---
CMakeLists.txt | 3 +-
liblogdiag/ld-diagram.c | 2 +-
tests/diagram.c | 143 ++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 146 insertions(+), 2 deletions(-)
create mode 100644 tests/diagram.c
diff --git a/CMakeLists.txt b/CMakeLists.txt
index ae5b6d5..9e771ce 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -143,7 +143,8 @@ set (liblogdiag_HEADERS
liblogdiag/ld-lua-symbol-private.h)
set (logdiag_TESTS
- point-array)
+ point-array
+ diagram)
set (logdiag_SOURCES
src/ld-window-main.c
diff --git a/liblogdiag/ld-diagram.c b/liblogdiag/ld-diagram.c
index 9acea59..bbe8d35 100644
--- a/liblogdiag/ld-diagram.c
+++ b/liblogdiag/ld-diagram.c
@@ -720,7 +720,7 @@ ld_diagram_undo (LdDiagram *self)
action = self->priv->undo_stack;
self->priv->undo_stack = g_list_remove_link (action, action);
- for (sub = g_list_last (action->data); sub; sub = g_list_previous (sub))
+ for (sub = action->data; sub; sub = g_list_next (sub))
ld_undo_action_undo (sub->data);
self->priv->redo_stack = g_list_concat (action, self->priv->redo_stack);
diff --git a/tests/diagram.c b/tests/diagram.c
new file mode 100644
index 0000000..9638ca2
--- /dev/null
+++ b/tests/diagram.c
@@ -0,0 +1,143 @@
+/*
+ * diagram.c
+ *
+ * This file is a part of logdiag.
+ * Copyright Přemysl Janouch 2011. All rights reserved.
+ *
+ * See the file LICENSE for licensing information.
+ *
+ */
+
+#include
+
+typedef struct
+{
+ LdDiagram *diagram;
+}
+Diagram;
+
+static void
+diagram_setup (Diagram *fixture, gconstpointer test_data)
+{
+ fixture->diagram = ld_diagram_new ();
+}
+
+static void
+diagram_teardown (Diagram *fixture, gconstpointer test_data)
+{
+ g_object_unref (fixture->diagram);
+}
+
+static void
+diagram_test_history (Diagram *fixture, gconstpointer user_data)
+{
+ const gdouble start_x = 1;
+ const guint move_length = 3;
+ LdDiagramObject *object;
+ guint i;
+ gdouble x;
+
+ object = ld_diagram_object_new (NULL);
+ g_object_set (object, "x", start_x, NULL);
+
+ /* Link an object with the diagram. */
+ ld_diagram_insert_object (fixture->diagram, object, 0);
+ g_assert (ld_diagram_can_undo (fixture->diagram) != FALSE);
+
+ /* Create some object actions to undo. */
+ for (i = 0; i++ < move_length;)
+ g_object_set (object, "x", start_x + i, NULL);
+
+ /* Undo them and check the state. */
+ for (i = move_length; i--;)
+ {
+ g_assert (ld_diagram_can_undo (fixture->diagram) != FALSE);
+ ld_diagram_undo (fixture->diagram);
+
+ g_object_get (object, "x", &x, NULL);
+ g_assert_cmpfloat (x, ==, start_x + i);
+ }
+
+ /* Redo them and check the state. */
+ for (i = 0; i++ < move_length;)
+ {
+ g_assert (ld_diagram_can_redo (fixture->diagram) != FALSE);
+ ld_diagram_redo (fixture->diagram);
+
+ g_object_get (object, "x", &x, NULL);
+ g_assert_cmpfloat (x, ==, start_x + i);
+ }
+
+ g_object_unref (object);
+}
+
+static void
+diagram_test_history_grouping (Diagram *fixture, gconstpointer user_data)
+{
+ const LdPoint start_position = {1, 3};
+ const guint move_length = 3;
+ LdDiagramObject *object;
+ guint i;
+ gdouble x, y;
+
+ object = ld_diagram_object_new (NULL);
+ g_object_set (object,
+ "x", start_position.x,
+ "y", start_position.y,
+ NULL);
+
+ /* Create a single user action. */
+ ld_diagram_begin_user_action (fixture->diagram);
+ ld_diagram_insert_object (fixture->diagram, object, 0);
+ for (i = 0; i++ < move_length;)
+ {
+ ld_diagram_begin_user_action (fixture->diagram);
+ g_object_set (object,
+ "x", start_position.x + i,
+ "y", start_position.y + i,
+ NULL);
+ ld_diagram_end_user_action (fixture->diagram);
+ }
+ ld_diagram_end_user_action (fixture->diagram);
+
+ /* Undo the action. */
+ g_assert (ld_diagram_get_objects (fixture->diagram) != NULL);
+ g_assert (ld_diagram_can_undo (fixture->diagram) != FALSE);
+ ld_diagram_undo (fixture->diagram);
+
+ /* Check that it has been undone correctly. */
+ g_assert (ld_diagram_get_objects (fixture->diagram) == NULL);
+ g_assert (ld_diagram_can_undo (fixture->diagram) == FALSE);
+
+ g_object_get (object, "x", &x, "y", &y, NULL);
+ g_assert_cmpfloat (x, ==, start_position.x);
+ g_assert_cmpfloat (y, ==, start_position.y);
+
+ /* Redo the action. */
+ g_assert (ld_diagram_can_redo (fixture->diagram) != FALSE);
+ ld_diagram_redo (fixture->diagram);
+
+ /* Check that it has been redone correctly. */
+ g_object_get (object, "x", &x, "y", &y, NULL);
+ g_assert_cmpfloat (x, ==, start_position.x + move_length);
+ g_assert_cmpfloat (y, ==, start_position.y + move_length);
+
+ g_object_unref (object);
+}
+
+int
+main (int argc, char *argv[])
+{
+ gtk_test_init (&argc, &argv, NULL);
+
+ /* History. */
+ g_test_add ("/diagram/history", Diagram, NULL,
+ diagram_setup, diagram_test_history,
+ diagram_teardown);
+ g_test_add ("/diagram/history-grouping", Diagram, NULL,
+ diagram_setup, diagram_test_history_grouping,
+ diagram_teardown);
+
+ return g_test_run ();
+}
+
--
cgit v1.2.3-70-g09d2