teak-llvm/clang/unittests/Format/FormatTestJS.cpp
Alexander Kornienko c1637f167c Allow predefined styles to define different options for different languages.
Summary:
Allow predefined styles to define different options for different
languages so that one can run:
  clang-format -style=google file1.cpp file2.js

or use a single .clang-format file with "BasedOnStyle: Google" for both c++ and
JS files.

Added Google style for JavaScript with "BreakBeforeTernaryOperators" set to
false.

Reviewers: djasper

Reviewed By: djasper

CC: cfe-commits, klimek

Differential Revision: http://llvm-reviews.chandlerc.com/D2364

llvm-svn: 196909
2013-12-10 11:28:13 +00:00

85 lines
2.7 KiB
C++

//===- unittest/Format/FormatTestJS.cpp - Formatting unit tests for JS ----===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#define DEBUG_TYPE "format-test"
#include "FormatTestUtils.h"
#include "clang/Format/Format.h"
#include "llvm/Support/Debug.h"
#include "gtest/gtest.h"
namespace clang {
namespace format {
class FormatTestJS : public ::testing::Test {
protected:
static std::string format(llvm::StringRef Code, unsigned Offset,
unsigned Length, const FormatStyle &Style) {
DEBUG(llvm::errs() << "---\n");
DEBUG(llvm::errs() << Code << "\n\n");
std::vector<tooling::Range> Ranges(1, tooling::Range(Offset, Length));
tooling::Replacements Replaces = reformat(Style, Code, Ranges);
std::string Result = applyAllReplacements(Code, Replaces);
EXPECT_NE("", Result);
DEBUG(llvm::errs() << "\n" << Result << "\n\n");
return Result;
}
static std::string format(llvm::StringRef Code,
const FormatStyle &Style = getGoogleJSStyle()) {
return format(Code, 0, Code.size(), Style);
}
static FormatStyle getGoogleJSStyleWithColumns(unsigned ColumnLimit) {
FormatStyle Style = getGoogleJSStyle();
Style.ColumnLimit = ColumnLimit;
return Style;
}
static void verifyFormat(llvm::StringRef Code,
const FormatStyle &Style = getGoogleJSStyle()) {
EXPECT_EQ(Code.str(), format(test::messUp(Code), Style));
}
};
TEST_F(FormatTestJS, UnderstandsJavaScriptOperators) {
verifyFormat("a == = b;");
verifyFormat("a != = b;");
verifyFormat("a === b;");
verifyFormat("aaaaaaa ===\n b;", getGoogleJSStyleWithColumns(10));
verifyFormat("a !== b;");
verifyFormat("aaaaaaa !==\n b;", getGoogleJSStyleWithColumns(10));
verifyFormat("if (a + b + c +\n"
" d !==\n"
" e + f + g)\n"
" q();",
getGoogleJSStyleWithColumns(20));
verifyFormat("a >> >= b;");
verifyFormat("a >>> b;");
verifyFormat("aaaaaaa >>>\n b;", getGoogleJSStyleWithColumns(10));
verifyFormat("a >>>= b;");
verifyFormat("aaaaaaa >>>=\n b;", getGoogleJSStyleWithColumns(10));
verifyFormat("if (a + b + c +\n"
" d >>>\n"
" e + f + g)\n"
" q();",
getGoogleJSStyleWithColumns(20));
verifyFormat("var x = aaaaaaaaaa ?\n"
" bbbbbb :\n"
" ccc;",
getGoogleJSStyleWithColumns(20));
}
} // end namespace tooling
} // end namespace clang