Search in sources :

Example 51 with ClassBuilder

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

the class BytecodeGeneratorTest method testPolymorphicDispatch.

@Test
public void testPolymorphicDispatch() throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
    Class constructor1 = new ClassBuilder("FooObject_testPolymorphicDispatch").method("foo ^3").method("bar: anObject ^(self boo: self) + (self boo: anObject)").method("boo: anObject ^anObject foo").build();
    Class constructor2 = new ClassBuilder("BorkObject_testPolymorphicDispatch").method("foo ^4").build();
    Object fooObject = constructor1.newInstance();
    Object borkObject = constructor2.newInstance();
    Method method = fooObject.getClass().getMethod("bar_", Object.class);
    Object result = method.invoke(fooObject, borkObject);
    assertEquals(Integer.valueOf(7), result);
}
Also used : Method(java.lang.reflect.Method) ClassBuilder(st.gravel.support.compiler.testtools.ClassBuilder) Test(org.junit.Test)

Example 52 with ClassBuilder

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

the class CollectionsTest method testSetCollect.

@Test
public void testSetCollect() throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
    Class stClass = new ClassBuilder("FooObject_testSetCollect").method("foo | set |" + "	set := Set new." + "	set add: 1." + "	set add: 2." + "	set add: 3." + "	^(set collect: [:each | each * each]) asJavaSet").build();
    Object fooObject = stClass.newInstance();
    Method method = fooObject.getClass().getMethod("foo");
    Object result = method.invoke(fooObject);
    assertEquals(3, ((Set) result).size());
    assertTrue(((Set) result).contains(9));
}
Also used : Method(java.lang.reflect.Method) ClassBuilder(st.gravel.support.compiler.testtools.ClassBuilder) Test(org.junit.Test)

Example 53 with ClassBuilder

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

the class CollectionsTest method test_asOrderedCollection.

@Test
public void test_asOrderedCollection() throws Throwable {
    Class stClass = new ClassBuilder("FooObject_test_asOrderedCollection").method("array" + "	^#(1 2 3)").method("foo" + "	^#(1 2 3) asOrderedCollection").build();
    Object fooObject = stClass.newInstance();
    Object coll = MethodTools.perform(fooObject, "foo");
    Object size = MethodTools.perform(coll, "size");
    assertEquals(3, ((int) size));
}
Also used : ClassBuilder(st.gravel.support.compiler.testtools.ClassBuilder) Test(org.junit.Test)

Example 54 with ClassBuilder

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

the class ExceptionTest method test_MessageNotUnderstood4.

@Test
public void test_MessageNotUnderstood4() throws Throwable {
    Class stClass = new ClassBuilder("FooObject_test_MessageNotUnderstood4").method("foo" + "^self zork: 4").method("doesNotUnderstand: aMessage" + "^3 + aMessage argument").build();
    Object fooObject = stClass.newInstance();
    assertEquals(Integer.valueOf(7), MethodTools.perform(fooObject, "foo"));
}
Also used : ClassBuilder(st.gravel.support.compiler.testtools.ClassBuilder) Test(org.junit.Test)

Example 55 with ClassBuilder

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

the class ExceptionTest method test_pass.

@Test
public void test_pass() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException {
    Class stClass = new ClassBuilder("FooObject_test_pass").instVar("bar").method("foo" + "	^self foo: [TestExceptionA raise. bar := #failed]").method("isFailed ^bar notNil").method("foo: aBlock  " + "^[[aBlock value] on: Exception do: [:ex | ex pass]] on: TestExceptionA do: [:ex1 | ex1 testValue]").build();
    Object fooObject = stClass.newInstance();
    Method method = fooObject.getClass().getMethod("foo");
    Method isFailed = fooObject.getClass().getMethod("isFailed");
    assertEquals(Integer.valueOf(7), method.invoke(fooObject));
    assertEquals(Boolean.FALSE, isFailed.invoke(fooObject));
}
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