/interpreter/tags/at2-build190607/test/edu/vub/at/objects/natives/NATObjectTest.java

http://ambienttalk.googlecode.com/ · Java · 110 lines · 84 code · 24 blank · 2 comment · 0 complexity · 6f218d4578c43f03f4f15a8f4cd9530a MD5 · raw file

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