Adds tests for metadata generators

This commit is contained in:
konsoletyper 2014-06-06 18:18:52 +04:00
parent 8956e8b8d0
commit c6e7b30bed
12 changed files with 262 additions and 38 deletions

View File

@ -178,6 +178,11 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.17</version>
</plugin>
</plugins>
</pluginManagement>
</build>

View File

@ -30,6 +30,12 @@
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.teavm</groupId>
<artifactId>teavm-platform</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.teavm</groupId>
<artifactId>teavm-core</artifactId>
@ -51,6 +57,13 @@
<groupId>org.teavm</groupId>
<artifactId>teavm-maven-plugin</artifactId>
<version>${project.version}</version>
<dependencies>
<dependency>
<groupId>org.teavm</groupId>
<artifactId>teavm-platform</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>generate-javascript-tests</id>
@ -96,6 +109,15 @@
</arguments>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<excludes>
<exclude>org/teavm/platform/metadata/*.java</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>

View File

@ -19,6 +19,9 @@ package org.teavm.platform.metadata;
*
* @author Alexey Andreev
*/
public class MetadataGeneratorTest {
@Resource
public interface DependentTestResource {
String getBar();
void setBar(String bar);
}

View File

@ -0,0 +1,59 @@
/*
* Copyright 2014 Alexey Andreev.
*
* 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.
*/
package org.teavm.platform.metadata;
import static org.junit.Assert.*;
import org.junit.Test;
/**
*
* @author Alexey Andreev
*/
public class MetadataGeneratorTest {
@MetadataProvider(TestResourceGenerator.class)
private native TestResource getNull();
@Test
public void nullExposed() {
assertNull(getNull());
}
@MetadataProvider(TestResourceGenerator.class)
private native TestResource getInt();
@Test
public void intExposed() {
assertEquals(23, getInt());
}
@MetadataProvider(TestResourceGenerator.class)
private native TestResource getResource();
@Test
public void resourceObjectExposed() {
TestResource res = getResource();
assertEquals(23, res.getA());
assertFalse(res.getB());
assertEquals(24, res.getD());
assertEquals(25, res.getE());
assertEquals(3.14, res.getF(), 0.001);
assertEquals(2.72, res.getG(), 0.001);
assertEquals(Integer.valueOf(26), res.getH());
assertNull(res.getI());
assertEquals(Byte.valueOf((byte)27), res.getJ());
assertEquals(Short.valueOf((short)28), res.getK());
}
}

View File

@ -0,0 +1,99 @@
/*
* Copyright 2014 Alexey Andreev.
*
* 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.
*/
package org.teavm.platform.metadata;
/**
*
* @author Alexey Andreev
*/
@Resource
public interface TestResource {
int getA();
void setA(int a);
boolean getB();
void setB(boolean b);
byte getD();
void setD(byte d);
short getE();
void setE(short e);
float getF();
void setF(float f);
double getG();
void setG(double g);
Integer getH();
void setH(Integer a);
Boolean getI();
void setI(Boolean i);
Byte getJ();
void setJ(Byte d);
Short getK();
void setK(Short k);
Float getL();
void setL(Float l);
Double getM();
void setM(Double g);
String getFoo();
void setFoo(String foo);
ResourceArray<Integer> getArrayA();
void setArrayA(ResourceArray<Integer> arrayA);
ResourceArray<DependentTestResource> getArrayB();
void setArrayB(ResourceArray<DependentTestResource> arrayB);
ResourceArray<ResourceArray<String>> getArrayC();
void setArrayC(ResourceArray<ResourceArray<String>> arrayC);
ResourceMap<Integer> getMapA();
void setMapA(ResourceMap<Integer> mapA);
ResourceMap<DependentTestResource> getMapB();
void setMapB(ResourceMap<DependentTestResource> mapB);
ResourceMap<ResourceArray<String>> getMapC();
void setMapC(ResourceMap<ResourceArray<String>> mapC);
}

View File

@ -0,0 +1,65 @@
/*
* Copyright 2014 Alexey Andreev.
*
* 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.
*/
package org.teavm.platform.metadata;
import org.teavm.model.MethodReference;
/**
*
* @author Alexey Andreev
*/
public class TestResourceGenerator implements MetadataGenerator {
@Override
public Object generateMetadata(MetadataGeneratorContext context, MethodReference method) {
switch (method.getName()) {
case "getNull":
return null;
case "getInt":
return 23;
case "getResource":
return getResource(context);
default:
throw new RuntimeException("Unsupported method: " + method);
}
}
private Object getResource(MetadataGeneratorContext context) {
TestResource resource = context.createResource(TestResource.class);
resource.setA(23);
resource.setB(false);
resource.setD((byte)24);
resource.setE((short)25);
resource.setF(3.14f);
resource.setG(2.72);
resource.setH(26);
resource.setI(null);
resource.setJ((byte)27);
resource.setK((short)28);
resource.setL(100f);
resource.setM(200.0);
ResourceArray<Integer> array = context.createResourceArray();
array.add(2);
array.add(3);
resource.setArrayA(array);
DependentTestResource dep = context.createResource(DependentTestResource.class);
dep.setBar("baz");
ResourceArray<DependentTestResource> resArray = context.createResourceArray();
resArray.add(dep);
resource.setArrayB(resArray);
return resource;
}
}

View File

@ -85,6 +85,7 @@ public class Decompiler {
@Override public void run() {
Decompiler copy = new Decompiler(classSource, classLoader, executor);
copy.generators = generators;
copy.methodsToPass = methodsToPass;
result.set(index, copy.decompile(classSource.get(className)));
}
});

