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