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