/interpreter/tags/at2-build190607/test/edu/vub/at/objects/mirrors/InvocationTest.java

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