add_clang_unittest(FormatTests
FormatTest.cpp
+ FormatTestJS.cpp
)
target_link_libraries(FormatTests
#define DEBUG_TYPE "format-test"
+#include "FormatTestUtils.h"
+
#include "clang/Format/Format.h"
-#include "clang/Lex/Lexer.h"
#include "llvm/Support/Debug.h"
#include "gtest/gtest.h"
return format(Code, 0, Code.size(), Style);
}
- std::string messUp(llvm::StringRef Code) {
- std::string MessedUp(Code.str());
- bool InComment = false;
- bool InPreprocessorDirective = false;
- bool JustReplacedNewline = false;
- for (unsigned i = 0, e = MessedUp.size() - 1; i != e; ++i) {
- if (MessedUp[i] == '/' && MessedUp[i + 1] == '/') {
- if (JustReplacedNewline)
- MessedUp[i - 1] = '\n';
- InComment = true;
- } else if (MessedUp[i] == '#' && (JustReplacedNewline || i == 0)) {
- if (i != 0)
- MessedUp[i - 1] = '\n';
- InPreprocessorDirective = true;
- } else if (MessedUp[i] == '\\' && MessedUp[i + 1] == '\n') {
- MessedUp[i] = ' ';
- MessedUp[i + 1] = ' ';
- } else if (MessedUp[i] == '\n') {
- if (InComment) {
- InComment = false;
- } else if (InPreprocessorDirective) {
- InPreprocessorDirective = false;
- } else {
- JustReplacedNewline = true;
- MessedUp[i] = ' ';
- }
- } else if (MessedUp[i] != ' ') {
- JustReplacedNewline = false;
- }
- }
- std::string WithoutWhitespace;
- if (MessedUp[0] != ' ')
- WithoutWhitespace.push_back(MessedUp[0]);
- for (unsigned i = 1, e = MessedUp.size(); i != e; ++i) {
- if (MessedUp[i] != ' ' || MessedUp[i - 1] != ' ')
- WithoutWhitespace.push_back(MessedUp[i]);
- }
- return WithoutWhitespace;
- }
-
FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) {
FormatStyle Style = getLLVMStyle();
Style.ColumnLimit = ColumnLimit;
void verifyFormat(llvm::StringRef Code,
const FormatStyle &Style = getLLVMStyle()) {
- EXPECT_EQ(Code.str(), format(messUp(Code), Style));
+ EXPECT_EQ(Code.str(), format(test::messUp(Code), Style));
}
void verifyGoogleFormat(llvm::StringRef Code) {
};
TEST_F(FormatTest, MessUp) {
- EXPECT_EQ("1 2 3", messUp("1 2 3"));
- EXPECT_EQ("1 2 3\n", messUp("1\n2\n3\n"));
- EXPECT_EQ("a\n//b\nc", messUp("a\n//b\nc"));
- EXPECT_EQ("a\n#b\nc", messUp("a\n#b\nc"));
- EXPECT_EQ("a\n#b c d\ne", messUp("a\n#b\\\nc\\\nd\ne"));
+ EXPECT_EQ("1 2 3", test::messUp("1 2 3"));
+ EXPECT_EQ("1 2 3\n", test::messUp("1\n2\n3\n"));
+ EXPECT_EQ("a\n//b\nc", test::messUp("a\n//b\nc"));
+ EXPECT_EQ("a\n#b\nc", test::messUp("a\n#b\nc"));
+ EXPECT_EQ("a\n#b c d\ne", test::messUp("a\n#b\\\nc\\\nd\ne"));
}
//===----------------------------------------------------------------------===//
" aaaaaaaaaaaaaaaaaaaaaaaaaaa;",
Style);
verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
- " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
+ " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
Style);
Spaces.Standard = FormatStyle::LS_Cpp11;
Spaces.SpacesInAngles = true;
verifyFormat("A< A< int > >();", Spaces);
-
+
Spaces.SpacesInAngles = false;
verifyFormat("A<A<int>>();", Spaces);
}
-TEST_F(FormatTest, UnderstandsJavaScript) {
- FormatStyle JS = getLLVMStyle();
- FormatStyle JS10Columns = getLLVMStyleWithColumns(10);
- FormatStyle JS20Columns = getLLVMStyleWithColumns(20);
- JS.Language = JS10Columns.Language = JS20Columns.Language =
- FormatStyle::LK_JavaScript;
-
- verifyFormat("a == = b;", JS);
- verifyFormat("a != = b;", JS);
-
- verifyFormat("a === b;", JS);
- verifyFormat("aaaaaaa ===\n b;", JS10Columns);
- verifyFormat("a !== b;", JS);
- verifyFormat("aaaaaaa !==\n b;", JS10Columns);
- verifyFormat("if (a + b + c +\n"
- " d !==\n"
- " e + f + g)\n"
- " q();",
- JS20Columns);
-
- verifyFormat("a >> >= b;", JS);
-
- verifyFormat("a >>> b;", JS);
- verifyFormat("aaaaaaa >>>\n b;", JS10Columns);
- verifyFormat("a >>>= b;", JS);
- verifyFormat("aaaaaaa >>>=\n b;", JS10Columns);
- verifyFormat("if (a + b + c +\n"
- " d >>>\n"
- " e + f + g)\n"
- " q();",
- JS20Columns);
-}
-
} // end namespace tooling
} // end namespace clang
--- /dev/null
+//===- unittest/Format/FormatTestJS.cpp - Formatting unit tests for JS ----===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#define DEBUG_TYPE "format-test"
+
+#include "FormatTestUtils.h"
+
+#include "clang/Format/Format.h"
+#include "llvm/Support/Debug.h"
+#include "gtest/gtest.h"
+
+namespace clang {
+namespace format {
+
+class FormatTestJS : public ::testing::Test {
+protected:
+ static std::string format(llvm::StringRef Code, unsigned Offset,
+ unsigned Length, const FormatStyle &Style) {
+ DEBUG(llvm::errs() << "---\n");
+ DEBUG(llvm::errs() << Code << "\n\n");
+ std::vector<tooling::Range> Ranges(1, tooling::Range(Offset, Length));
+ tooling::Replacements Replaces = reformat(Style, Code, Ranges);
+ std::string Result = applyAllReplacements(Code, Replaces);
+ EXPECT_NE("", Result);
+ DEBUG(llvm::errs() << "\n" << Result << "\n\n");
+ return Result;
+ }
+
+ static std::string format(llvm::StringRef Code,
+ const FormatStyle &Style = getJSStyle()) {
+ return format(Code, 0, Code.size(), Style);
+ }
+
+ static FormatStyle getJSStyle() {
+ FormatStyle Style = getLLVMStyle();
+ Style.Language = FormatStyle::LK_JavaScript;
+ return Style;
+ }
+
+ static FormatStyle getJSStyleWithColumns(unsigned ColumnLimit) {
+ FormatStyle Style = getJSStyle();
+ Style.ColumnLimit = ColumnLimit;
+ return Style;
+ }
+
+ static void verifyFormat(llvm::StringRef Code,
+ const FormatStyle &Style = getJSStyle()) {
+ EXPECT_EQ(Code.str(), format(test::messUp(Code), Style));
+ }
+};
+
+TEST_F(FormatTestJS, UnderstandsJavaScriptOperators) {
+ verifyFormat("a == = b;");
+ verifyFormat("a != = b;");
+
+ verifyFormat("a === b;");
+ verifyFormat("aaaaaaa ===\n b;", getJSStyleWithColumns(10));
+ verifyFormat("a !== b;");
+ verifyFormat("aaaaaaa !==\n b;", getJSStyleWithColumns(10));
+ verifyFormat("if (a + b + c +\n"
+ " d !==\n"
+ " e + f + g)\n"
+ " q();",
+ getJSStyleWithColumns(20));
+
+ verifyFormat("a >> >= b;");
+
+ verifyFormat("a >>> b;");
+ verifyFormat("aaaaaaa >>>\n b;", getJSStyleWithColumns(10));
+ verifyFormat("a >>>= b;");
+ verifyFormat("aaaaaaa >>>=\n b;", getJSStyleWithColumns(10));
+ verifyFormat("if (a + b + c +\n"
+ " d >>>\n"
+ " e + f + g)\n"
+ " q();",
+ getJSStyleWithColumns(20));
+}
+
+} // end namespace tooling
+} // end namespace clang
--- /dev/null
+//===- unittest/Format/FormatTestUtils.h - Formatting unit tests ----------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This file defines utility functions for Clang-Format related tests.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_CLANG_FORMAT_TEST_UTILS_H
+#define LLVM_CLANG_FORMAT_TEST_UTILS_H
+
+#include "llvm/ADT/StringRef.h"
+
+namespace clang {
+namespace format {
+namespace test {
+
+inline std::string messUp(llvm::StringRef Code) {
+ std::string MessedUp(Code.str());
+ bool InComment = false;
+ bool InPreprocessorDirective = false;
+ bool JustReplacedNewline = false;
+ for (unsigned i = 0, e = MessedUp.size() - 1; i != e; ++i) {
+ if (MessedUp[i] == '/' && MessedUp[i + 1] == '/') {
+ if (JustReplacedNewline)
+ MessedUp[i - 1] = '\n';
+ InComment = true;
+ } else if (MessedUp[i] == '#' && (JustReplacedNewline || i == 0)) {
+ if (i != 0)
+ MessedUp[i - 1] = '\n';
+ InPreprocessorDirective = true;
+ } else if (MessedUp[i] == '\\' && MessedUp[i + 1] == '\n') {
+ MessedUp[i] = ' ';
+ MessedUp[i + 1] = ' ';
+ } else if (MessedUp[i] == '\n') {
+ if (InComment) {
+ InComment = false;
+ } else if (InPreprocessorDirective) {
+ InPreprocessorDirective = false;
+ } else {
+ JustReplacedNewline = true;
+ MessedUp[i] = ' ';
+ }
+ } else if (MessedUp[i] != ' ') {
+ JustReplacedNewline = false;
+ }
+ }
+ std::string WithoutWhitespace;
+ if (MessedUp[0] != ' ')
+ WithoutWhitespace.push_back(MessedUp[0]);
+ for (unsigned i = 1, e = MessedUp.size(); i != e; ++i) {
+ if (MessedUp[i] != ' ' || MessedUp[i - 1] != ' ')
+ WithoutWhitespace.push_back(MessedUp[i]);
+ }
+ return WithoutWhitespace;
+}
+
+} // end namespace test
+} // end namespace format
+} // end namespace clang
+
+#endif // LLVM_CLANG_FORMAT_TEST_UTILS_H