blob: 5ea8e651a385872f437dabd41b08f0a351f47de5 [file] [log] [blame]
Eric Liu4cfb88a2016-04-25 15:09:22 +00001//===- unittest/Format/CleanupTest.cpp - Code cleanup unit tests ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "clang/Format/Format.h"
11
12#include "clang/Tooling/Core/Replacement.h"
13
14#include "gtest/gtest.h"
15
16namespace clang {
17namespace format {
18namespace {
19
20class CleanupTest : public ::testing::Test {
21protected:
22 std::string cleanup(llvm::StringRef Code,
23 const std::vector<tooling::Range> &Ranges,
24 const FormatStyle &Style = getLLVMStyle()) {
25 tooling::Replacements Replaces = format::cleanup(Style, Code, Ranges);
26
27 std::string Result = applyAllReplacements(Code, Replaces);
28 EXPECT_NE("", Result);
29 return Result;
30 }
31};
32
33TEST_F(CleanupTest, DeleteEmptyNamespaces) {
34 std::string Code = "namespace A {\n"
35 "namespace B {\n"
36 "} // namespace B\n"
37 "} // namespace A\n\n"
38 "namespace C {\n"
39 "namespace D { int i; }\n"
40 "inline namespace E { namespace { } }\n"
41 "}";
42 std::string Expected = "\n\n\n\n\nnamespace C {\n"
43 "namespace D { int i; }\n \n"
44 "}";
45 std::vector<tooling::Range> Ranges;
46 Ranges.push_back(tooling::Range(28, 0));
47 Ranges.push_back(tooling::Range(91, 6));
48 Ranges.push_back(tooling::Range(132, 0));
49 std::string Result = cleanup(Code, Ranges);
50 EXPECT_EQ(Expected, Result);
51}
52
53TEST_F(CleanupTest, NamespaceWithSyntaxError) {
54 std::string Code = "namespace A {\n"
55 "namespace B {\n" // missing r_brace
56 "} // namespace A\n\n"
57 "namespace C {\n"
58 "namespace D int i; }\n"
59 "inline namespace E { namespace { } }\n"
60 "}";
61 std::string Expected = "namespace A {\n"
62 "\n\n\nnamespace C {\n"
63 "namespace D int i; }\n \n"
64 "}";
65 std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size()));
66 std::string Result = cleanup(Code, Ranges);
67 EXPECT_EQ(Expected, Result);
68}
69
70TEST_F(CleanupTest, EmptyNamespaceNotAffected) {
71 std::string Code = "namespace A {\n\n"
72 "namespace {\n\n}}";
73 // Even though the namespaces are empty, but the inner most empty namespace
74 // block is not affected by the changed ranges.
75 std::string Expected = "namespace A {\n\n"
76 "namespace {\n\n}}";
77 // Set the changed range to be the second "\n".
78 std::vector<tooling::Range> Ranges(1, tooling::Range(14, 0));
79 std::string Result = cleanup(Code, Ranges);
80 EXPECT_EQ(Expected, Result);
81}
82
83TEST_F(CleanupTest, EmptyNamespaceWithCommentsNoBreakBeforeBrace) {
84 std::string Code = "namespace A {\n"
85 "namespace B {\n"
86 "// Yo\n"
87 "} // namespace B\n"
88 "} // namespace A\n"
89 "namespace C { // Yo\n"
90 "}";
91 std::string Expected = "\n\n\n\n\n\n";
92 std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size()));
93 std::string Result = cleanup(Code, Ranges);
94 EXPECT_EQ(Expected, Result);
95}
96
97TEST_F(CleanupTest, EmptyNamespaceWithCommentsBreakBeforeBrace) {
98 std::string Code = "namespace A\n"
99 "/* Yo */ {\n"
100 "namespace B\n"
101 "{\n"
102 "// Yo\n"
103 "} // namespace B\n"
104 "} // namespace A\n"
105 "namespace C\n"
106 "{ // Yo\n"
107 "}\n";
108 std::string Expected = "\n\n\n\n\n\n\n\n\n\n";
109 std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size()));
110 FormatStyle Style = getLLVMStyle();
111 Style.BraceWrapping.AfterNamespace = true;
112 std::string Result = cleanup(Code, Ranges, Style);
113 EXPECT_EQ(Expected, Result);
114}
115
116} // end namespace
117} // end namespace format
118} // end namespace clang