Add test for evaluating all NPC scripts
This commit is contained in:
5
pom.xml
5
pom.xml
@@ -126,6 +126,11 @@
|
|||||||
<artifactId>junit-jupiter-engine</artifactId>
|
<artifactId>junit-jupiter-engine</artifactId>
|
||||||
<version>${junit.version}</version>
|
<version>${junit.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.junit.jupiter</groupId>
|
||||||
|
<artifactId>junit-jupiter-params</artifactId>
|
||||||
|
<version>${junit.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
|
|||||||
@@ -23,13 +23,13 @@ package scripting;
|
|||||||
|
|
||||||
import client.Client;
|
import client.Client;
|
||||||
import com.oracle.truffle.js.scriptengine.GraalJSScriptEngine;
|
import com.oracle.truffle.js.scriptengine.GraalJSScriptEngine;
|
||||||
import constants.string.CharsetConstants;
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import javax.script.*;
|
import javax.script.*;
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
|
|
||||||
@@ -57,7 +57,7 @@ public abstract class AbstractScriptManager {
|
|||||||
|
|
||||||
enableScriptHostAccess(graalScriptEngine);
|
enableScriptHostAccess(graalScriptEngine);
|
||||||
|
|
||||||
try (BufferedReader br = Files.newBufferedReader(scriptFile, CharsetConstants.CHARSET)) {
|
try (BufferedReader br = Files.newBufferedReader(scriptFile, StandardCharsets.UTF_8)) {
|
||||||
engine.eval(br);
|
engine.eval(br);
|
||||||
} catch (final ScriptException | IOException t) {
|
} catch (final ScriptException | IOException t) {
|
||||||
log.warn("Exception during script eval for file: {}", path, t);
|
log.warn("Exception during script eval for file: {}", path, t);
|
||||||
|
|||||||
45
src/test/java/scripting/ScriptEvaluationTest.java
Normal file
45
src/test/java/scripting/ScriptEvaluationTest.java
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
package scripting;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.BeforeAll;
|
||||||
|
import org.junit.jupiter.params.ParameterizedTest;
|
||||||
|
import org.junit.jupiter.params.provider.MethodSource;
|
||||||
|
|
||||||
|
import javax.script.ScriptEngine;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||||
|
|
||||||
|
public class ScriptEvaluationTest {
|
||||||
|
private AbstractScriptManager scriptManager = new AbstractScriptManager() {};
|
||||||
|
|
||||||
|
@BeforeAll
|
||||||
|
static void muteGraal() {
|
||||||
|
System.setProperty("polyglot.engine.WarnInterpreterOnly", "false");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static List<String> npcScriptFiles() throws IOException {
|
||||||
|
Path npcScriptDirectory = Path.of("scripts", "npc");
|
||||||
|
try (Stream<Path> pathStream = Files.walk(npcScriptDirectory)) {
|
||||||
|
return pathStream
|
||||||
|
.filter(Files::isRegularFile)
|
||||||
|
.map(ScriptEvaluationTest::mapToNpcScriptPath)
|
||||||
|
.toList();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String mapToNpcScriptPath(Path path) {
|
||||||
|
return "npc/%s".formatted(path.getFileName().toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@MethodSource("npcScriptFiles")
|
||||||
|
void npcScriptShouldEvaluate(String npcScriptPath) {
|
||||||
|
ScriptEngine scriptEngine = scriptManager.getInvocableScriptEngine(npcScriptPath);
|
||||||
|
|
||||||
|
assertNotNull(scriptEngine);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user