1 //===- unittest/Format/FormatTestJson.cpp - Formatting tests for Json -===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "FormatTestUtils.h"
10 #include "clang/Format/Format.h"
11 #include "llvm/Support/Debug.h"
12 #include "gtest/gtest.h"
14 #define DEBUG_TYPE "format-test-json"
19 class FormatTestJson
: public ::testing::Test
{
21 static std::string
format(llvm::StringRef Code
, unsigned Offset
,
22 unsigned Length
, const FormatStyle
&Style
) {
23 LLVM_DEBUG(llvm::errs() << "---\n");
24 LLVM_DEBUG(llvm::errs() << Code
<< "\n\n");
26 tooling::Replacements Replaces
;
28 // Mock up what ClangFormat.cpp will do for JSON by adding a variable
29 // to trick JSON into being JavaScript
30 if (Style
.isJson() && !Style
.DisableFormat
) {
31 auto Err
= Replaces
.add(
32 tooling::Replacement(tooling::Replacement("", 0, 0, "x = ")));
34 llvm::errs() << "Bad Json variable insertion\n";
36 auto ChangedCode
= applyAllReplacements(Code
, Replaces
);
38 llvm::errs() << "Bad Json varibale replacement\n";
39 StringRef NewCode
= *ChangedCode
;
41 std::vector
<tooling::Range
> Ranges(1, tooling::Range(0, NewCode
.size()));
42 Replaces
= reformat(Style
, NewCode
, Ranges
);
43 auto Result
= applyAllReplacements(NewCode
, Replaces
);
44 EXPECT_TRUE(static_cast<bool>(Result
));
45 LLVM_DEBUG(llvm::errs() << "\n" << *Result
<< "\n\n");
50 format(llvm::StringRef Code
,
51 const FormatStyle
&Style
= getLLVMStyle(FormatStyle::LK_Json
)) {
52 return format(Code
, 0, Code
.size(), Style
);
55 static FormatStyle
getStyleWithColumns(unsigned ColumnLimit
) {
56 FormatStyle Style
= getLLVMStyle(FormatStyle::LK_Json
);
57 Style
.ColumnLimit
= ColumnLimit
;
61 static void verifyFormatStable(llvm::StringRef Code
,
62 const FormatStyle
&Style
) {
63 EXPECT_EQ(Code
.str(), format(Code
, Style
)) << "Expected code is not stable";
67 verifyFormat(llvm::StringRef Code
,
68 const FormatStyle
&Style
= getLLVMStyle(FormatStyle::LK_Json
)) {
69 verifyFormatStable(Code
, Style
);
70 EXPECT_EQ(Code
.str(), format(test::messUp(Code
), Style
));
74 TEST_F(FormatTestJson
, JsonRecord
) {
80 " \"name\": \"Foo\"\n"
121 "streetAddress
": "21 2nd Street
",
124 "postalCode
": "10021-3100"
129 "number
": "212 555-1234"
133 "number
": "646 555-4567"
141 TEST_F(FormatTestJson
, JsonArray
) {
162 TEST_F(FormatTestJson
, JsonArrayOneLine
) {
163 FormatStyle Style
= getLLVMStyle(FormatStyle::LK_Json
);
164 Style
.BreakArrays
= false;
165 Style
.SpacesInContainerLiterals
= false;
166 verifyFormat("[]", Style
);
167 verifyFormat("[1]", Style
);
168 verifyFormat("[1, 2]", Style
);
169 verifyFormat("[1, 2, 3]", Style
);
170 verifyFormat("[1, 2, 3, 4]", Style
);
171 verifyFormat("[1, 2, 3, 4, 5]", Style
);
183 TEST_F(FormatTestJson
, JsonNoStringSplit
) {
184 FormatStyle Style
= getLLVMStyle(FormatStyle::LK_Json
);
185 Style
.IndentWidth
= 4;
190 "\"naaaaaaaa\": \"foooooooooooooooooooooo oooooooooooooooooooooo\"\n"
199 "\"foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo"
200 "oooooooooooooooooooooooooo\"\n"
206 Style
.ColumnLimit
= 80;
212 "\"foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo"
213 "oooooooooooooooooooooooooo\"\n"
220 TEST_F(FormatTestJson
, DisableJsonFormat
) {
221 FormatStyle Style
= getLLVMStyle(FormatStyle::LK_Json
);
222 verifyFormatStable("{}", Style
);
223 verifyFormatStable("{\n"
228 // Since we have to disable formatting to run this test, we shall refrain from
229 // calling test::messUp lest we change the unformatted code and cannot format
230 // it back to how it started.
231 Style
.DisableFormat
= true;
232 verifyFormatStable("{}", Style
);
233 verifyFormatStable("{\n"
239 TEST_F(FormatTestJson
, SpaceBeforeJsonColon
) {
240 FormatStyle Style
= getLLVMStyle(FormatStyle::LK_Json
);
241 verifyFormatStable("{\n"
246 Style
.SpaceBeforeJsonColon
= true;
247 verifyFormatStable("{}", Style
);
248 verifyFormatStable("{\n"
254 TEST_F(FormatTestJson
, StartsWithWhitespaces
) {
255 FormatStyle Style
= getLLVMStyle(FormatStyle::LK_Json
);
264 // FIXME: The block below is over-indented.
274 } // namespace format
275 } // end namespace clang