use of de.fraunhofer.aisec.cpg.graph.statements.TryStatement in project cpg by Fraunhofer-AISEC.
the class CXXLanguageFrontendTest method testTryCatch.
@Test
void testTryCatch() throws Exception {
File file = new File("src/test/resources/components/trystmt.cpp");
TranslationUnitDeclaration tu = TestUtils.analyzeAndGetFirstTU(List.of(file), file.getParentFile().toPath(), true);
Set<FunctionDeclaration> main = tu.getDeclarationsByName("main", FunctionDeclaration.class);
assertFalse(main.isEmpty());
TryStatement tryStatement = main.iterator().next().getBodyStatementAs(0, TryStatement.class);
assertNotNull(tryStatement);
List<CatchClause> catchClauses = tryStatement.getCatchClauses();
// should have 3 catch clauses
assertEquals(3, catchClauses.size());
// declared exception variable
VariableDeclaration parameter = catchClauses.get(0).getParameter();
assertNotNull(parameter);
assertEquals("e", parameter.getName());
assertEquals(TypeParser.createFrom("const std::exception&", true), parameter.getType());
// anonymous variable (this is not 100% handled correctly but will do for now)
parameter = catchClauses.get(1).getParameter();
assertNotNull(parameter);
// this is currently our 'unnamed' parameter
assertEquals("", parameter.getName());
assertEquals(TypeParser.createFrom("const std::exception&", true), parameter.getType());
// catch all
parameter = catchClauses.get(2).getParameter();
assertNull(parameter);
}
Aggregations