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