Move expected exception handling off test runner.

Implement it in launcher genertor instead
This commit is contained in:
Alexey Andreev 2017-04-22 00:26:43 +03:00
parent e6606302cc
commit d5db7270be
5 changed files with 37 additions and 45 deletions

4
tests/src/test/js/run.sh Executable file
View File

@ -0,0 +1,4 @@
#!/bin/bash
npm run build
node start.js ../../../target/js-tests/

View File

@ -229,13 +229,9 @@ public class TeaVMTestRunner extends Runner implements Filterable {
for (TeaVMTestConfiguration configuration : configurations) { for (TeaVMTestConfiguration configuration : configurations) {
try { try {
TestRun run = compileByTeaVM(child, notifier, expectedExceptions, configuration, onSuccess.get(0)); TestRun run = compileByTeaVM(child, notifier, configuration, onSuccess.get(0));
if (run != null) { if (run != null) {
runs.add(run); runs.add(run);
} else {
notifier.fireTestFinished(description);
latch.countDown();
return;
} }
} catch (Throwable e) { } catch (Throwable e) {
notifier.fireTestFailure(new Failure(description, e)); notifier.fireTestFailure(new Failure(description, e));
@ -293,7 +289,7 @@ public class TeaVMTestRunner extends Runner implements Filterable {
return true; return true;
} }
private TestRun compileByTeaVM(Method child, RunNotifier notifier, Set<Class<?>> expectedExceptions, private TestRun compileByTeaVM(Method child, RunNotifier notifier,
TeaVMTestConfiguration configuration, Consumer<Boolean> onComplete) { TeaVMTestConfiguration configuration, Consumer<Boolean> onComplete) {
Description description = describeChild(child); Description description = describeChild(child);
@ -330,7 +326,7 @@ public class TeaVMTestRunner extends Runner implements Filterable {
return new TestRun(compileResult.file.getParentFile(), child, return new TestRun(compileResult.file.getParentFile(), child,
new MethodReference(testClass.getName(), getDescriptor(child)), new MethodReference(testClass.getName(), getDescriptor(child)),
description, callback, expectedExceptions); description, callback);
} }
private void submitRun(TestRun run) { private void submitRun(TestRun run) {

View File

@ -15,14 +15,20 @@
*/ */
package org.teavm.junit; package org.teavm.junit;
import org.junit.Test;
import org.teavm.diagnostics.Diagnostics; import org.teavm.diagnostics.Diagnostics;
import org.teavm.model.AnnotationReader;
import org.teavm.model.AnnotationValue;
import org.teavm.model.BasicBlock;
import org.teavm.model.ClassHolder; import org.teavm.model.ClassHolder;
import org.teavm.model.ClassHolderTransformer; import org.teavm.model.ClassHolderTransformer;
import org.teavm.model.ClassReaderSource; import org.teavm.model.ClassReaderSource;
import org.teavm.model.ElementModifier; import org.teavm.model.ElementModifier;
import org.teavm.model.MethodHolder; import org.teavm.model.MethodHolder;
import org.teavm.model.MethodReader;
import org.teavm.model.MethodReference; import org.teavm.model.MethodReference;
import org.teavm.model.Program; import org.teavm.model.Program;
import org.teavm.model.TryCatchBlock;
import org.teavm.model.ValueType; import org.teavm.model.ValueType;
import org.teavm.model.emit.ProgramEmitter; import org.teavm.model.emit.ProgramEmitter;
import org.teavm.model.emit.ValueEmitter; import org.teavm.model.emit.ValueEmitter;
@ -33,7 +39,7 @@ class TestEntryPointTransformer implements ClassHolderTransformer, TeaVMPlugin {
private String runnerClassName; private String runnerClassName;
private MethodReference testMethod; private MethodReference testMethod;
public TestEntryPointTransformer(String runnerClassName, MethodReference testMethod) { TestEntryPointTransformer(String runnerClassName, MethodReference testMethod) {
this.runnerClassName = runnerClassName; this.runnerClassName = runnerClassName;
this.testMethod = testMethod; this.testMethod = testMethod;
} }
@ -75,7 +81,27 @@ class TestEntryPointTransformer implements ClassHolderTransformer, TeaVMPlugin {
pe.getField(TestEntryPoint.class, "testCase", Object.class) pe.getField(TestEntryPoint.class, "testCase", Object.class)
.cast(ValueType.object(testMethod.getClassName())) .cast(ValueType.object(testMethod.getClassName()))
.invokeSpecial(testMethod); .invokeSpecial(testMethod);
MethodReader testMethodReader = innerSource.resolve(testMethod);
AnnotationReader testAnnotation = testMethodReader.getAnnotations().get(Test.class.getName());
AnnotationValue throwsValue = testAnnotation.getValue("expected");
if (throwsValue != null) {
BasicBlock handler = pe.getProgram().createBasicBlock();
TryCatchBlock tryCatch = new TryCatchBlock();
tryCatch.setExceptionType(((ValueType.Object) throwsValue.getJavaClass()).getClassName());
tryCatch.setHandler(handler);
pe.getBlock().getTryCatchBlocks().add(tryCatch);
BasicBlock nextBlock = pe.getProgram().createBasicBlock();
pe.jump(nextBlock);
pe.enter(nextBlock);
pe.construct(AssertionError.class, pe.constant("Expected exception not thrown")).raise();
pe.enter(handler);
pe.exit(); pe.exit();
} else {
pe.exit();
}
return pe.getProgram(); return pe.getProgram();
} }
} }

View File

@ -17,9 +17,6 @@ package org.teavm.junit;
import java.io.File; import java.io.File;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import org.junit.runner.Description; import org.junit.runner.Description;
import org.teavm.model.MethodReference; import org.teavm.model.MethodReference;
@ -29,16 +26,14 @@ class TestRun {
private MethodReference reference; private MethodReference reference;
private Description description; private Description description;
private TestRunCallback callback; private TestRunCallback callback;
private Set<Class<?>> expectedExceptions;
TestRun(File baseDirectory, Method method, MethodReference reference, Description description, TestRun(File baseDirectory, Method method, MethodReference reference, Description description,
TestRunCallback callback, Set<Class<?>> expectedExceptions) { TestRunCallback callback) {
this.baseDirectory = baseDirectory; this.baseDirectory = baseDirectory;
this.method = method; this.method = method;
this.reference = reference; this.reference = reference;
this.description = description; this.description = description;
this.callback = callback; this.callback = callback;
this.expectedExceptions = Collections.unmodifiableSet(new HashSet<>(expectedExceptions));
} }
public File getBaseDirectory() { public File getBaseDirectory() {
@ -60,8 +55,4 @@ class TestRun {
public TestRunCallback getCallback() { public TestRunCallback getCallback() {
return callback; return callback;
} }
public Set<Class<?>> getExpectedExceptions() {
return expectedExceptions;
}
} }

View File

@ -33,10 +33,6 @@ class TestRunner {
this.strategy = strategy; this.strategy = strategy;
} }
public int getNumThreads() {
return numThreads;
}
public void setNumThreads(int numThreads) { public void setNumThreads(int numThreads) {
this.numThreads = numThreads; this.numThreads = numThreads;
} }
@ -100,32 +96,11 @@ class TestRunner {
String status = resultObject.get("status").asText(); String status = resultObject.get("status").asText();
switch (status) { switch (status) {
case "ok": case "ok":
if (!run.getExpectedExceptions().isEmpty()) {
run.getCallback().error(new AssertionError("Expected exception was not thrown"));
} else {
run.getCallback().complete(); run.getCallback().complete();
}
break; break;
case "exception": { case "exception": {
String stack = resultObject.get("stack").asText(); String stack = resultObject.get("stack").asText();
String exception = resultObject.has("exception") ? resultObject.get("exception").asText() : null; String exception = resultObject.has("exception") ? resultObject.get("exception").asText() : null;
Class<?> exceptionClass;
if (exception != null) {
try {
exceptionClass = Class.forName(exception, false, TestRunner.class.getClassLoader());
} catch (ClassNotFoundException e) {
exceptionClass = null;
}
} else {
exceptionClass = null;
}
if (exceptionClass != null) {
Class<?> caught = exceptionClass;
if (run.getExpectedExceptions().stream().anyMatch(e -> e.isAssignableFrom(caught))) {
run.getCallback().complete();
break;
}
}
run.getCallback().error(new AssertionError(exception + "\n" + stack)); run.getCallback().error(new AssertionError(exception + "\n" + stack));
break; break;
} }