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