blob: 8a9ac9e20b855d92e273c831d01d39f93926377b [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
34 static std::string format(llvm::StringRef Code,
Alexander Kornienkoc1637f12013-12-10 11:28:13 +000035 const FormatStyle &Style = getGoogleJSStyle()) {
Alexander Kornienkoa48a12c2013-12-03 10:50:16 +000036 return format(Code, 0, Code.size(), Style);
37 }
38
Alexander Kornienkoc1637f12013-12-10 11:28:13 +000039 static FormatStyle getGoogleJSStyleWithColumns(unsigned ColumnLimit) {
40 FormatStyle Style = getGoogleJSStyle();
Alexander Kornienkoa48a12c2013-12-03 10:50:16 +000041 Style.ColumnLimit = ColumnLimit;
42 return Style;
43 }
44
45 static void verifyFormat(llvm::StringRef Code,
Alexander Kornienkoc1637f12013-12-10 11:28:13 +000046 const FormatStyle &Style = getGoogleJSStyle()) {
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};");
85}
86
Daniel Jasper86fee2f2014-01-31 12:49:42 +000087TEST_F(FormatTestJS, SingleQuoteStrings) {
88 verifyFormat("this.function('', true);");
89}
90
Alexander Kornienkoa48a12c2013-12-03 10:50:16 +000091} // end namespace tooling
92} // end namespace clang