PageRenderTime 18ms CodeModel.GetById 6ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/interpreter/tags/at2dist220411/test/edu/vub/at/objects/mirrors/CoercionTest.java

http://ambienttalk.googlecode.com/
Java | 137 lines | 84 code | 13 blank | 40 comment | 0 complexity | 58c8edada95bee64cea37d74c5243d8f MD5 | raw file
  1/**
  2 * AmbientTalk/2 Project
  3 * CoercionTest.java created on Oct 04, 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.eval.Evaluator;
 32import edu.vub.at.exceptions.InterpreterException;
 33import edu.vub.at.objects.ATClosure;
 34import edu.vub.at.objects.ATContext;
 35import edu.vub.at.objects.ATMethod;
 36import edu.vub.at.objects.ATObject;
 37import edu.vub.at.objects.ATTypeTag;
 38import edu.vub.at.objects.ATTable;
 39import edu.vub.at.objects.coercion.NativeTypeTags;
 40import edu.vub.at.objects.natives.NATMethod;
 41import edu.vub.at.objects.natives.NATNumber;
 42import edu.vub.at.objects.natives.NATObject;
 43import edu.vub.at.objects.natives.NATTable;
 44import edu.vub.at.objects.natives.grammar.AGBegin;
 45import edu.vub.at.objects.natives.grammar.AGSymbol;
 46
 47import junit.framework.TestCase;
 48
 49public class CoercionTest extends TestCase {
 50
 51	public static void main(String[] args) {
 52		junit.swingui.TestRunner.run(CoercionTest.class);
 53	}
 54	
 55	private NATObject customClosure_;
 56	private NATObject customContext_;
 57	
 58	/*
 59	 * def customContext := object: {
 60	 *   def self := 24
 61	 * } taggedAs: [ /.at.types.Context ]
 62	 * 
 63	 * def customClosure := object: {
 64	 *   def apply := { |args|
 65	 *     nil
 66	 *   }
 67	 *   def method := <NATMETHOD foo() { nil }>;
 68	 *   def context := customContext;
 69	 * } taggedAs: [ /.at.types.Closure ]
 70	 */
 71	public void setUp() {
 72		try {
 73			customClosure_ = new NATObject(new ATTypeTag[] { NativeTypeTags._CLOSURE_ });
 74			customClosure_.meta_defineField(AGSymbol.jAlloc("apply"), new NativeClosure(customClosure_) {
 75				public ATObject base_apply(ATTable args) throws InterpreterException {
 76					ATTable apply_args = get(args, 1).asTable();
 77					assertEquals(42, getNbr(apply_args, 1));
 78					return Evaluator.getNil();
 79				}
 80			});
 81			customClosure_.meta_defineField(AGSymbol.jAlloc("method"), new NATMethod(AGSymbol.jAlloc("foo"), NATTable.EMPTY, new AGBegin(NATTable.EMPTY), NATTable.EMPTY));
 82			customContext_ = new NATObject(new ATTypeTag[] { NativeTypeTags._CONTEXT_ });
 83			customContext_.meta_defineField(AGSymbol.jAlloc("receiver"), NATNumber.atValue(24));
 84			customClosure_.meta_defineField(AGSymbol.jAlloc("context"), customContext_);
 85		} catch (InterpreterException e) {
 86			fail(e.getMessage());
 87		}
 88	}
 89	
 90	public void testCoercedBaselevelInvocation() {
 91		try {
 92			ATClosure coercedObject = customClosure_.asClosure();
 93			coercedObject.base_apply(NATTable.atValue(new ATObject[] { NATNumber.atValue(42) }));
 94		} catch (InterpreterException e) {
 95			fail(e.getMessage());
 96		}
 97	}
 98	
 99	public void testCoercedMetalevelInvocation() {
100		try {
101			ATClosure coercedObject = customClosure_.asClosure();
102			assertTrue(coercedObject.meta_respondsTo(AGSymbol.jAlloc("apply")).asNativeBoolean().javaValue);
103		} catch (InterpreterException e) {
104			fail(e.getMessage());
105		}
106	}
107	
108	public void testCoercedPrimitiveInvocation() {
109		try {
110			ATClosure coercedObject = customClosure_.asClosure();
111			assertEquals(customClosure_.hashCode(), coercedObject.hashCode());
112		} catch (InterpreterException e) {
113			fail(e.getMessage());
114		}
115	}
116	
117	public void testCoercedBaselevelFieldAccess() {
118		try {
119			ATClosure coercedObject = customClosure_.asClosure();
120			ATMethod m = coercedObject.base_method();
121			assertEquals("foo", m.base_name().base_text().asNativeText().javaValue);
122		} catch (InterpreterException e) {
123			fail(e.getMessage());
124		}
125	}
126
127	public void testCoercedReturnValue() {
128		try {
129			ATClosure coercedObject = customClosure_.asClosure();
130			ATContext coercedContext = coercedObject.base_context();
131			assertEquals(24, coercedContext.base_receiver().asNativeNumber().javaValue);
132		} catch (InterpreterException e) {
133			fail(e.getMessage());
134		}
135	}
136	
137}