99 lines
2.8 KiB
Java
Raw Permalink Normal View History

2017-04-08 21:18:27 +02:00
/*
* Copyright 2017 - 2022 Volker Berlin (i-net software)
2017-04-08 21:18:27 +02:00
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
2017-04-17 08:23:26 +02:00
package de.inetsoftware.jwebassembly.runtime;
2017-04-08 21:18:27 +02:00
2017-04-14 16:31:35 +02:00
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
2017-04-08 21:18:27 +02:00
2017-04-14 16:31:35 +02:00
import java.io.IOException;
2017-04-08 21:18:27 +02:00
import java.util.Collection;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import de.inetsoftware.jwebassembly.ScriptEngine;
2017-04-14 16:31:35 +02:00
import de.inetsoftware.jwebassembly.WasmException;
2017-04-08 21:18:27 +02:00
import de.inetsoftware.jwebassembly.WasmRule;
import de.inetsoftware.jwebassembly.api.annotation.Export;
import de.inetsoftware.jwebassembly.api.annotation.Import;
2017-04-08 21:18:27 +02:00
/**
* @author Volker Berlin
*/
2017-04-09 13:05:34 +02:00
@RunWith( Parameterized.class )
2017-04-08 21:18:27 +02:00
public class RuntimeErrors {
private final ScriptEngine script;
public RuntimeErrors( ScriptEngine script ) {
this.script = script;
}
@Parameters( name = "{0}" )
public static Collection<ScriptEngine[]> data() {
2017-04-08 21:18:27 +02:00
return ScriptEngine.testParams();
}
private void compileErrorTest( String expectedMessge, Class<?> classes ) throws IOException {
WasmRule wasm = new WasmRule( classes );
2017-04-14 16:31:35 +02:00
try {
wasm.compile();
fail( "Exception expected with: " + expectedMessge );
2017-04-14 16:31:35 +02:00
} catch( WasmException ex ) {
assertTrue( "Wrong error message: " + ex.getMessage(), ex.getMessage().contains( expectedMessge ) );
2017-04-14 16:31:35 +02:00
} finally {
wasm.delete();
}
}
@Test
public void nonStaticExport() throws IOException {
compileErrorTest( "Export method must be static:", NonStaticExport.class );
}
static class NonStaticExport {
@Export
float function() {
return 1;
}
}
@Test
public void nonStaticImport() throws IOException {
compileErrorTest( "Import method must be static:", NonStaticImport.class );
}
static class NonStaticImport {
@Import( module = "m", name = "n" )
float function() {
return 1;
}
}
2019-03-12 22:36:43 +01:00
@Test
public void nativeMethod() throws IOException {
compileErrorTest( "Native methods cannot be compiled to WebAssembly:", NativeMethod.class );
2019-03-12 22:36:43 +01:00
}
static class NativeMethod {
@Export
native static float function();
}
2017-04-08 21:18:27 +02:00
}