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(StringRef Code
, unsigned Offset
, unsigned Length
,
22 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(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(StringRef Code
, const FormatStyle
&Style
) {
62 EXPECT_EQ(Code
.str(), format(Code
, Style
)) << "Expected code is not stable";
66 verifyFormat(StringRef Code
,
67 const FormatStyle
&Style
= getLLVMStyle(FormatStyle::LK_Json
)) {
68 verifyFormatStable(Code
, Style
);
69 EXPECT_EQ(Code
.str(), format(test::messUp(Code
), Style
));
73 TEST_F(FormatTestJson
, JsonRecord
) {
79 " \"name\": \"Foo\"\n"
120 "streetAddress
": "21 2nd Street
",
123 "postalCode
": "10021-3100"
128 "number
": "212 555-1234"
132 "number
": "646 555-4567"
140 TEST_F(FormatTestJson
, JsonArray
) {
161 TEST_F(FormatTestJson
, JsonArrayOneLine
) {
162 FormatStyle Style
= getLLVMStyle(FormatStyle::LK_Json
);
163 Style
.BreakArrays
= false;
164 Style
.SpacesInContainerLiterals
= false;
165 verifyFormat("[]", Style
);
166 verifyFormat("[1]", Style
);
167 verifyFormat("[1, 2]", Style
);
168 verifyFormat("[1, 2, 3]", Style
);
169 verifyFormat("[1, 2, 3, 4]", Style
);
170 verifyFormat("[1, 2, 3, 4, 5]", Style
);
182 TEST_F(FormatTestJson
, JsonNoStringSplit
) {
183 FormatStyle Style
= getLLVMStyle(FormatStyle::LK_Json
);
184 Style
.IndentWidth
= 4;
189 "\"naaaaaaaa\": \"foooooooooooooooooooooo oooooooooooooooooooooo\"\n"
198 "\"foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo"
199 "oooooooooooooooooooooooooo\"\n"
205 Style
.ColumnLimit
= 80;
211 "\"foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo"
212 "oooooooooooooooooooooooooo\"\n"
219 TEST_F(FormatTestJson
, DisableJsonFormat
) {
220 FormatStyle Style
= getLLVMStyle(FormatStyle::LK_Json
);
221 verifyFormatStable("{}", Style
);
222 verifyFormatStable("{\n"
227 // Since we have to disable formatting to run this test, we shall refrain from
228 // calling test::messUp lest we change the unformatted code and cannot format
229 // it back to how it started.
230 Style
.DisableFormat
= true;
231 verifyFormatStable("{}", Style
);
232 verifyFormatStable("{\n"
238 TEST_F(FormatTestJson
, SpaceBeforeJsonColon
) {
239 FormatStyle Style
= getLLVMStyle(FormatStyle::LK_Json
);
240 verifyFormatStable("{\n"
245 Style
.SpaceBeforeJsonColon
= true;
246 verifyFormatStable("{}", Style
);
247 verifyFormatStable("{\n"
253 TEST_F(FormatTestJson
, StartsWithWhitespaces
) {
254 FormatStyle Style
= getLLVMStyle(FormatStyle::LK_Json
);
263 // FIXME: The block below is over-indented.
273 } // namespace format
274 } // end namespace clang