PageRenderTime 44ms CodeModel.GetById 32ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/interpreter/tags/at2dist220411/test/edu/vub/at/objects/natives/NATNamespaceTest.java

http://ambienttalk.googlecode.com/
Java | 161 lines | 95 code | 26 blank | 40 comment | 4 complexity | cbd1ce3d429c452e2a0a5c92921885dd MD5 | raw file
  1package edu.vub.at.objects.natives;
  2
  3import edu.vub.at.eval.Evaluator;
  4import edu.vub.at.exceptions.InterpreterException;
  5import edu.vub.at.exceptions.XSelectorNotFound;
  6import edu.vub.at.objects.ATObject;
  7import edu.vub.at.objects.natives.grammar.AGAssignmentSymbol;
  8import edu.vub.at.objects.natives.grammar.AGSymbol;
  9
 10import java.io.File;
 11import java.io.FileWriter;
 12import java.io.IOException;
 13
 14import junit.framework.TestCase;
 15
 16/**
 17 * A unit test for the NATNamespace class.
 18 * 
 19 * @author tvc
 20 */
 21public class NATNamespaceTest extends TestCase {
 22
 23	public static void main(String[] args) {
 24		junit.swingui.TestRunner.run(NATNamespaceTest.class);
 25	}
 26
 27	/* create the following directories and files for the test:
 28	 *  /tmp/at/
 29	 *  /tmp/at/test
 30	 *  /tmp/at/test/file1.at, containing the following code:
 31	 *     def x := 1;
 32	 *     def y := /.at;
 33	 *     def z := ~.file2.x;
 34	 *     x
 35	 *  /tmp/at/test/file2.at, containing the following code:
 36	 *     def x := 0;
 37	 *     def y := /.at.test.file1;
 38	 *     self
 39	 * When loading file1.at, then file2.at, file1.z is bound to 0 and file2.y is bound to nil
 40	 * When loading file2.at, then file1.at, an error will occur because ~.file2.x will result in evaluating nil.x
 41	 */
 42	private File at_;
 43	private File at_test_;
 44	private File at_test_file1_at;
 45	private File at_test_file2_at;
 46	
 47	public void setUp() {
 48		try {
 49			boolean success;
 50			at_ = new File("/tmp/at");
 51			success = at_.mkdir();
 52			at_test_ = new File("/tmp/at/test");
 53			success &= at_test_.mkdir();
 54			at_test_file1_at = new File("/tmp/at/test/file1.at");
 55			success &= at_test_file1_at.createNewFile();
 56			at_test_file2_at = new File("/tmp/at/test/file2.at");
 57			success &= at_test_file2_at.createNewFile();
 58			if (!success) {
 59				fail("could not create test directories and files");
 60			}
 61			
 62			FileWriter fw = new FileWriter(at_test_file1_at);
 63			fw.write("def x := 1; \n def y := /.at; \n def z := ~.file2.x; \n x");
 64			fw.close();
 65			
 66			fw = new FileWriter(at_test_file2_at);
 67			fw.write("def x := 0; \n def y := /.at.test.file1; \n self");
 68			fw.close();
 69			
 70		} catch (IOException e) {
 71			fail(e.getMessage());
 72		}
 73	}
 74	
 75	public void tearDown() {
 76		boolean success = at_test_file2_at.delete();
 77		success &= at_test_file1_at.delete();
 78		success &= at_test_.delete();
 79		success &= at_.delete();
 80		if (!success) {
 81			fail("could not delete test directories and files");
 82		}
 83	}
 84	
 85	
 86	public void testNamespaces() {
 87		try {
 88			NATObject lobby = Evaluator.getLobbyNamespace();
 89			
 90			// create the namespace 'at' bound to the path /tmp/at
 91			NATNamespace atNS = new NATNamespace("/at", at_);
 92			// bind the name 'at' to the atNS namespace in the lobby
 93			lobby.meta_defineField(AGSymbol.jAlloc("at"), atNS);
 94
 95			// now, try to select the 'at' slot from the lobby
 96			ATObject at = lobby.impl_invokeAccessor(lobby, AGSymbol.jAlloc("at"), NATTable.EMPTY);
 97			// the at slot should equal a namespace object
 98			assertTrue(at instanceof NATNamespace);
 99			assertEquals("<ns:/at>", at.meta_print().javaValue);
100			
101			ATObject test = at.impl_invokeAccessor(at, AGSymbol.jAlloc("test"), NATTable.EMPTY);
102			// the test slot should equal a namespace object
103			assertTrue(test instanceof NATNamespace);
104			assertEquals("<ns:/at/test>", test.meta_print().javaValue);	
105			
106			// select at.test.file1 which should load file1 and return 1
107			ATObject result = test.impl_invokeAccessor(test, AGSymbol.jAlloc("file1"), NATTable.EMPTY);
108			assertEquals(NATNumber.ONE, result);
109			
110			// ensure file1 is now really bound to 1 in the namespace 'test'
111			assertTrue(test.meta_respondsTo(AGSymbol.jAlloc("file1")).asNativeBoolean().javaValue);
112			
113			// normally, by loading file1, file2 should have been loaded as well:
114			assertTrue(test.meta_respondsTo(AGSymbol.jAlloc("file2")).asNativeBoolean().javaValue);
115			
116			// test.file2 should be a normaly object with a ~ slot bound to test
117			ATObject fileScope = test.impl_invokeAccessor(test, AGSymbol.jAlloc("file2"), NATTable.EMPTY);
118			assertEquals(test, fileScope.impl_invokeAccessor(fileScope, AGSymbol.jAlloc("~"), NATTable.EMPTY));
119			
120			// test.file2.y should equal nil
121			assertEquals(Evaluator.getNil(), fileScope.impl_invokeAccessor(fileScope, AGSymbol.jAlloc("y"), NATTable.EMPTY));
122		} catch (InterpreterException e) {
123			fail(e.getMessage());
124		}
125	}
126	
127	/**
128	 * This test loads file2 before file1
129	 */
130	public void testReverseNamespaces() {
131		try {
132			NATObject lobby = Evaluator.getLobbyNamespace();
133			
134			// create the namespace 'at' bound to the path /tmp/at
135			NATNamespace atNS = new NATNamespace("/at", at_);
136			// bind the name 'at' to the atNS namespace in the lobby
137			lobby.impl_invoke(lobby, AGAssignmentSymbol.jAlloc("at:="), NATTable.of(atNS));
138
139			// select '/.at.test'
140			ATObject test = atNS.impl_invokeAccessor(atNS, AGSymbol.jAlloc("test"), NATTable.EMPTY);
141			// the test slot should equal a namespace object
142			assertTrue(test instanceof NATNamespace);
143			assertEquals("<ns:/at/test>", test.meta_print().javaValue);	
144			
145			// select at.test.file2 which should load file2 and raise an error
146			// because ~.file2.x in file1.at will result in evaluating nil.x
147			try {
148				test.impl_invokeAccessor(test, AGSymbol.jAlloc("file2"), NATTable.EMPTY);
149			} catch(XSelectorNotFound e) {
150				if (e.getSelector().equals(AGSymbol.jAlloc("x")) && e.getInObject().equals(Evaluator.getNil())) {
151					// ok
152					System.out.println("[expected]: "+e.getMessage());
153				} else
154					throw e;
155			}
156		} catch (InterpreterException e) {
157			fail(e.getMessage());
158		}
159	}
160	
161}