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

http://ambienttalk.googlecode.com/ · Java · 148 lines · 71 code · 22 blank · 55 comment · 0 complexity · fb2cf88fb51d0e83809db5d40720beb6 MD5 · raw file

  1. /**
  2. * AmbientTalk/2 Project
  3. * InvocationTest.java created on Aug 11, 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.AmbientTalkTestCase;
  30. import edu.vub.at.OBJUnit;
  31. import edu.vub.at.exceptions.InterpreterException;
  32. import edu.vub.at.exceptions.XParseError;
  33. import edu.vub.at.objects.ATAbstractGrammar;
  34. import edu.vub.at.objects.ATBoolean;
  35. import edu.vub.at.objects.ATContext;
  36. import edu.vub.at.objects.ATObject;
  37. import edu.vub.at.objects.ATTable;
  38. import edu.vub.at.objects.natives.NATBoolean;
  39. import edu.vub.at.objects.natives.NATClosure;
  40. import edu.vub.at.objects.natives.NATNil;
  41. import edu.vub.at.objects.natives.NATNumber;
  42. import edu.vub.at.objects.natives.NATObject;
  43. import edu.vub.at.objects.natives.NATTable;
  44. import edu.vub.at.objects.natives.NATText;
  45. import edu.vub.at.objects.natives.OBJLexicalRoot;
  46. import edu.vub.at.objects.natives.grammar.AGSymbol;
  47. import edu.vub.at.parser.NATParser;
  48. /**
  49. * ReflectiveAccessTest is a superclass to a framework of test cases geared towards
  50. * exploiting the reflective machinery provided in the mirrors package to explore
  51. * whether the mechanism works correctly and therefore allows one to access and
  52. * invoke java fields and methods from within AmbientTalk (both at the base and
  53. * meta-level) and to access and invoke Ambienttalk fields and methods through
  54. * the mediation of Mirages.
  55. *
  56. * This file establishes a common vocabulary for these idiosyncratic tests and allows
  57. * code reuse for commonly used features.
  58. *
  59. * @author smostinc
  60. */
  61. public class ReflectiveAccessTest extends AmbientTalkTestCase {
  62. /* ---------------------------
  63. * -- Auxiliary definitions --
  64. * --------------------------- */
  65. protected final NATClosure fail = new NativeClosure(NATNil._INSTANCE_) {
  66. public ATObject base_apply(ATTable arguments) throws InterpreterException {
  67. fail();
  68. return NATNil._INSTANCE_;
  69. }
  70. };
  71. protected final NATClosure success = new NativeClosure(NATNil._INSTANCE_) {
  72. public ATObject base_apply(ATTable arguments) throws InterpreterException {
  73. return NATNil._INSTANCE_;
  74. }
  75. };
  76. protected final NATClosure symbol = new NativeClosure(NATNil._INSTANCE_) {
  77. public ATObject base_apply(ATTable arguments) throws InterpreterException {
  78. return AGSymbol.alloc(arguments.base_at(NATNumber.ONE).asNativeText());
  79. }
  80. };
  81. protected final NATClosure echo_ = new NativeClosure(NATNil._INSTANCE_) {
  82. public ATObject base_apply(ATTable arguments) throws InterpreterException {
  83. System.out.println(arguments.base_at(NATNumber.ONE).meta_print().javaValue);
  84. return NATNil._INSTANCE_;
  85. }
  86. };
  87. protected final ATBoolean True = NATBoolean._TRUE_;
  88. protected final ATBoolean False = NATBoolean._FALSE_;
  89. protected ATObject lexicalRoot = null;
  90. protected ATTable closures = NATTable.atValue(new ATObject[] { fail, fail, success });
  91. protected void evaluateInput(String input, ATContext ctx) throws InterpreterException {
  92. try {
  93. ATAbstractGrammar ag = NATParser._INSTANCE_.base_parse(NATText.atValue(input));
  94. // Evaluate the corresponding tree of ATAbstractGrammar objects
  95. ag.meta_eval(ctx);
  96. } catch(XParseError e) {
  97. e.printStackTrace();
  98. fail("exception: "+e);
  99. }
  100. }
  101. /**
  102. * Initializes the lexical root for the purpose of this test.
  103. */
  104. protected void setUp() throws Exception {
  105. lexicalRoot = new NATObject(OBJLexicalRoot._INSTANCE_);
  106. lexicalRoot.meta_defineField(AGSymbol.jAlloc("success"), success);
  107. lexicalRoot.meta_defineField(AGSymbol.jAlloc("fail"), fail);
  108. lexicalRoot.meta_defineField(AGSymbol.jAlloc("echo:"), echo_);
  109. lexicalRoot.meta_defineField(AGSymbol.jAlloc("symbol"), symbol);
  110. lexicalRoot.meta_defineField(AGSymbol.jAlloc("true"), True);
  111. lexicalRoot.meta_defineField(AGSymbol.jAlloc("false"), False);
  112. lexicalRoot.meta_defineField(AGSymbol.jAlloc("closures"), closures);
  113. lexicalRoot.meta_defineField(AGSymbol.jAlloc("symIfTrue"), AGSymbol.jAlloc("ifTrue:"));
  114. //ATObject mirrors = new NATObject(lexicalRoot);
  115. //mirrors.meta_defineField(AGSymbol.jAlloc("Factory"), NATMirrorFactory._INSTANCE_);
  116. //ATObject natives = new NATObject(lexicalRoot);
  117. //natives.meta_defineField(AGSymbol.jAlloc("Context"), NATMirrorFactory._INSTANCE_);
  118. //ATObject at = new NATObject(lexicalRoot);
  119. //at.meta_defineField(AGSymbol.jAlloc("mirrors"), mirrors);
  120. //at.meta_defineField(AGSymbol.jAlloc("natives"), natives);
  121. //lexicalRoot.meta_defineField(AGSymbol.jAlloc("at"), at);
  122. lexicalRoot.meta_defineField(AGSymbol.jAlloc("root"), lexicalRoot);
  123. lexicalRoot.meta_defineField(AGSymbol.jAlloc("unittest:"), unittest_);
  124. lexicalRoot.meta_defineField(AGSymbol.jAlloc("unit"), OBJUnit._INSTANCE_);
  125. }
  126. }