PageRenderTime 25ms CodeModel.GetById 14ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/interpreter/tags/at_build150307/test/edu/vub/at/objects/mirrors/MirrorsOnNativesTest.java

http://ambienttalk.googlecode.com/
Java | 81 lines | 64 code | 11 blank | 6 comment | 0 complexity | b377f9abda5e80f5426bfdcb023d9922 MD5 | raw file
 1package edu.vub.at.objects.mirrors;
 2
 3import edu.vub.at.AmbientTalkTest;
 4import edu.vub.at.exceptions.InterpreterException;
 5import edu.vub.at.exceptions.XIllegalOperation;
 6import edu.vub.at.exceptions.XSelectorNotFound;
 7import edu.vub.at.objects.ATClosure;
 8import edu.vub.at.objects.ATMethod;
 9import edu.vub.at.objects.ATObject;
10import edu.vub.at.objects.natives.NATNumber;
11import edu.vub.at.objects.natives.NATTable;
12import edu.vub.at.objects.natives.grammar.AGSymbol;
13
14/**
15 * This class tests introspective mirror facilities on native objects.
16 * 
17 * @author tvcutsem
18 */
19public class MirrorsOnNativesTest extends AmbientTalkTest {
20
21	public static void main(String[] args) {
22		junit.swingui.TestRunner.run(MirrorsOnNativesTest.class);
23	}
24	
25	public void setUp() {
26		evalAndReturn("def mirrorOn1 := (reflect: 1)");
27	}
28	
29	public void testInvocation() {
30		evalAndCompareTo("mirrorOn1.invoke(mirrorOn1.base, `+, [2])", "3");
31		evalAndTestException("mirrorOn1.invoke(mirrorOn1.base, `foo, [2])", XSelectorNotFound.class);
32	}
33	
34	public void testRespondsTo() {
35		evalAndCompareTo("mirrorOn1.respondsTo(`+)", "true");
36		evalAndCompareTo("mirrorOn1.respondsTo(`foo)", "false");
37		evalAndCompareTo("(reflect: mirrorOn1).respondsTo(`invoke)", "true");
38	}
39	
40	public void testSelection() {
41		try {
42			ATObject clo = evalAndReturn("mirrorOn1.select(mirrorOn1.base, `+)");
43			assertTrue(clo.isClosure());
44			ATClosure c = clo.asClosure();
45			ATMethod m = c.base_getMethod();
46			assertEquals(NativeMethod.class, m.getClass());
47			assertEquals(AGSymbol.jAlloc("+"), m.base_getName());
48			assertEquals(NATNumber.atValue(2), c.base_apply(NATTable.atValue(new ATObject[] { NATNumber.ONE })));
49		} catch (InterpreterException e) {
50			fail(e.getMessage());
51		}
52	}
53	
54	public void testAddition() {
55		evalAndTestException("mirrorOn1.addField(object: { nil })", XIllegalOperation.class);
56		evalAndTestException("mirrorOn1.addMethod(mirrorOn1.grabMethod(`+))", XIllegalOperation.class);
57	}
58	
59	public void testAcquisition() {
60		try {
61			ATMethod nativeMethod = evalAndReturn("mirrorOn1.grabMethod(`+)").asMethod();
62			assertEquals(NativeMethod.class, nativeMethod.getClass());
63			assertEquals(AGSymbol.jAlloc("+"), nativeMethod.base_getName());
64		} catch (InterpreterException e) {
65			fail(e.getMessage());
66		}
67	}
68	
69	public void testListing() {
70		try {
71			evalAndReturn("mirrorOn1.listMethods()").asTable();
72			evalAndCompareTo("mirrorOn1.listFields()", "[<native field:super of 1>]");
73			// when mirroring a mirror and querying its fields, the field 'base' should always be present
74			evalAndCompareTo("{ |exit| (reflect: mirrorOn1).listFields().each: { |field|" +
75					         "  if: (field.name == `base) then: { exit(`foundit) } } }.escape()", "foundit");
76		} catch (InterpreterException e) {
77			fail(e.getMessage());
78		}
79	}
80
81}