1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
|
//===-- DiagnosticManagerTest.cpp -----------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
#include "lldb/Expression/DiagnosticManager.h"
#include "gtest/gtest.h"
using namespace lldb_private;
static const uint32_t custom_diag_id = 42;
namespace {
class FixItDiag : public Diagnostic {
bool m_has_fixits;
public:
FixItDiag(llvm::StringRef msg, bool has_fixits)
: Diagnostic(msg, DiagnosticSeverity::eDiagnosticSeverityError,
DiagnosticOrigin::eDiagnosticOriginLLDB, custom_diag_id),
m_has_fixits(has_fixits) {}
bool HasFixIts() const override { return m_has_fixits; }
};
} // namespace
namespace {
class TextDiag : public Diagnostic {
public:
TextDiag(llvm::StringRef msg, DiagnosticSeverity severity)
: Diagnostic(msg, severity, DiagnosticOrigin::eDiagnosticOriginLLDB,
custom_diag_id) {}
};
} // namespace
TEST(DiagnosticManagerTest, AddDiagnostic) {
DiagnosticManager mgr;
EXPECT_EQ(0U, mgr.Diagnostics().size());
std::string msg = "foo bar has happened";
DiagnosticSeverity severity = DiagnosticSeverity::eDiagnosticSeverityError;
DiagnosticOrigin origin = DiagnosticOrigin::eDiagnosticOriginLLDB;
auto diag =
std::make_unique<Diagnostic>(msg, severity, origin, custom_diag_id);
mgr.AddDiagnostic(std::move(diag));
EXPECT_EQ(1U, mgr.Diagnostics().size());
const Diagnostic *got = mgr.Diagnostics().front().get();
EXPECT_EQ(DiagnosticOrigin::eDiagnosticOriginLLDB, got->getKind());
EXPECT_EQ(msg, got->GetMessage());
EXPECT_EQ(severity, got->GetSeverity());
EXPECT_EQ(custom_diag_id, got->GetCompilerID());
EXPECT_EQ(false, got->HasFixIts());
}
TEST(DiagnosticManagerTest, HasFixits) {
DiagnosticManager mgr;
// By default we shouldn't have any fixits.
EXPECT_FALSE(mgr.HasFixIts());
// Adding a diag without fixits shouldn't make HasFixIts return true.
mgr.AddDiagnostic(std::make_unique<FixItDiag>("no fixit", false));
EXPECT_FALSE(mgr.HasFixIts());
// Adding a diag with fixits will mark the manager as containing fixits.
mgr.AddDiagnostic(std::make_unique<FixItDiag>("fixit", true));
EXPECT_TRUE(mgr.HasFixIts());
// Adding another diag without fixit shouldn't make it return false.
mgr.AddDiagnostic(std::make_unique<FixItDiag>("no fixit", false));
EXPECT_TRUE(mgr.HasFixIts());
// Adding a diag with fixits. The manager should still return true.
mgr.AddDiagnostic(std::make_unique<FixItDiag>("fixit", true));
EXPECT_TRUE(mgr.HasFixIts());
}
TEST(DiagnosticManagerTest, GetStringNoDiags) {
DiagnosticManager mgr;
EXPECT_EQ("", mgr.GetString());
}
TEST(DiagnosticManagerTest, GetStringBasic) {
DiagnosticManager mgr;
mgr.AddDiagnostic(
std::make_unique<TextDiag>("abc", eDiagnosticSeverityError));
EXPECT_EQ("error: abc\n", mgr.GetString());
}
TEST(DiagnosticManagerTest, GetStringMultiline) {
DiagnosticManager mgr;
// Multiline diagnostics should only get one severity label.
mgr.AddDiagnostic(
std::make_unique<TextDiag>("b\nc", eDiagnosticSeverityError));
EXPECT_EQ("error: b\nc\n", mgr.GetString());
}
TEST(DiagnosticManagerTest, GetStringMultipleDiags) {
DiagnosticManager mgr;
mgr.AddDiagnostic(
std::make_unique<TextDiag>("abc", eDiagnosticSeverityError));
EXPECT_EQ("error: abc\n", mgr.GetString());
mgr.AddDiagnostic(
std::make_unique<TextDiag>("def", eDiagnosticSeverityError));
EXPECT_EQ("error: abc\nerror: def\n", mgr.GetString());
}
TEST(DiagnosticManagerTest, GetStringSeverityLabels) {
DiagnosticManager mgr;
// Different severities should cause different labels.
mgr.AddDiagnostic(
std::make_unique<TextDiag>("foo", eDiagnosticSeverityError));
mgr.AddDiagnostic(
std::make_unique<TextDiag>("bar", eDiagnosticSeverityWarning));
// Remarks have no labels.
mgr.AddDiagnostic(
std::make_unique<TextDiag>("baz", eDiagnosticSeverityRemark));
EXPECT_EQ("error: foo\nwarning: bar\nbaz\n", mgr.GetString());
}
TEST(DiagnosticManagerTest, GetStringPreserveOrder) {
DiagnosticManager mgr;
// Make sure we preserve the diagnostic order and do not sort them in any way.
mgr.AddDiagnostic(
std::make_unique<TextDiag>("baz", eDiagnosticSeverityRemark));
mgr.AddDiagnostic(
std::make_unique<TextDiag>("bar", eDiagnosticSeverityWarning));
mgr.AddDiagnostic(
std::make_unique<TextDiag>("foo", eDiagnosticSeverityError));
EXPECT_EQ("baz\nwarning: bar\nerror: foo\n", mgr.GetString());
}
TEST(DiagnosticManagerTest, AppendMessageNoDiag) {
DiagnosticManager mgr;
// FIXME: This *really* should not just fail silently.
mgr.AppendMessageToDiagnostic("no diag has been pushed yet");
EXPECT_EQ(0U, mgr.Diagnostics().size());
}
TEST(DiagnosticManagerTest, AppendMessageAttachToLastDiag) {
DiagnosticManager mgr;
mgr.AddDiagnostic(
std::make_unique<TextDiag>("foo", eDiagnosticSeverityError));
mgr.AddDiagnostic(
std::make_unique<TextDiag>("bar", eDiagnosticSeverityError));
// This should append to 'bar' and not to 'foo'.
mgr.AppendMessageToDiagnostic("message text");
EXPECT_EQ("error: foo\nerror: bar\nmessage text\n", mgr.GetString());
}
TEST(DiagnosticManagerTest, AppendMessageSubsequentDiags) {
DiagnosticManager mgr;
mgr.AddDiagnostic(
std::make_unique<TextDiag>("bar", eDiagnosticSeverityError));
mgr.AppendMessageToDiagnostic("message text");
// Pushing another diag after the message should work fine.
mgr.AddDiagnostic(
std::make_unique<TextDiag>("foo", eDiagnosticSeverityError));
EXPECT_EQ("error: bar\nmessage text\nerror: foo\n", mgr.GetString());
}
TEST(DiagnosticManagerTest, PutString) {
DiagnosticManager mgr;
mgr.PutString(eDiagnosticSeverityError, "foo");
EXPECT_EQ(1U, mgr.Diagnostics().size());
EXPECT_EQ(eDiagnosticOriginLLDB, mgr.Diagnostics().front()->getKind());
EXPECT_EQ("error: foo\n", mgr.GetString());
}
TEST(DiagnosticManagerTest, PutStringMultiple) {
DiagnosticManager mgr;
// Multiple PutString should behave like multiple diagnostics.
mgr.PutString(eDiagnosticSeverityError, "foo");
mgr.PutString(eDiagnosticSeverityError, "bar");
EXPECT_EQ(2U, mgr.Diagnostics().size());
EXPECT_EQ("error: foo\nerror: bar\n", mgr.GetString());
}
TEST(DiagnosticManagerTest, PutStringSeverities) {
DiagnosticManager mgr;
// Multiple PutString with different severities should behave like we
// created multiple diagnostics.
mgr.PutString(eDiagnosticSeverityError, "foo");
mgr.PutString(eDiagnosticSeverityWarning, "bar");
EXPECT_EQ(2U, mgr.Diagnostics().size());
EXPECT_EQ("error: foo\nwarning: bar\n", mgr.GetString());
}
TEST(DiagnosticManagerTest, FixedExpression) {
DiagnosticManager mgr;
// By default there should be no fixed expression.
EXPECT_EQ("", mgr.GetFixedExpression());
// Setting the fixed expression should change it.
mgr.SetFixedExpression("foo");
EXPECT_EQ("foo", mgr.GetFixedExpression());
// Setting the fixed expression again should also change it.
mgr.SetFixedExpression("bar");
EXPECT_EQ("bar", mgr.GetFixedExpression());
}
|