From 1771fd1d42c6026b6de7c2a99fc3ed0515d31012 Mon Sep 17 00:00:00 2001 From: Anthony Debucquoy Date: Tue, 21 Mar 2023 23:58:59 +0100 Subject: [PATCH] Testing + Wip Signed-off-by: Anthony Debucquoy --- app/src/main/java/school_project/Map.java | 2 ++ .../school_project/Parsers/JsonParser.java | 1 - app/src/main/java/school_project/Shape.java | 4 ++- .../Parsers/JsonParserTest.java | 34 +++++++++++++++++++ 4 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 app/src/test/java/school_project/Parsers/JsonParserTest.java diff --git a/app/src/main/java/school_project/Map.java b/app/src/main/java/school_project/Map.java index e67944a..418a7f2 100644 --- a/app/src/main/java/school_project/Map.java +++ b/app/src/main/java/school_project/Map.java @@ -42,6 +42,8 @@ public class Map extends Shape{ } for (Piece p : pieces) { + if(p.getPosition() == null) + continue; for(int x = 0; x < p.height; x++){ for(int y = 0; y < p.width; y++){ if (p.getShape()[x][y]){ diff --git a/app/src/main/java/school_project/Parsers/JsonParser.java b/app/src/main/java/school_project/Parsers/JsonParser.java index 4331b0c..14d56d8 100644 --- a/app/src/main/java/school_project/Parsers/JsonParser.java +++ b/app/src/main/java/school_project/Parsers/JsonParser.java @@ -22,7 +22,6 @@ public class JsonParser implements FileParser{ * @return true if it is a text File */ public static boolean isJsonFile(File file) throws IOException { - System.out.println(Files.probeContentType(file.toPath())); return Files.probeContentType(file.toPath()).equals("application/json"); } diff --git a/app/src/main/java/school_project/Shape.java b/app/src/main/java/school_project/Shape.java index 7061bc2..401fdcf 100644 --- a/app/src/main/java/school_project/Shape.java +++ b/app/src/main/java/school_project/Shape.java @@ -1,11 +1,13 @@ package school_project; +import java.io.Serializable; + /** * Base class for everything that is a shape kind, like map and pieces * it contain a matrix of boolean where the shape is defined by the true's */ -public class Shape { +public class Shape implements Serializable { protected boolean[][] matrix; protected int height, width; diff --git a/app/src/test/java/school_project/Parsers/JsonParserTest.java b/app/src/test/java/school_project/Parsers/JsonParserTest.java new file mode 100644 index 0000000..08f2185 --- /dev/null +++ b/app/src/test/java/school_project/Parsers/JsonParserTest.java @@ -0,0 +1,34 @@ +package school_project.Parsers; + +import org.junit.jupiter.api.Test; +import school_project.Map; +import school_project.Piece; + +import java.io.File; +import java.io.IOException; + +import static org.junit.jupiter.api.Assertions.*; + +class JsonParserTest { + + @Test + void saveLevelData() throws IOException { + boolean[][] map_matrix = { + {true, true, true}, + {false, true, false}, + {true, true, true}, + }; + Map map = new Map(map_matrix); + boolean[][] piece1 = {{true, true, true}}; + boolean[][] piece2 = {{false, true, false}, {true, true, true}}; + map.addPiece(new Piece(piece1)); + map.addPiece(new Piece(piece2)); + JsonParser parser = new JsonParser(); + parser.saveLevelData(new File("test.json"), map); + + FileParser p2 = FileParserFactory.createParser(new File("test.json")); + Map map2 = p2.getLevel(new File("test.json")); + assertArrayEquals(map2.getShape(), map.getShape()); + assertArrayEquals(map2.getUsedSpace(), map.getUsedSpace()); + } +} \ No newline at end of file