Search in sources :

Example 1 with NamespaceDeclaration

use of de.fraunhofer.aisec.cpg.graph.declarations.NamespaceDeclaration in project cpg by Fraunhofer-AISEC.

the class CXXLanguageFrontendTest method testNamespaces.

@Test
void testNamespaces() throws Exception {
    File file = new File("src/test/resources/namespaces.cpp");
    TranslationUnitDeclaration tu = TestUtils.analyzeAndGetFirstTU(List.of(file), file.getParentFile().toPath(), true);
    assertNotNull(tu);
    NamespaceDeclaration firstNamespace = tu.getDeclarationsByName("FirstNamespace", NamespaceDeclaration.class).iterator().next();
    assertNotNull(firstNamespace);
    RecordDeclaration someClass = firstNamespace.getDeclarationsByName("FirstNamespace::SomeClass", RecordDeclaration.class).iterator().next();
    assertNotNull(someClass);
    RecordDeclaration anotherClass = tu.getDeclarationsByName("AnotherClass", RecordDeclaration.class).iterator().next();
    assertNotNull(anotherClass);
}
Also used : RecordDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.RecordDeclaration) File(java.io.File) TranslationUnitDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.TranslationUnitDeclaration) NamespaceDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.NamespaceDeclaration) BaseTest(de.fraunhofer.aisec.cpg.BaseTest) Test(org.junit.jupiter.api.Test)

Example 2 with NamespaceDeclaration

use of de.fraunhofer.aisec.cpg.graph.declarations.NamespaceDeclaration in project cpg by Fraunhofer-AISEC.

the class SubgraphWalkerTest method testASTChildrenGetter.

@Test
void testASTChildrenGetter() throws Exception {
    File file = new File("src/test/resources/compiling/RecordDeclaration.java");
    TranslationUnitDeclaration tu = TestUtils.analyzeAndGetFirstTU(List.of(file), file.getParentFile().toPath(), false);
    NamespaceDeclaration namespace = tu.getDeclarationAs(0, NamespaceDeclaration.class);
    RecordDeclaration recordDeclaration = namespace.getDeclarationAs(0, RecordDeclaration.class);
    var ast = SubgraphWalker.getAstChildren(recordDeclaration);
    assertFalse(ast.isEmpty());
    // should contain 4 AST nodes, 1 field (+1 this field), 1 method, 1 constructor
    assertEquals(4, ast.size());
}
Also used : RecordDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.RecordDeclaration) File(java.io.File) TranslationUnitDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.TranslationUnitDeclaration) NamespaceDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.NamespaceDeclaration) Test(org.junit.jupiter.api.Test) BaseTest(de.fraunhofer.aisec.cpg.BaseTest)

Aggregations

BaseTest (de.fraunhofer.aisec.cpg.BaseTest)2 NamespaceDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.NamespaceDeclaration)2 RecordDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.RecordDeclaration)2 TranslationUnitDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.TranslationUnitDeclaration)2 File (java.io.File)2 Test (org.junit.jupiter.api.Test)2