1 //===- unittest/Format/FormatTestJS.cpp - Formatting unit tests for JS ----===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #define DEBUG_TYPE "format-test" 11 12 #include "FormatTestUtils.h" 13 #include "clang/Format/Format.h" 14 #include "llvm/Support/Debug.h" 15 #include "gtest/gtest.h" 16 17 namespace clang { 18 namespace format { 19 20 class FormatTestJS : public ::testing::Test { 21 protected: 22 static std::string format(llvm::StringRef Code, unsigned Offset, 23 unsigned Length, const FormatStyle &Style) { 24 DEBUG(llvm::errs() << "---\n"); 25 DEBUG(llvm::errs() << Code << "\n\n"); 26 std::vector<tooling::Range> Ranges(1, tooling::Range(Offset, Length)); 27 tooling::Replacements Replaces = reformat(Style, Code, Ranges); 28 std::string Result = applyAllReplacements(Code, Replaces); 29 EXPECT_NE("", Result); 30 DEBUG(llvm::errs() << "\n" << Result << "\n\n"); 31 return Result; 32 } 33 34 static std::string format(llvm::StringRef Code, 35 const FormatStyle &Style = getGoogleJSStyle()) { 36 return format(Code, 0, Code.size(), Style); 37 } 38 39 static FormatStyle getGoogleJSStyleWithColumns(unsigned ColumnLimit) { 40 FormatStyle Style = getGoogleJSStyle(); 41 Style.ColumnLimit = ColumnLimit; 42 return Style; 43 } 44 45 static void verifyFormat(llvm::StringRef Code, 46 const FormatStyle &Style = getGoogleJSStyle()) { 47 EXPECT_EQ(Code.str(), format(test::messUp(Code), Style)); 48 } 49 }; 50 51 TEST_F(FormatTestJS, UnderstandsJavaScriptOperators) { 52 verifyFormat("a == = b;"); 53 verifyFormat("a != = b;"); 54 55 verifyFormat("a === b;"); 56 verifyFormat("aaaaaaa ===\n b;", getGoogleJSStyleWithColumns(10)); 57 verifyFormat("a !== b;"); 58 verifyFormat("aaaaaaa !==\n b;", getGoogleJSStyleWithColumns(10)); 59 verifyFormat("if (a + b + c +\n" 60 " d !==\n" 61 " e + f + g)\n" 62 " q();", 63 getGoogleJSStyleWithColumns(20)); 64 65 verifyFormat("a >> >= b;"); 66 67 verifyFormat("a >>> b;"); 68 verifyFormat("aaaaaaa >>>\n b;", getGoogleJSStyleWithColumns(10)); 69 verifyFormat("a >>>= b;"); 70 verifyFormat("aaaaaaa >>>=\n b;", getGoogleJSStyleWithColumns(10)); 71 verifyFormat("if (a + b + c +\n" 72 " d >>>\n" 73 " e + f + g)\n" 74 " q();", 75 getGoogleJSStyleWithColumns(20)); 76 verifyFormat("var x = aaaaaaaaaa ?\n" 77 " bbbbbb :\n" 78 " ccc;", 79 getGoogleJSStyleWithColumns(20)); 80 } 81 82 TEST_F(FormatTestJS, SpacesInContainerLiterals) { 83 verifyFormat("var arr = [1, 2, 3];"); 84 verifyFormat("var obj = {a: 1, b: 2, c: 3};"); 85 } 86 87 TEST_F(FormatTestJS, SingleQuoteStrings) { 88 verifyFormat("this.function('', true);"); 89 } 90 91 } // end namespace tooling 92 } // end namespace clang 93