diff --git a/api/src/test/java/io/github/applecommander/bastools/api/shapes/ShapeGeneratorTest.java b/api/src/test/java/io/github/applecommander/bastools/api/shapes/ShapeGeneratorTest.java index 564822d..adf01af 100644 --- a/api/src/test/java/io/github/applecommander/bastools/api/shapes/ShapeGeneratorTest.java +++ b/api/src/test/java/io/github/applecommander/bastools/api/shapes/ShapeGeneratorTest.java @@ -91,6 +91,39 @@ public class ShapeGeneratorTest { assertShapeMatches(mouse, vectorShape); } + @Test + public void testRobotShape() throws IOException { + final String robot = "+-------------+\n" + + "|....XXXXX...+|\n" + + "|XXXXX...XX...|\n" + + "|....XXXXX....|\n" + + "|.............|\n" + + "|..XX..XXX....|\n" + + "|...XX.XXX....|\n" + + "|...XX.XXXX...|\n" + + "|..XX.XXXXX...|\n" + + "|....XXXXXX...|\n" + + "|.XXXXXXXXXXX.|\n" + + "|XX.........XX|\n" + + "|XX.........XX|\n" + + "|.XXXXXXXXXXX.|\n" + + "+-------------+\n"; + + ShapeTable st = ShapeGenerator.generate(getClass().getResourceAsStream("/robot-bitmap.st")); + assertNotNull(st); + assertEquals(1, st.shapes.size()); + + // Verify we read the shape correctly... + Shape shape = st.shapes.get(0); + assertNotNull(shape); + assertShapeMatches(robot, shape); + + // Run vector transform to be certain we're ok + Shape vectorShape = shape.toVector(); + assertNotNull(vectorShape); + assertShapeMatches(robot, vectorShape); + } + public void assertShapeMatches(final String expected, Shape shape) throws IOException { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); ShapeExporter exp = ShapeExporter.text().asciiTextBorder().build(); diff --git a/api/src/test/java/io/github/applecommander/bastools/api/shapes/ShapeWriteAndReadTests.java b/api/src/test/java/io/github/applecommander/bastools/api/shapes/ShapeWriteAndReadTests.java new file mode 100644 index 0000000..00cfcd6 --- /dev/null +++ b/api/src/test/java/io/github/applecommander/bastools/api/shapes/ShapeWriteAndReadTests.java @@ -0,0 +1,59 @@ +package io.github.applecommander.bastools.api.shapes; + +import static org.junit.Assert.*; +import static org.junit.Assert.assertNotNull; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.util.Arrays; +import java.util.Collection; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameter; +import org.junit.runners.Parameterized.Parameters; + +@RunWith(Parameterized.class) +public class ShapeWriteAndReadTests { + @Parameters(name = "{index}: file= {0}") + public static Collection data() { + return Arrays.asList("/mouse-bitmap.st", "/robot-bitmap.st"); + } + + @Parameter + public String filename; + + private ShapeExporter textExporter; + + @Before + public void setup() { + textExporter = ShapeExporter.text().asciiTextBorder().skipEmptyShapes(true).build(); + } + + @Test + public void test() throws IOException { + ShapeTable stBefore = ShapeGenerator.generate(getClass().getResourceAsStream(filename)); + assertNotNull(stBefore); + assertFalse(stBefore.shapes.isEmpty()); + + final String expected = format(stBefore); + + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + stBefore.write(outputStream); + + ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray()); + ShapeTable stAfter = ShapeTable.read(inputStream); + + String actual = format(stAfter); + assertEquals(expected, actual); + } + + public String format(ShapeTable st) throws IOException { + ByteArrayOutputStream text = new ByteArrayOutputStream(); + textExporter.export(st, text); + return new String(text.toByteArray()); + } +} diff --git a/api/src/test/resources/robot-bitmap.st b/api/src/test/resources/robot-bitmap.st new file mode 100644 index 0000000..164a1df --- /dev/null +++ b/api/src/test/resources/robot-bitmap.st @@ -0,0 +1,15 @@ +.bitmap + ....XXXXX...+ + XXXXX...XX... + ....XXXXX.... + ............. + ..XX..XXX.... + ...XX.XXX.... + ...XX.XXXX... + ..XX.XXXXX... + ....XXXXXX... + .XXXXXXXXXXX. + XX.........XX + XX.........XX + .XXXXXXXXXXX. +