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

0001 // ct_lvtmdl_modelhelpers.t.cpp                                        -*-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 #include <ct_lvtldr_lakosiannode.h>
0021 #include <ct_lvtldr_nodestorage.h>
0022 #include <ct_lvtmdl_modelhelpers.h>
0023 #include <ct_lvtshr_graphenums.h>
0024 
0025 #include <catch2-local-includes.h>
0026 #include <ct_lvtldr_nodestoragetestutils.h>
0027 #include <ct_lvttst_tmpdir.h>
0028 
0029 using namespace Codethink::lvtmdl;
0030 using namespace Codethink::lvtldr;
0031 
0032 TEST_CASE("Model helpers test")
0033 {
0034     auto tmpDir = TmpDir{"model_helpers_test"};
0035     auto dbPath = tmpDir.path() / "codedb.db";
0036     auto ns = NodeStorageTestUtils::createEmptyNodeStorage(dbPath);
0037 
0038     Codethink::lvtldr::LakosianNode *tmz = ns.addPackage("tmz", "tmz", nullptr, std::any()).value();
0039     Codethink::lvtldr::LakosianNode *tmz_a = ns.addPackage("tmz_a", "tmz_a", tmz, std::any()).value();
0040     Codethink::lvtldr::LakosianNode *tmp_a_cmp = ns.addComponent("cmp", "tmz/tmz_a/cmp", tmz_a).value();
0041     Codethink::lvtldr::LakosianNode *cmp_klass =
0042         ns.addLogicalEntity("klass", "tmz/tmz_a/cmp/klass", tmp_a_cmp, Codethink::lvtshr::UDTKind::Class).value();
0043 
0044     {
0045         auto *item = ModelUtil::createTreeItemFromLakosianNode(*tmz);
0046         REQUIRE(item->text().toStdString() == tmz->name());
0047         REQUIRE(item->data(ModelRoles::e_Id) == tmz->id());
0048         REQUIRE(item->data(ModelRoles::e_QualifiedName).toString().toStdString() == tmz->qualifiedName());
0049         REQUIRE(item->data(ModelRoles::e_IsBranch) == true);
0050         REQUIRE(item->data(ModelRoles::e_NodeType) == NodeType::Enum::e_Package);
0051         REQUIRE(item->data(ModelRoles::e_RecursiveLakosian) == false);
0052         // If no information is provided, item will not load children, but will have 1 dummy child
0053         REQUIRE(item->data(ModelRoles::e_ChildItemsLoaded) == false);
0054         REQUIRE(item->rowCount() == 1);
0055         auto *dummy_child = item->child(0);
0056         REQUIRE(dummy_child->text().isEmpty());
0057     }
0058 
0059     auto *item = ModelUtil::createTreeItemFromLakosianNode(*tmz, [](LakosianNode const&) {
0060         return true;
0061     });
0062     REQUIRE(item->text().toStdString() == tmz->name());
0063     REQUIRE(item->data(ModelRoles::e_Id) == tmz->id());
0064     REQUIRE(item->data(ModelRoles::e_QualifiedName).toString().toStdString() == tmz->qualifiedName());
0065     REQUIRE(item->data(ModelRoles::e_IsBranch) == true);
0066     REQUIRE(item->data(ModelRoles::e_NodeType) == NodeType::Enum::e_Package);
0067     REQUIRE(item->data(ModelRoles::e_RecursiveLakosian) == false);
0068     REQUIRE(item->data(ModelRoles::e_ChildItemsLoaded) == true);
0069     REQUIRE(item->rowCount() == 1);
0070 
0071     auto *tmz_a_item = item->child(0);
0072     REQUIRE(tmz_a_item->text().toStdString() == tmz_a->name());
0073     REQUIRE(tmz_a_item->data(ModelRoles::e_Id) == tmz_a->id());
0074     REQUIRE(tmz_a_item->data(ModelRoles::e_QualifiedName).toString().toStdString() == tmz_a->qualifiedName());
0075     REQUIRE(tmz_a_item->data(ModelRoles::e_IsBranch) == true);
0076     REQUIRE(tmz_a_item->data(ModelRoles::e_NodeType) == NodeType::Enum::e_Package);
0077     REQUIRE(tmz_a_item->data(ModelRoles::e_RecursiveLakosian) == false);
0078     REQUIRE(tmz_a_item->rowCount() == 1);
0079 
0080     auto *cmp_item = tmz_a_item->child(0);
0081     REQUIRE(cmp_item->text().toStdString() == tmp_a_cmp->name());
0082     REQUIRE(cmp_item->data(ModelRoles::e_Id) == tmp_a_cmp->id());
0083     REQUIRE(cmp_item->data(ModelRoles::e_QualifiedName).toString().toStdString() == tmp_a_cmp->qualifiedName());
0084     REQUIRE(cmp_item->data(ModelRoles::e_IsBranch) == true);
0085     REQUIRE(cmp_item->data(ModelRoles::e_NodeType) == NodeType::Enum::e_Component);
0086     REQUIRE(cmp_item->data(ModelRoles::e_RecursiveLakosian) == false);
0087     REQUIRE(cmp_item->rowCount() == 1);
0088 
0089     auto *cmp_klass_item = cmp_item->child(0);
0090     REQUIRE(cmp_klass_item->text().toStdString() == cmp_klass->name());
0091     REQUIRE(cmp_klass_item->data(ModelRoles::e_Id) == cmp_klass->id());
0092     REQUIRE(cmp_klass_item->data(ModelRoles::e_QualifiedName).toString().toStdString() == cmp_klass->qualifiedName());
0093     REQUIRE(cmp_klass_item->data(ModelRoles::e_IsBranch) == false);
0094     REQUIRE(cmp_klass_item->data(ModelRoles::e_NodeType) == NodeType::Enum::e_Class);
0095     REQUIRE(cmp_klass_item->data(ModelRoles::e_RecursiveLakosian) == true);
0096     REQUIRE(cmp_klass_item->rowCount() == 0);
0097 }