PageRenderTime 53ms CodeModel.GetById 13ms app.highlight 34ms RepoModel.GetById 2ms app.codeStats 0ms

/interpreter/tags/at2dist091109/test/edu/vub/at/objects/mirrors/InvocationTest.java

http://ambienttalk.googlecode.com/
Java | 268 lines | 156 code | 26 blank | 86 comment | 0 complexity | 60a98f1491e9a43ea8d47315dcffa8e5 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.eval.Evaluator;
 32import edu.vub.at.exceptions.InterpreterException;
 33import edu.vub.at.objects.ATClosure;
 34import edu.vub.at.objects.ATMessage;
 35import edu.vub.at.objects.ATObject;
 36import edu.vub.at.objects.natives.NATContext;
 37import edu.vub.at.objects.natives.NATMethodInvocation;
 38import edu.vub.at.objects.natives.NATTable;
 39import edu.vub.at.objects.natives.grammar.AGAssignmentSymbol;
 40import edu.vub.at.objects.natives.grammar.AGSymbol;
 41
 42/**
 43 * @author smostinc
 44 * 
 45 * InvocationTest tests the various reflective machinery provided in the mirrors
 46 * package such as to see whether they work accurately. Three ATTypes are used in the
 47 * course of this tests, namely ATBoolean (which provides easy cases to test),
 48 * ATTable (which is used amongst others to test field access), and ATMessage (which
 49 * has fields that can be set at the base level).
 50 */
 51public class InvocationTest extends ReflectiveAccessTest {
 52
 53	public static void main(String[] args) {
 54		junit.swingui.TestRunner.run(InvocationTest.class);
 55	}
 56
 57	/**
 58	 * Tests the invocation of methods on natively implemented objects which thus
 59	 * adhere to the expected interfaces. This test checks the semantics of the
 60	 * classes under test such that faults can be traced back to either the 
 61	 * NAT-objects or the reflective infrastructure.
 62	 */
 63	public void testJavaBaseMethodInvocation() {
 64		try {
 65			True.base_ifTrue_(success);
 66			True.base_ifFalse_(fail);
 67			True.base_ifTrue_ifFalse_(success, fail);
 68			
 69			False.base_ifTrue_(fail);
 70			False.base_ifFalse_(success);
 71			False.base_ifTrue_ifFalse_(fail, success);
 72		} catch (InterpreterException e) {
 73			e.printStackTrace();
 74			fail("exception: "+e);
 75		}
 76	}
 77	
 78	/**
 79	 * Simulates invocation from the base-level by manually calling meta_invoke.
 80	 * This test determines in case of failures whether they are due to a fault 
 81	 * in the meta_invoke implementation of NATNil or in the semantics of method
 82	 * invocation as a result of meta_eval on an AG-component.
 83	 */
 84	public void testSimulatedBaseInvocation() {
 85		try {
 86			True.impl_invoke(
 87					True, AGSymbol.jAlloc("ifTrue:"),
 88					NATTable.atValue(new ATObject[] { success }));
 89			True.impl_invoke(
 90					True, AGSymbol.jAlloc("ifFalse:"),
 91					NATTable.atValue(new ATObject[] { fail }));
 92			True.impl_invoke(
 93					True, AGSymbol.jAlloc("ifTrue:ifFalse:"),
 94					NATTable.atValue(new ATObject[] { success, fail }));
 95
 96			False.impl_invoke(
 97					False, AGSymbol.jAlloc("ifTrue:"),
 98					NATTable.atValue(new ATObject[] { fail }));
 99			False.impl_invoke(
100					False, AGSymbol.jAlloc("ifFalse:"),
101					NATTable.atValue(new ATObject[] { success }));
102			False.impl_invoke(
103					False, AGSymbol.jAlloc("ifTrue:ifFalse:"),
104					NATTable.atValue(new ATObject[] { fail, success }));
105		} catch (InterpreterException e) {
106			e.printStackTrace();
107			fail("exception: "+e);
108		}
109	}
110	
111	/**
112	 * This test initialises a lexical root with the values success, fail, true and
113	 * false. Then it invokes methods from base-level ambienttalk. This test 
114	 * concludes the first installment of test which test the plain invocation of
115	 * base-level methods on native types in AmbientTalk.
116	 */
117	public void testBaseInvocation() {
118		try {
119			evaluateInput(
120					"true.ifTrue: &success;" +
121					"true.ifFalse: &fail;" +
122					"true.ifTrue: &success ifFalse: &fail;" +
123					"false.ifTrue: &fail;" +
124					"false.ifFalse: &success;" +
125					"false.ifTrue: &fail ifFalse: &success",
126					new NATContext(lexicalRoot, lexicalRoot));
127		} catch (InterpreterException e) {
128			e.printStackTrace();
129			fail("exception: "+ e);
130		}
131	}
132
133	/**
134	 * Tests the accessing of fields on a natively implemented table. This test 
135	 * calls the methods from java and thus will fail only when the corresponding
136	 * implementation is corrupted.
137	 */
138	public void testJavaBaseFieldAccess() {
139		try {
140			ATObject element = closures.base_at(closures.base_length());
141			element.asClosure().base_apply(NATTable.EMPTY);
142		} catch (InterpreterException e) {
143			e.printStackTrace();
144			fail("exception: "+e);
145		}
146		
147	}
148
149	/**
150	 * Tests the accessing of fields on a natively implemented table. If this test
151	 * succeeds and the next test fails, the fault is due to the implementation of
152	 * the AG-objects for tabulation and or application.
153	 */
154	public void testSimulatedBaseFieldAccess() {
155		try {
156			ATClosure accessor = closures.meta_select(closures, AGSymbol.jAlloc("at"));
157			ATObject element = accessor.base_apply(
158					NATTable.atValue(new ATObject[] {
159							closures.impl_invoke(closures, AGSymbol.jAlloc("length"), NATTable.EMPTY)}));
160			element.asClosure().base_apply(NATTable.EMPTY);
161		} catch (InterpreterException e) {
162			e.printStackTrace();
163			fail("exception: "+e);
164		}
165	}
166	
167	/**
168	 * Tests the accessing of fields on a natively implemented table. This test
169	 * uses ambienttalk code for the evaluation, so 
170	 *
171	 */
172	public void testBaseFieldAccess() {
173		try {
174			evaluateInput(
175					"def accessor := closures.&at;" +
176					"def expanded := accessor(closures.length);" +
177					"def coated := closures[closures.length];" +
178					"expanded();" +
179					"coated()",
180					new NATContext(lexicalRoot, lexicalRoot));
181		} catch (InterpreterException e) {
182			e.printStackTrace();
183			fail("exception: "+ e);
184		}
185		
186		try {
187			evaluateInput(
188					"closures.at(closures.length)();" +
189					"closures[closures.length]()",
190					new NATContext(lexicalRoot, lexicalRoot));
191		} catch (InterpreterException e) {
192			e.printStackTrace();
193			fail("exception: "+ e);
194		}
195		
196	}
197
198	/**
199	 * Tests the assignment of fields on a natively implemented message send parse
200	 * tree element. This test calls the methods from java and thus will fail only 
201	 * when the corresponding implementation is corrupted.
202	 */
203	public void testJavaBaseFieldAssignment() {
204		try {
205			ATMessage message = new NATMethodInvocation(
206					AGSymbol.jAlloc("at"), 
207					NATTable.EMPTY,
208					NATTable.EMPTY);
209			
210			message.base_arguments__opeql_(NATTable.of(closures.base_length()));
211
212			ATObject element = message.base_sendTo(closures, Evaluator.getNil());
213			
214			element.asClosure().base_apply(NATTable.EMPTY);
215
216		} catch (InterpreterException e) {
217			e.printStackTrace();
218			fail("exception: "+e);
219		}
220		
221	}
222
223	/**
224	 * Tests the assignment of fields on a natively implemented message send parse
225	 * tree element. If this test succeeds and the next test fails, the fault is 
226	 * due to the implementation of the AG-objects for assignment, quotation and or 
227	 * method invocation.
228	 */
229	public void testSimulatedBaseFieldAssignment() {
230		try {
231			ATMessage message = new NATMethodInvocation(
232					AGSymbol.jAlloc("at"), 
233					NATTable.EMPTY,
234					NATTable.EMPTY);
235			
236			// message.arguments := [3]
237			message.impl_call(
238					AGAssignmentSymbol.jAlloc("arguments:="), 
239					NATTable.of(NATTable.of(closures.base_length())));	
240
241			ATObject element = message.base_sendTo(closures, Evaluator.getNil());
242			
243			element.asClosure().base_apply(NATTable.EMPTY);
244		} catch (InterpreterException e) {
245			e.printStackTrace();
246			fail("exception: "+e);
247		}
248	}
249	
250	/**
251	 * Tests the accessing of fields on a natively implemented table. This test
252	 * uses ambienttalk code for the evaluation, so 
253	 *
254	 */
255	public void testBaseFieldAssignment() {
256		try {
257			evaluateInput(
258					"def message       := .at();" +
259					"message.arguments := [closures.length];" +
260					"def result        := closures <+ message;" +
261					"result()",
262					new NATContext(lexicalRoot, lexicalRoot));
263		} catch (InterpreterException e) {
264			e.printStackTrace();
265			fail("exception: "+ e);
266		}
267	}
268}