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

0001 // ct_lvtqtc_tool_add_logical_entity.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 #include <catch2-local-includes.h>
0020 
0021 #include <ct_lvtclr_colormanagement.h>
0022 #include <ct_lvtldr_nodestoragetestutils.h>
0023 #include <ct_lvtqtc_graphicsview.h>
0024 #include <ct_lvtqtc_lakosentity.h>
0025 #include <ct_lvtqtc_logicalentity.h>
0026 #include <ct_lvtqtc_testing_utils.h>
0027 #include <ct_lvtqtc_tool_add_logical_entity.h>
0028 #include <ct_lvttst_fixture_qt.h>
0029 #include <ct_lvttst_tmpdir.h>
0030 
0031 #include <QComboBox>
0032 #include <QLineEdit>
0033 #include <QMouseEvent>
0034 #include <memory>
0035 
0036 using namespace Codethink::lvtqtc;
0037 using namespace Codethink::lvtldr;
0038 using namespace Codethink::lvtclr;
0039 using namespace Codethink::lvtshr;
0040 using namespace Codethink::lvtprj;
0041 
0042 TEST_CASE_METHOD(QTApplicationFixture, "Add logical entity")
0043 {
0044     auto tmpDir = TmpDir{"add_log_entity"};
0045     auto dbPath = tmpDir.path() / "codedb.db";
0046     auto nodeStorage = NodeStorageTestUtils::createEmptyNodeStorage(dbPath);
0047 
0048     auto projectFileForTesting = ProjectFileForTesting{};
0049     auto graphicsView = GraphicsView{nodeStorage, projectFileForTesting};
0050     graphicsView.setColorManagement(std::make_shared<ColorManagement>(false));
0051     auto *pkg = nodeStorage.addPackage("pkg", "pkg", nullptr).value();
0052     nodeStorage.addComponent("pkgcmp", "pkg/pkgcmp", pkg).expect("Unexpected error trying to create component");
0053 
0054     auto findItemOnGV = [&](const std::string& name) {
0055         auto allItems = graphicsView.allItemsByType<LakosEntity>();
0056         auto *item = (*std::find_if(allItems.begin(), allItems.end(), [&name](auto *e) {
0057             return e->name() == name;
0058         }));
0059         assert(item);
0060         return item;
0061     };
0062 
0063     auto createUDTUsingTool = [&](LakosEntity *parent, const std::string& name, const std::string& kind) {
0064         auto tool = ToolAddLogicalEntity{&graphicsView, nodeStorage};
0065         auto *inputDialog = tool.inputDialog();
0066         inputDialog->overrideExec([inputDialog, name, kind] {
0067             inputDialog->findChild<QLineEdit *>("name")->setText(QString::fromStdString(name));
0068             inputDialog->findChild<QComboBox *>("kind")->setCurrentText(QString::fromStdString(kind));
0069             return true;
0070         });
0071 
0072         auto pos = graphicsView.mapFromScene(parent->getTextPos());
0073         auto e = QMouseEvent{QEvent::MouseButtonPress, pos, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier};
0074         tool.mousePressEvent(&e);
0075     };
0076 
0077     auto *pkgcmp = findItemOnGV("pkgcmp");
0078     REQUIRE(nodeStorage.findByQualifiedName(DiagramType::ClassType, "pkgcmp::Klass") == nullptr);
0079     REQUIRE(graphicsView.allItemsByType<LogicalEntity>().empty());
0080     REQUIRE(pkgcmp->lakosEntities().empty());
0081     createUDTUsingTool(pkgcmp, "Klass", "Class");
0082     REQUIRE(pkgcmp->lakosEntities().size() == 1);
0083     REQUIRE(nodeStorage.findByQualifiedName(DiagramType::ClassType, "pkgcmp::Klass"));
0084     REQUIRE(graphicsView.allItemsByType<LogicalEntity>().size() == 1);
0085 
0086     auto *klass = findItemOnGV("Klass");
0087     REQUIRE(klass->lakosEntities().empty());
0088     createUDTUsingTool(findItemOnGV("Klass"), "InnerKlass", "Class");
0089     REQUIRE(nodeStorage.findByQualifiedName(DiagramType::ClassType, "Klass::InnerKlass"));
0090     REQUIRE(graphicsView.allItemsByType<LogicalEntity>().size() == 2);
0091     REQUIRE(klass->lakosEntities().size() == 1);
0092 }