Skip to content

Instantly share code, notes, and snippets.

@timaschew
Created February 13, 2011 14:56
Show Gist options
  • Save timaschew/824737 to your computer and use it in GitHub Desktop.
Save timaschew/824737 to your computer and use it in GitHub Desktop.
public void testResult(String string, Double expected) {
testResult(string, new Value(expected));
}
public void testResult(String string, Boolean expected) {
testResult(string, new Value(expected), Type.BOOLEAN);
}
private final static String CLASS_NAME = "MyGeneratedClass";
private static Integer classCounter = new Integer(1);
public void testResult(String string, Value expected) {
testResult(string, expected, Type.DOUBLE);
}
public void testResult(String string, Value expected, Type type) {
String className = CLASS_NAME+(classCounter++);
ParserBroken parser = new ParserBroken();
// generate code
parser.parse(className, type, string);
// load code
ClassLoader classLoader = this.getClass().getClassLoader();
Class<?> loadedClass = null;
try {
loadedClass = classLoader.loadClass(className);
} catch (ClassNotFoundException e) {
System.err.println("could not load class: "+className);
e.printStackTrace();
}
// instance class
Object instance = null;
Method method = null;
try {
instance = loadedClass.newInstance();
method = loadedClass.getDeclaredMethod("doIt", new Class[]{});
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
// execute
Value value = null;
try {
Object result = method.invoke(instance, new Object[]{});
value = new Value(result);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
Assert.assertEquals(expected, value);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment