File indexing completed on 2024-05-19 05:42:21

0001 // ct_lvtqtc_nodestorage_testing_helpers.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 INCLUDED_CT_LVTQTC_NS_TESTING_HELPERS_H
0021 #define INCLUDED_CT_LVTQTC_NS_TESTING_HELPERS_H
0022 
0023 #include <ct_lvtldr_nodestorage.h>
0024 #include <ct_lvtqtc_componententity.h>
0025 #include <ct_lvtqtc_packagedependency.h>
0026 #include <ct_lvtqtc_packageentity.h>
0027 #include <ct_lvtshr_loaderinfo.h>
0028 
0029 #include <catch2-local-includes.h>
0030 
0031 #include <optional>
0032 #include <string>
0033 
0034 struct ScopedPackageEntity {
0035     ScopedPackageEntity(Codethink::lvtldr::NodeStorage& ns,
0036                         std::string const& name,
0037                         std::optional<std::reference_wrapper<Codethink::lvtqtc::PackageEntity>> parent = std::nullopt):
0038         d_ns(ns)
0039     {
0040         auto result = ns.addPackage(name, name, parent ? parent.value().get().internalNode() : nullptr);
0041         if (result.has_error()) {
0042             FAIL("Error adding package: code " + std::to_string(static_cast<int>(result.error().kind)));
0043         }
0044         auto *pkg = result.value();
0045         d_value = std::make_unique<Codethink::lvtqtc::PackageEntity>(pkg, Codethink::lvtshr::LoaderInfo{});
0046     }
0047 
0048     ~ScopedPackageEntity()
0049     {
0050         auto *internalNode = d_value->internalNode();
0051         d_value.reset();
0052         auto result = d_ns.removePackage(internalNode);
0053         if (result.has_error()) {
0054             FAIL("Error removing package: code " + std::to_string(static_cast<int>(result.error().kind)));
0055         }
0056     }
0057 
0058     inline Codethink::lvtqtc::PackageEntity& value()
0059     {
0060         return *d_value;
0061     }
0062 
0063   private:
0064     Codethink::lvtldr::NodeStorage& d_ns;
0065     std::unique_ptr<Codethink::lvtqtc::PackageEntity> d_value;
0066 };
0067 
0068 struct ScopedComponentEntity {
0069     ScopedComponentEntity(Codethink::lvtldr::NodeStorage& ns,
0070                           std::string const& name,
0071                           Codethink::lvtqtc::PackageEntity& parent):
0072         d_ns(ns)
0073     {
0074         auto result = ns.addComponent(name, name, parent.internalNode());
0075         if (result.has_error()) {
0076             FAIL("Error adding component: code " + std::to_string(static_cast<int>(result.error().kind)));
0077         }
0078         auto *component = result.value();
0079         d_value = std::make_unique<Codethink::lvtqtc::ComponentEntity>(component, Codethink::lvtshr::LoaderInfo{});
0080     }
0081 
0082     ~ScopedComponentEntity()
0083     {
0084         auto *internalNode = d_value->internalNode();
0085         d_value.reset();
0086         auto result = d_ns.removeComponent(internalNode);
0087         if (result.has_error()) {
0088             FAIL("Error removing component: code " + std::to_string(static_cast<int>(result.error().kind)));
0089         }
0090     }
0091 
0092     inline Codethink::lvtqtc::ComponentEntity& value()
0093     {
0094         return *d_value;
0095     }
0096 
0097   private:
0098     Codethink::lvtldr::NodeStorage& d_ns;
0099     std::unique_ptr<Codethink::lvtqtc::ComponentEntity> d_value;
0100 };
0101 
0102 struct ScopedPackageDependency {
0103     ScopedPackageDependency(Codethink::lvtldr::NodeStorage& ns,
0104                             Codethink::lvtqtc::LakosEntity& source,
0105                             Codethink::lvtqtc::LakosEntity& target):
0106         d_ns(ns)
0107     {
0108         auto result = ns.addPhysicalDependency(source.internalNode(), target.internalNode());
0109         if (result.has_error()) {
0110             FAIL("Error adding dependency: code " + std::to_string(static_cast<int>(result.error().kind)));
0111         }
0112         d_value = std::make_unique<Codethink::lvtqtc::PackageDependency>(&source, &target);
0113     }
0114 
0115     ~ScopedPackageDependency()
0116     {
0117         d_ns.removePhysicalDependency(d_value->from()->internalNode(), d_value->to()->internalNode())
0118             .expect("Unexpected error removing physical dependency");
0119     }
0120 
0121     inline Codethink::lvtqtc::PackageDependency& value()
0122     {
0123         return *d_value;
0124     }
0125 
0126   private:
0127     Codethink::lvtldr::NodeStorage& d_ns;
0128     std::unique_ptr<Codethink::lvtqtc::PackageDependency> d_value;
0129 };
0130 
0131 #endif