2013-08-21 03:07:21 +08:00
|
|
|
//===- unittests/Tooling/ReplacementsYamlTest.cpp - Serialization tests ---===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2013-08-21 03:07:21 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Tests for serialization of Replacements.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/Tooling/ReplacementsYaml.h"
|
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace clang::tooling;
|
|
|
|
|
|
|
|
TEST(ReplacementsYamlTest, serializesReplacements) {
|
|
|
|
|
|
|
|
TranslationUnitReplacements Doc;
|
|
|
|
|
|
|
|
Doc.MainSourceFile = "/path/to/source.cpp";
|
2017-01-03 22:35:47 +08:00
|
|
|
Doc.Replacements.emplace_back("/path/to/file1.h", 232, 56, "replacement #1");
|
|
|
|
Doc.Replacements.emplace_back("/path/to/file2.h", 301, 2, "replacement #2");
|
2013-08-21 03:07:21 +08:00
|
|
|
|
|
|
|
std::string YamlContent;
|
|
|
|
llvm::raw_string_ostream YamlContentStream(YamlContent);
|
|
|
|
|
|
|
|
yaml::Output YAML(YamlContentStream);
|
|
|
|
YAML << Doc;
|
|
|
|
|
|
|
|
// NOTE: If this test starts to fail for no obvious reason, check whitespace.
|
|
|
|
ASSERT_STREQ("---\n"
|
2018-10-13 00:31:20 +08:00
|
|
|
"MainSourceFile: '/path/to/source.cpp'\n"
|
2019-07-12 13:59:28 +08:00
|
|
|
"Replacements:\n"
|
2018-10-13 00:31:20 +08:00
|
|
|
" - FilePath: '/path/to/file1.h'\n"
|
2013-08-21 03:07:21 +08:00
|
|
|
" Offset: 232\n"
|
|
|
|
" Length: 56\n"
|
2013-09-11 12:00:35 +08:00
|
|
|
" ReplacementText: 'replacement #1'\n"
|
2018-10-13 00:31:20 +08:00
|
|
|
" - FilePath: '/path/to/file2.h'\n"
|
2013-08-21 03:07:21 +08:00
|
|
|
" Offset: 301\n"
|
|
|
|
" Length: 2\n"
|
2013-09-11 12:00:35 +08:00
|
|
|
" ReplacementText: 'replacement #2'\n"
|
2013-08-21 03:07:21 +08:00
|
|
|
"...\n",
|
|
|
|
YamlContentStream.str().c_str());
|
|
|
|
}
|
|
|
|
|
2019-07-03 18:21:50 +08:00
|
|
|
TEST(ReplacementsYamlTest, serializesNewLines) {
|
|
|
|
TranslationUnitReplacements Doc;
|
|
|
|
|
|
|
|
Doc.MainSourceFile = "/path/to/source.cpp";
|
|
|
|
Doc.Replacements.emplace_back("/path/to/file1.h", 0, 0, "#include <utility>\n");
|
|
|
|
|
|
|
|
std::string YamlContent;
|
|
|
|
llvm::raw_string_ostream YamlContentStream(YamlContent);
|
|
|
|
|
|
|
|
yaml::Output YAML(YamlContentStream);
|
|
|
|
YAML << Doc;
|
|
|
|
|
|
|
|
// NOTE: If this test starts to fail for no obvious reason, check whitespace.
|
|
|
|
ASSERT_STREQ("---\n"
|
|
|
|
"MainSourceFile: '/path/to/source.cpp'\n"
|
2019-07-12 13:59:28 +08:00
|
|
|
"Replacements:\n"
|
2019-07-03 18:21:50 +08:00
|
|
|
" - FilePath: '/path/to/file1.h'\n"
|
|
|
|
" Offset: 0\n"
|
|
|
|
" Length: 0\n"
|
2020-05-20 23:06:41 +08:00
|
|
|
" ReplacementText: \"#include <utility>\\n\"\n"
|
2019-07-03 18:21:50 +08:00
|
|
|
"...\n",
|
|
|
|
YamlContentStream.str().c_str());
|
|
|
|
}
|
|
|
|
|
2013-08-21 03:07:21 +08:00
|
|
|
TEST(ReplacementsYamlTest, deserializesReplacements) {
|
|
|
|
std::string YamlContent = "---\n"
|
2013-09-11 12:00:35 +08:00
|
|
|
"MainSourceFile: /path/to/source.cpp\n"
|
2013-08-21 03:07:21 +08:00
|
|
|
"Replacements:\n"
|
2013-09-11 12:00:35 +08:00
|
|
|
" - FilePath: /path/to/file1.h\n"
|
2013-08-21 03:07:21 +08:00
|
|
|
" Offset: 232\n"
|
|
|
|
" Length: 56\n"
|
2013-09-11 12:00:35 +08:00
|
|
|
" ReplacementText: 'replacement #1'\n"
|
|
|
|
" - FilePath: /path/to/file2.h\n"
|
2013-08-21 03:07:21 +08:00
|
|
|
" Offset: 301\n"
|
|
|
|
" Length: 2\n"
|
2013-09-11 12:00:35 +08:00
|
|
|
" ReplacementText: 'replacement #2'\n"
|
2013-08-21 03:07:21 +08:00
|
|
|
"...\n";
|
|
|
|
TranslationUnitReplacements DocActual;
|
|
|
|
yaml::Input YAML(YamlContent);
|
|
|
|
YAML >> DocActual;
|
|
|
|
ASSERT_FALSE(YAML.error());
|
|
|
|
ASSERT_EQ(2u, DocActual.Replacements.size());
|
|
|
|
ASSERT_EQ("/path/to/source.cpp", DocActual.MainSourceFile);
|
|
|
|
ASSERT_EQ("/path/to/file1.h", DocActual.Replacements[0].getFilePath());
|
|
|
|
ASSERT_EQ(232u, DocActual.Replacements[0].getOffset());
|
|
|
|
ASSERT_EQ(56u, DocActual.Replacements[0].getLength());
|
|
|
|
ASSERT_EQ("replacement #1", DocActual.Replacements[0].getReplacementText());
|
|
|
|
ASSERT_EQ("/path/to/file2.h", DocActual.Replacements[1].getFilePath());
|
|
|
|
ASSERT_EQ(301u, DocActual.Replacements[1].getOffset());
|
|
|
|
ASSERT_EQ(2u, DocActual.Replacements[1].getLength());
|
|
|
|
ASSERT_EQ("replacement #2", DocActual.Replacements[1].getReplacementText());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(ReplacementsYamlTest, deserializesWithoutContext) {
|
|
|
|
// Make sure a doc can be read without the context field.
|
|
|
|
std::string YamlContent = "---\n"
|
2013-09-11 12:00:35 +08:00
|
|
|
"MainSourceFile: /path/to/source.cpp\n"
|
2013-08-21 03:07:21 +08:00
|
|
|
"Replacements:\n"
|
2013-09-11 12:00:35 +08:00
|
|
|
" - FilePath: target_file.h\n"
|
2013-08-21 03:07:21 +08:00
|
|
|
" Offset: 1\n"
|
|
|
|
" Length: 10\n"
|
2013-09-11 12:00:35 +08:00
|
|
|
" ReplacementText: replacement\n"
|
2013-08-21 03:07:21 +08:00
|
|
|
"...\n";
|
|
|
|
TranslationUnitReplacements DocActual;
|
|
|
|
yaml::Input YAML(YamlContent);
|
|
|
|
YAML >> DocActual;
|
|
|
|
ASSERT_FALSE(YAML.error());
|
|
|
|
ASSERT_EQ("/path/to/source.cpp", DocActual.MainSourceFile);
|
|
|
|
ASSERT_EQ(1u, DocActual.Replacements.size());
|
|
|
|
ASSERT_EQ("target_file.h", DocActual.Replacements[0].getFilePath());
|
|
|
|
ASSERT_EQ(1u, DocActual.Replacements[0].getOffset());
|
|
|
|
ASSERT_EQ(10u, DocActual.Replacements[0].getLength());
|
|
|
|
ASSERT_EQ("replacement", DocActual.Replacements[0].getReplacementText());
|
|
|
|
}
|