blob: c0215e7d142db86573555a396dcbb1054fb7a833 [file] [log] [blame]
Alexander Kornienkoa48a12c2013-12-03 10:50:16 +00001//===- 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"
Alexander Kornienkoa48a12c2013-12-03 10:50:16 +000013#include "clang/Format/Format.h"
14#include "llvm/Support/Debug.h"
15#include "gtest/gtest.h"
16
17namespace clang {
18namespace format {
19
20class FormatTestJS : public ::testing::Test {
21protected:
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
Nico Weber514ecc82014-02-02 20:50:45 +000034 static std::string format(llvm::StringRef Code, const FormatStyle &Style) {
Alexander Kornienkoa48a12c2013-12-03 10:50:16 +000035 return format(Code, 0, Code.size(), Style);
36 }
37
Alexander Kornienkoc1637f12013-12-10 11:28:13 +000038 static FormatStyle getGoogleJSStyleWithColumns(unsigned ColumnLimit) {
Nico Weber514ecc82014-02-02 20:50:45 +000039 FormatStyle Style = getGoogleStyle(FormatStyle::LK_JavaScript);
Alexander Kornienkoa48a12c2013-12-03 10:50:16 +000040 Style.ColumnLimit = ColumnLimit;
41 return Style;
42 }
43
Nico Weber514ecc82014-02-02 20:50:45 +000044 static void verifyFormat(
45 llvm::StringRef Code,
46 const FormatStyle &Style = getGoogleStyle(FormatStyle::LK_JavaScript)) {
Alexander Kornienkoa48a12c2013-12-03 10:50:16 +000047 EXPECT_EQ(Code.str(), format(test::messUp(Code), Style));
48 }
49};
50
51TEST_F(FormatTestJS, UnderstandsJavaScriptOperators) {
52 verifyFormat("a == = b;");
53 verifyFormat("a != = b;");
54
55 verifyFormat("a === b;");
Alexander Kornienkoc1637f12013-12-10 11:28:13 +000056 verifyFormat("aaaaaaa ===\n b;", getGoogleJSStyleWithColumns(10));
Alexander Kornienkoa48a12c2013-12-03 10:50:16 +000057 verifyFormat("a !== b;");
Alexander Kornienkoc1637f12013-12-10 11:28:13 +000058 verifyFormat("aaaaaaa !==\n b;", getGoogleJSStyleWithColumns(10));
Alexander Kornienkoa48a12c2013-12-03 10:50:16 +000059 verifyFormat("if (a + b + c +\n"
60 " d !==\n"
61 " e + f + g)\n"
62 " q();",
Alexander Kornienkoc1637f12013-12-10 11:28:13 +000063 getGoogleJSStyleWithColumns(20));
Alexander Kornienkoa48a12c2013-12-03 10:50:16 +000064
65 verifyFormat("a >> >= b;");
66
67 verifyFormat("a >>> b;");
Alexander Kornienkoc1637f12013-12-10 11:28:13 +000068 verifyFormat("aaaaaaa >>>\n b;", getGoogleJSStyleWithColumns(10));
Alexander Kornienkoa48a12c2013-12-03 10:50:16 +000069 verifyFormat("a >>>= b;");
Alexander Kornienkoc1637f12013-12-10 11:28:13 +000070 verifyFormat("aaaaaaa >>>=\n b;", getGoogleJSStyleWithColumns(10));
Alexander Kornienkoa48a12c2013-12-03 10:50:16 +000071 verifyFormat("if (a + b + c +\n"
72 " d >>>\n"
73 " e + f + g)\n"
74 " q();",
Alexander Kornienkoc1637f12013-12-10 11:28:13 +000075 getGoogleJSStyleWithColumns(20));
76 verifyFormat("var x = aaaaaaaaaa ?\n"
77 " bbbbbb :\n"
78 " ccc;",
79 getGoogleJSStyleWithColumns(20));
Alexander Kornienkoa48a12c2013-12-03 10:50:16 +000080}
81
Daniel Jasperb2e10a52014-01-15 15:09:08 +000082TEST_F(FormatTestJS, SpacesInContainerLiterals) {
83 verifyFormat("var arr = [1, 2, 3];");
84 verifyFormat("var obj = {a: 1, b: 2, c: 3};");
Nico Weber514ecc82014-02-02 20:50:45 +000085
86 verifyFormat("var obj = {a: 1, b: 2, c: 3};",
87 getChromiumStyle(FormatStyle::LK_JavaScript));
Daniel Jasperb2e10a52014-01-15 15:09:08 +000088}
89
Daniel Jasper86fee2f2014-01-31 12:49:42 +000090TEST_F(FormatTestJS, SingleQuoteStrings) {
91 verifyFormat("this.function('', true);");
92}
93
Alexander Kornienkoa48a12c2013-12-03 10:50:16 +000094} // end namespace tooling
95} // end namespace clang