TEST_F(getFunctionStatementsTest, should_store_statments_source_code) { parse("void f(int) {\n f(1 + 3); int a =\n 9; }"); setRanges({ { stmtNo(0), {15, 24} }, { stmtNo(1), {25, 36} } }); auto stmts = getFunctionStatements(); EXPECT_EQ("f(1 + 3);", stmts[0]->getSourceCode()); ASSERT_EQ("int a =\n 9;", stmts[1]->getSourceCode()); }
TEST_F(getFunctionStatementsTest, should_store_source_code_between_statements) { parse("void f() {\n f();/* text */f(); /* whitespace */ f( );// not stored\n }"); setRanges({ { stmtNo(0), {12, 16} }, { stmtNo(1), {26, 30} }, { stmtNo(2), {50, 55} } }); auto stmts = getFunctionStatements(); EXPECT_EQ("/* text */", stmts[0]->getSourceCodeAfter()); EXPECT_EQ(" /* whitespace */ ", stmts[1]->getSourceCodeAfter()); EXPECT_EQ("", stmts[2]->getSourceCodeAfter()); }
TEST_F(getFunctionStatementsTest, should_return_the_ranges_of_each_statements) { parse("void f() {\n int x = 1; if (x == 1)\n; }"); ast::SourceOffsetRange range0{1, 2}, range1{3, 4}; setRanges({{ stmtNo(0), range0 }, { stmtNo(1), range1 }}); auto stmts = getFunctionStatements(); ASSERT_EQ(2u, stmts.size()); ASSERT_EQ(range0, stmts[0]->getRange()); ASSERT_EQ(range1, stmts[1]->getRange()); }
TEST_F(findStatementsInFunctionOverlappingSelectionTest, should_return_statements_overlapping_the_selection) { LocationRange selection{ rowCol(2, 0), rowCol(3, 4)}; parseFunctionWithStmts("\n int x;\n int y;\n int z;\n int w;\n"); const auto INT_X = 0, INT_Y = 1, INT_Z = 2, INT_W = 3; expectGetRangeForStmtNoAndReturn(INT_X, { rowCol(1, 2), rowCol(1, 8) }); expectGetRangeForStmtNoAndReturn(INT_Y, { rowCol(2, 2), rowCol(2, 8) }); expectGetRangeForStmtNoAndReturn(INT_Z, { rowCol(3, 2), rowCol(3, 8) }); expectGetRangeForStmtNoAndReturn(INT_W, { rowCol(4, 2), rowCol(4, 8) }); auto stmts = findStatementsInFunctionOverlappingSelection( *parsedFunctionDecl, selection, [&](clang::SourceManager& sm, clang::Stmt& s) { return getStmtRange(sm, s); }); expectRangeIs(stmts, { stmtNo(INT_Y), stmtNo(INT_Z) }); }
void expectGetRangeForStmtNoAndReturn(int index, LocationRange range) { auto& sourceManager = parsedFunctionDecl->getASTContext().getSourceManager(); EXPECT_CALL(*this, getStmtRange(Ref(sourceManager), Ref(*stmtNo(index)))) .WillRepeatedly(Return(range)); }