r196266 - Create a separate file for JS-specific unit tests.

Alexander Kornienko alexfh at google.com
Tue Dec 3 02:50:16 PST 2013


Author: alexfh
Date: Tue Dec  3 04:50:16 2013
New Revision: 196266

URL: http://llvm.org/viewvc/llvm-project?rev=196266&view=rev
Log:
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

Added:
    cfe/trunk/unittests/Format/FormatTestJS.cpp
    cfe/trunk/unittests/Format/FormatTestUtils.h
Modified:
    cfe/trunk/unittests/Format/CMakeLists.txt
    cfe/trunk/unittests/Format/FormatTest.cpp

Modified: cfe/trunk/unittests/Format/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/CMakeLists.txt?rev=196266&r1=196265&r2=196266&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/CMakeLists.txt (original)
+++ cfe/trunk/unittests/Format/CMakeLists.txt Tue Dec  3 04:50:16 2013
@@ -8,6 +8,7 @@ set(LLVM_LINK_COMPONENTS
 
 add_clang_unittest(FormatTests
   FormatTest.cpp
+  FormatTestJS.cpp
   )
 
 target_link_libraries(FormatTests

Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=196266&r1=196265&r2=196266&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Tue Dec  3 04:50:16 2013
@@ -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, BreaksConditionalExpr
                "    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

Added: cfe/trunk/unittests/Format/FormatTestJS.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTestJS.cpp?rev=196266&view=auto
==============================================================================
--- cfe/trunk/unittests/Format/FormatTestJS.cpp (added)
+++ cfe/trunk/unittests/Format/FormatTestJS.cpp Tue Dec  3 04:50:16 2013
@@ -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

Added: cfe/trunk/unittests/Format/FormatTestUtils.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTestUtils.h?rev=196266&view=auto
==============================================================================
--- cfe/trunk/unittests/Format/FormatTestUtils.h (added)
+++ cfe/trunk/unittests/Format/FormatTestUtils.h Tue Dec  3 04:50:16 2013
@@ -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





More information about the cfe-commits mailing list