1 //===- unittests/Tooling/ReplacementsYamlTest.cpp - Serialization tests ---===//
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 // Tests for serialization of Replacements.
11 //===----------------------------------------------------------------------===//
13 #include "clang/Tooling/ReplacementsYaml.h"
14 #include "gtest/gtest.h"
17 using namespace clang::tooling
;
19 TEST(ReplacementsYamlTest
, serializesReplacements
) {
21 TranslationUnitReplacements Doc
;
23 Doc
.MainSourceFile
= "/path/to/source.cpp";
24 Doc
.Replacements
.emplace_back("/path/to/file1.h", 232, 56, "replacement #1");
25 Doc
.Replacements
.emplace_back("/path/to/file2.h", 301, 2, "replacement #2");
27 std::string YamlContent
;
28 llvm::raw_string_ostream
YamlContentStream(YamlContent
);
30 yaml::Output
YAML(YamlContentStream
);
33 // NOTE: If this test starts to fail for no obvious reason, check whitespace.
35 "MainSourceFile: '/path/to/source.cpp'\n"
37 " - FilePath: '/path/to/file1.h'\n"
40 " ReplacementText: 'replacement #1'\n"
41 " - FilePath: '/path/to/file2.h'\n"
44 " ReplacementText: 'replacement #2'\n"
49 TEST(ReplacementsYamlTest
, serializesNewLines
) {
50 TranslationUnitReplacements Doc
;
52 Doc
.MainSourceFile
= "/path/to/source.cpp";
53 Doc
.Replacements
.emplace_back("/path/to/file1.h", 0, 0, "#include <utility>\n");
55 std::string YamlContent
;
56 llvm::raw_string_ostream
YamlContentStream(YamlContent
);
58 yaml::Output
YAML(YamlContentStream
);
61 // NOTE: If this test starts to fail for no obvious reason, check whitespace.
63 "MainSourceFile: '/path/to/source.cpp'\n"
65 " - FilePath: '/path/to/file1.h'\n"
68 " ReplacementText: \"#include <utility>\\n\"\n"
73 TEST(ReplacementsYamlTest
, deserializesReplacements
) {
74 std::string YamlContent
= "---\n"
75 "MainSourceFile: /path/to/source.cpp\n"
77 " - FilePath: /path/to/file1.h\n"
80 " ReplacementText: 'replacement #1'\n"
81 " - FilePath: /path/to/file2.h\n"
84 " ReplacementText: 'replacement #2'\n"
86 TranslationUnitReplacements DocActual
;
87 yaml::Input
YAML(YamlContent
);
89 ASSERT_FALSE(YAML
.error());
90 ASSERT_EQ(2u, DocActual
.Replacements
.size());
91 ASSERT_EQ("/path/to/source.cpp", DocActual
.MainSourceFile
);
92 ASSERT_EQ("/path/to/file1.h", DocActual
.Replacements
[0].getFilePath());
93 ASSERT_EQ(232u, DocActual
.Replacements
[0].getOffset());
94 ASSERT_EQ(56u, DocActual
.Replacements
[0].getLength());
95 ASSERT_EQ("replacement #1", DocActual
.Replacements
[0].getReplacementText());
96 ASSERT_EQ("/path/to/file2.h", DocActual
.Replacements
[1].getFilePath());
97 ASSERT_EQ(301u, DocActual
.Replacements
[1].getOffset());
98 ASSERT_EQ(2u, DocActual
.Replacements
[1].getLength());
99 ASSERT_EQ("replacement #2", DocActual
.Replacements
[1].getReplacementText());
102 TEST(ReplacementsYamlTest
, deserializesWithoutContext
) {
103 // Make sure a doc can be read without the context field.
104 std::string YamlContent
= "---\n"
105 "MainSourceFile: /path/to/source.cpp\n"
107 " - FilePath: target_file.h\n"
110 " ReplacementText: replacement\n"
112 TranslationUnitReplacements DocActual
;
113 yaml::Input
YAML(YamlContent
);
115 ASSERT_FALSE(YAML
.error());
116 ASSERT_EQ("/path/to/source.cpp", DocActual
.MainSourceFile
);
117 ASSERT_EQ(1u, DocActual
.Replacements
.size());
118 ASSERT_EQ("target_file.h", DocActual
.Replacements
[0].getFilePath());
119 ASSERT_EQ(1u, DocActual
.Replacements
[0].getOffset());
120 ASSERT_EQ(10u, DocActual
.Replacements
[0].getLength());
121 ASSERT_EQ("replacement", DocActual
.Replacements
[0].getReplacementText());