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

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