From 67bd80fba5a5c75448dc36e54cb918dc910caf0c Mon Sep 17 00:00:00 2001 From: Michael Lemaire Date: Mon, 17 Aug 2015 18:18:31 +0200 Subject: [PATCH] Fixed undo/redo system --- src/definition/DiffManager.cpp | 9 +++- src/tests/DiffManager_Test.cpp | 85 +++++++++++++++++++++++++++++++++- 2 files changed, 91 insertions(+), 3 deletions(-) diff --git a/src/definition/DiffManager.cpp b/src/definition/DiffManager.cpp index cda53b9..385afda 100644 --- a/src/definition/DiffManager.cpp +++ b/src/definition/DiffManager.cpp @@ -16,6 +16,13 @@ void DiffManager::addWatcher(const DefinitionNode *node, DefinitionWatcher *watc void DiffManager::addDiff(DefinitionNode *node, const DefinitionDiff *diff) { + while (undone > 0) + { + // truncate diffs ahead + diffs.pop_back(); + undone--; + } + diffs.push_back(diff); // TODO Delayed commit (with merge of consecutive diffs) @@ -29,7 +36,7 @@ void DiffManager::addDiff(DefinitionNode *node, const DefinitionDiff *diff) void DiffManager::undo() { - if (undone <= (int)diffs.size()) + if (undone < (int)diffs.size()) { undone++; const DefinitionDiff *diff = diffs[diffs.size() - undone]; diff --git a/src/tests/DiffManager_Test.cpp b/src/tests/DiffManager_Test.cpp index fe0984f..4e60b5e 100644 --- a/src/tests/DiffManager_Test.cpp +++ b/src/tests/DiffManager_Test.cpp @@ -4,12 +4,12 @@ #include "DefinitionNode.h" #include "FloatNode.h" -TEST(DiffManager, undoRedoSimple) +TEST(DiffManager, undoRedo) { DefinitionNode root(NULL, "root"); - FloatNode leaf(&root, "value", 2.6); DiffManager *diffs = root.getDiffManager(); + FloatNode leaf(&root, "value", 2.6); EXPECT_DOUBLE_EQ(2.6, leaf.getValue()); leaf.setValue(4.3); @@ -30,3 +30,84 @@ TEST(DiffManager, undoRedoSimple) diffs->redo(); EXPECT_DOUBLE_EQ(-2.1, leaf.getValue()); } + +TEST(DiffManager, undoTooMuch) +{ + DefinitionNode root(NULL, "root"); + DiffManager *diffs = root.getDiffManager(); + + FloatNode leaf(&root, "value", 1.1); + EXPECT_DOUBLE_EQ(1.1, leaf.getValue()); + + leaf.setValue(2.2); + EXPECT_DOUBLE_EQ(2.2, leaf.getValue()); + + diffs->undo(); + EXPECT_DOUBLE_EQ(1.1, leaf.getValue()); + + diffs->undo(); + EXPECT_DOUBLE_EQ(1.1, leaf.getValue()); + + diffs->redo(); + EXPECT_DOUBLE_EQ(2.2, leaf.getValue()); +} + +TEST(DiffManager, redoTooMuch) +{ + DefinitionNode root(NULL, "root"); + DiffManager *diffs = root.getDiffManager(); + + FloatNode leaf(&root, "value", 1.1); + EXPECT_DOUBLE_EQ(1.1, leaf.getValue()); + + leaf.setValue(2.2); + EXPECT_DOUBLE_EQ(2.2, leaf.getValue()); + + diffs->undo(); + EXPECT_DOUBLE_EQ(1.1, leaf.getValue()); + + diffs->redo(); + EXPECT_DOUBLE_EQ(2.2, leaf.getValue()); + + diffs->redo(); + EXPECT_DOUBLE_EQ(2.2, leaf.getValue()); + + diffs->undo(); + EXPECT_DOUBLE_EQ(1.1, leaf.getValue()); +} + +TEST(DiffManager, undoBranch) +{ + DefinitionNode root(NULL, "root"); + DiffManager *diffs = root.getDiffManager(); + + FloatNode leaf(&root, "value", 1.1); + EXPECT_DOUBLE_EQ(1.1, leaf.getValue()); + + leaf.setValue(2.2); + EXPECT_DOUBLE_EQ(2.2, leaf.getValue()); + + leaf.setValue(3.3); + EXPECT_DOUBLE_EQ(3.3, leaf.getValue()); + + diffs->undo(); + EXPECT_DOUBLE_EQ(2.2, leaf.getValue()); + + leaf.setValue(4.4); + EXPECT_DOUBLE_EQ(4.4, leaf.getValue()); + + diffs->undo(); + EXPECT_DOUBLE_EQ(2.2, leaf.getValue()); + + diffs->undo(); + EXPECT_DOUBLE_EQ(1.1, leaf.getValue()); + + diffs->redo(); + EXPECT_DOUBLE_EQ(2.2, leaf.getValue()); + + diffs->redo(); + EXPECT_DOUBLE_EQ(4.4, leaf.getValue()); + + diffs->redo(); + EXPECT_DOUBLE_EQ(4.4, leaf.getValue()); +}