/interpreter/tags/at2dist030708/test/edu/vub/at/objects/natives/NATObjectTest.java

http://ambienttalk.googlecode.com/ · Java · 111 lines · 85 code · 24 blank · 2 comment · 0 complexity · 333f7a1a180bf7aba42eebe65cc84779 MD5 · raw file

  1. package edu.vub.at.objects.natives;
  2. import edu.vub.at.AmbientTalkTest;
  3. import edu.vub.at.eval.Evaluator;
  4. import edu.vub.at.exceptions.InterpreterException;
  5. import edu.vub.at.exceptions.XSelectorNotFound;
  6. import edu.vub.at.objects.ATContext;
  7. import edu.vub.at.objects.ATObject;
  8. import edu.vub.at.objects.ATTable;
  9. import edu.vub.at.objects.mirrors.NativeClosure;
  10. import edu.vub.at.objects.natives.grammar.AGSymbol;
  11. public class NATObjectTest extends AmbientTalkTest {
  12. private class TestException extends RuntimeException {
  13. private static final long serialVersionUID = 7666632653525022022L;
  14. public int code;
  15. public TestException(String message, int code) {
  16. super(message);
  17. this.code = code;
  18. }
  19. }
  20. private NATObject original;
  21. private NATObject extension;
  22. public static void main(String[] args) {
  23. junit.swingui.TestRunner.run(NATObjectTest.class);
  24. }
  25. public void setUp() throws Exception {
  26. original = new NATObject();
  27. original.meta_addMethod(
  28. new NATMethod(AGSymbol.jAlloc("defaultMethod"), NATTable.EMPTY, null, NATTable.EMPTY) {
  29. public ATObject base_apply(ATTable arguments, ATContext ctx) throws InterpreterException {
  30. throw new TestException("Application of this method is expected to fail", 0);
  31. }
  32. });
  33. extension = new NATObject(original.base_super(), original.lexicalParent_, NATObject._IS_A_);
  34. }
  35. public void testisCloneOf() throws Exception {
  36. ATObject clone = original.meta_clone();
  37. original.meta_isCloneOf(clone).base_ifFalse_(
  38. new NativeClosure(clone) {
  39. public ATObject base_apply(ATTable arguments) throws InterpreterException {
  40. fail("Cloning is not properly defined under the isCloneOf test.");
  41. return Evaluator.getNil();
  42. }
  43. });
  44. clone.meta_addMethod(
  45. new NATMethod(AGSymbol.jAlloc("addedMethod"), NATTable.EMPTY, null, NATTable.EMPTY) {
  46. public ATObject base_apply(ATTable arguments, ATContext ctx) throws InterpreterException {
  47. throw new TestException("This method needs to be visible in the clone", 1);
  48. }
  49. });
  50. try {
  51. clone.impl_invoke(clone, AGSymbol.jAlloc("addedMethod"), NATTable.EMPTY);
  52. } catch (TestException ae) {
  53. // given the definition, this should happen!!!
  54. } catch (XSelectorNotFound se) {
  55. // implies the addMethod to the clone was not performed correctly
  56. fail("performing meta_addMethod did not add the method as expected");
  57. }
  58. original.meta_isCloneOf(clone).base_ifTrue_(
  59. new NativeClosure(clone) {
  60. public ATObject base_apply(ATTable arguments) throws InterpreterException {
  61. fail("Adding fields to a clone should disrupt the isCloneOf test when comparing the original to the extended object.");
  62. return Evaluator.getNil();
  63. }
  64. });
  65. clone.meta_isCloneOf(original).base_ifFalse_(
  66. new NativeClosure(original) {
  67. public ATObject base_apply(ATTable arguments) throws InterpreterException {
  68. fail("Adding fields to a clone should NOT disrupt the isCloneOf test when comparing the extended object to the original.");
  69. return Evaluator.getNil();
  70. }
  71. });
  72. extension.meta_isCloneOf(original).base_ifTrue_(
  73. new NativeClosure(original) {
  74. public ATObject base_apply(ATTable arguments) throws InterpreterException {
  75. fail("Extensions should not return true to the isCloneOf test.");
  76. return Evaluator.getNil();
  77. }
  78. });
  79. extension.meta_isCloneOf(clone).base_ifTrue_(
  80. new NativeClosure(clone) {
  81. public ATObject base_apply(ATTable arguments) throws InterpreterException {
  82. fail("Extensions should not return true to the isCloneOf test.");
  83. return Evaluator.getNil();
  84. }
  85. });
  86. }
  87. }