1*c58c70e2SDaniel Jasper //===- unittest/Format/FormatTestJava.cpp - Formatting tests for Java -----===//
2*c58c70e2SDaniel Jasper //
3*c58c70e2SDaniel Jasper //                     The LLVM Compiler Infrastructure
4*c58c70e2SDaniel Jasper //
5*c58c70e2SDaniel Jasper // This file is distributed under the University of Illinois Open Source
6*c58c70e2SDaniel Jasper // License. See LICENSE.TXT for details.
7*c58c70e2SDaniel Jasper //
8*c58c70e2SDaniel Jasper //===----------------------------------------------------------------------===//
9*c58c70e2SDaniel Jasper 
10*c58c70e2SDaniel Jasper #include "FormatTestUtils.h"
11*c58c70e2SDaniel Jasper #include "clang/Format/Format.h"
12*c58c70e2SDaniel Jasper #include "llvm/Support/Debug.h"
13*c58c70e2SDaniel Jasper #include "gtest/gtest.h"
14*c58c70e2SDaniel Jasper 
15*c58c70e2SDaniel Jasper #define DEBUG_TYPE "format-test"
16*c58c70e2SDaniel Jasper 
17*c58c70e2SDaniel Jasper namespace clang {
18*c58c70e2SDaniel Jasper namespace format {
19*c58c70e2SDaniel Jasper 
20*c58c70e2SDaniel Jasper class FormatTestJava : public ::testing::Test {
21*c58c70e2SDaniel Jasper protected:
22*c58c70e2SDaniel Jasper   static std::string format(llvm::StringRef Code, unsigned Offset,
23*c58c70e2SDaniel Jasper                             unsigned Length, const FormatStyle &Style) {
24*c58c70e2SDaniel Jasper     DEBUG(llvm::errs() << "---\n");
25*c58c70e2SDaniel Jasper     DEBUG(llvm::errs() << Code << "\n\n");
26*c58c70e2SDaniel Jasper     std::vector<tooling::Range> Ranges(1, tooling::Range(Offset, Length));
27*c58c70e2SDaniel Jasper     tooling::Replacements Replaces = reformat(Style, Code, Ranges);
28*c58c70e2SDaniel Jasper     std::string Result = applyAllReplacements(Code, Replaces);
29*c58c70e2SDaniel Jasper     EXPECT_NE("", Result);
30*c58c70e2SDaniel Jasper     DEBUG(llvm::errs() << "\n" << Result << "\n\n");
31*c58c70e2SDaniel Jasper     return Result;
32*c58c70e2SDaniel Jasper   }
33*c58c70e2SDaniel Jasper 
34*c58c70e2SDaniel Jasper   static std::string format(
35*c58c70e2SDaniel Jasper       llvm::StringRef Code,
36*c58c70e2SDaniel Jasper       const FormatStyle &Style = getGoogleStyle(FormatStyle::LK_Java)) {
37*c58c70e2SDaniel Jasper     return format(Code, 0, Code.size(), Style);
38*c58c70e2SDaniel Jasper   }
39*c58c70e2SDaniel Jasper 
40*c58c70e2SDaniel Jasper   static FormatStyle getGoogleJSStyleWithColumns(unsigned ColumnLimit) {
41*c58c70e2SDaniel Jasper     FormatStyle Style = getGoogleStyle(FormatStyle::LK_Java);
42*c58c70e2SDaniel Jasper     Style.ColumnLimit = ColumnLimit;
43*c58c70e2SDaniel Jasper     return Style;
44*c58c70e2SDaniel Jasper   }
45*c58c70e2SDaniel Jasper 
46*c58c70e2SDaniel Jasper   static void verifyFormat(
47*c58c70e2SDaniel Jasper       llvm::StringRef Code,
48*c58c70e2SDaniel Jasper       const FormatStyle &Style = getGoogleStyle(FormatStyle::LK_Java)) {
49*c58c70e2SDaniel Jasper     EXPECT_EQ(Code.str(), format(test::messUp(Code), Style));
50*c58c70e2SDaniel Jasper   }
51*c58c70e2SDaniel Jasper };
52*c58c70e2SDaniel Jasper 
53*c58c70e2SDaniel Jasper TEST_F(FormatTestJava, ClassDeclarations) {
54*c58c70e2SDaniel Jasper   verifyFormat("public class SomeClass {\n"
55*c58c70e2SDaniel Jasper                "  private int a;\n"
56*c58c70e2SDaniel Jasper                "  private int b;\n"
57*c58c70e2SDaniel Jasper                "}");
58*c58c70e2SDaniel Jasper   verifyFormat("public class A {\n"
59*c58c70e2SDaniel Jasper                "  class B {\n"
60*c58c70e2SDaniel Jasper                "    int i;\n"
61*c58c70e2SDaniel Jasper                "  }\n"
62*c58c70e2SDaniel Jasper                "  class C {\n"
63*c58c70e2SDaniel Jasper                "    int j;\n"
64*c58c70e2SDaniel Jasper                "  }\n"
65*c58c70e2SDaniel Jasper                "}");
66*c58c70e2SDaniel Jasper }
67*c58c70e2SDaniel Jasper 
68*c58c70e2SDaniel Jasper } // end namespace tooling
69*c58c70e2SDaniel Jasper } // end namespace clang
70