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.

IncludeRelationTest.java 4.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package depends.extractor.cpp;
  2. import static org.junit.Assert.assertEquals;
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7. import org.junit.Before;
  8. import org.junit.Test;
  9. import depends.deptypes.DependencyType;
  10. import depends.entity.AliasEntity;
  11. import multilang.depends.util.file.FileUtil;
  12. public class IncludeRelationTest extends CppParserTest{
  13. @Before
  14. public void setUp() {
  15. super.init();
  16. }
  17. @Test
  18. public void test_includefiles_should_be_imported_relations() throws IOException {
  19. String[] srcs = new String[] {
  20. "./src/test/resources/cpp-code-examples/includesTest/EntryFile.cpp",
  21. "./src/test/resources/cpp-code-examples/includesTest/LocalHeader.h",
  22. "./src/test/resources/cpp-code-examples/includesTest/IndirectIncluded.h",
  23. "./src/test/resources/cpp-code-examples/includesTest/RelativeInclude.h",
  24. "./src/test/resources/cpp-code-examples/includesTest/path/Header.h",
  25. };
  26. for (String src:srcs) {
  27. CppFileParser parser = createParser();
  28. parser.parse(src);
  29. }
  30. resolveAllBindings();
  31. File f = new File(srcs[0]);
  32. assertEquals(3, entityRepo.getEntity(f.getCanonicalPath()).getRelations().size());
  33. }
  34. @Test
  35. public void test_could_found_files_in_include_path() throws IOException {
  36. String[] srcs = new String[] {
  37. "./src/test/resources/cpp-code-examples/includesTest/EntryFileIncludePathTest.cpp",
  38. "./src/test/resources/cpp-code-examples/includesTest/path/HeadersWithoutPath.h",
  39. };
  40. List<String> includePaths = new ArrayList<>();
  41. includePaths.add("./src/test/resources/cpp-code-examples/includesTest/path/");
  42. for (String src:srcs) {
  43. CppFileParser parser = createParser();
  44. parser.parse(src);
  45. }
  46. resolveAllBindings();
  47. File f = new File(srcs[0]);
  48. assertEquals(1, entityRepo.getEntity(f.getCanonicalPath()).getRelations().size());
  49. }
  50. @Test
  51. public void test_type_t_should_be_treat_as_structure() throws IOException {
  52. String[] srcs = new String[] {
  53. "./src/test/resources/cpp-code-examples/typedeftest2.cpp",
  54. };
  55. for (String src:srcs) {
  56. CppFileParser parser = createParser();
  57. parser.parse(src);
  58. }
  59. resolveAllBindings();
  60. assertEquals("abc",((AliasEntity) entityRepo.getEntity("abc_t")).getOriginType().getRawName().uniqName());
  61. }
  62. @Test
  63. public void test_call_should_only_in_relations_with_include() throws IOException {
  64. String[] srcs = new String[] {
  65. "./src/test/resources/cpp-code-examples/includeTest2/f0.cpp",
  66. "./src/test/resources/cpp-code-examples/includeTest2/f_with_include.cpp",
  67. "./src/test/resources/cpp-code-examples/includeTest2/f_without_include.cpp",
  68. };
  69. for (String src:srcs) {
  70. CppFileParser parser = createParser();
  71. parser.parse(src);
  72. }
  73. resolveAllBindings();
  74. this.assertContainsRelation(this.entityRepo.getEntity("foo"), DependencyType.CALL, "bar");
  75. this.assertNotContainsRelation(this.entityRepo.getEntity("foo2"), DependencyType.CALL, "bar");
  76. }
  77. @Test
  78. public void should_find_include_relation_in_conditional_macro_block() throws IOException {
  79. String[] srcs = new String[] {
  80. "./src/test/resources/cpp-code-examples/includeTest3/inc_macro_test.c",
  81. "./src/test/resources/cpp-code-examples/includeTest3/fx.h",
  82. "./src/test/resources/cpp-code-examples/includeTest3/fy.h",
  83. };
  84. for (String src:srcs) {
  85. CppFileParser parser = createParser();
  86. parser.parse(src);
  87. }
  88. resolveAllBindings();
  89. this.assertContainsRelation(this.entityRepo.getEntity(FileUtil.uniqFilePath(srcs[0])), DependencyType.IMPORT, FileUtil.uniqFilePath(srcs[1]));
  90. this.assertContainsRelation(this.entityRepo.getEntity(FileUtil.uniqFilePath(srcs[0])), DependencyType.IMPORT, FileUtil.uniqFilePath(srcs[2]));
  91. }
  92. }

人工智能研发终端

Contributors (2)