From 0ee6db8bb0723525ee37d081c4158fcd2e29f5d0 Mon Sep 17 00:00:00 2001 From: Alexander Kornienko Date: Tue, 3 Dec 2013 10:50:16 +0000 Subject: [PATCH] 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 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@196266 91177308-0d34-0410-b5e6-96231b3b80d8 --- unittests/Format/CMakeLists.txt | 1 + unittests/Format/FormatTest.cpp | 92 ++++-------------------------- unittests/Format/FormatTestJS.cpp | 86 ++++++++++++++++++++++++++++ unittests/Format/FormatTestUtils.h | 67 ++++++++++++++++++++++ 4 files changed, 164 insertions(+), 82 deletions(-) create mode 100644 unittests/Format/FormatTestJS.cpp create mode 100644 unittests/Format/FormatTestUtils.h diff --git a/unittests/Format/CMakeLists.txt b/unittests/Format/CMakeLists.txt index 16d5764faa..83aadb56b9 100644 --- a/unittests/Format/CMakeLists.txt +++ b/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/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index 17972c62f8..88b2a0b31a 100644 --- a/unittests/Format/FormatTest.cpp +++ b/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>();", 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/unittests/Format/FormatTestJS.cpp b/unittests/Format/FormatTestJS.cpp new file mode 100644 index 0000000000..1f8f139574 --- /dev/null +++ b/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 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/unittests/Format/FormatTestUtils.h b/unittests/Format/FormatTestUtils.h new file mode 100644 index 0000000000..649f5b3822 --- /dev/null +++ b/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 -- 2.40.0