package com.projectsacrifice.game; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertTrue; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; import java.io.ByteArrayOutputStream; import java.io.PrintStream; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; public final class LevelContentToolTest { @Rule public final TemporaryFolder temporary = new TemporaryFolder(); @Test public void validateCommandLoadsARealDirectoryAndReportsVersions() throws Exception { Path levels = temporary.newFolder("levels").toPath(); writeFixture(levels); ByteArrayOutputStream output = new ByteArrayOutputStream(); ByteArrayOutputStream errors = new ByteArrayOutputStream(); int exitCode = LevelContentTool.run( new String[]{"validate", levels.toString()}, new PrintStream(output, true, StandardCharsets.UTF_8), new PrintStream(errors, true, StandardCharsets.UTF_8)); assertEquals(0, exitCode); assertEquals("Validated 1 level(s); format=1; physics=1\n", output.toString(StandardCharsets.UTF_8)); assertEquals("", errors.toString(StandardCharsets.UTF_8)); } @Test public void previewCommandProducesByteIdenticalSvgAndCoordinateExport() throws Exception { Path levels = temporary.newFolder("preview-levels").toPath(); Path first = temporary.newFolder("preview-first").toPath(); Path second = temporary.newFolder("preview-second").toPath(); writeFixture(levels); assertEquals(0, LevelContentTool.run( new String[]{"preview", levels.toString(), first.toString()}, new PrintStream(new ByteArrayOutputStream()), new PrintStream(new ByteArrayOutputStream()))); assertEquals(0, LevelContentTool.run( new String[]{"preview", levels.toString(), second.toString()}, new PrintStream(new ByteArrayOutputStream()), new PrintStream(new ByteArrayOutputStream()))); byte[] firstSvg = Files.readAllBytes(first.resolve("level-01.svg")); byte[] secondSvg = Files.readAllBytes(second.resolve("level-01.svg")); assertArrayEquals(firstSvg, secondSvg); assertArrayEquals(Files.readAllBytes(first.resolve("catalog.tsv")), Files.readAllBytes(second.resolve("catalog.tsv"))); String svg = new String(firstSvg, StandardCharsets.UTF_8); assertTrue(svg, svg.contains("viewBox=\"0 0 1920 1080\"")); assertTrue(svg, svg.contains("data-kind=\"spawn\"")); assertTrue(svg, svg.contains("data-kind=\"goal\"")); assertTrue(svg, svg.contains("data-kind=\"object-COUNTERWEIGHT\"")); String coordinates = Files.readString(first.resolve("catalog.tsv")); assertTrue(coordinates, coordinates.contains("1\tobject\t0\tCOUNTERWEIGHT")); assertTrue(coordinates, coordinates.contains("mass=3")); } private static void writeFixture(Path levels) throws Exception { Files.writeString(levels.resolve("index.json"), "{\"formatVersion\":1,\"physicsVersion\":1,\"levels\":[\"level-01.json\"]}"); Files.writeString(levels.resolve("level-01.json"), validLevelJson()); } private static String validLevelJson() { return "{\"formatVersion\":1,\"physicsVersion\":1,\"number\":1," + "\"copy\":{\"en\":{\"title\":\"Test\",\"hint\":\"Go.\"}," + "\"ru\":{\"title\":\"Тест\",\"hint\":\"Идите.\"}}," + "\"world\":{\"width\":1920,\"height\":1080}," + "\"spawn\":{\"x\":130,\"y\":760}," + "\"goal\":{\"x\":1690,\"y\":770,\"width\":86,\"height\":110}," + "\"targets\":{\"deaths\":0,\"seconds\":30}," + "\"objects\":[{\"type\":\"COUNTERWEIGHT\"," + "\"box\":{\"x\":700,\"y\":780,\"width\":80,\"height\":100}," + "\"mass\":3}]," + "\"platforms\":[{\"box\":{\"x\":0,\"y\":880,\"width\":1920,\"height\":200}}]," + "\"hazards\":[],\"switches\":[],\"doors\":[]}"; } }