PageRenderTime 35ms CodeModel.GetById 12ms app.highlight 18ms RepoModel.GetById 2ms app.codeStats 0ms

/interpreter/tags/reactive-pattern-matching/test/edu/vub/at/objects/natives/TypeTagTest.java

http://ambienttalk.googlecode.com/
Java | 103 lines | 51 code | 18 blank | 34 comment | 0 complexity | 9b4d6b4479f73bff4ef93ff4e422e3fc MD5 | raw file
  1package edu.vub.at.objects.natives;
  2
  3import edu.vub.at.AmbientTalkTest;
  4import edu.vub.at.eval.Evaluator;
  5import edu.vub.at.exceptions.InterpreterException;
  6import edu.vub.at.objects.ATObject;
  7import edu.vub.at.objects.ATTypeTag;
  8import edu.vub.at.objects.natives.grammar.AGSymbol;
  9
 10/**
 11 * Tests type definition, tagging, checking.
 12 * 
 13 * @author tvcutsem
 14 */
 15public class TypeTagTest extends AmbientTalkTest {
 16
 17	// test fixture
 18	
 19	// deftype A <: Type;
 20	private NATTypeTag typeA_ = NATTypeTag.atValue(AGSymbol.jAlloc("A"), NATTable.EMPTY);
 21
 22	// deftype B <: A;
 23	private NATTypeTag typeB_ = NATTypeTag.atValue(AGSymbol.jAlloc("B"),
 24			NATTable.atValue(new ATObject[] { typeA_ }));
 25	
 26	// deftype C <: A;
 27	private NATTypeTag typeC_ = NATTypeTag.atValue(AGSymbol.jAlloc("C"),
 28			NATTable.atValue(new ATObject[] { typeA_ }));
 29	
 30	// deftype D <: B, C;
 31	private NATTypeTag typeD_ = NATTypeTag.atValue(AGSymbol.jAlloc("D"),
 32			NATTable.atValue(new ATObject[] { typeB_, typeC_ }));
 33	
 34	// object: { nil } taggedAs: [ B ]
 35	private NATObject typed_ = new NATObject(Evaluator.getGlobalLexicalScope(), new ATTypeTag[] { typeB_ });
 36	
 37	// types defined without supertypes have the root type as single supertype
 38	
 39	public void testParentlessType() throws InterpreterException {
 40		assertTrue(typeA_.base_superTypes().base_length().equals(NATNumber.ONE));
 41		
 42		ATTypeTag root = typeA_.base_superTypes().base_at(NATNumber.ONE).asTypeTag();
 43		assertEquals(NATTypeTag.OBJRootType._INSTANCE_, root);
 44	}
 45	
 46	// type tag subtyping:
 47	
 48	public void testPositiveSubtyping() throws InterpreterException {
 49		ATTypeTag root = NATTypeTag.OBJRootType._INSTANCE_;
 50        // root <: root
 51		assertTrue(root.base_isSubtypeOf(root).asNativeBoolean().javaValue);
 52        // A <: A
 53		assertTrue(typeA_.base_isSubtypeOf(typeA_).asNativeBoolean().javaValue);
 54		// A <: root
 55		assertTrue(typeA_.base_isSubtypeOf(root).asNativeBoolean().javaValue);
 56		// B <: A
 57		assertTrue(typeB_.base_isSubtypeOf(typeA_).asNativeBoolean().javaValue);
 58		// D <: B
 59		assertTrue(typeD_.base_isSubtypeOf(typeB_).asNativeBoolean().javaValue);
 60		// D <: C
 61		assertTrue(typeD_.base_isSubtypeOf(typeC_).asNativeBoolean().javaValue);
 62		// D <: A
 63		assertTrue(typeD_.base_isSubtypeOf(typeA_).asNativeBoolean().javaValue);
 64		// D <: root
 65		assertTrue(typeD_.base_isSubtypeOf(root).asNativeBoolean().javaValue);
 66	}
 67	
 68	public void testNegativeSubtyping() throws InterpreterException {
 69		ATTypeTag root = NATTypeTag.OBJRootType._INSTANCE_;
 70        // root <: A
 71		assertFalse(root.base_isSubtypeOf(typeA_).asNativeBoolean().javaValue);
 72        // A <: D
 73		assertFalse(typeA_.base_isSubtypeOf(typeD_).asNativeBoolean().javaValue);
 74		// A <: B
 75		assertFalse(typeA_.base_isSubtypeOf(typeB_).asNativeBoolean().javaValue);
 76		// B <: C
 77		assertFalse(typeB_.base_isSubtypeOf(typeC_).asNativeBoolean().javaValue);
 78		// C <: B
 79		assertFalse(typeC_.base_isSubtypeOf(typeB_).asNativeBoolean().javaValue);
 80		// B <: D
 81		assertFalse(typeB_.base_isSubtypeOf(typeD_).asNativeBoolean().javaValue);
 82	}
 83	
 84	// object type testing:
 85	
 86	public void testObjectTypes() throws InterpreterException {
 87		// is: typed_ taggedAs: B => true
 88		assertTrue(typed_.meta_isTaggedAs(typeB_).asNativeBoolean().javaValue);
 89		// is: typed_ taggedAs: A => true
 90		assertTrue(typed_.meta_isTaggedAs(typeA_).asNativeBoolean().javaValue);
 91		// is: typed_ taggedAs: D => false
 92		assertFalse(typed_.meta_isTaggedAs(typeD_).asNativeBoolean().javaValue);
 93		// is: typed_ taggedAs: root => true
 94		assertTrue(typed_.meta_isTaggedAs(NATTypeTag.OBJRootType._INSTANCE_).asNativeBoolean().javaValue);
 95		// is: typed_ taggedAs: C => false
 96		assertFalse(typed_.meta_isTaggedAs(typeC_).asNativeBoolean().javaValue);
 97		
 98	    // negative test: object has no types
 99		NATObject notypes = new NATObject();
100		assertFalse(notypes.meta_isTaggedAs(typeB_).asNativeBoolean().javaValue);
101	}
102	
103}