From 2773d360fbed11f38c4245be61ca074daedf7bcf Mon Sep 17 00:00:00 2001 From: kske Date: Sat, 11 Dec 2021 13:51:12 +0100 Subject: [PATCH] Implement change manager unit tests --- .../dev/kske/undoredo/ChangeManagerTest.java | 138 +++++++++++------- .../java/dev/kske/undoredo/IntChange.java | 31 ++++ .../java/dev/kske/undoredo/IntWrapper.java | 10 ++ 3 files changed, 124 insertions(+), 55 deletions(-) create mode 100644 src/test/java/dev/kske/undoredo/IntChange.java create mode 100644 src/test/java/dev/kske/undoredo/IntWrapper.java diff --git a/src/test/java/dev/kske/undoredo/ChangeManagerTest.java b/src/test/java/dev/kske/undoredo/ChangeManagerTest.java index 0d4cbc0..b3e1b72 100644 --- a/src/test/java/dev/kske/undoredo/ChangeManagerTest.java +++ b/src/test/java/dev/kske/undoredo/ChangeManagerTest.java @@ -1,8 +1,8 @@ package dev.kske.undoredo; -import static org.junit.jupiter.api.Assertions.fail; +import static org.junit.jupiter.api.Assertions.*; -import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.*; /** * @author Kai S. K. Engelbart @@ -10,67 +10,95 @@ import org.junit.jupiter.api.Test; */ class ChangeManagerTest { + ChangeManager manager; + IntWrapper wrapper; + IntChange change; + + @BeforeEach + void prepareChangeManager() { + manager = new ChangeManager<>(); + wrapper = new IntWrapper(); + change = new IntChange(wrapper, 1); + } + /** - * Test method for {@link dev.kske.undoredo.ChangeManager#addChange(dev.kske.undoredo.Change)}. + * Tests adding a change. + * + * @since 0.0.1 */ @Test + @Order(1) void testAddChange() { - fail("Not yet implemented"); + assertSame(0, wrapper.value); + manager.addChange(change); + assertSame(1, wrapper.value); } - + /** - * Test method for {@link dev.kske.undoredo.ChangeManager#undo()}. - */ - @Test - void testUndo() { - fail("Not yet implemented"); - } - - /** - * Test method for {@link dev.kske.undoredo.ChangeManager#redo()}. - */ - @Test - void testRedo() { - fail("Not yet implemented"); - } - - /** - * Test method for {@link dev.kske.undoredo.ChangeManager#mark()}. - */ - @Test - void testMark() { - fail("Not yet implemented"); - } - - /** - * Test method for {@link dev.kske.undoredo.ChangeManager#isAtMarkedIndex()}. - */ - @Test - void testIsAtMarkedIndex() { - fail("Not yet implemented"); - } - - /** - * Test method for {@link dev.kske.undoredo.ChangeManager#isUndoAvailable()}. - */ - @Test - void testIsUndoAvailable() { - fail("Not yet implemented"); - } - - /** - * Test method for {@link dev.kske.undoredo.ChangeManager#isRedoAvailable()}. - */ - @Test - void testIsRedoAvailable() { - fail("Not yet implemented"); - } - - /** - * Test method for {@link dev.kske.undoredo.ChangeManager#getChanges()}. + * Tests the consistency of the change list. + * + * @since 0.0.1 */ @Test + @Order(2) void testGetChanges() { - fail("Not yet implemented"); + assertTrue(manager.getChanges().isEmpty()); + manager.addChange(change); + assertSame(1, manager.getChanges().size()); + assertEquals(change, manager.getChanges().get(0)); + } + + /** + * Test undoing a change. + * + * @since 0.0.1 + */ + @Test + @Order(2) + void testUndo() { + assertFalse(manager.isUndoAvailable()); + assertFalse(manager.undo()); + manager.addChange(change); + assertTrue(manager.isUndoAvailable()); + assertTrue(manager.undo()); + assertFalse(manager.isUndoAvailable()); + assertFalse(manager.undo()); + } + + /** + * Tests redoing a change. + * + * @since 0.0.1 + */ + @Test + @Order(4) + void testRedo() { + assertFalse(manager.isRedoAvailable()); + assertFalse(manager.redo()); + manager.addChange(change); + assertFalse(manager.isRedoAvailable()); + assertFalse(manager.redo()); + manager.undo(); + assertTrue(manager.isRedoAvailable()); + assertTrue(manager.redo()); + assertFalse(manager.isRedoAvailable()); + assertFalse(manager.redo()); + } + + /** + * Tests marking a change. + * + * @since 0.0.1 + */ + @Test + @Order(5) + void testMark() { + assertTrue(manager.isAtMarkedIndex()); + manager.addChange(change); + assertFalse(manager.isAtMarkedIndex()); + manager.mark(); + assertTrue(manager.isAtMarkedIndex()); + manager.undo(); + assertFalse(manager.isAtMarkedIndex()); } } diff --git a/src/test/java/dev/kske/undoredo/IntChange.java b/src/test/java/dev/kske/undoredo/IntChange.java new file mode 100644 index 0000000..76299a4 --- /dev/null +++ b/src/test/java/dev/kske/undoredo/IntChange.java @@ -0,0 +1,31 @@ +package dev.kske.undoredo; + +/** + * @author Kai S. K. Engelbart + * @since 0.0.1 + */ +class IntChange implements Change { + + private final IntWrapper wrapper; + private final int value; + + IntChange(IntWrapper wrapper, int value) { + this.wrapper = wrapper; + this.value = value; + } + + @Override + public void apply() { + wrapper.value += value; + } + + @Override + public Change invert() { + return new IntChange(wrapper, -value); + } + + @Override + public boolean isIdentity() { + return value == 0; + } +} diff --git a/src/test/java/dev/kske/undoredo/IntWrapper.java b/src/test/java/dev/kske/undoredo/IntWrapper.java new file mode 100644 index 0000000..bae74ef --- /dev/null +++ b/src/test/java/dev/kske/undoredo/IntWrapper.java @@ -0,0 +1,10 @@ +package dev.kske.undoredo; + +/** + * @author Kai S. K. Engelbart + * @since 0.0.1 + */ +class IntWrapper { + + int value; +}