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