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

http://ambienttalk.googlecode.com/ · Java · 292 lines · 172 code · 44 blank · 76 comment · 0 complexity · e9bbfea769d0d87f7ab0f893eb427e7b MD5 · raw file

  1. /**
  2. * AmbientTalk/2 Project
  3. * MirageTest.java created on Aug 11, 2006 at 10:54:29 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.AmbientTalkTest;
  30. import edu.vub.at.exceptions.InterpreterException;
  31. import edu.vub.at.objects.ATObject;
  32. import edu.vub.at.objects.ATTable;
  33. import edu.vub.at.objects.natives.NATBoolean;
  34. import edu.vub.at.objects.natives.NATNil;
  35. import edu.vub.at.objects.natives.NATNumber;
  36. import edu.vub.at.objects.natives.grammar.AGSymbol;
  37. /**
  38. * MirageTest tests the creation of Mirages (objects with custom meta-behaviour) given
  39. * a NATIntercessiveMirror instance.
  40. *
  41. * @author smostinc
  42. */
  43. public class MirageTest extends AmbientTalkTest {
  44. public static void main(String[] args) {
  45. junit.swingui.TestRunner.run(MirageTest.class);
  46. }
  47. protected void setUp() throws Exception {
  48. super.setUp();
  49. evalAndReturn("def let: clo { clo(); };");
  50. }
  51. /**
  52. * This test verifies invariants with respect to the creation of mirages in AmbientTalk.
  53. * First of all, it tests the relationship between the original mirror and the mirage's
  54. * mirror (they should be non-identical yet cloned from each other).
  55. */
  56. public void testMirageCreation() throws InterpreterException {
  57. ATObject mirrorP = evalAndReturn(
  58. "def mirrorP := mirror: { \n" +
  59. " def iAm := \"the original\"; \n" +
  60. " def init(@args) { \n" +
  61. " iAm := \"a clone\"; \n" +
  62. " super^init(@args); \n" +
  63. " } \n" +
  64. "} \n");
  65. ATObject baseP = evalAndReturn(
  66. "def baseP := mirrorP.base;");
  67. // when creating a mirror, its base field is initialised to an empty mirage
  68. assertEquals(baseP.getClass(), NATMirage.class);
  69. evalAndCompareTo("mirrorP.listMethods().length", "3"); // default objects have three primitve methods
  70. evalAndCompareTo( // default objects have one primitive field super
  71. "mirrorP.listFields()",
  72. "[<field:super>]");
  73. // when creating an ex nihilo object, the init is not called
  74. evalAndCompareTo(
  75. "mirrorP.iAm",
  76. "\"the original\"");
  77. ATObject subject = evalAndReturn(
  78. "def subject := object: { \n" +
  79. " def field := `field; \n" +
  80. " def canonical() { nil }; \n" +
  81. " def keyworded: arg1 message: arg2 { nil }; \n" +
  82. "} mirroredBy: mirrorP; \n");
  83. ATObject mirror = evalAndReturn(
  84. "def mirror := reflect: subject;");
  85. // mirror should be a clone of the mirrorP prototype
  86. assertNotSame(mirrorP, mirror);
  87. assertTrue(mirror.meta_isCloneOf(mirrorP).asNativeBoolean().javaValue);
  88. // test for identical methods by printing the table of methods (both return a new table)
  89. assertEquals(
  90. mirrorP.meta_listMethods().toString(),
  91. mirror.meta_listMethods().toString());
  92. // the init method should have been called, which has set the iAm field
  93. evalAndCompareTo(
  94. "mirror.iAm",
  95. "\"a clone\"");
  96. }
  97. /**
  98. * This test verifies invariants with respect to the cloning of custom mirrors in AmbientTalk.
  99. * First of all, it tests the relationship between the original mirror and the clone to ensure
  100. * they are proper clones of each other. Secondly, when cloning custom mirrors their base field
  101. * is also cloned.
  102. *
  103. * @throws InterpreterException
  104. */ public void testCustomMirrorCloning() throws InterpreterException {
  105. ATObject subject = evalAndReturn(
  106. "def subject := object: { \n" +
  107. " def field := `field; \n" +
  108. " def canonical() { nil }; \n" +
  109. " def keyworded: arg1 message: arg2 { nil }; \n" +
  110. "} mirroredBy: (mirror: { nil }); \n");
  111. ATObject mirror = evalAndReturn(
  112. "def mirror := reflect: subject;");
  113. // clone the mirror
  114. ATObject mirrorC = evalAndReturn(
  115. "def mirrorC := clone: mirror;");
  116. // mirror should be a clone of the mirrorP prototype
  117. assertNotSame(mirror, mirrorC);
  118. assertTrue(mirrorC.meta_isCloneOf(mirror).asNativeBoolean().javaValue);
  119. // test for identical methods by printing the table of methods (both return a new table)
  120. assertEquals(
  121. mirror.meta_listMethods().toString(),
  122. mirrorC.meta_listMethods().toString());
  123. ATObject subjectC = evalAndReturn(
  124. "def subjectC := mirrorC.base;");
  125. // mirror should be a clone of the mirrorP prototype
  126. assertNotSame(subject, subjectC);
  127. assertTrue(subjectC.meta_isCloneOf(subject).asNativeBoolean().javaValue);
  128. // test for identical methods by printing the table of methods (both return a new table)
  129. assertEquals(
  130. subject.meta_listMethods().toString(),
  131. subjectC.meta_listMethods().toString());
  132. }
  133. /**
  134. * This test ensures that default extensions of mirrors share the base object of the parent.
  135. */
  136. public void testChildMirrorSharesBase() throws InterpreterException {
  137. ATObject result = evalAndReturn(
  138. "def meta := mirror: { nil }; \n" +
  139. "def base := object: { nil } mirroredBy: meta; \n" +
  140. " meta := reflect: base; \n" +
  141. "def extension := extend: meta with: { nil }; \n" +
  142. "(extension.base == meta.base);");
  143. assertTrue(result.asNativeBoolean().javaValue);
  144. }
  145. /**
  146. * This test ensures that the 'base' field of a mirror is not modified
  147. * when the mirror is used to create a new mirage.
  148. */
  149. public void testMirrorBaseNotModifiedOnClone() {
  150. ATObject result = evalAndReturn(
  151. "def origMirror := mirror: { nil };\n" +
  152. "def origBase := origMirror.base;\n" +
  153. "def newBase := object: { nil } mirroredBy: origMirror; \n" +
  154. "(origMirror.base) == origBase\n");
  155. assertEquals(NATBoolean._TRUE_, result);
  156. }
  157. public void testMirageInvocation() throws InterpreterException {
  158. class BufferedEcho extends NativeClosure {
  159. StringBuffer output;
  160. public BufferedEcho() {
  161. super(NATNil._INSTANCE_);
  162. initialize();
  163. }
  164. public ATObject base_apply(ATTable arguments) throws InterpreterException {
  165. output.append(arguments.base_at(NATNumber.ONE).asNativeText().javaValue + "\n");
  166. return scope_;
  167. }
  168. public void initialize() {
  169. output = new StringBuffer();
  170. }
  171. public String readOutput() {
  172. return output.toString();
  173. }
  174. }
  175. BufferedEcho buffer = new BufferedEcho();
  176. ctx_.base_getLexicalScope().meta_defineField(AGSymbol.jAlloc("echo:"), buffer);
  177. // define a simple logging mirror which uses a shared lexical scope to logs method invocations with proper indentation
  178. evalAndReturn(
  179. "def loggingMirror := let: { | defaultSpacing := 2 | \n" +
  180. " def indentLevel := 0;" +
  181. " def spaces() {" +
  182. " def result := \"\";" +
  183. " indentLevel.doTimes: { | i |" +
  184. " defaultSpacing.doTimes: { | j |" +
  185. " result := result + \" \";" +
  186. " }" +
  187. " };" +
  188. " result;" +
  189. " };" +
  190. "" +
  191. " mirror: {" +
  192. " def invoke(receiver, selector, args) {" +
  193. " echo: (spaces() + \"Invocation of method \" + selector + \" with arguments \" + args + \" on \" + receiver + \"(\" + super.base + \")\");" +
  194. " indentLevel := indentLevel + 1;" +
  195. " def result := super^invoke(receiver, selector, args);" +
  196. " indentLevel := indentLevel - 1;" +
  197. " echo: (spaces() + \"Invocation of method \" + selector + \" yielded \" + result );" +
  198. " result;" +
  199. " }" +
  200. " };" +
  201. "};");
  202. // Test setup 1: parent with logging mirror
  203. ATObject parent = evalAndReturn(
  204. "def mirroredParent := object: {" +
  205. " def m() { self.n() };" +
  206. " def n() { \"ok\" };" +
  207. "} mirroredBy: loggingMirror;");
  208. evalAndReturn("echo: mirroredParent.m();");
  209. assertEquals(
  210. "Invocation of method m with arguments [] on " + parent + "(" + parent + ")\n" +
  211. " Invocation of method n with arguments [] on " + parent + "(" + parent + ")\n" +
  212. " Invocation of method n yielded ok\n" +
  213. "Invocation of method m yielded ok\n" +
  214. "ok\n",
  215. buffer.readOutput());
  216. buffer.initialize();
  217. ATObject child = evalAndReturn(
  218. "def unmirroredChild := object: {" +
  219. " super := mirroredParent; " +
  220. " def m() { " +
  221. " echo: \"My parent will start logging now\";" +
  222. " super^m();" +
  223. " };" +
  224. "};");
  225. evalAndReturn("echo: unmirroredChild.m();");
  226. assertEquals(
  227. "My parent will start logging now\n" +
  228. "Invocation of method m with arguments [] on " + child + "(" + parent + ")\n" +
  229. " Invocation of method n with arguments [] on " + child + "(" + parent + ")\n" +
  230. " Invocation of method n yielded ok\n" +
  231. "Invocation of method m yielded ok\n" +
  232. "ok\n",
  233. buffer.readOutput());
  234. buffer.initialize();
  235. // child = evalAndReturn(
  236. // "def mirroredChild := object: {\n" +
  237. // " super := mirroredParent;\n" +
  238. // " def n() { \n" +
  239. // " echo: \" Indentation of this call should be correct as the lexical scope is shared by both mirrors\"; \n" +
  240. // " super^n(); \n" +
  241. // " }; \n" +
  242. // "} mirroredBy: loggingMirror;");
  243. //
  244. // evalAndReturn("echo: mirroredChild.m();");
  245. //
  246. // assertEquals("", buffer.readOutput());
  247. }
  248. }