Fixed undo/redo system

This commit is contained in:
Michaël Lemaire 2015-08-17 18:18:31 +02:00
parent 5afd5ec24a
commit 67bd80fba5
2 changed files with 91 additions and 3 deletions

View file

@ -16,6 +16,13 @@ void DiffManager::addWatcher(const DefinitionNode *node, DefinitionWatcher *watc
void DiffManager::addDiff(DefinitionNode *node, const DefinitionDiff *diff) void DiffManager::addDiff(DefinitionNode *node, const DefinitionDiff *diff)
{ {
while (undone > 0)
{
// truncate diffs ahead
diffs.pop_back();
undone--;
}
diffs.push_back(diff); diffs.push_back(diff);
// TODO Delayed commit (with merge of consecutive diffs) // TODO Delayed commit (with merge of consecutive diffs)
@ -29,7 +36,7 @@ void DiffManager::addDiff(DefinitionNode *node, const DefinitionDiff *diff)
void DiffManager::undo() void DiffManager::undo()
{ {
if (undone <= (int)diffs.size()) if (undone < (int)diffs.size())
{ {
undone++; undone++;
const DefinitionDiff *diff = diffs[diffs.size() - undone]; const DefinitionDiff *diff = diffs[diffs.size() - undone];

View file

@ -4,12 +4,12 @@
#include "DefinitionNode.h" #include "DefinitionNode.h"
#include "FloatNode.h" #include "FloatNode.h"
TEST(DiffManager, undoRedoSimple) TEST(DiffManager, undoRedo)
{ {
DefinitionNode root(NULL, "root"); DefinitionNode root(NULL, "root");
FloatNode leaf(&root, "value", 2.6);
DiffManager *diffs = root.getDiffManager(); DiffManager *diffs = root.getDiffManager();
FloatNode leaf(&root, "value", 2.6);
EXPECT_DOUBLE_EQ(2.6, leaf.getValue()); EXPECT_DOUBLE_EQ(2.6, leaf.getValue());
leaf.setValue(4.3); leaf.setValue(4.3);
@ -30,3 +30,84 @@ TEST(DiffManager, undoRedoSimple)
diffs->redo(); diffs->redo();
EXPECT_DOUBLE_EQ(-2.1, leaf.getValue()); 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());
}