forked from davidbkemp/redline-smalltalk
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: StefanKrecher <stefan.krecher@googlemail.com>
- Loading branch information
1 parent
319b3b6
commit 59d91ee
Showing
2 changed files
with
149 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* Redline Smalltalk, Copyright (c) James C. Ladd. All rights reserved. See LICENSE in the root of this distribution */ | ||
package st.redline; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertTrue; | ||
import static st.redline.RedlineTestHelper.assertIntegerEquals; | ||
import static st.redline.RedlineTestHelper.integerClass; | ||
import static st.redline.RedlineTestHelper.isSmalltalkFalse; | ||
import static st.redline.RedlineTestHelper.isSmalltalkTrue; | ||
import static st.redline.RedlineTestHelper.one; | ||
import static st.redline.RedlineTestHelper.send; | ||
import static st.redline.RedlineTestHelper.three; | ||
import static st.redline.RedlineTestHelper.two; | ||
|
||
import java.lang.reflect.Field; | ||
import java.util.Map; | ||
|
||
import org.junit.Test; | ||
|
||
public class IntegerTest { | ||
|
||
@Test | ||
public void testProtoObject() throws Exception { | ||
assertEquals("st.redline.Integer", integerClass.toString()); | ||
|
||
Class<?> clazz = Class.forName("st.redline.ProtoObject"); | ||
Field dataField = clazz.getDeclaredField("data"); | ||
dataField.setAccessible(true); | ||
ProtoObjectData poExampleClassData = (ProtoObjectData) dataField.get(integerClass); | ||
|
||
assertTrue(poExampleClassData.isClass()); | ||
|
||
clazz = Class.forName("st.redline.ProtoObjectData$ClassData"); | ||
Field methodsField = clazz.getDeclaredField("methods"); | ||
methodsField.setAccessible(true); | ||
@SuppressWarnings("unchecked") | ||
Map<String, ProtoMethod> methods = (Map<String, ProtoMethod>) methodsField.get(poExampleClassData); | ||
|
||
assertTrue(methods.keySet().contains("+")); | ||
assertTrue(methods.keySet().contains("-")); | ||
assertTrue(methods.keySet().contains("<")); | ||
assertTrue(methods.keySet().contains("\\>")); | ||
assertTrue(methods.keySet().contains(">=")); | ||
assertTrue(methods.keySet().contains("<=")); | ||
assertTrue(methods.keySet().contains("=")); | ||
|
||
} | ||
|
||
@Test | ||
public void testAdd() throws Exception { | ||
assertIntegerEquals(3, send(one, "+", two)); | ||
} | ||
|
||
@Test | ||
public void testSub() throws Exception { | ||
assertIntegerEquals(1, send(three, "-", two)); | ||
} | ||
|
||
@Test | ||
public void testLessThan() throws Exception { | ||
assertTrue(isSmalltalkTrue(send(one, "<", two))); | ||
assertTrue(isSmalltalkFalse(send(two, "<", one))); | ||
} | ||
|
||
@Test | ||
public void testGreaterThan() throws Exception { | ||
assertTrue(isSmalltalkFalse(send(one, "\\>", two))); | ||
assertTrue(isSmalltalkTrue(send(two, "\\>", one))); | ||
} | ||
|
||
@Test | ||
public void testLessEquals() throws Exception { | ||
assertTrue(isSmalltalkTrue(send(one, "<=", two))); | ||
assertTrue(isSmalltalkTrue(send(one, "<=", one))); | ||
} | ||
|
||
@Test | ||
public void testGreaterEquals() throws Exception { | ||
assertTrue(isSmalltalkTrue(send(two, ">=", one))); | ||
assertTrue(isSmalltalkTrue(send(two, ">=", two))); | ||
} | ||
|
||
@Test | ||
public void testEquals() throws Exception { | ||
assertTrue(isSmalltalkTrue(send(two, "=", two))); | ||
assertTrue(isSmalltalkFalse(send(two, "=", three))); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* Redline Smalltalk, Copyright (c) James C. Ladd. All rights reserved. See LICENSE in the root of this distribution */ | ||
package st.redline; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import java.math.BigDecimal; | ||
|
||
public class RedlineTestHelper { | ||
|
||
public static final Stic stic = new Stic(new CommandLine(new String[]{"-v"})); | ||
public static final ProtoObject integerClass = createIntegerClass();; | ||
public static final ProtoObject one = createIntegerInstance(1); | ||
public static final ProtoObject two = createIntegerInstance(2); | ||
public static final ProtoObject three = createIntegerInstance(3); | ||
|
||
public static ProtoObject createIntegerClass() { | ||
try { | ||
return stic.invoke("st.redline.Integer"); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
return null; | ||
} | ||
|
||
public static ProtoObject createIntegerInstance() { | ||
try { | ||
return ProtoObject.primitiveSend(integerClass, "new", integerClass); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
return null; | ||
} | ||
|
||
public static ProtoObject createIntegerInstance(Integer value) { | ||
ProtoObject po = createIntegerInstance(); | ||
po.javaValue(new BigDecimal(value)); | ||
return po; | ||
} | ||
|
||
public static ProtoObject send(ProtoObject integer1, String messageSelector, ProtoObject integer2) { | ||
ProtoObject result = ProtoObject.primitiveSend(integer1, integer2, messageSelector, integerClass); | ||
return result; | ||
} | ||
|
||
public static void assertIntegerEquals(Integer expected, ProtoObject result) { | ||
assertEquals(new BigDecimal(expected), (BigDecimal)result.javaValue()); | ||
} | ||
|
||
public static Boolean isSmalltalkTrue(ProtoObject protoObject) { | ||
String value = protoObject.cls().toString(); | ||
return value.equalsIgnoreCase("True"); | ||
} | ||
|
||
public static Boolean isSmalltalkFalse(ProtoObject protoObject) { | ||
String value = protoObject.cls().toString(); | ||
return value.equalsIgnoreCase("False"); | ||
} | ||
|
||
|
||
} |