You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

RubyVarTest.java 5.3 kB

2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. package depends.extractor.ruby;
  2. import static org.junit.Assert.assertEquals;
  3. import java.io.IOException;
  4. import org.junit.Before;
  5. import org.junit.Test;
  6. import depends.entity.ContainerEntity;
  7. import depends.entity.FunctionEntity;
  8. import depends.entity.TypeEntity;
  9. import depends.entity.repo.EntityRepo;
  10. import depends.extractor.FileParser;
  11. public class RubyVarTest extends RubyParserTest {
  12. @Before
  13. public void setUp() {
  14. super.init();
  15. }
  16. @Test
  17. public void test_parameter_should_be_created() throws IOException {
  18. String[] srcs = new String[] {
  19. "./src/test/resources/ruby-code-examples/auto_var.rb",
  20. };
  21. for (String src:srcs) {
  22. FileParser parser = createFileParser();
  23. parser.parse(src);
  24. }
  25. FunctionEntity function = (FunctionEntity)(entityRepo.getEntity("method1"));
  26. assertEquals(1,function.getParameters().size());
  27. assertContainsParametersWithRawName(function, "param1");
  28. }
  29. @Test
  30. public void test_var_should_be_created_if_not_declared() throws IOException {
  31. String[] srcs = new String[] {
  32. "./src/test/resources/ruby-code-examples/auto_var.rb",
  33. };
  34. for (String src:srcs) {
  35. FileParser parser = createFileParser();
  36. parser.parse(src);
  37. }
  38. FunctionEntity function = (FunctionEntity)(entityRepo.getEntity("method_with_local_var"));
  39. assertEquals(1,function.getVars().size());
  40. assertContainsVarWithRawName(function, "var_1");
  41. }
  42. @Test
  43. public void test_var_should_only_create_once() throws IOException {
  44. String[] srcs = new String[] {
  45. "./src/test/resources/ruby-code-examples/auto_var.rb",
  46. };
  47. for (String src:srcs) {
  48. FileParser parser = createFileParser();
  49. parser.parse(src);
  50. }
  51. FunctionEntity function = (FunctionEntity)(entityRepo.getEntity("method_with_local_var_2times"));
  52. assertEquals(1,function.getVars().size());
  53. }
  54. @Test
  55. public void test_var_should_not_be_created_if_it_actually_parameter() throws IOException {
  56. String[] srcs = new String[] {
  57. "./src/test/resources/ruby-code-examples/auto_var.rb",
  58. };
  59. for (String src:srcs) {
  60. FileParser parser = createFileParser();
  61. parser.parse(src);
  62. }
  63. FunctionEntity function = (FunctionEntity)(entityRepo.getEntity("method_without_local_var_and_param"));
  64. assertEquals(0,function.getVars().size());
  65. }
  66. @Test
  67. public void test_var_should_not_be_created_if_it_actually_a_file_level_var() throws IOException {
  68. String[] srcs = new String[] {
  69. "./src/test/resources/ruby-code-examples/auto_var.rb",
  70. };
  71. for (String src:srcs) {
  72. FileParser parser = createFileParser();
  73. parser.parse(src);
  74. }
  75. FunctionEntity function = (FunctionEntity)(entityRepo.getEntity("method_access_file_level_var"));
  76. assertEquals(0,function.getVars().size());
  77. }
  78. @Test
  79. public void test_var_should_not_be_created_with_a_lot_of_levels() throws IOException {
  80. String[] srcs = new String[] {
  81. "./src/test/resources/ruby-code-examples/auto_var.rb",
  82. };
  83. for (String src:srcs) {
  84. FileParser parser = createFileParser();
  85. parser.parse(src);
  86. }
  87. FunctionEntity function = (FunctionEntity)(entityRepo.getEntity("M.C.method"));
  88. assertEquals(1,function.getVars().size());
  89. }
  90. @Test
  91. public void test_var_should_not_be_created_not_in_scope() throws IOException {
  92. String[] srcs = new String[] {
  93. "./src/test/resources/ruby-code-examples/auto_var.rb",
  94. };
  95. for (String src:srcs) {
  96. FileParser parser = createFileParser();
  97. parser.parse(src);
  98. }
  99. FunctionEntity function = (FunctionEntity)(entityRepo.getEntity("M.C.method2"));
  100. assertEquals(1,function.getVars().size());
  101. }
  102. @Test
  103. public void test_var_should_created_at_class_level() throws IOException {
  104. String[] srcs = new String[] {
  105. "./src/test/resources/ruby-code-examples/auto_var.rb",
  106. };
  107. for (String src:srcs) {
  108. FileParser parser = createFileParser();
  109. parser.parse(src);
  110. }
  111. TypeEntity c = (TypeEntity)(entityRepo.getEntity("M.C"));
  112. assertEquals(3,c.getVars().size());
  113. assertContainsVarWithRawName(c,"class_level_var");
  114. assertContainsVarWithRawName(c,"class_var");
  115. assertContainsVarWithRawName(c,"instance_var");
  116. }
  117. @Test
  118. public void test_var_in_block() throws IOException {
  119. String[] srcs = new String[] {
  120. "./src/test/resources/ruby-code-examples/auto_var.rb",
  121. };
  122. for (String src:srcs) {
  123. FileParser parser = createFileParser();
  124. parser.parse(src);
  125. }
  126. ContainerEntity c = (ContainerEntity)(entityRepo.getEntity("Block"));
  127. assertEquals(1,c.getVars().size());
  128. assertContainsVarWithRawName(c,"a");
  129. }
  130. @Test
  131. public void test_global_var()throws IOException {
  132. String[] srcs = new String[] {
  133. "./src/test/resources/ruby-code-examples/auto_var.rb",
  134. };
  135. for (String src:srcs) {
  136. FileParser parser = createFileParser();
  137. parser.parse(src);
  138. }
  139. ContainerEntity c = (ContainerEntity)(entityRepo.getEntity(EntityRepo.GLOBAL_SCOPE_NAME));
  140. assertEquals(1,c.getVars().size());
  141. assertContainsVarWithRawName(c,"global_var");
  142. }
  143. }