/interpreter/tags/at_build150307/test/edu/vub/at/objects/mirrors/MirageTest.java

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