From d928b16eb2d89d697ee1a302c6c7c5c70a680a38 Mon Sep 17 00:00:00 2001 From: David Maydew Date: Thu, 7 Apr 2016 17:33:40 -0400 Subject: [PATCH] dmm68 ras70 junit lab --- .classpath | 7 +++ src/debug/ContainerArray.java | 22 ++++--- src/debug/ContainerArrayTest.java | 25 ++++++++ src/tdd/Sheet.java | 32 ++++++++++ src/tdd/TestSheet.java | 84 +++++++++++++++++++++++++++ src/voogasalad/README.md | 2 + src/voogasalad/TestPathFollow.java | 54 +++++++++++++++++ src/voogasalad/TestSpriteManager.java | 49 ++++++++++++++++ 8 files changed, 266 insertions(+), 9 deletions(-) create mode 100644 .classpath create mode 100644 src/tdd/Sheet.java create mode 100644 src/tdd/TestSheet.java create mode 100644 src/voogasalad/TestPathFollow.java create mode 100644 src/voogasalad/TestSpriteManager.java diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..3e0fb27 --- /dev/null +++ b/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/src/debug/ContainerArray.java b/src/debug/ContainerArray.java index 124ece6..d36eeb1 100755 --- a/src/debug/ContainerArray.java +++ b/src/debug/ContainerArray.java @@ -1,33 +1,37 @@ package debug; +import java.util.ArrayList; +import java.util.List; public class ContainerArray { - private int initialCapacity = 10; - private int currentSize = 0; - private Object[] internalArray; + private int limit = 10; + private List internalArray; public ContainerArray () { this(10); } - public ContainerArray (int initialCapacity) { - internalArray = new Object[initialCapacity]; + public ContainerArray (int limit) { + internalArray = new ArrayList<>(); } public void add (E element) { - internalArray[currentSize++] = element; + if(internalArray.size() == limit) { + throw new IndexOutOfBoundsException(); + } + internalArray.add(element); } public int size () { - return currentSize; + return internalArray.size(); } public void remove (E objectToRemove) { - currentSize--; + internalArray.remove(objectToRemove); } @SuppressWarnings("unchecked") public E get (int index) { - return (E)internalArray[index]; + return internalArray.get(index); } } diff --git a/src/debug/ContainerArrayTest.java b/src/debug/ContainerArrayTest.java index c566d50..ab20c53 100755 --- a/src/debug/ContainerArrayTest.java +++ b/src/debug/ContainerArrayTest.java @@ -44,4 +44,29 @@ public void testObjectIsRemoved () { myContainer.remove("Bear"); assertEquals("Remove should be same reference", alligator, myContainer.get(0)); } + + @Test + public void testFirstObjectIsRemoved () { + String bear = "Bear"; + myContainer.add("Alligator"); + myContainer.add("Bear"); + myContainer.remove("Alligator"); + assertEquals("Remove should be same reference", bear, myContainer.get(0)); + } + + @Test(expected=IndexOutOfBoundsException.class) + public void testAddLimitDefault10 () { + for(int i =0; i<10; i++) { + myContainer.add("Alligator"); + } + myContainer.add("Bear"); + + } + + @Test(expected=IndexOutOfBoundsException.class) + public void testGetLimitDefault10 () { + myContainer.get(11); + + } + } diff --git a/src/tdd/Sheet.java b/src/tdd/Sheet.java new file mode 100644 index 0000000..43ff6dd --- /dev/null +++ b/src/tdd/Sheet.java @@ -0,0 +1,32 @@ +package tdd; + +import java.util.HashMap; +import java.util.Map; + +public class Sheet { + + private Map myMap = new HashMap<>(); + + public String get (String key) { + if (!myMap.containsKey(key)) { + return ""; + } + String value = myMap.get(key); + if (!value.matches("[ ]+") && value.matches("[0-9 ]+")) { + value = value.trim(); + } + return value; + } + + public void put (String theCell, String value) { + myMap.put(theCell, value); + } + + public Object getLiteral (String key) { + if (!myMap.containsKey(key)) { + return ""; + } + return myMap.get(key); + } + +} diff --git a/src/tdd/TestSheet.java b/src/tdd/TestSheet.java new file mode 100644 index 0000000..bf6a88c --- /dev/null +++ b/src/tdd/TestSheet.java @@ -0,0 +1,84 @@ +package tdd; + +import static org.junit.Assert.*; +import org.junit.Test; + +public class TestSheet { + + @Test + public void testThatCellsAreEmptyByDefault() { + Sheet sheet = new Sheet(); + assertEquals("", sheet.get("A1")); + assertEquals("", sheet.get("ZX347")); + } + + @Test + public void testThatTextCellsAreStored() { + Sheet sheet = new Sheet(); + String theCell = "A21"; + + sheet.put(theCell, "A string"); + assertEquals("A string", sheet.get(theCell)); + + sheet.put(theCell, "A different string"); + assertEquals("A different string", sheet.get(theCell)); + + sheet.put(theCell, ""); + assertEquals("", sheet.get(theCell)); + } + + @Test + public void testThatManyCellsExist() { + Sheet sheet = new Sheet(); + sheet.put("A1", "First"); + sheet.put("X27", "Second"); + sheet.put("ZX901", "Third"); + + assertEquals("A1", "First", sheet.get("A1")); + assertEquals("X27", "Second", sheet.get("X27")); + assertEquals("ZX901", "Third", sheet.get("ZX901")); + + sheet.put("A1", "Fourth"); + assertEquals("A1 after", "Fourth", sheet.get("A1")); + assertEquals("X27 same", "Second", sheet.get("X27")); + assertEquals("ZX901 same", "Third", sheet.get("ZX901")); + } + + @Test + public void testThatNumericCellsAreIdentifiedAndStored() { + Sheet sheet = new Sheet(); + String theCell = "A21"; + + sheet.put(theCell, "X99"); // "Obvious" string + assertEquals("X99", sheet.get(theCell)); + + sheet.put(theCell, "14"); // "Obvious" number + assertEquals("14", sheet.get(theCell)); + + sheet.put(theCell, " 99 X"); // Whole string must be numeric + assertEquals(" 99 X", sheet.get(theCell)); + + sheet.put(theCell, " 1234 "); // Blanks ignored + assertEquals("1234", sheet.get(theCell)); + + sheet.put(theCell, " "); // Just a blank + assertEquals(" ", sheet.get(theCell)); + } + + @Test + public void testThatWeHaveAccessToCellLiteralValuesForEditing() { + Sheet sheet = new Sheet(); + String theCell = "A21"; + + sheet.put(theCell, "Some string"); + assertEquals("Some string", sheet.getLiteral(theCell)); + + sheet.put(theCell, " 1234 "); + assertEquals(" 1234 ", sheet.getLiteral(theCell)); + + sheet.put(theCell, "=7"); // Foreshadowing formulas:) + assertEquals("=7", sheet.getLiteral(theCell)); + } + + +} diff --git a/src/voogasalad/README.md b/src/voogasalad/README.md index cee0dd7..c9bafe0 100644 --- a/src/voogasalad/README.md +++ b/src/voogasalad/README.md @@ -1 +1,3 @@ Practice writing Unit Tests by choosing two classes from your VOOGASalad project to test. + +We chose to test our path following mechanism for sprites, as well as our manager that handles interactions and updating sprites. We chose these classes because they were modular enough to be tested without having to construct an entire game, but have complex enough functionality to construct interesting tests. \ No newline at end of file diff --git a/src/voogasalad/TestPathFollow.java b/src/voogasalad/TestPathFollow.java new file mode 100644 index 0000000..ffc28e5 --- /dev/null +++ b/src/voogasalad/TestPathFollow.java @@ -0,0 +1,54 @@ +package voogasalad; + +import static org.junit.Assert.*; +import java.util.ArrayList; +import java.util.List; +import org.junit.Before; +import org.junit.Test; +import engine.modules.PathFollowMover; +import engine.sprite.Sprite; +import util.Coordinate; +import util.TimeDuration; + + +public class TestPathFollow { + + private Sprite mySprite; + + @Before + public void setUp () { + mySprite = new Sprite(); + } + + @Test + public void testDontMove () { + + mySprite.getLocation().set(new Coordinate(10, 10)); + List path = new ArrayList<>(); + PathFollowMover mover = new PathFollowMover(1, path, mySprite); + mySprite.getMovementStrategyProperty().set(mover); + Coordinate beforeMove = mySprite.getLocation().get(); + mySprite.update(new TimeDuration(100)); + Coordinate afterMove = mySprite.getLocation().get(); + assertEquals("Should not has moved", beforeMove.getX(), afterMove.getX(), .001); + assertEquals("Should not has moved", beforeMove.getY(), afterMove.getY(), .001); + + } + + @Test + public void testInitialMovement () { + + mySprite.getLocation().set(new Coordinate(10, 10)); + List path = new ArrayList<>(); + path.add(new Coordinate(11, 11)); + PathFollowMover mover = new PathFollowMover(1, path, mySprite); + mySprite.getMovementStrategyProperty().set(mover); + Coordinate beforeMove = mySprite.getLocation().get(); + mySprite.update(new TimeDuration(100)); + Coordinate afterMove = mySprite.getLocation().get(); + assert(afterMove.getX() > beforeMove.getX()); + assert(afterMove.getY() > beforeMove.getY()); + + } + +} diff --git a/src/voogasalad/TestSpriteManager.java b/src/voogasalad/TestSpriteManager.java new file mode 100644 index 0000000..e9299bf --- /dev/null +++ b/src/voogasalad/TestSpriteManager.java @@ -0,0 +1,49 @@ +package voogasalad; + +import static org.junit.Assert.*; +import java.util.ArrayList; +import java.util.List; +import org.junit.Before; +import org.junit.Test; +import engine.ISpriteManager; +import engine.SpriteManager; +import engine.modules.PathFollowMover; +import engine.sprite.ISprite; +import engine.sprite.Sprite; +import util.Coordinate; +import util.TimeDuration; + + +public class TestSpriteManager { + + private ISpriteManager mySpriteManager; + + @Before + public void setUp () { + mySpriteManager = new SpriteManager(); + } + + @Test + public void updateAllSprites () { + for (int i = 0; i < 10; i++) { + mySpriteManager.add(createMovementSprite(), new Coordinate(10, 10)); + } + mySpriteManager.update(new TimeDuration(100)); + mySpriteManager.getSprites().forEach(sprite -> testSprite(sprite.get())); + } + + private void testSprite (ISprite sprite) { + assert (sprite.getLocation().get().getX() != 10 && sprite.getLocation().get().getY() != 10); + } + + private ISprite createMovementSprite () { + ISprite mySprite = new Sprite(); + mySprite.getLocation().set(new Coordinate(10, 10)); + List path = new ArrayList<>(); + PathFollowMover mover = new PathFollowMover(1, path, mySprite); + mySprite.getMovementStrategyProperty().set(mover); + return mySprite; + + } + +}