Refactored test code a bit.
This commit is contained in:
@@ -3,7 +3,6 @@ package org.jetbrains.k2js.test;
|
||||
import org.junit.Test;
|
||||
import org.mozilla.javascript.Context;
|
||||
import org.mozilla.javascript.Function;
|
||||
import org.mozilla.javascript.NativeObject;
|
||||
import org.mozilla.javascript.Scriptable;
|
||||
|
||||
import java.util.Arrays;
|
||||
@@ -11,8 +10,6 @@ import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* @author Talanov Pavel
|
||||
*/
|
||||
@@ -30,39 +27,27 @@ public class KotlinLibTest extends TranslationTest {
|
||||
return MAIN;
|
||||
}
|
||||
|
||||
protected void verifyObjectHasExpectedPropertiesOfExpectedTypes
|
||||
(NativeObject object, Map<String, Class<? extends Scriptable>> nameToClassMap) {
|
||||
for (Map.Entry<String, Class<? extends Scriptable>> entry : nameToClassMap.entrySet()) {
|
||||
String name = entry.getKey();
|
||||
Class expectedClass = entry.getValue();
|
||||
assertTrue(object + " must contain key " + name, object.containsKey(name));
|
||||
assertTrue(object + "'s property " + name + " must be of type " + expectedClass,
|
||||
expectedClass.isInstance(object.get(name)));
|
||||
}
|
||||
private void runPropertyTypeCheck(String objectName, Map<String, Class<? extends Scriptable>> propertyToType)
|
||||
throws Exception {
|
||||
runRhinoTest(Arrays.asList(kotlinLibraryPath()), new RhinoPropertyTypesChecker(objectName, propertyToType));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void kotlinJsLibRunsWithRhino() throws Exception {
|
||||
Context context = Context.enter();
|
||||
Scriptable scope = context.initStandardObjects();
|
||||
// ss
|
||||
runFileWithRhino(kotlinLibraryPath(), context, scope);
|
||||
Context.exit();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void classObjectHasCreateMethod() throws Exception {
|
||||
final Map<String, Class<? extends Scriptable>> nameToClassMap =
|
||||
new HashMap<String, Class<? extends Scriptable>>();
|
||||
nameToClassMap.put("create", Function.class);
|
||||
|
||||
runRhinoTest(Arrays.asList(kotlinLibraryPath()),
|
||||
new RhinoResultChecker() {
|
||||
@Override
|
||||
public void runChecks(Context context, Scriptable scope) throws Exception {
|
||||
NativeObject object = RhinoUtils.extractObject("Class", scope);
|
||||
verifyObjectHasExpectedPropertiesOfExpectedTypes(object, nameToClassMap);
|
||||
}
|
||||
});
|
||||
String objectName = "Class";
|
||||
final Map<String, Class<? extends Scriptable>> propertyToType
|
||||
= new HashMap<String, Class<? extends Scriptable>>();
|
||||
propertyToType.put("create", Function.class);
|
||||
runPropertyTypeCheck(objectName, propertyToType);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
package org.jetbrains.k2js.test;
|
||||
|
||||
import org.mozilla.javascript.Context;
|
||||
import org.mozilla.javascript.NativeObject;
|
||||
import org.mozilla.javascript.Scriptable;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* @author Talanov Pavel
|
||||
*/
|
||||
public class RhinoPropertyTypesChecker implements RhinoResultChecker {
|
||||
|
||||
final private String objectName;
|
||||
final private Map<String, Class<? extends Scriptable>> propertyToType;
|
||||
|
||||
public RhinoPropertyTypesChecker(String objectName, Map<String, Class<? extends Scriptable>> propertyToType) {
|
||||
this.objectName = objectName;
|
||||
this.propertyToType = propertyToType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void runChecks(Context context, Scriptable scope) throws Exception {
|
||||
NativeObject object = RhinoUtils.extractObject(objectName, scope);
|
||||
verifyObjectHasExpectedPropertiesOfExpectedTypes(object, propertyToType);
|
||||
}
|
||||
|
||||
private void verifyObjectHasExpectedPropertiesOfExpectedTypes
|
||||
(NativeObject object, Map<String, Class<? extends Scriptable>> nameToClassMap) {
|
||||
for (Map.Entry<String, Class<? extends Scriptable>> entry : nameToClassMap.entrySet()) {
|
||||
String name = entry.getKey();
|
||||
Class expectedClass = entry.getValue();
|
||||
assertTrue(object + " must contain key " + name, object.containsKey(name));
|
||||
assertTrue(object + "'s property " + name + " must be of type " + expectedClass,
|
||||
expectedClass.isInstance(object.get(name)));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user