| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | // Copyright 2019 Juan Palacios <[email protected]> |
| 3 | |
| 4 | #include <catch2/catch_all.hpp> |
| 5 | #include <catch2/trompeloeil.hpp> |
| 6 | |
| 7 | #include "common/commandqueuestub.h" |
| 8 | #include "core/info/iswinfo.h" |
| 9 | #include "core/isyscomponent.h" |
| 10 | #include "core/sysmodel.h" |
| 11 | |
| 12 | extern template struct trompeloeil::reporter<trompeloeil::specialized>; |
| 13 | |
| 14 | namespace Tests::SysModel { |
| 15 | |
| 16 | class SWInfoStub : public ISWInfo |
| 17 | { |
| 18 | public: |
| 19 | std::string info(std::string_view) const override |
| 20 | { |
| 21 | return "sw_info"; |
| 22 | } |
| 23 | |
| 24 | std::vector<std::string> keys() const override |
| 25 | { |
| 26 | return {"sw_info_key"}; |
| 27 | } |
| 28 | |
| 29 | void initialize(std::vector<std::unique_ptr<ISWInfo::IProvider>> const &) override |
| 30 | { |
| 31 | } |
| 32 | }; |
| 33 | |
| 34 | class SysComponentMock : public ISysComponent |
| 35 | { |
| 36 | public: |
| 37 | MAKE_MOCK0(init, void(), override); |
| 38 | MAKE_MOCK1(preInit, void(ICommandQueue &), override); |
| 39 | MAKE_MOCK1(postInit, void(ICommandQueue &), override); |
| 40 | |
| 41 | MAKE_CONST_MOCK0(active, bool(), override); |
| 42 | MAKE_MOCK1(activate, void(bool), override); |
| 43 | MAKE_CONST_MOCK0(key, std::string const &(), override); |
| 44 | |
| 45 | // trompeloeil fails to mock this method |
| 46 | std::pair<std::string, std::vector<std::pair<std::string, std::string>>> |
| 47 | componentInfo() const override |
| 48 | { |
| 49 | std::vector<std::pair<std::string, std::string>> cInfo; |
| 50 | cInfo.emplace_back("c_info_key", "c_info"); |
| 51 | return {"component", std::move(cInfo)}; |
| 52 | } |
| 53 | |
| 54 | MAKE_MOCK1(sync, void(ICommandQueue &), override); |
| 55 | MAKE_MOCK1(updateSensors, |
| 56 | void(std::unordered_map<std::string, |
| 57 | std::unordered_set<std::string>> const &), |
| 58 | override); |
| 59 | MAKE_CONST_MOCK0(ID, std::string const &(), override); |
| 60 | MAKE_MOCK1(importWith, void(Importable::Importer &), override); |
| 61 | MAKE_CONST_MOCK1(exportWith, void(Exportable::Exporter &), override); |
| 62 | }; |
| 63 | |
| 64 | class SysModelImporterStub : public ISysModel::Importer |
| 65 | { |
| 66 | public: |
| 67 | std::optional<std::reference_wrapper<Importable::Importer>> |
| 68 | provideImporter(Item const &) override |
| 69 | { |
| 70 | return *this; |
| 71 | } |
| 72 | }; |
| 73 | |
| 74 | class SysModelExporterStub : public ISysModel::Exporter |
| 75 | { |
| 76 | public: |
| 77 | std::optional<std::reference_wrapper<Exportable::Exporter>> |
| 78 | provideExporter(Item const &) override |
| 79 | { |
| 80 | return *this; |
| 81 | } |
| 82 | }; |
| 83 | |
| 84 | TEST_CASE("SysModel tests", "[SysModel]") |
| 85 | { |
| 86 | CommandQueueStub ctlCmds; |
| 87 | |
| 88 | auto info = std::make_unique<SWInfoStub>(); |
| 89 | |
| 90 | std::vector<std::unique_ptr<ISysComponent>> components; |
| 91 | components.emplace_back(std::make_unique<SysComponentMock>()); |
| 92 | auto &componentMock = static_cast<SysComponentMock &>(*components[0]); |
| 93 | |
| 94 | ::SysModel ts(std::move(info), std::move(components)); |
| 95 | |
| 96 | SECTION("Has ISysModel ID") |
| 97 | { |
| 98 | REQUIRE(ts.ID() == ISysModel::ItemID); |
| 99 | } |
| 100 | |
| 101 | SECTION("Pre-init components") |
| 102 | { |
| 103 | REQUIRE_CALL(componentMock, preInit(trompeloeil::_)); |
| 104 | ts.preInit(ctlCmds); |
| 105 | } |
| 106 | |
| 107 | SECTION("Init components") |
| 108 | { |
| 109 | REQUIRE_CALL(componentMock, init()); |
| 110 | ts.init(); |
| 111 | } |
| 112 | |
| 113 | SECTION("Post-init components") |
| 114 | { |
| 115 | REQUIRE_CALL(componentMock, postInit(trompeloeil::_)); |
| 116 | ts.postInit(ctlCmds); |
| 117 | } |
| 118 | |
| 119 | SECTION("Update component's sensors") |
| 120 | { |
| 121 | std::unordered_map<std::string, std::unordered_set<std::string>> ignored; |
| 122 | REQUIRE_CALL(componentMock, updateSensors(trompeloeil::_)) |
| 123 | .LR_WITH(&_1 == &ignored); |
| 124 | ts.updateSensors(ignored); |
| 125 | } |
| 126 | |
| 127 | SECTION("Sync components") |
| 128 | { |
| 129 | REQUIRE_CALL(componentMock, sync(trompeloeil::_)); |
| 130 | ts.sync(ctlCmds); |
| 131 | } |
| 132 | |
| 133 | SECTION("System info can be retrieved") |
| 134 | { |
| 135 | auto sysInfo = ts.info(); |
| 136 | |
| 137 | REQUIRE_FALSE(sysInfo.empty()); |
| 138 | |
| 139 | SECTION("Software info is always present") |
| 140 | { |
| 141 | std::vector<std::pair<std::string, std::string>> swInfoData; |
| 142 | swInfoData.emplace_back("sw_info_key", "sw_info"); |
| 143 | auto swInfo = std::make_pair(std::string("Software"), |
| 144 | std::move(swInfoData)); |
| 145 | |
| 146 | REQUIRE_THAT(sysInfo, Catch::Matchers::VectorContains(swInfo)); |
| 147 | } |
| 148 | |
| 149 | SECTION("Has components info") |
| 150 | { |
| 151 | auto compInfo = componentMock.componentInfo(); |
| 152 | |
| 153 | REQUIRE_THAT(sysInfo, Catch::Matchers::VectorContains(compInfo)); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | SECTION("Imports its components") |
| 158 | { |
| 159 | REQUIRE_CALL(componentMock, importWith(trompeloeil::_)); |
| 160 | |
| 161 | SysModelImporterStub i; |
| 162 | ts.importWith(i); |
| 163 | } |
| 164 | |
| 165 | SECTION("Exports its components") |
| 166 | { |
| 167 | REQUIRE_CALL(componentMock, exportWith(trompeloeil::_)); |
| 168 | |
| 169 | SysModelExporterStub e; |
| 170 | ts.exportWith(e); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | } // namespace Tests::SysModel |