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

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