/interpreter/tags/at2-build190607/test/edu/vub/at/objects/natives/CustomFieldsTest.java

http://ambienttalk.googlecode.com/ · Java · 136 lines · 80 code · 15 blank · 41 comment · 0 complexity · 1ab52673bbebaeaa110d2268ff2fd4ce MD5 · raw file

  1. package edu.vub.at.objects.natives;
  2. import edu.vub.at.AmbientTalkTest;
  3. import edu.vub.at.exceptions.InterpreterException;
  4. import edu.vub.at.exceptions.XDuplicateSlot;
  5. import edu.vub.at.objects.ATObject;
  6. import edu.vub.at.objects.coercion.NativeTypeTags;
  7. import edu.vub.at.objects.natives.grammar.AGSymbol;
  8. /**
  9. * Tests custom fields that can be added to an object.
  10. *
  11. * @author tvcutsem
  12. */
  13. public class CustomFieldsTest extends AmbientTalkTest {
  14. public static void main(String[] args) {
  15. junit.swingui.TestRunner.run(CustomFieldsTest.class);
  16. }
  17. private NATObject testHost_;
  18. // a custom field that when set, increments the new value by 1
  19. private ATObject testField_;
  20. private AGSymbol foo_;
  21. // one-time setUp for all tests
  22. public void setUp() throws InterpreterException {
  23. // def testHost := object: { def x := 1 }
  24. testHost_ = new NATObject();
  25. testHost_.meta_defineField(AGSymbol.jAlloc("x"), NATNumber.ONE);
  26. foo_ = AGSymbol.jAlloc("foo");
  27. ctx_.base_getLexicalScope().meta_defineField(AGSymbol.jAlloc("Field"), NativeTypeTags._FIELD_);
  28. testField_ = evalAndReturn(
  29. "object: { def name := `foo;" +
  30. "def host := nil; def init(newhost) { host := newhost; };" +
  31. "def v := nil;" +
  32. "def readField() { v };" +
  33. "def writeField(n) { v := n+1 } } taggedAs: [ Field ]");
  34. }
  35. /**
  36. * Tests whether a custom field can be added to a native object
  37. */
  38. public void testCustomFieldAddition() throws Exception {
  39. assertNull(testHost_.customFields_);
  40. // (reflect: testHost).addField(testField)
  41. testHost_.meta_addField(testField_.asField());
  42. assertNotNull(testHost_.customFields_);
  43. assertEquals(1, testHost_.customFields_.size());
  44. assertTrue(testHost_.meta_respondsTo(foo_).asNativeBoolean().javaValue);
  45. ATObject foo = testHost_.meta_grabField(foo_);
  46. assertEquals(testHost_, foo.meta_select(foo, AGSymbol.jAlloc("host")));
  47. }
  48. /**
  49. * Tests whether a custom field can be read via readField
  50. */
  51. public void testCustomFieldRead() throws Exception {
  52. testHost_.meta_addField(testField_.asField());
  53. assertEquals(NATNil._INSTANCE_, testHost_.meta_select(testHost_, foo_));
  54. }
  55. /**
  56. * Tests whether a custom field can be written via writeField
  57. */
  58. public void testCustomFieldWrite() throws Exception {
  59. testHost_.meta_addField(testField_.asField());
  60. // testHost.foo := 1
  61. assertEquals(NATNil._INSTANCE_, testHost_.meta_assignField(testHost_, foo_, NATNumber.ONE));
  62. // testHost.foo == 2
  63. assertEquals(NATNumber.atValue(2), testHost_.meta_select(testHost_, foo_));
  64. }
  65. /**
  66. * Tests that duplicate slots are still trapped, even with custom fields
  67. */
  68. public void testCustomDuplicate() throws Exception {
  69. testHost_.meta_addField(testField_.asField());
  70. try {
  71. // try to add a native field for which a custom one is already defined
  72. testHost_.meta_defineField(foo_, NATNumber.ONE);
  73. fail("expected a duplicate slot exception");
  74. } catch (XDuplicateSlot e) {
  75. // expected exception: success
  76. }
  77. try {
  78. // try to add a custom field for which another custom one is already defined
  79. testHost_.meta_addField(testField_.meta_clone().asField());
  80. fail("expected a duplicate slot exception");
  81. } catch (XDuplicateSlot e) {
  82. // expected exception: success
  83. }
  84. }
  85. /**
  86. * Tests whether custom fields appear in the listFields table
  87. */
  88. public void testFieldListing() throws Exception {
  89. testHost_.meta_addField(testField_.meta_clone().asField());
  90. assertEquals(3, testHost_.meta_listFields().base_getLength().asNativeNumber().javaValue);
  91. }
  92. /**
  93. * Tests whether the fields of clones are properly re-initialized
  94. */
  95. public void testCloneFieldReinit() throws Exception {
  96. testHost_.meta_addField(testField_.meta_clone().asField());
  97. // set foo field of testHost to 1
  98. testHost_.meta_assignField(testHost_, foo_, NATNumber.ONE);
  99. ATObject clone = testHost_.meta_clone();
  100. // set foo field of clone to 55
  101. clone.meta_assignField(clone, foo_, NATNumber.atValue(55));
  102. // check whether original foo field of testHost is not modified (remember: writeField increments with + 1)
  103. assertEquals(2, testHost_.meta_select(testHost_, foo_).asNativeNumber().javaValue);
  104. }
  105. /**
  106. * Tests whether native fields added to another object are not added as custom fields,
  107. * but again as native fields
  108. */
  109. public void testNativeFieldAdd() throws Exception {
  110. testHost_.meta_addField(testField_.meta_clone().asField());
  111. NATObject empty = new NATObject();
  112. assertNull(empty.customFields_);
  113. empty.meta_addField(testHost_.meta_grabField(AGSymbol.jAlloc("x")));
  114. assertNull(empty.customFields_);
  115. assertEquals(testHost_.meta_select(testHost_, AGSymbol.jAlloc("x")),
  116. empty.meta_select(empty, AGSymbol.jAlloc("x")));
  117. // only when custom fields are added does the customFields_ list grow
  118. empty.meta_addField(testHost_.meta_grabField(foo_));
  119. assertNotNull(empty.customFields_);
  120. }
  121. }