3
0
Mirror von https://github.com/IntellectualSites/FastAsyncWorldEdit.git synchronisiert 2024-11-07 20:10:06 +01:00
Dieser Commit ist enthalten in:
MattBDev 2019-08-15 15:35:45 -04:00
Ursprung cbbe8b5dfa
Commit f8758994c3
2 geänderte Dateien mit 71 neuen und 73 gelöschten Zeilen

Datei anzeigen

@ -50,6 +50,11 @@ dependencies {
compile 'com.mojang:datafixerupper:1.0.20' compile 'com.mojang:datafixerupper:1.0.20'
compile 'com.github.luben:zstd-jni:1.1.1' compile 'com.github.luben:zstd-jni:1.1.1'
compile 'co.aikar:fastutil-lite:1.0' compile 'co.aikar:fastutil-lite:1.0'
testImplementation ("org.junit.jupiter:junit-jupiter-api:5.5.0")
testImplementation ("org.junit.jupiter:junit-jupiter-params:5.5.0")
testImplementation ("org.mockito:mockito-core:3.0.0")
testImplementation ("org.mockito:mockito-junit-jupiter:3.0.0")
testRuntime ("org.junit.jupiter:junit-jupiter-engine:5.5.0")
} }
tasks.withType(JavaCompile).configureEach { tasks.withType(JavaCompile).configureEach {

Datei anzeigen

@ -19,16 +19,6 @@
package com.sk89q.worldedit.internal.expression; package com.sk89q.worldedit.internal.expression;
import static java.lang.Math.atan2;
import static java.lang.Math.sin;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
import com.sk89q.worldedit.LocalConfiguration; import com.sk89q.worldedit.LocalConfiguration;
import com.sk89q.worldedit.WorldEdit; import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.extension.platform.Platform; import com.sk89q.worldedit.extension.platform.Platform;
@ -36,17 +26,27 @@ import com.sk89q.worldedit.internal.expression.lexer.LexerException;
import com.sk89q.worldedit.internal.expression.parser.ParserException; import com.sk89q.worldedit.internal.expression.parser.ParserException;
import com.sk89q.worldedit.internal.expression.runtime.EvaluationException; import com.sk89q.worldedit.internal.expression.runtime.EvaluationException;
import com.sk89q.worldedit.internal.expression.runtime.ExpressionEnvironment; import com.sk89q.worldedit.internal.expression.runtime.ExpressionEnvironment;
import com.sk89q.worldedit.internal.expression.runtime.ExpressionTimeoutException;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static java.lang.Math.atan2; import static java.lang.Math.atan2;
import static java.lang.Math.sin; import static java.lang.Math.sin;
import static org.junit.Assert.assertEquals; import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.Assert.fail; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class ExpressionTest { public class ExpressionTest {
@Before
private Platform mockPlat = mock(Platform.class);
@BeforeEach
public void setup() { public void setup() {
Platform mockPlat = Mockito.mock(Platform.class); when(mockPlat.getConfiguration()).thenReturn(new LocalConfiguration() {
Mockito.when(mockPlat.getConfiguration()).thenReturn(new LocalConfiguration() {
@Override @Override
public void load() { public void load() {
} }
@ -54,6 +54,11 @@ public class ExpressionTest {
WorldEdit.getInstance().getPlatformManager().register(mockPlat); WorldEdit.getInstance().getPlatformManager().register(mockPlat);
} }
@AfterEach
public void tearDown() {
WorldEdit.getInstance().getPlatformManager().unregister(mockPlat);
}
@Test @Test
public void testEvaluate() throws ExpressionException { public void testEvaluate() throws ExpressionException {
// check // check
@ -73,56 +78,46 @@ public class ExpressionTest {
} }
@Test @Test
public void testErrors() throws ExpressionException { public void testErrors() {
assertAll(
// test lexer errors // test lexer errors
try { () -> {
compile("#"); LexerException e = assertThrows(LexerException.class,
fail("Error expected"); () -> compile("#"));
} catch (LexerException e) { assertEquals(0, e.getPosition(), "Error position");
assertEquals("Error position", 0, e.getPosition()); },
}
// test parser errors // test parser errors
try { () -> {
compile("x"); ParserException e = assertThrows(ParserException.class,
fail("Error expected"); () -> compile("x"));
} catch (ParserException e) { assertEquals(0, e.getPosition(), "Error position");
assertEquals("Error position", 0, e.getPosition()); },
} () -> {
try { ParserException e = assertThrows(ParserException.class,
compile("x()"); () -> compile("x()"));
fail("Error expected"); assertEquals(0, e.getPosition(), "Error position");
} catch (ParserException e) { },
assertEquals("Error position", 0, e.getPosition()); () -> assertThrows(ParserException.class,
} () -> compile("(")),
try { () -> assertThrows(ParserException.class,
compile("("); () -> compile("x(")),
fail("Error expected");
} catch (ParserException ignored) {}
try {
compile("x(");
fail("Error expected");
} catch (ParserException ignored) {}
// test overloader errors // test overloader errors
try { () -> {
compile("atan2(1)"); ParserException e = assertThrows(ParserException.class,
fail("Error expected"); () -> compile("atan2(1)"));
} catch (ParserException e) { assertEquals(0, e.getPosition(), "Error position");
assertEquals("Error position", 0, e.getPosition()); },
} () -> {
try { ParserException e = assertThrows(ParserException.class,
compile("atan2(1, 2, 3)"); () -> compile("atan2(1, 2, 3)"));
fail("Error expected"); assertEquals(0, e.getPosition(), "Error position");
} catch (ParserException e) { },
assertEquals("Error position", 0, e.getPosition()); () -> {
} ParserException e = assertThrows(ParserException.class,
try { () -> compile("rotate(1, 2, 3)"));
compile("rotate(1, 2, 3)"); assertEquals(0, e.getPosition(), "Error position");
fail("Error expected");
} catch (ParserException e) {
assertEquals("Error position", 0, e.getPosition());
} }
);
} }
@Test @Test
@ -186,14 +181,12 @@ public class ExpressionTest {
} }
@Test @Test
public void testTimeout() throws Exception { public void testTimeout() {
try { ExpressionTimeoutException e = assertThrows(ExpressionTimeoutException.class,
simpleEval("for(i=0;i<256;i++){for(j=0;j<256;j++){for(k=0;k<256;k++){for(l=0;l<256;l++){ln(pi)}}}}"); () -> simpleEval("for(i=0;i<256;i++){for(j=0;j<256;j++){for(k=0;k<256;k++){for(l=0;l<256;l++){ln(pi)}}}}"),
fail("Loop was not stopped."); "Loop was not stopped.");
} catch (EvaluationException e) {
assertTrue(e.getMessage().contains("Calculations exceeded time limit")); assertTrue(e.getMessage().contains("Calculations exceeded time limit"));
} }
}
private double simpleEval(String expressionString) throws ExpressionException { private double simpleEval(String expressionString) throws ExpressionException {
final Expression expression = compile(expressionString); final Expression expression = compile(expressionString);