Basic API Structure #2

Merged
kske merged 12 commits from f/basics into develop 2021-12-11 21:45:59 +01:00
3 changed files with 124 additions and 55 deletions
Showing only changes of commit 2773d360fb - Show all commits

View File

@ -1,8 +1,8 @@
package dev.kske.undoredo; 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 * @author Kai S. K. Engelbart
@ -10,67 +10,95 @@ import org.junit.jupiter.api.Test;
*/ */
class ChangeManagerTest { class ChangeManagerTest {
ChangeManager<IntChange> 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 @Test
@Order(1)
void testAddChange() { 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()}. * Tests the consistency of the change list.
*/ *
@Test * @since 0.0.1
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()}.
*/ */
@Test @Test
@Order(2)
void testGetChanges() { 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());
} }
} }

View File

@ -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;
}
}

View File

@ -0,0 +1,10 @@
package dev.kske.undoredo;
/**
* @author Kai S. K. Engelbart
* @since 0.0.1
*/
class IntWrapper {
int value;
}