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

0001 // // mainwindow_fields.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_lvtmdl_fieldstreemodel.h>
0021 
0022 #include <ct_lvtmdl_modelhelpers.h>
0023 
0024 #include <ct_lvtqtc_graphicsscene.h>
0025 #include <ct_lvtqtc_graphicsview.h>
0026 #include <ct_lvtqtc_lakosrelation.h>
0027 
0028 #include <ct_lvttst_fixture_qt.h>
0029 
0030 #include <ct_lvtldr_lakosiannode.h>
0031 #include <ct_lvtldr_nodestorage.h>
0032 #include <ct_lvtldr_nodestoragetestutils.h>
0033 #include <ct_lvtldr_typenode.h>
0034 
0035 #include <ct_lvtqtc_testing_utils.h>
0036 #include <ct_lvttst_tmpdir.h>
0037 
0038 #include <catch2-local-includes.h>
0039 #include <variant>
0040 
0041 #include "ct_lvtclr_colormanagement.h"
0042 
0043 // in a header
0044 Q_DECLARE_LOGGING_CATEGORY(LogTest)
0045 
0046 // in one source file
0047 Q_LOGGING_CATEGORY(LogTest, "log.test")
0048 
0049 using namespace Codethink::lvtldr;
0050 using namespace Codethink::lvtmdl;
0051 
0052 TEST_CASE("Ensure FieldTreeModel can format itself from fields in a given collection of class nodes")
0053 {
0054     // Generate empty nodeStorage
0055     auto tmpDir = TmpDir{"fields_tree_model_test"};
0056     auto dbPath = tmpDir.path() / "codedb.db";
0057     auto nodeStorage = NodeStorageTestUtils::createEmptyNodeStorage(dbPath);
0058 
0059     // Create some nodes.
0060     Codethink::lvtldr::LakosianNode *pkg = nodeStorage.addPackage("pkg", "pkg", nullptr, std::any()).value();
0061     Codethink::lvtldr::LakosianNode *pkg_cmp = nodeStorage.addComponent("cmp", "pkg/cmp", pkg).value();
0062     Codethink::lvtldr::LakosianNode *classA =
0063         nodeStorage.addLogicalEntity("classA", "pkg/cmp/classA", pkg_cmp, Codethink::lvtshr::UDTKind::Class).value();
0064     Codethink::lvtldr::LakosianNode *classB =
0065         nodeStorage.addLogicalEntity("classB", "pkg/cmp/classB", pkg_cmp, Codethink::lvtshr::UDTKind::Class).value();
0066 
0067     // Add fields to class nodes.
0068     auto classATypeNode = dynamic_cast<TypeNode *>(classA);
0069     auto classBTypeNode = dynamic_cast<TypeNode *>(classB);
0070 
0071     classATypeNode->addFieldName("field1");
0072     classATypeNode->addFieldName("field2");
0073     classATypeNode->addFieldName("field3");
0074 
0075     classBTypeNode->addFieldName("field4");
0076     classBTypeNode->addFieldName("field5");
0077     classBTypeNode->addFieldName("field6");
0078     classBTypeNode->addFieldName("field7");
0079 
0080     // Pass class nodes into a FeildTreeModel
0081     FieldsTreeModel fieldsTreeModel;
0082     LakosianNodes nodes;
0083     nodes.push_back(classA);
0084     nodes.push_back(classB);
0085     fieldsTreeModel.refreshData(nodes);
0086 
0087     // Check the FeildTreeModel has been set correctly
0088     auto getTextFrom = [&fieldsTreeModel](QModelIndex const& i) {
0089         return fieldsTreeModel.itemFromIndex(i)->text().toStdString();
0090     };
0091 
0092     REQUIRE(fieldsTreeModel.columnCount() == 1);
0093     REQUIRE(fieldsTreeModel.rowCount() == 2);
0094 
0095     auto classAIndex = fieldsTreeModel.index(0, 0);
0096     REQUIRE(getTextFrom(classAIndex) == "classA");
0097     REQUIRE(getTextFrom(fieldsTreeModel.index(0, 0, classAIndex)) == "field1");
0098     REQUIRE(getTextFrom(fieldsTreeModel.index(0, 1, classAIndex)) == "field2");
0099     REQUIRE(getTextFrom(fieldsTreeModel.index(0, 2, classAIndex)) == "field3");
0100 
0101     auto classBIndex = fieldsTreeModel.index(1, 0);
0102     REQUIRE(getTextFrom(classBIndex) == "classB");
0103     REQUIRE(getTextFrom(fieldsTreeModel.index(0, 0, classAIndex)) == "field4");
0104     REQUIRE(getTextFrom(fieldsTreeModel.index(0, 1, classAIndex)) == "field5");
0105     REQUIRE(getTextFrom(fieldsTreeModel.index(0, 2, classAIndex)) == "field6");
0106     REQUIRE(getTextFrom(fieldsTreeModel.index(0, 2, classAIndex)) == "field7");
0107 }