From 2a250b8b49e70f53b185749a94956d54d941ef2d Mon Sep 17 00:00:00 2001 From: Daniel Jasper Date: Tue, 21 May 2013 12:21:39 +0000 Subject: Let clang-format move the cursor appropriately. With this patch, clang-format will try to keep the cursor at the original code position in editor integrations (implemented for emacs and vim). This means, after formatting, clang-format will try to keep the cursor on the same character of the same token. llvm-svn: 182373 --- clang/unittests/Tooling/RefactoringTest.cpp | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'clang/unittests/Tooling/RefactoringTest.cpp') diff --git a/clang/unittests/Tooling/RefactoringTest.cpp b/clang/unittests/Tooling/RefactoringTest.cpp index 3e0d7280b1d..cea6c417c63 100644 --- a/clang/unittests/Tooling/RefactoringTest.cpp +++ b/clang/unittests/Tooling/RefactoringTest.cpp @@ -151,6 +151,30 @@ TEST_F(ReplacementTest, ApplyAllFailsIfOneApplyFails) { EXPECT_EQ("z", Context.getRewrittenText(IDz)); } +TEST(ShiftedCodePositionTest, FindsNewCodePosition) { + Replacements Replaces; + Replaces.insert(Replacement("", 0, 1, "")); + Replaces.insert(Replacement("", 4, 3, " ")); + // Assume ' int i;' is turned into 'int i;' and cursor is located at '|'. + EXPECT_EQ(0u, shiftedCodePosition(Replaces, 0)); // |int i; + EXPECT_EQ(0u, shiftedCodePosition(Replaces, 1)); // |nt i; + EXPECT_EQ(1u, shiftedCodePosition(Replaces, 2)); // i|t i; + EXPECT_EQ(2u, shiftedCodePosition(Replaces, 3)); // in| i; + EXPECT_EQ(3u, shiftedCodePosition(Replaces, 4)); // int| i; + EXPECT_EQ(4u, shiftedCodePosition(Replaces, 5)); // int | i; + EXPECT_EQ(4u, shiftedCodePosition(Replaces, 6)); // int |i; + EXPECT_EQ(4u, shiftedCodePosition(Replaces, 7)); // int |; + EXPECT_EQ(5u, shiftedCodePosition(Replaces, 8)); // int i| +} + +TEST(ShiftedCodePositionTest, FindsNewCodePositionWithInserts) { + Replacements Replaces; + Replaces.insert(Replacement("", 4, 0, "\"\n\"")); + // Assume '"12345678"' is turned into '"1234"\n"5678"'. + EXPECT_EQ(4u, shiftedCodePosition(Replaces, 4)); // "123|5678" + EXPECT_EQ(8u, shiftedCodePosition(Replaces, 5)); // "1234|678" +} + class FlushRewrittenFilesTest : public ::testing::Test { public: FlushRewrittenFilesTest() { -- cgit v1.2.3