PageRenderTime 19ms CodeModel.GetById 11ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

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