Hi djasper,

http://llvm-reviews.chandlerc.com/D2307

Files:
  unittests/Format/CMakeLists.txt
  unittests/Format/FormatTest.cpp
  unittests/Format/FormatTest.h
  unittests/Format/FormatTestJS.cpp
Index: unittests/Format/CMakeLists.txt
===================================================================
--- unittests/Format/CMakeLists.txt
+++ unittests/Format/CMakeLists.txt
@@ -8,6 +8,7 @@
 
 add_clang_unittest(FormatTests
   FormatTest.cpp
+  FormatTestJS.cpp
   )
 
 target_link_libraries(FormatTests
Index: unittests/Format/FormatTest.cpp
===================================================================
--- unittests/Format/FormatTest.cpp
+++ unittests/Format/FormatTest.cpp
@@ -7,105 +7,11 @@
 //
 //===----------------------------------------------------------------------===//
 
-#define DEBUG_TYPE "format-test"
-
-#include "clang/Format/Format.h"
-#include "clang/Lex/Lexer.h"
-#include "llvm/Support/Debug.h"
-#include "gtest/gtest.h"
+#include "FormatTest.h"
 
 namespace clang {
 namespace format {
 
-class FormatTest : public ::testing::Test {
-protected:
-  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);
-    ReplacementCount = Replaces.size();
-    std::string Result = applyAllReplacements(Code, Replaces);
-    EXPECT_NE("", Result);
-    DEBUG(llvm::errs() << "\n" << Result << "\n\n");
-    return Result;
-  }
-
-  std::string
-  format(llvm::StringRef Code, const FormatStyle &Style = getLLVMStyle()) {
-    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;
-    return Style;
-  }
-
-  FormatStyle getGoogleStyleWithColumns(unsigned ColumnLimit) {
-    FormatStyle Style = getGoogleStyle();
-    Style.ColumnLimit = ColumnLimit;
-    return Style;
-  }
-
-  void verifyFormat(llvm::StringRef Code,
-                    const FormatStyle &Style = getLLVMStyle()) {
-    EXPECT_EQ(Code.str(), format(messUp(Code), Style));
-  }
-
-  void verifyGoogleFormat(llvm::StringRef Code) {
-    verifyFormat(Code, getGoogleStyle());
-  }
-
-  void verifyIndependentOfContext(llvm::StringRef text) {
-    verifyFormat(text);
-    verifyFormat(llvm::Twine("void f() { " + text + " }").str());
-  }
-
-  int ReplacementCount;
-};
-
 TEST_F(FormatTest, MessUp) {
   EXPECT_EQ("1 2 3", messUp("1 2 3"));
   EXPECT_EQ("1 2 3\n", messUp("1\n2\n3\n"));
@@ -3598,7 +3504,7 @@
                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa;",
                Style);
   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
-               "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 
+               "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
                Style);
Index: unittests/Format/FormatTest.h
===================================================================
--- /dev/null
+++ unittests/Format/FormatTest.h
@@ -0,0 +1,120 @@
+//===- unittest/Format/FormatTest.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 a utility class for Clang-Format related tests.
+//
+//===----------------------------------------------------------------------===//
+
+
+#ifndef LLVM_CLANG_FORMAT_TEST_H
+#define LLVM_CLANG_FORMAT_TEST_H
+
+#define DEBUG_TYPE "format-test"
+
+#include "clang/Format/Format.h"
+#include "clang/Lex/Lexer.h"
+#include "llvm/Support/Debug.h"
+#include "gtest/gtest.h"
+
+namespace clang {
+namespace format {
+
+class FormatTest : public ::testing::Test {
+protected:
+  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);
+    ReplacementCount = Replaces.size();
+    std::string Result = applyAllReplacements(Code, Replaces);
+    EXPECT_NE("", Result);
+    DEBUG(llvm::errs() << "\n" << Result << "\n\n");
+    return Result;
+  }
+
+  std::string
+  format(llvm::StringRef Code, const FormatStyle &Style = getLLVMStyle()) {
+    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;
+    return Style;
+  }
+
+  FormatStyle getGoogleStyleWithColumns(unsigned ColumnLimit) {
+    FormatStyle Style = getGoogleStyle();
+    Style.ColumnLimit = ColumnLimit;
+    return Style;
+  }
+
+  void verifyFormat(llvm::StringRef Code,
+                    const FormatStyle &Style = getLLVMStyle()) {
+    EXPECT_EQ(Code.str(), format(messUp(Code), Style));
+  }
+
+  void verifyGoogleFormat(llvm::StringRef Code) {
+    verifyFormat(Code, getGoogleStyle());
+  }
+
+  void verifyIndependentOfContext(llvm::StringRef text) {
+    verifyFormat(text);
+    verifyFormat(llvm::Twine("void f() { " + text + " }").str());
+  }
+
+  int ReplacementCount;
+};
+
+} // end namespace format
+} // end namespace clang
+
+#endif // LLVM_CLANG_FORMAT_TEST_H
Index: unittests/Format/FormatTestJS.cpp
===================================================================
--- /dev/null
+++ unittests/Format/FormatTestJS.cpp
@@ -0,0 +1,49 @@
+//===- unittest/Format/FormatTest.cpp - Formatting unit tests -------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "FormatTest.h"
+
+namespace clang {
+namespace format {
+
+TEST_F(FormatTest, UnderstandsJavaScriptOperators) {
+  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
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits

Reply via email to