package ruscz.game.gameOfProd.Map; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; import ruscz.game.gameOfProd.Cells.*; import ruscz.game.gameOfProd.Dialog.Dialog; import ruscz.game.gameOfProd.Dialog.DialogBar; import ruscz.game.gameOfProd.Windows.WindowOfFreeMove; import javax.swing.*; import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; import java.util.stream.Stream; public class MapTest { @Test public void loadMapFromFile_parseMapFileToMapArray_mapArrayParsedCorrectly() { //Arrange Map testMap; TypeOfCell[][] mapArray; WindowOfFreeMove window; //Act mapArray = parseMap("m6"); window = new WindowOfFreeMove(new JFrame()); window.setCHAPTER("999"); testMap = Map.loadMapFromFile("m6", window); //Assert for (int i = 0; i < mapArray.length; i++) { for (int j = 0; j < mapArray[0].length; j++) { Assertions.assertEquals(mapArray[i][j], testMap.getMapArray()[i][j]); } } } public static Stream argumentsStream() { WindowOfFreeMove window = new WindowOfFreeMove(new JFrame()); Dialog dialog = new Dialog(new DialogBar()); Map map = new Map(new TypeOfCell[1][1]); map.setName("test"); return Stream.of( Arguments.of(new Block(0, 0, map, "u", true), "BLOCK,u"), Arguments.of(new InteractionCell(0, 0, "test", dialog, map, window, true), "INTER,test"), Arguments.of(new TriggerCell(0, 0, "test", dialog, map, window, false), "TRIGGER,test,false") ); } @ParameterizedTest @MethodSource("argumentsStream") public void loadMapFromFile_parseStringToCorrectObject_MapObjectParsedCorrectly(Cell cell, String cellText) { //Arrange Map testMap; WindowOfFreeMove window; //Act testMap = new Map(new TypeOfCell[1][1]); String[] size = {"1", "1"}; window= new WindowOfFreeMove(new JFrame()); Map.loadMap(testMap, cellText, size, 0, window); //Assert Assertions.assertTrue(cell.equals(testMap.findCellByXY(0, 0))); } private TypeOfCell[][] parseMap(String mapName) { try { File file = new File("src/main/resources/maps/999/" + mapName + ".txt"); Scanner scanner = new Scanner(file); String[] size = scanner.nextLine().split(" "); TypeOfCell[][] mapArray = new TypeOfCell[Integer.parseInt(size[0])][Integer.parseInt(size[1])]; int i = 0; while (scanner.hasNextLine()) { String[] line = scanner.nextLine().split(" "); for (int j = 0; j < line.length; j++) { if (line[j].equals("EMPTY")) { mapArray[j][i] = TypeOfCell.EMPTY; } else if (line[j].toUpperCase().contains("BLOCK")) { mapArray[j][i] = TypeOfCell.BLOCK; } else if (line[j].toUpperCase().contains("INTER") || line[j].toUpperCase().contains("TRIGGER") || line[j].toUpperCase().contains("DOOR") || line[j].toUpperCase().contains("CHEST")) { mapArray[j][i] = TypeOfCell.EVENT; } else if (line[j].toUpperCase().contains("MOVETO")) { mapArray[j][i] = TypeOfCell.MOVETO; } else if (line[j].equals("SPAWN")) { mapArray[j][i] = TypeOfCell.SPAWNPOINT; } } i++; } return mapArray; } catch (FileNotFoundException e) { e.printStackTrace(); } return null; } }