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

0001 // ct_lvtqtc_tool_add_package.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_lvtqtc_graphicsview.h>
0023 #include <ct_lvtqtc_testing_utils.h>
0024 #include <ct_lvtqtc_tool_add_package.h>
0025 #include <ct_lvttst_fixture_qt.h>
0026 
0027 #include <ct_lvtldr_nodestoragetestutils.h>
0028 #include <ct_lvttst_tmpdir.h>
0029 
0030 #include <QLineEdit>
0031 #include <QMouseEvent>
0032 #include <memory>
0033 
0034 using namespace Codethink::lvtqtc;
0035 using namespace Codethink::lvtldr;
0036 using namespace Codethink::lvtclr;
0037 using namespace Codethink::lvtshr;
0038 using namespace Codethink::lvtprj;
0039 using namespace Codethink::lvtldr;
0040 
0041 TEST_CASE_METHOD(QTApplicationFixture, "Add package")
0042 {
0043     auto tmpDir = TmpDir{"add_pkg_test"};
0044     auto dbPath = tmpDir.path() / "codedb.db";
0045     auto nodeStorage = NodeStorageTestUtils::createEmptyNodeStorage(dbPath);
0046 
0047     auto projectFileForTesting = ProjectFileForTesting{};
0048     auto graphicsView = GraphicsView{nodeStorage, projectFileForTesting};
0049     graphicsView.setColorManagement(std::make_shared<ColorManagement>(false));
0050     auto addPkgTool = ToolAddPackage{&graphicsView, nodeStorage};
0051 
0052     auto e = QMouseEvent{QEvent::MouseButtonPress, QPointF{0, 0}, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier};
0053 
0054     // Setup the internal dialog on the mouse event to create
0055     // a name called "pkg".
0056     // multiple uses should just change the text on nameLineEdit.
0057     auto *inputDialog = addPkgTool.inputDialog();
0058     inputDialog->overrideExec([inputDialog] {
0059         auto *nameLineEdit = inputDialog->findChild<QLineEdit *>("name");
0060         REQUIRE(nameLineEdit->text() == "");
0061         nameLineEdit->setText("pkg");
0062         return true;
0063     });
0064 
0065     REQUIRE(nodeStorage.findByQualifiedName(DiagramType::PackageType, "pkg") == nullptr);
0066     addPkgTool.mouseReleaseEvent(&e);
0067     REQUIRE(nodeStorage.findByQualifiedName(DiagramType::PackageType, "pkg") != nullptr);
0068 }
0069 
0070 TEST_CASE_METHOD(QTApplicationFixture, "Lakosian Name Verification Test")
0071 {
0072     const QString header = QObject::tr("BDE Guidelines Enforced: ");
0073 
0074     bool hasParent = false;
0075     std::string parentName;
0076     std::string name;
0077 
0078     // No name, error - package name is too short.
0079     auto result = LakosianNameRules::checkName(hasParent, name, parentName);
0080     REQUIRE(result.has_error() == true);
0081     REQUIRE(result.error().what == header + QObject::tr("top level packages must be three letters long."));
0082 
0083     // No name, error - package name is too long.
0084     name = "abcdehkjerht";
0085     result = LakosianNameRules::checkName(hasParent, name, parentName);
0086     REQUIRE(result.has_error() == true);
0087     REQUIRE(result.error().what == header + QObject::tr("top level packages must be three letters long."));
0088 
0089     // three letter name for toplevel package (without parent) == success.
0090     name = "abc";
0091     result = LakosianNameRules::checkName(hasParent, name, parentName);
0092     REQUIRE(result.has_error() == false);
0093 
0094     hasParent = true;
0095     parentName = "abc";
0096     name = "";
0097 
0098     // Full name for the package name is parentName + name. the name needs to be at least 1 letter long, no more than 5
0099     // letters.
0100     result = LakosianNameRules::checkName(hasParent, "_def", parentName);
0101     REQUIRE(result.has_error() == true);
0102     REQUIRE(result.error().what == header + QObject::tr("Package names should not contain underscore"));
0103 
0104     // Full name for the package name is parentName + name. the name needs to be at least 1 letter long, no more than 5
0105     // letters.
0106     result = LakosianNameRules::checkName(hasParent, name, parentName);
0107     REQUIRE(result.has_error() == true);
0108     REQUIRE(result.error().what
0109             == header + QObject::tr("Name too short, must be at least four letters (parent package(3) + name(1-5))"));
0110 
0111     name = "abcdefghi";
0112     result = LakosianNameRules::checkName(hasParent, name, parentName);
0113     REQUIRE(result.has_error() == true);
0114     REQUIRE(result.error().what
0115             == header + QObject::tr("Name too long, must be at most eight letters (parent package(3) + name(1-5))"));
0116 
0117     // names must always start with the parent's name. soo if I have a parent `bal`, `balb` is an accepted child.
0118     name = "defl";
0119     result = LakosianNameRules::checkName(hasParent, name, parentName);
0120     REQUIRE(result.has_error() == true);
0121     REQUIRE(result.error().what
0122             == header
0123                 + QObject::tr("the package name must start with the parent's name (%1)")
0124                       .arg(QString::fromStdString(parentName)));
0125 
0126     // package name follows the convention. full package: abc/abcde
0127     name = "abcde";
0128     result = LakosianNameRules::checkName(hasParent, name, parentName);
0129     REQUIRE(result.has_error() == false);
0130 }