PageRenderTime 22ms CodeModel.GetById 14ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/interpreter/tags/at2-build190607/test/edu/vub/at/objects/mirrors/MirrorsOnNativesTest.java

http://ambienttalk.googlecode.com/
Java | 80 lines | 63 code | 11 blank | 6 comment | 0 complexity | cf2c77a25f42114043e54abd3c7d2025 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			ATClosure c = clo.asClosure();
44			ATMethod m = c.base_getMethod();
45			assertEquals(NativeMethod.class, m.getClass());
46			assertEquals(AGSymbol.jAlloc("+"), m.base_getName());
47			assertEquals(NATNumber.atValue(2), c.base_apply(NATTable.atValue(new ATObject[] { NATNumber.ONE })));
48		} catch (InterpreterException e) {
49			fail(e.getMessage());
50		}
51	}
52	
53	public void testAddition() {
54		evalAndTestException("mirrorOn1.addField(object: { nil })", XIllegalOperation.class);
55		evalAndTestException("mirrorOn1.addMethod(mirrorOn1.grabMethod(`+))", XIllegalOperation.class);
56	}
57	
58	public void testAcquisition() {
59		try {
60			ATMethod nativeMethod = evalAndReturn("mirrorOn1.grabMethod(`+)").asMethod();
61			assertEquals(NativeMethod.class, nativeMethod.getClass());
62			assertEquals(AGSymbol.jAlloc("+"), nativeMethod.base_getName());
63		} catch (InterpreterException e) {
64			fail(e.getMessage());
65		}
66	}
67	
68	public void testListing() {
69		try {
70			evalAndReturn("mirrorOn1.listMethods()").asTable();
71			evalAndCompareTo("mirrorOn1.listFields()", "[<native field:super of 1>]");
72			// when mirroring a mirror and querying its fields, the field 'base' should always be present
73			evalAndCompareTo("{ |exit| (reflect: mirrorOn1).listFields().each: { |field|" +
74					         "  if: (field.name == `base) then: { exit(`foundit) } } }.escape()", "foundit");
75		} catch (InterpreterException e) {
76			fail(e.getMessage());
77		}
78	}
79
80}