xref: /llvm-project/clang/unittests/Format/FormatTestJS.cpp (revision a48a12cf81cb0804506ddeb4994f17008cf097b3)
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 = getJSStyle()) {
37     return format(Code, 0, Code.size(), Style);
38   }
39 
40   static FormatStyle getJSStyle() {
41     FormatStyle Style = getLLVMStyle();
42     Style.Language = FormatStyle::LK_JavaScript;
43     return Style;
44   }
45 
46   static FormatStyle getJSStyleWithColumns(unsigned ColumnLimit) {
47     FormatStyle Style = getJSStyle();
48     Style.ColumnLimit = ColumnLimit;
49     return Style;
50   }
51 
52   static void verifyFormat(llvm::StringRef Code,
53                            const FormatStyle &Style = getJSStyle()) {
54     EXPECT_EQ(Code.str(), format(test::messUp(Code), Style));
55   }
56 };
57 
58 TEST_F(FormatTestJS, UnderstandsJavaScriptOperators) {
59   verifyFormat("a == = b;");
60   verifyFormat("a != = b;");
61 
62   verifyFormat("a === b;");
63   verifyFormat("aaaaaaa ===\n    b;", getJSStyleWithColumns(10));
64   verifyFormat("a !== b;");
65   verifyFormat("aaaaaaa !==\n    b;", getJSStyleWithColumns(10));
66   verifyFormat("if (a + b + c +\n"
67                "        d !==\n"
68                "    e + f + g)\n"
69                "  q();",
70                getJSStyleWithColumns(20));
71 
72   verifyFormat("a >> >= b;");
73 
74   verifyFormat("a >>> b;");
75   verifyFormat("aaaaaaa >>>\n    b;", getJSStyleWithColumns(10));
76   verifyFormat("a >>>= b;");
77   verifyFormat("aaaaaaa >>>=\n    b;", getJSStyleWithColumns(10));
78   verifyFormat("if (a + b + c +\n"
79                "        d >>>\n"
80                "    e + f + g)\n"
81                "  q();",
82                getJSStyleWithColumns(20));
83 }
84 
85 } // end namespace tooling
86 } // end namespace clang
87