PageRenderTime 44ms CodeModel.GetById 14ms app.highlight 21ms RepoModel.GetById 2ms app.codeStats 0ms

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

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