/interpreter/tags/at2-build190607/test/edu/vub/at/objects/mirrors/InvocationTest.java
Java | 270 lines | 159 code | 26 blank | 85 comment | 0 complexity | 14e88d61dfb891c0e2b2d41a6da3bd2a MD5 | raw file
1/** 2 * AmbientTalk/2 Project 3 * InvocationTest.java created on Jul 31, 2006 at 11:12:57 PM 4 * (c) Programming Technology Lab, 2006 - 2007 5 * Authors: Tom Van Cutsem & Stijn Mostinckx 6 * 7 * Permission is hereby granted, free of charge, to any person 8 * obtaining a copy of this software and associated documentation 9 * files (the "Software"), to deal in the Software without 10 * restriction, including without limitation the rights to use, 11 * copy, modify, merge, publish, distribute, sublicense, and/or 12 * sell copies of the Software, and to permit persons to whom the 13 * Software is furnished to do so, subject to the following 14 * conditions: 15 * 16 * The above copyright notice and this permission notice shall be 17 * included in all copies or substantial portions of the Software. 18 * 19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 20 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 21 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 22 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 23 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 24 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 25 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 26 * OTHER DEALINGS IN THE SOFTWARE. 27 */ 28 29package edu.vub.at.objects.mirrors; 30 31import edu.vub.at.exceptions.InterpreterException; 32import edu.vub.at.objects.ATMessage; 33import edu.vub.at.objects.ATObject; 34import edu.vub.at.objects.natives.NATContext; 35import edu.vub.at.objects.natives.NATMethodInvocation; 36import edu.vub.at.objects.natives.NATNil; 37import edu.vub.at.objects.natives.NATTable; 38import edu.vub.at.objects.natives.grammar.AGSymbol; 39 40/** 41 * @author smostinc 42 * 43 * InvocationTest tests the various reflective machinery provided in the mirrors 44 * package such as to see whether they work accurately. Three ATTypes are used in the 45 * course of this tests, namely ATBoolean (which provides easy cases to test), 46 * ATTable (which is used amongst others to test field access), and ATMessage (which 47 * has fields that can be set at the base level). 48 */ 49public class InvocationTest extends ReflectiveAccessTest { 50 51 public static void main(String[] args) { 52 junit.swingui.TestRunner.run(InvocationTest.class); 53 } 54 55 /** 56 * Tests the invocation of methods on natively implemented objects which thus 57 * adhere to the expected interfaces. This test checks the semantics of the 58 * classes under test such that faults can be traced back to either the 59 * NAT-objects or the reflective infrastructure. 60 */ 61 public void testJavaBaseMethodInvocation() { 62 try { 63 True.base_ifTrue_(success); 64 True.base_ifFalse_(fail); 65 True.base_ifTrue_ifFalse_(success, fail); 66 67 False.base_ifTrue_(fail); 68 False.base_ifFalse_(success); 69 False.base_ifTrue_ifFalse_(fail, success); 70 } catch (InterpreterException e) { 71 e.printStackTrace(); 72 fail("exception: "+e); 73 } 74 } 75 76 /** 77 * Simulates invocation from the base-level by manually calling meta_invoke. 78 * This test determines in case of failures whether they are due to a fault 79 * in the meta_invoke implementation of NATNil or in the semantics of method 80 * invocation as a result of meta_eval on an AG-component. 81 */ 82 public void testSimulatedBaseInvocation() { 83 try { 84 True.meta_invoke( 85 True, AGSymbol.jAlloc("ifTrue:"), 86 NATTable.atValue(new ATObject[] { success })); 87 True.meta_invoke( 88 True, AGSymbol.jAlloc("ifFalse:"), 89 NATTable.atValue(new ATObject[] { fail })); 90 True.meta_invoke( 91 True, AGSymbol.jAlloc("ifTrue:ifFalse:"), 92 NATTable.atValue(new ATObject[] { success, fail })); 93 94 False.meta_invoke( 95 False, AGSymbol.jAlloc("ifTrue:"), 96 NATTable.atValue(new ATObject[] { fail })); 97 False.meta_invoke( 98 False, AGSymbol.jAlloc("ifFalse:"), 99 NATTable.atValue(new ATObject[] { success })); 100 False.meta_invoke( 101 False, AGSymbol.jAlloc("ifTrue:ifFalse:"), 102 NATTable.atValue(new ATObject[] { fail, success })); 103 } catch (InterpreterException e) { 104 e.printStackTrace(); 105 fail("exception: "+e); 106 } 107 } 108 109 /** 110 * This test initialises a lexical root with the values success, fail, true and 111 * false. Then it invokes methods from base-level ambienttalk. This test 112 * concludes the first installment of test which test the plain invocation of 113 * base-level methods on native types in AmbientTalk. 114 */ 115 public void testBaseInvocation() { 116 try { 117 evaluateInput( 118 "true.ifTrue: success;" + 119 "true.ifFalse: fail;" + 120 "true.ifTrue: success ifFalse: fail;" + 121 "false.ifTrue: fail;" + 122 "false.ifFalse: success;" + 123 "false.ifTrue: fail ifFalse: success", 124 new NATContext(lexicalRoot, lexicalRoot)); 125 } catch (InterpreterException e) { 126 e.printStackTrace(); 127 fail("exception: "+ e); 128 } 129 } 130 131 /** 132 * Tests the accessing of fields on a natively implemented table. This test 133 * calls the methods from java and thus will fail only when the corresponding 134 * implementation is corrupted. 135 */ 136 public void testJavaBaseFieldAccess() { 137 try { 138 ATObject element = closures.base_at(closures.base_getLength()); 139 element.asClosure().base_apply(NATTable.EMPTY); 140 } catch (InterpreterException e) { 141 e.printStackTrace(); 142 fail("exception: "+e); 143 } 144 145 } 146 147 /** 148 * Tests the accessing of fields on a natively implemented table. If this test 149 * succeeds and the next test fails, the fault is due to the implementation of 150 * the AG-objects for tabulation and or application. 151 */ 152 public void testSimulatedBaseFieldAccess() { 153 try { 154 ATObject accessor = closures.meta_select(closures, AGSymbol.jAlloc("at")); 155 ATObject element = accessor.asClosure().base_apply( 156 NATTable.atValue(new ATObject[] { 157 closures.meta_select(closures, AGSymbol.jAlloc("length"))})); 158 element.asClosure().base_apply(NATTable.EMPTY); 159 } catch (InterpreterException e) { 160 e.printStackTrace(); 161 fail("exception: "+e); 162 } 163 } 164 165 /** 166 * Tests the accessing of fields on a natively implemented table. This test 167 * uses ambienttalk code for the evaluation, so 168 * 169 */ 170 public void testBaseFieldAccess() { 171 try { 172 evaluateInput( 173 "def accessor := closures.at;" + 174 "def expanded := accessor(closures.length);" + 175 "def coated := closures[closures.length];" + 176 "expanded();" + 177 "coated()", 178 new NATContext(lexicalRoot, lexicalRoot)); 179 } catch (InterpreterException e) { 180 e.printStackTrace(); 181 fail("exception: "+ e); 182 } 183 184 try { 185 evaluateInput( 186 "closures.at(closures.length)();" + 187 "closures[closures.length]()", 188 new NATContext(lexicalRoot, lexicalRoot)); 189 } catch (InterpreterException e) { 190 e.printStackTrace(); 191 fail("exception: "+ e); 192 } 193 194 } 195 196 /** 197 * Tests the assignment of fields on a natively implemented message send parse 198 * tree element. This test calls the methods from java and thus will fail only 199 * when the corresponding implementation is corrupted. 200 */ 201 public void testJavaBaseFieldAssignment() { 202 try { 203 ATMessage message = new NATMethodInvocation( 204 AGSymbol.jAlloc("at"), 205 NATTable.EMPTY, 206 NATTable.EMPTY); 207 208 message.base_setArguments( 209 NATTable.atValue(new ATObject[] { 210 closures.base_getLength() 211 })); 212 213 ATObject element = message.base_sendTo(closures, NATNil._INSTANCE_); 214 215 element.asClosure().base_apply(NATTable.EMPTY); 216 217 } catch (InterpreterException e) { 218 e.printStackTrace(); 219 fail("exception: "+e); 220 } 221 222 } 223 224 /** 225 * Tests the assignment of fields on a natively implemented message send parse 226 * tree element. If this test succeeds and the next test fails, the fault is 227 * due to the implementation of the AG-objects for assignment, quotation and or 228 * method invocation. 229 */ 230 public void testSimulatedBaseFieldAssignment() { 231 try { 232 ATMessage message = new NATMethodInvocation( 233 AGSymbol.jAlloc("at"), 234 NATTable.EMPTY, 235 NATTable.EMPTY); 236 237 message.meta_assignVariable( 238 AGSymbol.jAlloc("arguments"), 239 NATTable.atValue(new ATObject[] { 240 closures.base_getLength() 241 })); 242 243 ATObject element = message.base_sendTo(closures, NATNil._INSTANCE_); 244 245 element.asClosure().base_apply(NATTable.EMPTY); 246 } catch (InterpreterException e) { 247 e.printStackTrace(); 248 fail("exception: "+e); 249 } 250 } 251 252 /** 253 * Tests the accessing of fields on a natively implemented table. This test 254 * uses ambienttalk code for the evaluation, so 255 * 256 */ 257 public void testBaseFieldAssignment() { 258 try { 259 evaluateInput( 260 "def message := .at();" + 261 "message.arguments := [closures.length];" + 262 "def result := closures <+ message;" + 263 "result()", 264 new NATContext(lexicalRoot, lexicalRoot)); 265 } catch (InterpreterException e) { 266 e.printStackTrace(); 267 fail("exception: "+ e); 268 } 269 } 270}