summaryrefslogtreecommitdiffstats
path: root/clang-tools-extra/unittests/cpp11-migrate/ReformattingTest.cpp
blob: b4b9a3654783318ccba0df9ba1ac7faf88c6f45b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//===- cpp11-migrate/ReformattingTest.cpp - Reformatting unit tests -------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "Core/Reformatting.h"
#include "Core/FileOverrides.h"
#include "Core/Refactoring.h"
#include "gtest/gtest.h"
#include "VirtualFileHelper.h"

using namespace clang;
using namespace clang::tooling;

namespace {
// convenience function to create a ChangedRanges containing one Range
ChangedRanges makeChangedRanges(unsigned Offset, unsigned Length) {
  ChangedRanges Changes;
  ReplacementsVec Replaces;

  Replaces.push_back(Replacement("", Offset, 0, std::string(Length, '~')));
  Changes.adjustChangedRanges(Replaces);
  return Changes;
}
} // end anonymous namespace

TEST(Reformatter, SingleReformat) {
  VirtualFileHelper VFHelper;
  llvm::StringRef FileName = "<test>";
  VFHelper.mapFile(FileName, "int  a;\n"
                             "int  b;\n");

  Reformatter ChangesReformatter(format::getLLVMStyle());
  ChangedRanges Changes = makeChangedRanges(0, 6);
  tooling::ReplacementsVec Replaces;
  ChangesReformatter.reformatSingleFile(
      FileName, Changes, VFHelper.getNewSourceManager(), Replaces);

  // We expect the code above to reformatted like so:
  //
  // int a;
  // int  b;
  //
  // This test is slightly fragile since there's more than one Replacement that
  // results in the above change. However, testing the result of applying the
  // replacement is more trouble than it's worth in this context.
  ASSERT_EQ(1u, Replaces.size());
  EXPECT_EQ(3u, Replaces[0].getOffset());
  EXPECT_EQ(2u, Replaces[0].getLength());
  EXPECT_EQ(" ", Replaces[0].getReplacementText());
}
OpenPOWER on IntegriCloud