Updated file comment in FormatTestJS.cpp

Hi djasper,

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

CHANGE SINCE LAST DIFF
  http://llvm-reviews.chandlerc.com/D2307?vs=5856&id=5857#toc

BRANCH
  svn

ARCANIST PROJECT
  clang

Files:
  unittests/Format/CMakeLists.txt
  unittests/Format/FormatTest.cpp
  unittests/Format/FormatTestJS.cpp
  unittests/Format/FormatTestUtils.h
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
@@ -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 @@
     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 @@
 
   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 @@
 };
 
 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 @@
                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa;",
                Style);
   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
-               "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 
+               "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
                Style);
Index: unittests/Format/FormatTestJS.cpp
===================================================================
--- /dev/null
+++ 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
Index: unittests/Format/FormatTestUtils.h
===================================================================
--- /dev/null
+++ 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
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits

Reply via email to