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