PageRenderTime 32ms CodeModel.GetById 20ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/interpreter/tags/at2dist091109/test/edu/vub/at/OBJUnit.java

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