diff options
author | Alexander Kornienko <alexfh@google.com> | 2013-12-03 10:50:16 +0000 |
---|---|---|
committer | Alexander Kornienko <alexfh@google.com> | 2013-12-03 10:50:16 +0000 |
commit | a48a12cf81cb0804506ddeb4994f17008cf097b3 (patch) | |
tree | 00f58af46ff99f2706774862affea8019e248005 /clang | |
parent | f54dcbc4e65a1d9d6a9c5731ed5ffd86a87c89ac (diff) | |
download | bcm5719-llvm-a48a12cf81cb0804506ddeb4994f17008cf097b3.tar.gz bcm5719-llvm-a48a12cf81cb0804506ddeb4994f17008cf097b3.zip |
Create a separate file for JS-specific unit tests.
Reviewers: djasper
Reviewed By: djasper
CC: cfe-commits, klimek
Differential Revision: http://llvm-reviews.chandlerc.com/D2307
llvm-svn: 196266
Diffstat (limited to 'clang')
-rw-r--r-- | clang/unittests/Format/CMakeLists.txt | 1 | ||||
-rw-r--r-- | clang/unittests/Format/FormatTest.cpp | 92 | ||||
-rw-r--r-- | clang/unittests/Format/FormatTestJS.cpp | 86 | ||||
-rw-r--r-- | clang/unittests/Format/FormatTestUtils.h | 67 |
4 files changed, 164 insertions, 82 deletions
diff --git a/clang/unittests/Format/CMakeLists.txt b/clang/unittests/Format/CMakeLists.txt index 16d5764faa9..83aadb56b9d 100644 --- a/clang/unittests/Format/CMakeLists.txt +++ b/clang/unittests/Format/CMakeLists.txt @@ -8,6 +8,7 @@ set(LLVM_LINK_COMPONENTS add_clang_unittest(FormatTests FormatTest.cpp + FormatTestJS.cpp ) target_link_libraries(FormatTests diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index 17972c62f8a..88b2a0b31a5 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -9,8 +9,9 @@ #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" @@ -37,46 +38,6 @@ protected: 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; @@ -91,7 +52,7 @@ protected: 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) { @@ -107,11 +68,11 @@ protected: }; 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")); } //===----------------------------------------------------------------------===// @@ -3598,7 +3559,7 @@ TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) { " aaaaaaaaaaaaaaaaaaaaaaaaaaa;", Style); verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" - " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" + " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;", Style); @@ -7561,43 +7522,10 @@ TEST_F(FormatTest, SpacesInAngles) { 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 diff --git a/clang/unittests/Format/FormatTestJS.cpp b/clang/unittests/Format/FormatTestJS.cpp new file mode 100644 index 00000000000..1f8f139574a --- /dev/null +++ b/clang/unittests/Format/FormatTestJS.cpp @@ -0,0 +1,86 @@ +//===- 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 diff --git a/clang/unittests/Format/FormatTestUtils.h b/clang/unittests/Format/FormatTestUtils.h new file mode 100644 index 00000000000..649f5b3822c --- /dev/null +++ b/clang/unittests/Format/FormatTestUtils.h @@ -0,0 +1,67 @@ +//===- 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 |