|
| 1 | +/* |
| 2 | + * Cppcheck - A tool for static C/C++ code analysis |
| 3 | + * Copyright (C) 2007-2025 Cppcheck team. |
| 4 | + * |
| 5 | + * This program is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU General Public License as published by |
| 7 | + * the Free Software Foundation, either version 3 of the License, or |
| 8 | + * (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License |
| 16 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | + */ |
| 18 | + |
| 19 | +#include "executor.h" |
| 20 | +#include "filesettings.h" |
| 21 | +#include "fixture.h" |
| 22 | +#include "suppressions.h" |
| 23 | + |
| 24 | +#include <stdexcept> |
| 25 | + |
| 26 | +class DummyExecutor : public Executor |
| 27 | +{ |
| 28 | +public: |
| 29 | + DummyExecutor(const std::list<FileWithDetails> &files, const std::list<FileSettings>& fileSettings, const Settings &settings, Suppressions &suppressions, ErrorLogger &errorLogger) |
| 30 | + : Executor(files, fileSettings, settings, suppressions, errorLogger) |
| 31 | + {} |
| 32 | + |
| 33 | + unsigned int check() override |
| 34 | + { |
| 35 | + throw std::runtime_error("not implemented"); |
| 36 | + } |
| 37 | + |
| 38 | + bool hasToLog_(const ErrorMessage &msg) |
| 39 | + { |
| 40 | + return hasToLog(msg); |
| 41 | + } |
| 42 | +}; |
| 43 | + |
| 44 | +class TestExecutor : public TestFixture { |
| 45 | +public: |
| 46 | + TestExecutor() : TestFixture("TestExecutor") {} |
| 47 | + |
| 48 | +private: |
| 49 | + void run() override { |
| 50 | + TEST_CASE(hasToLogDefault); |
| 51 | + TEST_CASE(hasToLogSimple); |
| 52 | + } |
| 53 | + |
| 54 | + void hasToLogDefault() { |
| 55 | + const std::list<FileWithDetails> files{FileWithDetails{"test.c"}}; |
| 56 | + const std::list<FileSettings> fileSettings; |
| 57 | + Suppressions supprs; |
| 58 | + DummyExecutor executor(files, fileSettings, settingsDefault, supprs, *this); |
| 59 | + |
| 60 | + ErrorMessage::FileLocation loc1("test.c", 1, 2); |
| 61 | + ErrorMessage msg({std::move(loc1)}, "test.c", Severity::error, "error", "id", Certainty::normal); |
| 62 | + |
| 63 | + ASSERT(executor.hasToLog_(msg)); |
| 64 | + ASSERT(!executor.hasToLog_(msg)); |
| 65 | + |
| 66 | + ErrorMessage::FileLocation loc2("test.c", 1, 12); |
| 67 | + msg.callStack = {std::move(loc2)}; |
| 68 | + |
| 69 | + // TODO: the default message does not include the column |
| 70 | + TODO_ASSERT(executor.hasToLog_(msg)); |
| 71 | + |
| 72 | + msg.id = "id2"; |
| 73 | + |
| 74 | + // TODO: the default message does not include the id |
| 75 | + TODO_ASSERT(executor.hasToLog_(msg)); |
| 76 | + } |
| 77 | + |
| 78 | + void hasToLogSimple() { |
| 79 | + const std::list<FileWithDetails> files{FileWithDetails{"test.c"}}; |
| 80 | + const std::list<FileSettings> fileSettings; |
| 81 | + Settings settings; |
| 82 | + // this is the "simple" format |
| 83 | + settings.templateFormat = "{file}:{line}:{column}: {severity}:{inconclusive:inconclusive:} {message} [{id}]"; |
| 84 | + Suppressions supprs; |
| 85 | + DummyExecutor executor(files, fileSettings, settings, supprs, *this); |
| 86 | + |
| 87 | + ErrorMessage::FileLocation loc1("test.c", 1, 2); |
| 88 | + ErrorMessage msg({std::move(loc1)}, "test.c", Severity::error, "error", "id", Certainty::normal); |
| 89 | + |
| 90 | + ASSERT(executor.hasToLog_(msg)); |
| 91 | + ASSERT(!executor.hasToLog_(msg)); |
| 92 | + |
| 93 | + ErrorMessage::FileLocation loc2("test.c", 1, 12); |
| 94 | + msg.callStack = {std::move(loc2)}; |
| 95 | + |
| 96 | + ASSERT(executor.hasToLog_(msg)); |
| 97 | + |
| 98 | + msg.id = "id2"; |
| 99 | + |
| 100 | + ASSERT(executor.hasToLog_(msg)); |
| 101 | + } |
| 102 | +}; |
| 103 | + |
| 104 | +REGISTER_TEST(TestExecutor) |
0 commit comments