Search in sources :

Example 6 with ClassBuilder

use of st.gravel.support.compiler.testtools.ClassBuilder in project gravel by gravel-st.

the class LoopTest method test_whileTrue_.

@Test
public void test_whileTrue_() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException {
    Class stClass = new ClassBuilder("FooObject_test_whileTrue_").method("foo: sz | sum arr | arr := (1 to: sz) asArray.  sum := 0. [sum := sum + arr first. arr size ~= 1] whileTrue: [arr := arr tail].  ^sum").build();
    Object fooObject = stClass.newInstance();
    Method method = fooObject.getClass().getMethod("foo_", Object.class);
    assertEquals(Integer.valueOf(1), method.invoke(fooObject, 1));
    assertEquals(Integer.valueOf(21), method.invoke(fooObject, 6));
}
Also used : Method(java.lang.reflect.Method) ClassBuilder(st.gravel.support.compiler.testtools.ClassBuilder) Test(org.junit.Test)

Example 7 with ClassBuilder

use of st.gravel.support.compiler.testtools.ClassBuilder in project gravel by gravel-st.

the class LoopTest method test_int_to_BigInt_do_.

@Test
public void test_int_to_BigInt_do_() throws Throwable {
    Class stClass = new ClassBuilder("FooObject_test_int_to_BigInt_do_").instVar("startRuns").instVar("stopRuns").method("initialize startRuns := 0. stopRuns := 0").method("validateRun ^startRuns = 1 and: [stopRuns = 1]").method("start startRuns := startRuns + 1. ^16r7FFFFFFF").method("stop stopRuns := stopRuns + 1. ^(16r7FFFFFFF + 1)").method("foo | sum | sum := 0. self start to: self stop do: [:i | sum := sum + i].  ^sum").build();
    Object fooObject = stClass.newInstance();
    MethodTools.perform(fooObject, "initialize");
    Object result = MethodTools.perform(fooObject, "foo");
    boolean validateRun = (Boolean) MethodTools.perform(fooObject, "validateRun");
    assertEquals(new BigInteger("4294967295"), result);
    assertTrue(validateRun);
}
Also used : BigInteger(java.math.BigInteger) ClassBuilder(st.gravel.support.compiler.testtools.ClassBuilder) Test(org.junit.Test)

Example 8 with ClassBuilder

use of st.gravel.support.compiler.testtools.ClassBuilder in project gravel by gravel-st.

the class ReflectionTest method testReflect.

@Test
public void testReflect() throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
    Class classA = new ClassBuilder("ClassA_testReflect").method("reflect: anObject" + "	^Reflection reflect: anObject").method("className: anObject" + "	^(self reflect: anObject) classMirror name").method("metaclassName: anObject" + "	^(self reflect: anObject) classMirror meta name").build();
    Object classAObj = classA.newInstance();
    Method className_ = classAObj.getClass().getMethod("className_", Object.class);
    Object result = className_.invoke(classAObj, new Object());
    assertEquals(Symbol.value("Object"), result);
    Method metaclassName_ = classAObj.getClass().getMethod("metaclassName_", Object.class);
    result = metaclassName_.invoke(classAObj, new Object());
    assertEquals("Object class", result);
}
Also used : Method(java.lang.reflect.Method) ClassBuilder(st.gravel.support.compiler.testtools.ClassBuilder) Test(org.junit.Test)

Example 9 with ClassBuilder

use of st.gravel.support.compiler.testtools.ClassBuilder in project gravel by gravel-st.

the class StringTest method test_String_equals.

@Test
public void test_String_equals() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException {
    Class stClass = new ClassBuilder("FooObject_String_equals").method("foo: x equals: y ^x=y").method("foo ^self foo: 'abc' equals: 'ab','c'").build();
    Object fooObject = stClass.newInstance();
    Method method = fooObject.getClass().getMethod("foo");
    assertEquals(Boolean.TRUE, method.invoke(fooObject));
}
Also used : Method(java.lang.reflect.Method) ClassBuilder(st.gravel.support.compiler.testtools.ClassBuilder) Test(org.junit.Test)

Example 10 with ClassBuilder

use of st.gravel.support.compiler.testtools.ClassBuilder in project gravel by gravel-st.

the class FlowControlTest method testIfTrue1.

@Test
public void testIfTrue1() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException {
    Class stClass = new ClassBuilder("FooObject_testIfTrue1").method("foo: bar bar ifTrue: [^1]. ^2").build();
    Object fooObject = stClass.newInstance();
    Method method = fooObject.getClass().getMethod("foo_", Object.class);
    assertEquals(Integer.valueOf(1), method.invoke(fooObject, true));
    assertEquals(Integer.valueOf(2), method.invoke(fooObject, false));
}
Also used : Method(java.lang.reflect.Method) ClassBuilder(st.gravel.support.compiler.testtools.ClassBuilder) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)69 ClassBuilder (st.gravel.support.compiler.testtools.ClassBuilder)69 Method (java.lang.reflect.Method)65 BigInteger (java.math.BigInteger)4 Date (java.util.Date)1