From d484839c8bd9cedf7bdb3982e81f5f548293c75e Mon Sep 17 00:00:00 2001 From: kske Date: Thu, 23 Dec 2021 09:42:33 +0200 Subject: [PATCH] Fix divergent changes removal, improve unit test --- .../undoredo/core/UnlimitedChangeManager.java | 3 +-- .../kske/undoredo/core/ChangeManagerTest.java | 22 +++++++++++++++---- .../dev/kske/undoredo/core/IntChange.java | 22 +++++++++++++++++++ .../dev/kske/undoredo/core/IntWrapper.java | 22 +++++++++++++++++++ 4 files changed, 63 insertions(+), 6 deletions(-) diff --git a/core/src/main/java/dev/kske/undoredo/core/UnlimitedChangeManager.java b/core/src/main/java/dev/kske/undoredo/core/UnlimitedChangeManager.java index d3da43e..e62dd44 100644 --- a/core/src/main/java/dev/kske/undoredo/core/UnlimitedChangeManager.java +++ b/core/src/main/java/dev/kske/undoredo/core/UnlimitedChangeManager.java @@ -27,8 +27,7 @@ public final class UnlimitedChangeManager implements ChangeMan change.apply(); // Remove divergent changes - for (int i = index + 1; i < changes.size(); ++i) - changes.remove(i); + changes.subList(index + 1, changes.size()).clear(); changes.add(change); ++index; diff --git a/core/src/test/java/dev/kske/undoredo/core/ChangeManagerTest.java b/core/src/test/java/dev/kske/undoredo/core/ChangeManagerTest.java index 21ab08d..3f77620 100644 --- a/core/src/test/java/dev/kske/undoredo/core/ChangeManagerTest.java +++ b/core/src/test/java/dev/kske/undoredo/core/ChangeManagerTest.java @@ -14,11 +14,22 @@ class ChangeManagerTest { IntWrapper wrapper; IntChange change; + /** + * Generates an int change with the given value. + * + * @param value the value of the change + * @return the created change + * @since 0.0.1 + */ + IntChange change(int value) { + return new IntChange(wrapper, value); + } + @BeforeEach void prepareChangeManager() { manager = new UnlimitedChangeManager<>(); wrapper = new IntWrapper(); - change = new IntChange(wrapper, 1); + change = change(1); } /** @@ -43,7 +54,7 @@ class ChangeManagerTest { @Order(15) void testDiscardIdentity() { manager.addChange(change); - assertFalse(manager.addChange(new IntChange(wrapper, 0))); + assertFalse(manager.addChange(change(0))); assertSame(1, wrapper.value); } @@ -122,10 +133,13 @@ class ChangeManagerTest { @Order(55) void testDiscardDivergentHistory() { manager.addChange(change); + manager.addChange(change(2)); manager.undo(); - assertTrue(manager.addChange(new IntChange(wrapper, 2))); + manager.undo(); + assertTrue(manager.addChange(change(3))); assertFalse(manager.isRedoAvailable()); - assertSame(2, wrapper.value); + assertSame(3, wrapper.value); + assertSame(1, manager.getChanges().size()); } /** diff --git a/core/src/test/java/dev/kske/undoredo/core/IntChange.java b/core/src/test/java/dev/kske/undoredo/core/IntChange.java index f6e8a32..d3fabe1 100644 --- a/core/src/test/java/dev/kske/undoredo/core/IntChange.java +++ b/core/src/test/java/dev/kske/undoredo/core/IntChange.java @@ -1,5 +1,7 @@ package dev.kske.undoredo.core; +import java.util.Objects; + /** * @author Kai S. K. Engelbart * @since 0.0.1 @@ -28,4 +30,24 @@ class IntChange implements Change { public boolean isIdentity() { return value == 0; } + + @Override + public String toString() { + return String.valueOf(value); + } + + @Override + public int hashCode() { + return Objects.hash(value, wrapper); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (!(obj instanceof IntChange)) + return false; + IntChange other = (IntChange) obj; + return value == other.value && Objects.equals(wrapper, other.wrapper); + } } diff --git a/core/src/test/java/dev/kske/undoredo/core/IntWrapper.java b/core/src/test/java/dev/kske/undoredo/core/IntWrapper.java index 2b6907d..74a667d 100644 --- a/core/src/test/java/dev/kske/undoredo/core/IntWrapper.java +++ b/core/src/test/java/dev/kske/undoredo/core/IntWrapper.java @@ -1,5 +1,7 @@ package dev.kske.undoredo.core; +import java.util.Objects; + /** * @author Kai S. K. Engelbart * @since 0.0.1 @@ -7,4 +9,24 @@ package dev.kske.undoredo.core; class IntWrapper { int value; + + @Override + public String toString() { + return String.valueOf(value); + } + + @Override + public int hashCode() { + return Objects.hash(value); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (!(obj instanceof IntWrapper)) + return false; + IntWrapper other = (IntWrapper) obj; + return value == other.value; + } }