PageRenderTime 22ms CodeModel.GetById 7ms app.highlight 10ms RepoModel.GetById 2ms app.codeStats 0ms

/interpreter/tags/at2-build190607/test/edu/vub/at/OBJUnit.java

http://ambienttalk.googlecode.com/
Java | 156 lines | 79 code | 19 blank | 58 comment | 4 complexity | b041ed5a381964714c055c0bfbfe1cee MD5 | raw file
  1/**
  2 * AmbientTalk/2 Project
  3 * OBJUnit.java created on Aug 22, 2006 at 11:32:30 AM
  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 */
 28package edu.vub.at;
 29
 30import edu.vub.at.exceptions.InterpreterException;
 31import edu.vub.at.exceptions.XParseError;
 32import edu.vub.at.objects.ATAbstractGrammar;
 33import edu.vub.at.objects.ATClosure;
 34import edu.vub.at.objects.ATContext;
 35import edu.vub.at.objects.ATObject;
 36import edu.vub.at.objects.ATText;
 37import edu.vub.at.objects.natives.NATContext;
 38import edu.vub.at.objects.natives.NATNil;
 39import edu.vub.at.objects.natives.NATObject;
 40import edu.vub.at.objects.natives.NATText;
 41import edu.vub.at.parser.NATParser;
 42
 43import junit.framework.Assert;
 44
 45/**
 46 * OBJUnit is a preliminary version of a unit test framework to be used in AmbientTalk.
 47 * It contains a set of methods comparable to (and translated to) the methods offered
 48 * by the JUnit framework. 
 49 *
 50 * @author smostinc
 51 */
 52public class OBJUnit extends NATNil {
 53		
 54	/**
 55	 * Default instance : used in general to store in the at dictionary. New 
 56	 * instances can be made using the unittest: constructor.
 57	 */
 58	public static final OBJUnit _INSTANCE_ = new OBJUnit();
 59	
 60	private ATContext ctx_ = new NATContext(
 61			OBJUnit._INSTANCE_,
 62			OBJUnit._INSTANCE_);
 63	
 64	private OBJUnit() { }
 65	
 66	public NATNil base_echo_(ATObject message) throws InterpreterException {
 67		System.out.println(message.meta_print().javaValue);
 68		return NATNil._INSTANCE_;
 69	};
 70	
 71	
 72	public NATNil base_fail()  {
 73		Assert.fail();
 74		return NATNil._INSTANCE_;
 75	};
 76	
 77	public NATNil base_fail_(NATText description)  {
 78		Assert.fail(description.javaValue);
 79		return NATNil._INSTANCE_;
 80	};
 81		
 82	
 83	public NATNil base_success() {
 84		return NATNil._INSTANCE_;
 85	};
 86	
 87	public NATNil base_assert_equals_(ATObject expected, ATObject actual) {
 88		Assert.assertEquals(expected, actual);
 89		return NATNil._INSTANCE_;
 90	}
 91	
 92	public ATObject meta_evaluate(ATText source) {
 93        try {
 94			ATAbstractGrammar ast = NATParser._INSTANCE_.base_parse(source);
 95			return ast.meta_eval(ctx_);
 96		} catch (XParseError e) {
 97			Assert.fail("Parse error: "+e.getMessage());
 98		} catch (InterpreterException e) {
 99			Assert.fail("Eval error: "+e.getMessage());
100		}
101		return null;
102	}
103	
104	public NATNil base_assert_evaluatesTo(ATText source, ATObject expected) {
105		ATObject actual = meta_evaluate(source);
106		if(actual != null) {
107			this.base_assert_equals_(expected, actual);
108		} 
109		return NATNil._INSTANCE_;
110	}
111	
112	public NATNil base_assert_printsTo(ATText source, ATObject expected) {
113		ATObject actual = meta_evaluate(source);
114		try {
115			if(actual != null) {
116				this.base_assert_equals_(expected, actual.meta_print());
117			}
118		} catch (InterpreterException e) {
119			Assert.fail("Value cannot be represented in a textual format : " + e);
120		} 
121		return NATNil._INSTANCE_;
122	}
123	/**
124	 * The unittest: primitive, implemented as base-level code.
125	 * unit: expects to be passed a closure such that it can extract the correct
126	 * scope to be used as the object's lexical parent.
127	 * 
128	 * usage:
129	 *  unittest: { someCode }
130	 *  
131	 * pseudo-implementation:
132	 *  { def obj := objectP.new(at.unit, mirrorOf(someCode).context.lexicalScope);
133	 *    mirrorOf(someCode).method.body.eval(contextP.new(obj, obj, at.unit));
134	 *    obj }
135	 *  
136	 * @param code a closure containing both the code with which to initialize the object and the new object's lexical parent
137	 * @return a new object whose dynamic parent is NIL, whose lexical parent is the closure's lexical scope, initialized by the closure's code
138	 * @throws InterpreterException if raised inside the code closure.
139	 */
140	public ATObject base_unittest_(ATClosure code) throws InterpreterException {
141		OBJUnit clone = new OBJUnit();
142		NATObject extension = new NATObject(
143				/* dynamic parent */
144				clone,
145				/* lexical parent */
146				code.base_getContext().base_getLexicalScope(),
147				/* parent pointer type */
148				NATObject._SHARES_A_);
149		
150		clone.ctx_ = new NATContext(extension, extension);
151		extension.initializeWithCode(code);
152		return extension;
153	}
154}
155
156