View File

@ -74,7 +74,7 @@ public class MetadataProviderNativeGenerator implements Generator {
// Generate resource loader
Object resource = generator.generateMetadata(metadataContext, methodRef);
writer.append("if (!").appendMethodBody(methodRef).append("$$resource === undefined").append(") {")
writer.append("if (!window.hasOwnProperty(\"").appendMethodBody(methodRef).append("$$resource\")) {")
.indent().softNewLine();
writer.appendMethodBody(methodRef).append("$$resource = ");
ResourceWriterHelper.write(writer, resource);

View File

@ -29,10 +29,11 @@ class MetadataProviderTransformer implements ClassHolderTransformer {
for (MethodHolder method : cls.getMethods()) {
AnnotationReader providerAnnot = method.getAnnotations().get(MetadataProvider.class.getName());
if (providerAnnot == null) {
return;
continue;
}
AnnotationHolder genAnnot = new AnnotationHolder(GeneratedBy.class.getName());
genAnnot.getValues().put("value", new AnnotationValue(ValueType.object(null)));
genAnnot.getValues().put("value", new AnnotationValue(ValueType.object(
MetadataProviderNativeGenerator.class.getName())));
method.getAnnotations().add(genAnnot);
}
}

View File

@ -34,8 +34,8 @@ class ResourceAccessorTransformer implements ClassHolderTransformer {
@Override
public void transformClass(ClassHolder cls, ClassReaderSource innerSource) {
ResourceAccessorGenerator generator = new ResourceAccessorGenerator();
if (cls.getName().equals(ResourceAccessor.class)) {
if (cls.getName().equals(ResourceAccessor.class.getName())) {
ResourceAccessorGenerator generator = new ResourceAccessorGenerator();
for (MethodHolder method : cls.getMethods()) {
vm.add(method.getReference(), generator);
}

View File

@ -211,7 +211,7 @@ class ResourceTransformer implements ClassHolderTransformer {
String primitiveCapitalized = primitive.getName();
primitiveCapitalized = Character.toUpperCase(primitiveCapitalized.charAt(0)) +
primitiveCapitalized.substring(1);
castInvoke.setMethod(new MethodReference(ResourceAccessor.class, "castTo" + primitiveCapitalized + "Primitive",
castInvoke.setMethod(new MethodReference(ResourceAccessor.class, "castTo" + primitiveCapitalized + "Wrapper",
Object.class, wrapper));
castInvoke.getArguments().add(resultVar);
castInvoke.setReceiver(insn.getReceiver());

View File

@ -1,31 +0,0 @@
/*
* Copyright 2014 Alexey Andreev.
*
* 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.
*/
package org.teavm.platform.metadata;
/**
*
* @author Alexey Andreev
*/
@Resource
public interface TestResource {
int getInt();
void setInt(int value);
String getString();
void setString(String string);
}