Search in sources :

Example 61 with ClassBuilder

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

the class FlowControlTest method testIfTrueIfFalse3.

@Test
public void testIfTrueIfFalse3() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException {
    Class stClass = new ClassBuilder("FooObject_testIfTrueIfFalse3").method("foo: bar\n" + "	|res|\n" + "	bar ifTrue: [res := 1] ifFalse: [res := 2].\n" + "	^res").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)

Example 62 with ClassBuilder

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

the class FlowControlTest method testToDo.

@Test
public void testToDo() throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
    Class stClass = new ClassBuilder("FooObject_testToDo").method("foo ^self foo: 4").method("foo: bar |t| t := 0. 1 to: bar do: [:i | t := t + i]. ^t").build();
    Object fooObject = stClass.newInstance();
    Method method = fooObject.getClass().getMethod("foo");
    Object result = method.invoke(fooObject);
    assertEquals(Integer.valueOf(10), result);
}
Also used : Method(java.lang.reflect.Method) ClassBuilder(st.gravel.support.compiler.testtools.ClassBuilder) Test(org.junit.Test)

Example 63 with ClassBuilder

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

the class BlockTest method testCopyingBlockWithSelfReference.

@Test
public void testCopyingBlockWithSelfReference() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException {
    Class stClass = new ClassBuilder("FooObject_testCopyingBlockWithSelfReference").method("foo ^11").method("foo: bar\n" + "	| block |\n" + "	block := [:a | a + bar + self foo].\n" + "	^block value: 4").build();
    Object fooObject = stClass.newInstance();
    Method method = fooObject.getClass().getMethod("foo_", Object.class);
    assertEquals(Integer.valueOf(18), method.invoke(fooObject, 3));
}
Also used : Method(java.lang.reflect.Method) ClassBuilder(st.gravel.support.compiler.testtools.ClassBuilder) Test(org.junit.Test)

Example 64 with ClassBuilder

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

the class BlockTest method testNestedReturn.

@Test
public void testNestedReturn() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException {
    Class stClass = new ClassBuilder("FooObject_testNestedReturn").method("foo: bar\n" + "| a ar aw |\n" + "a := 0.\n" + "ar := [[^a] value].\n" + "aw := [:x | [:y | a := y + 7] value: x].\n" + "aw value: bar.	\n" + "^ar value").build();
    Object fooObject = stClass.newInstance();
    Method method = fooObject.getClass().getMethod("foo_", Object.class);
    assertEquals(Integer.valueOf(9), method.invoke(fooObject, 2));
    assertEquals(Integer.valueOf(8), method.invoke(fooObject, 1));
}
Also used : Method(java.lang.reflect.Method) ClassBuilder(st.gravel.support.compiler.testtools.ClassBuilder) Test(org.junit.Test)

Example 65 with ClassBuilder

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

the class BytecodeGeneratorTest method testReturnInt.

@Test
public void testReturnInt() throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
    Class stClass = new ClassBuilder("FooObject_testReturnInt").method("foo ^1").build();
    Object fooObject = stClass.newInstance();
    Method method = fooObject.getClass().getMethod("foo");
    Object result = method.invoke(fooObject);
    assertEquals(Integer.valueOf(1), result);
}
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