/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

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