Changeset View
Changeset View
Standalone View
Standalone View
unittests/Tooling/DiagnosticsYamlTest.cpp
- This file was added.
//===- unittests/Tooling/DiagnosticsYamlTest.cpp - Serialization tests ---===// | |||||
// | |||||
// The LLVM Compiler Infrastructure | |||||
// | |||||
// This file is distributed under the University of Illinois Open Source | |||||
// License. See LICENSE.TXT for details. | |||||
// | |||||
//===----------------------------------------------------------------------===// | |||||
// | |||||
// Tests for serialization of Diagnostics. | |||||
// | |||||
//===----------------------------------------------------------------------===// | |||||
#include "clang/Tooling/Core/Diagnostic.h" | |||||
#include "clang/Tooling/DiagnosticsYaml.h" | |||||
#include "clang/Tooling/ReplacementsYaml.h" | |||||
#include "gtest/gtest.h" | |||||
using namespace llvm; | |||||
using namespace clang::tooling; | |||||
TEST(DiagnosticsYamlTest, SerializesDiagnostics) { | |||||
TranslationUnitDiagnostics TUD; | |||||
TUD.MainSourceFile = "path/to/source.cpp"; | |||||
DiagnosticMessage Message1; | |||||
Message1.Message = "message #1"; | |||||
Message1.FileOffset = 55; | |||||
Message1.FilePath = "path/to/source.cpp"; | |||||
StringMap<Replacements> Fix1{ | |||||
{"path/to/source.cpp", Replacements(Replacement("path/to/source.cpp", 100, | |||||
12, "replacement #1"))}}; | |||||
DiagnosticMessage Message2; | |||||
Message2.Message = "message #2"; | |||||
Message2.FileOffset = 60; | |||||
Message2.FilePath = "path/to/header.h"; | |||||
StringMap<Replacements> Fix2{ | |||||
{"path/to/header.h", | |||||
Replacements(Replacement("path/to/header.h", 62, 2, "replacement #2"))}}; | |||||
SmallVector<DiagnosticMessage, 1> EmptyNotes; | |||||
TUD.Diagnostics.emplace_back("diagnostic#1", Message1, Fix1, EmptyNotes, | |||||
alexfh: The assignment form of initialization is clearer for containers (and actually any type for… | |||||
Diagnostic::Warning, "path/to/build/directory"); | |||||
TUD.Diagnostics.emplace_back("diagnostic#2", Message2, Fix2, EmptyNotes, | |||||
Diagnostic::Error, | |||||
"path/to/another/build/directory"); | |||||
std::string YamlContent; | |||||
Not Done ReplyInline ActionsNote: there's a bunch of trailing whitespace here and the formatting is off in a few places. git-clang-format fixed both issues, but it would be nice if you set up your editor properly or run clang-format yourself next time. Same for the other CL, but there I had to also clean up trailing whitespace from yaml files as well. alexfh: Note: there's a bunch of trailing whitespace here and the formatting is off in a few places. | |||||
raw_string_ostream YamlContentStream(YamlContent); | |||||
yaml::Output YAML(YamlContentStream); | |||||
YAML << TUD; | |||||
ASSERT_STREQ("---\n" | |||||
Not Done ReplyInline ActionsEXPECT_STREQ is more appropriate here. ASSERT_* family of macros terminates the test, and thus should only be used to verify the invariants that are absolutely required to continue the execution of the test, which is not the case here. alexfh: EXPECT_STREQ is more appropriate here. ASSERT_* family of macros terminates the test, and thus… | |||||
"MainSourceFile: path/to/source.cpp\n" | |||||
"Diagnostics: \n" | |||||
" - DiagnosticName: \"diagnostic#1\"\n" | |||||
" Message: \"message #1\"\n" | |||||
" FileOffset: 55\n" | |||||
" FilePath: path/to/source.cpp\n" | |||||
" Replacements: \n" | |||||
" - FilePath: path/to/source.cpp\n" | |||||
" Offset: 100\n" | |||||
Not Done ReplyInline ActionsWhy not EXPECT_EQ (and remove .c_str() from the second argument)? alexfh: Why not EXPECT_EQ (and remove `.c_str()` from the second argument)? | |||||
" Length: 12\n" | |||||
" ReplacementText: \"replacement #1\"\n" | |||||
" - DiagnosticName: \"diagnostic#2\"\n" | |||||
" Message: \"message #2\"\n" | |||||
" FileOffset: 60\n" | |||||
" FilePath: path/to/header.h\n" | |||||
" Replacements: \n" | |||||
" - FilePath: path/to/header.h\n" | |||||
" Offset: 62\n" | |||||
" Length: 2\n" | |||||
" ReplacementText: \"replacement #2\"\n" | |||||
"...\n", | |||||
YamlContentStream.str().c_str()); | |||||
} | |||||
TEST(DiagnosticsYamlTest, SerializesDiagnosticWithNoFix) { | |||||
TranslationUnitDiagnostics TUD; | |||||
TUD.MainSourceFile = "path/to/source.cpp"; | |||||
DiagnosticMessage Message; | |||||
Message.Message = "message #1"; | |||||
Message.FileOffset = 55; | |||||
Message.FilePath = "path/to/source.cpp"; | |||||
StringMap<Replacements> EmptyReplacements; | |||||
SmallVector<DiagnosticMessage, 1> EmptyNotes; | |||||
TUD.Diagnostics.emplace_back("diagnostic#1", Message, EmptyReplacements, | |||||
EmptyNotes, Diagnostic::Warning, | |||||
"path/to/build/directory"); | |||||
std::string YamlContent; | |||||
raw_string_ostream YamlContentStream(YamlContent); | |||||
yaml::Output YAML(YamlContentStream); | |||||
YAML << TUD; | |||||
ASSERT_STREQ("---\n" | |||||
"MainSourceFile: path/to/source.cpp\n" | |||||
"Diagnostics: \n" | |||||
" - DiagnosticName: \"diagnostic#1\"\n" | |||||
" Message: \"message #1\"\n" | |||||
" FileOffset: 55\n" | |||||
" FilePath: path/to/source.cpp\n" | |||||
"...\n", | |||||
YamlContentStream.str().c_str()); | |||||
} | |||||
TEST(DiagnosticsYamlTest, DeserializesDiagnostics) { | |||||
std::string YamlContent = "---\n" | |||||
"MainSourceFile: path/to/source.cpp\n" | |||||
"Diagnostics: \n" | |||||
" - DiagnosticName: 'diagnostic#1'\n" | |||||
" Message: 'message #1'\n" | |||||
" FileOffset: 55\n" | |||||
" FilePath: path/to/source.cpp\n" | |||||
" Replacements: \n" | |||||
" - FilePath: path/to/source.cpp\n" | |||||
" Offset: 100\n" | |||||
" Length: 12\n" | |||||
" ReplacementText: 'replacement #1'\n" | |||||
" - DiagnosticName: 'diagnostic#2'\n" | |||||
" Message: 'message #2'\n" | |||||
" FileOffset: 60\n" | |||||
" FilePath: path/to/header.h\n" | |||||
" Replacements: \n" | |||||
" - FilePath: path/to/header.h\n" | |||||
" Offset: 62\n" | |||||
" Length: 2\n" | |||||
" ReplacementText: 'replacement #2'\n" | |||||
"...\n"; | |||||
TranslationUnitDiagnostics TUDActual; | |||||
yaml::Input YAML(YamlContent); | |||||
YAML >> TUDActual; | |||||
ASSERT_FALSE(YAML.error()); | |||||
ASSERT_EQ(2u, TUDActual.Diagnostics.size()); | |||||
ASSERT_EQ("path/to/source.cpp", TUDActual.MainSourceFile); | |||||
auto getFixes = [](const StringMap<Replacements> &Fix) { | |||||
std::vector<Replacement> Fixes; | |||||
for (auto &Replacements : Fix) { | |||||
for (auto &Replacement : Replacements.second) { | |||||
Fixes.push_back(Replacement); | |||||
} | |||||
} | |||||
return Fixes; | |||||
}; | |||||
Diagnostic D1 = TUDActual.Diagnostics[0]; | |||||
ASSERT_EQ("diagnostic#1", D1.DiagnosticName); | |||||
ASSERT_EQ("message #1", D1.Message.Message); | |||||
ASSERT_EQ(55u, D1.Message.FileOffset); | |||||
ASSERT_EQ("path/to/source.cpp", D1.Message.FilePath); | |||||
std::vector<Replacement> Fixes1 = getFixes(D1.Fix); | |||||
ASSERT_EQ(1u, Fixes1.size()); | |||||
ASSERT_EQ("path/to/source.cpp", Fixes1[0].getFilePath()); | |||||
ASSERT_EQ(100, Fixes1[0].getOffset()); | |||||
ASSERT_EQ(12, Fixes1[0].getLength()); | |||||
ASSERT_EQ("replacement #1", Fixes1[0].getReplacementText()); | |||||
Diagnostic D2 = TUDActual.Diagnostics[1]; | |||||
ASSERT_EQ("diagnostic#2", D2.DiagnosticName); | |||||
ASSERT_EQ("message #2", D2.Message.Message); | |||||
ASSERT_EQ(60, D2.Message.FileOffset); | |||||
ASSERT_EQ("path/to/header.h", D2.Message.FilePath); | |||||
std::vector<Replacement> Fixes2 = getFixes(D2.Fix); | |||||
ASSERT_EQ(1u, Fixes2.size()); | |||||
ASSERT_EQ("path/to/header.h", Fixes2[0].getFilePath()); | |||||
ASSERT_EQ(62, Fixes2[0].getOffset()); | |||||
ASSERT_EQ(2, Fixes2[0].getLength()); | |||||
ASSERT_EQ("replacement #2", Fixes2[0].getReplacementText()); | |||||
} | |||||
No newline at end of file | |||||
alexfhUnsubmitted Not Done ReplyInline ActionsPlease add a newline. alexfh: Please add a newline. |
The assignment form of initialization is clearer for containers (and actually any type for which the initialization means "this variable is whatever is on the right side of the assignment" as opposed to "this variable is initialized by a constructor call taking these parameters").