File indexing completed on 2024-05-19 05:41:58

0001 // ct_lvtcgn_testutils.h                                              -*-C++-*-
0002 
0003 /*
0004 // Copyright 2023 Codethink Ltd <codethink@codethink.co.uk>
0005 // SPDX-License-Identifier: Apache-2.0
0006 //
0007 // Licensed under the Apache License, Version 2.0 (the "License");
0008 // you may not use this file except in compliance with the License.
0009 // You may obtain a copy of the License at
0010 //
0011 //     http://www.apache.org/licenses/LICENSE-2.0
0012 //
0013 // Unless required by applicable law or agreed to in writing, software
0014 // distributed under the License is distributed on an "AS IS" BASIS,
0015 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0016 // See the License for the specific language governing permissions and
0017 // limitations under the License.
0018 */
0019 
0020 #ifndef CT_LVTCGN_TESTUTILS_H_INCLUDED
0021 #define CT_LVTCGN_TESTUTILS_H_INCLUDED
0022 
0023 #include <memory>
0024 #include <unordered_map>
0025 #include <utility>
0026 
0027 using namespace Codethink::lvtcgn::mdl;
0028 
0029 struct FakeEntity {
0030     std::string name;
0031     std::string type;
0032     bool selected;
0033     std::vector<std::reference_wrapper<FakeEntity>> children;
0034     std::vector<std::reference_wrapper<FakeEntity>> fwdDeps;
0035     std::optional<std::reference_wrapper<FakeEntity>> parent;
0036 
0037     FakeEntity(std::string name, std::string type, bool selected):
0038         name(std::move(name)), type(std::move(type)), selected(selected), parent(std::nullopt)
0039     {
0040     }
0041 
0042     void addChild(FakeEntity& child)
0043     {
0044         child.parent = *this;
0045         children.emplace_back(child);
0046     }
0047 
0048     void addFwdDep(FakeEntity& other)
0049     {
0050         fwdDeps.emplace_back(other);
0051     }
0052 };
0053 
0054 class FakeContentProvider : public ICodeGenerationDataProvider {
0055   public:
0056     FakeContentProvider();
0057     virtual ~FakeContentProvider() = default;
0058 
0059     [[nodiscard]] std::vector<std::reference_wrapper<IPhysicalEntityInfo>> topLevelEntities() override
0060     {
0061         return rootEntities;
0062     }
0063 
0064     [[nodiscard]] int numberOfPhysicalEntities() const override
0065     {
0066         return data.size();
0067     }
0068 
0069     [[nodiscard]] IPhysicalEntityInfo& getInfoFor(FakeEntity *entity)
0070     {
0071         return *infos[entity];
0072     }
0073 
0074   private:
0075     std::vector<std::reference_wrapper<IPhysicalEntityInfo>> rootEntities;
0076     std::vector<std::unique_ptr<FakeEntity>> data;
0077     std::unordered_map<FakeEntity *, std::unique_ptr<IPhysicalEntityInfo>> infos;
0078 };
0079 
0080 class FakeEntityInfo : public IPhysicalEntityInfo {
0081   public:
0082     explicit FakeEntityInfo(FakeEntity& entity, FakeContentProvider& provider): d_entity(entity), d_provider(provider)
0083     {
0084     }
0085 
0086     ~FakeEntityInfo() override = default;
0087 
0088     [[nodiscard]] std::string name() const override
0089     {
0090         return d_entity.name;
0091     }
0092 
0093     [[nodiscard]] std::string type() const override
0094     {
0095         return d_entity.type;
0096     }
0097 
0098     [[nodiscard]] std::optional<std::reference_wrapper<IPhysicalEntityInfo>> parent() const override
0099     {
0100         if (!d_entity.parent.has_value()) {
0101             return std::nullopt;
0102         }
0103         return d_provider.getInfoFor(&d_entity.parent.value().get());
0104     }
0105 
0106     [[nodiscard]] std::vector<std::reference_wrapper<IPhysicalEntityInfo>> children() const override
0107     {
0108         std::vector<std::reference_wrapper<IPhysicalEntityInfo>> children;
0109         for (auto const& e : d_entity.children) {
0110             children.emplace_back(d_provider.getInfoFor(&(e.get())));
0111         }
0112         return children;
0113     }
0114 
0115     [[nodiscard]] std::vector<std::reference_wrapper<IPhysicalEntityInfo>> fwdDependencies() const override
0116     {
0117         std::vector<std::reference_wrapper<IPhysicalEntityInfo>> fwdDeps;
0118         for (auto const& e : d_entity.fwdDeps) {
0119             fwdDeps.emplace_back(d_provider.getInfoFor(&(e.get())));
0120         }
0121         return fwdDeps;
0122     }
0123 
0124     [[nodiscard]] bool selectedForCodeGeneration() const override
0125     {
0126         return d_entity.selected;
0127     }
0128 
0129     void setSelectedForCodeGeneration(bool value) override
0130     {
0131         d_entity.selected = value;
0132     }
0133 
0134   private:
0135     FakeEntity& d_entity;
0136     FakeContentProvider& d_provider;
0137 };
0138 
0139 FakeContentProvider::FakeContentProvider()
0140 {
0141     auto somepkg_a = std::make_unique<FakeEntity>("somepkg_a", "Package", true);
0142     auto somepkg_b = std::make_unique<FakeEntity>("somepkg_b", "Package", false);
0143     auto somepkg_c = std::make_unique<FakeEntity>("somepkg_c", "Package", true);
0144     auto component_a = std::make_unique<FakeEntity>("component_a", "Component", false);
0145     auto component_b = std::make_unique<FakeEntity>("component_b", "Component", true);
0146     component_b->addFwdDep(*component_a);
0147     somepkg_c->addChild(*component_a);
0148     somepkg_c->addChild(*component_b);
0149 
0150     infos[somepkg_a.get()] = std::make_unique<FakeEntityInfo>(*somepkg_a, *this);
0151     infos[somepkg_b.get()] = std::make_unique<FakeEntityInfo>(*somepkg_b, *this);
0152     infos[somepkg_c.get()] = std::make_unique<FakeEntityInfo>(*somepkg_c, *this);
0153     infos[component_a.get()] = std::make_unique<FakeEntityInfo>(*component_a, *this);
0154     infos[component_b.get()] = std::make_unique<FakeEntityInfo>(*component_b, *this);
0155     rootEntities.emplace_back(*infos[somepkg_a.get()]);
0156     rootEntities.emplace_back(*infos[somepkg_b.get()]);
0157     rootEntities.emplace_back(*infos[somepkg_c.get()]);
0158     data.emplace_back(std::move(somepkg_a));
0159     data.emplace_back(std::move(somepkg_b));
0160     data.emplace_back(std::move(somepkg_c));
0161     data.emplace_back(std::move(component_a));
0162     data.emplace_back(std::move(component_b));
0163 }
0164 
0165 #endif