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.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@182373 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/unittests/Tooling/RefactoringTest.cpp b/unittests/Tooling/RefactoringTest.cpp
index 3e0d728..cea6c41 100644
--- a/unittests/Tooling/RefactoringTest.cpp
+++ b/unittests/Tooling/RefactoringTest.cpp
@@ -151,6 +151,30 @@
   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() {