|
| 1 | +// Copyright 2021 The Terasology Foundation |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +package org.terasology.engine.logic.behavior.core; |
| 4 | + |
| 5 | +import org.junit.jupiter.api.Test; |
| 6 | +import org.junit.jupiter.api.io.TempDir; |
| 7 | + |
| 8 | +import java.io.IOException; |
| 9 | +import java.nio.charset.StandardCharsets; |
| 10 | +import java.nio.file.Files; |
| 11 | +import java.nio.file.Path; |
| 12 | +import java.util.Arrays; |
| 13 | +import java.util.List; |
| 14 | +import java.util.stream.Collectors; |
| 15 | +import java.util.stream.Stream; |
| 16 | + |
| 17 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 18 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 19 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 20 | + |
| 21 | +/** |
| 22 | + * See https://github.com/MovingBlocks/Terasology/issues/5099. The tool exists so a content author who hits the |
| 23 | + * load-time rejection from {@link BehaviorTreeBuilder} doesn't have to hand-edit the file's JSON to find and |
| 24 | + * remove the offending comma - it does the same fix mechanically. |
| 25 | + */ |
| 26 | +public class BehaviorTreeRepairToolTest { |
| 27 | + |
| 28 | + @Test |
| 29 | + public void trailingCommaInCompositeArrayIsRemoved() { |
| 30 | + BehaviorTreeRepairTool.Result result = BehaviorTreeRepairTool.clean( |
| 31 | + "{ \"selector\": [\"success\", \"success\",] }"); |
| 32 | + |
| 33 | + assertTrue(result.changed); |
| 34 | + assertEquals(1, result.nullArrayEntriesRemoved); |
| 35 | + assertTrue(result.unfixableIssues.isEmpty()); |
| 36 | + // The cleaned JSON should load through the real builder with no custom actions/decorators needed. |
| 37 | + BehaviorNode node = new BehaviorTreeBuilder().fromJson(result.cleanedJson); |
| 38 | + assertEquals(2, node.getChildrenCount()); |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + public void explicitNullInCompositeArrayIsRemoved() { |
| 43 | + BehaviorTreeRepairTool.Result result = BehaviorTreeRepairTool.clean( |
| 44 | + "{ \"selector\": [\"success\", null, \"success\"] }"); |
| 45 | + |
| 46 | + assertTrue(result.changed); |
| 47 | + assertEquals(1, result.nullArrayEntriesRemoved); |
| 48 | + BehaviorNode node = new BehaviorTreeBuilder().fromJson(result.cleanedJson); |
| 49 | + assertEquals(2, node.getChildrenCount()); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + public void nothingToFixIsReportedAsUnchanged() { |
| 54 | + BehaviorTreeRepairTool.Result result = BehaviorTreeRepairTool.clean( |
| 55 | + "{ \"selector\": [\"success\", \"success\"] }"); |
| 56 | + |
| 57 | + assertFalse(result.changed); |
| 58 | + assertEquals(0, result.nullArrayEntriesRemoved); |
| 59 | + assertTrue(result.unfixableIssues.isEmpty()); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void explicitNullDecoratorChildIsReportedNotSilentlyDropped() { |
| 64 | + BehaviorTreeRepairTool.Result result = BehaviorTreeRepairTool.clean( |
| 65 | + "{ \"invert\": { \"child\": null } }"); |
| 66 | + |
| 67 | + assertFalse(result.unfixableIssues.isEmpty()); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + public void repairWritesTheFileAndKeepsABackup(@TempDir Path dir) throws IOException { |
| 72 | + Path file = dir.resolve("broken.behavior"); |
| 73 | + String original = "{ \"selector\": [\"success\", \"success\",] }"; |
| 74 | + Files.write(file, original.getBytes(StandardCharsets.UTF_8)); |
| 75 | + |
| 76 | + BehaviorTreeRepairTool.Result result = BehaviorTreeRepairTool.repair(file); |
| 77 | + |
| 78 | + assertTrue(result.changed); |
| 79 | + Path backup = dir.resolve("broken.behavior.bak"); |
| 80 | + assertTrue(Files.exists(backup)); |
| 81 | + assertEquals(original, new String(Files.readAllBytes(backup), StandardCharsets.UTF_8)); |
| 82 | + BehaviorNode node = new BehaviorTreeBuilder().fromJson( |
| 83 | + new String(Files.readAllBytes(file), StandardCharsets.UTF_8)); |
| 84 | + assertEquals(2, node.getChildrenCount()); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void repairLeavesUnfixableFileUntouched(@TempDir Path dir) throws IOException { |
| 89 | + Path file = dir.resolve("broken.behavior"); |
| 90 | + String original = "{ \"invert\": { \"child\": null } }"; |
| 91 | + Files.write(file, original.getBytes(StandardCharsets.UTF_8)); |
| 92 | + |
| 93 | + BehaviorTreeRepairTool.Result result = BehaviorTreeRepairTool.repair(file); |
| 94 | + |
| 95 | + assertFalse(result.changed); |
| 96 | + assertFalse(Files.exists(dir.resolve("broken.behavior.bak"))); |
| 97 | + assertEquals(original, new String(Files.readAllBytes(file), StandardCharsets.UTF_8)); |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + public void repairNeverClobbersAnExistingBackup(@TempDir Path dir) throws IOException { |
| 102 | + Path file = dir.resolve("broken.behavior"); |
| 103 | + String original = "{ \"selector\": [\"success\", \"success\",] }"; |
| 104 | + Files.write(file, original.getBytes(StandardCharsets.UTF_8)); |
| 105 | + Path existingBackup = dir.resolve("broken.behavior.bak"); |
| 106 | + String someoneElsesBackup = "not the original - already there before repair() ran"; |
| 107 | + Files.write(existingBackup, someoneElsesBackup.getBytes(StandardCharsets.UTF_8)); |
| 108 | + |
| 109 | + BehaviorTreeRepairTool.repair(file); |
| 110 | + |
| 111 | + assertEquals(someoneElsesBackup, new String(Files.readAllBytes(existingBackup), StandardCharsets.UTF_8)); |
| 112 | + Path versionedBackup = dir.resolve("broken.behavior.bak.1"); |
| 113 | + assertTrue(Files.exists(versionedBackup)); |
| 114 | + assertEquals(original, new String(Files.readAllBytes(versionedBackup), StandardCharsets.UTF_8)); |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + public void repairLeavesOnlyTheFinalFileBehindNoStrayTempFiles(@TempDir Path dir) throws IOException { |
| 119 | + Path file = dir.resolve("broken.behavior"); |
| 120 | + Files.write(file, "{ \"selector\": [\"success\", \"success\",] }".getBytes(StandardCharsets.UTF_8)); |
| 121 | + |
| 122 | + BehaviorTreeRepairTool.repair(file); |
| 123 | + |
| 124 | + try (Stream<Path> entries = Files.list(dir)) { |
| 125 | + List<String> names = entries.map(p -> p.getFileName().toString()) |
| 126 | + .sorted().collect(Collectors.toList()); |
| 127 | + assertEquals(Arrays.asList("broken.behavior", "broken.behavior.bak"), names); |
| 128 | + } |
| 129 | + } |
| 130 | +} |
0 commit comments