xref: /llvm-project/clang/unittests/Format/FormatTestJS.cpp (revision c1637f167c7a2b6a3b4f30634b074a04bd60cd9a)
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 
14 #include "clang/Format/Format.h"
15 #include "llvm/Support/Debug.h"
16 #include "gtest/gtest.h"
17 
18 namespace clang {
19 namespace format {
20 
21 class FormatTestJS : public ::testing::Test {
22 protected:
23   static std::string format(llvm::StringRef Code, unsigned Offset,
24                             unsigned Length, const FormatStyle &Style) {
25     DEBUG(llvm::errs() << "---\n");
26     DEBUG(llvm::errs() << Code << "\n\n");
27     std::vector<tooling::Range> Ranges(1, tooling::Range(Offset, Length));
28     tooling::Replacements Replaces = reformat(Style, Code, Ranges);
29     std::string Result = applyAllReplacements(Code, Replaces);
30     EXPECT_NE("", Result);
31     DEBUG(llvm::errs() << "\n" << Result << "\n\n");
32     return Result;
33   }
34 
35   static std::string format(llvm::StringRef Code,
36                             const FormatStyle &Style = getGoogleJSStyle()) {
37     return format(Code, 0, Code.size(), Style);
38   }
39 
40   static FormatStyle getGoogleJSStyleWithColumns(unsigned ColumnLimit) {
41     FormatStyle Style = getGoogleJSStyle();
42     Style.ColumnLimit = ColumnLimit;
43     return Style;
44   }
45 
46   static void verifyFormat(llvm::StringRef Code,
47                            const FormatStyle &Style = getGoogleJSStyle()) {
48     EXPECT_EQ(Code.str(), format(test::messUp(Code), Style));
49   }
50 };
51 
52 TEST_F(FormatTestJS, UnderstandsJavaScriptOperators) {
53   verifyFormat("a == = b;");
54   verifyFormat("a != = b;");
55 
56   verifyFormat("a === b;");
57   verifyFormat("aaaaaaa ===\n    b;", getGoogleJSStyleWithColumns(10));
58   verifyFormat("a !== b;");
59   verifyFormat("aaaaaaa !==\n    b;", getGoogleJSStyleWithColumns(10));
60   verifyFormat("if (a + b + c +\n"
61                "        d !==\n"
62                "    e + f + g)\n"
63                "  q();",
64                getGoogleJSStyleWithColumns(20));
65 
66   verifyFormat("a >> >= b;");
67 
68   verifyFormat("a >>> b;");
69   verifyFormat("aaaaaaa >>>\n    b;", getGoogleJSStyleWithColumns(10));
70   verifyFormat("a >>>= b;");
71   verifyFormat("aaaaaaa >>>=\n    b;", getGoogleJSStyleWithColumns(10));
72   verifyFormat("if (a + b + c +\n"
73                "        d >>>\n"
74                "    e + f + g)\n"
75                "  q();",
76                getGoogleJSStyleWithColumns(20));
77   verifyFormat("var x = aaaaaaaaaa ?\n"
78                "            bbbbbb :\n"
79                "            ccc;",
80                getGoogleJSStyleWithColumns(20));
81 }
82 
83 } // end namespace tooling
84 } // end namespace clang
85