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