diff --git a/unittests/CMakeLists.txt b/unittests/CMakeLists.txt index cac9fe026..31819d11f 100644 --- a/unittests/CMakeLists.txt +++ b/unittests/CMakeLists.txt @@ -1,165 +1,172 @@ set(EXECUTABLE_OUTPUT_PATH ${CMAKE_CURRENT_BINARY_DIR}) set(SRC_PATH ../umbrello) include_directories( ${LIBXML2_INCLUDE_DIR} ${LIBXSLT_INCLUDE_DIR} ${CMAKE_SOURCE_DIR} ${SRC_PATH} ${SRC_PATH}/debug/ ${SRC_PATH}/dialogs/ ${SRC_PATH}/dialogs/pages ${SRC_PATH}/dialogs/widgets ${SRC_PATH}/clipboard ${SRC_PATH}/cmds ${SRC_PATH}/codegenerators ${SRC_PATH}/codegenerators/ada/ ${SRC_PATH}/codegenerators/as/ ${SRC_PATH}/codegenerators/cpp/ ${SRC_PATH}/codegenerators/csharp/ ${SRC_PATH}/codegenerators/d/ ${SRC_PATH}/codegenerators/idl/ ${SRC_PATH}/codegenerators/java/ ${SRC_PATH}/codegenerators/js/ ${SRC_PATH}/codegenerators/pascal/ ${SRC_PATH}/codegenerators/perl/ ${SRC_PATH}/codegenerators/php/ ${SRC_PATH}/codegenerators/python/ ${SRC_PATH}/codegenerators/ruby/ ${SRC_PATH}/codegenerators/sql/ ${SRC_PATH}/codegenerators/tcl/ ${SRC_PATH}/codegenerators/vala/ ${SRC_PATH}/codegenerators/xml/ ${SRC_PATH}/codegenwizard ${SRC_PATH}/codeimport ${SRC_PATH}/debug ${SRC_PATH}/dialogs ${SRC_PATH}/docgenerators ${SRC_PATH}/menus/ ${SRC_PATH}/refactoring ${SRC_PATH}/uml1model/ ${SRC_PATH}/umlwidgets/ ${CMAKE_CURRENT_BINARY_DIR} ) if(NOT BUILD_KF5) set(LIBS Qt4::QtCore Qt4::QtGui Qt4::QtXml Qt4::QtTest Qt4::QtWebKit ${KDE4_KFILE_LIBS} ${LIBXML2_LIBRARIES} ${LIBXSLT_LIBRARIES} libumbrello ) else() set(LIBS Qt5::Xml Qt5::Test Qt5::Widgets Qt5::WebKitWidgets KF5::I18n KF5::Crash ${LIBXML2_LIBRARIES} ${LIBXSLT_LIBRARIES} libumbrello ) endif() ecm_add_test( testbasictypes.cpp LINK_LIBRARIES ${LIBS} TEST_NAME testbasictypes ) ecm_add_test( testumlobject.cpp testbase.cpp LINK_LIBRARIES ${LIBS} TEST_NAME testumlobject ) +ecm_add_test( + testassociation.cpp + testbase.cpp + LINK_LIBRARIES ${LIBS} + TEST_NAME testassociation +) + ecm_add_test( testclassifier.cpp testbase.cpp LINK_LIBRARIES ${LIBS} TEST_NAME testclassifier ) ecm_add_test( testcppwriter.cpp testbase.cpp LINK_LIBRARIES ${LIBS} TEST_NAME testcppwriter ) ecm_add_test( testpythonwriter.cpp testbase.cpp LINK_LIBRARIES ${LIBS} TEST_NAME testpythonwriter ) ecm_add_test( testoptionstate.cpp testbase.cpp LINK_LIBRARIES ${LIBS} TEST_NAME testoptionstate ) set(testumlroledialog_SRCS testumlroledialog.cpp ) add_executable(testumlroledialog ${testumlroledialog_SRCS}) target_link_libraries(testumlroledialog ${LIBS}) add_executable(testcrashhandler testcrashhandler.cpp) target_link_libraries(testcrashhandler ${LIBS}) add_executable(testlistpopupmenu testlistpopupmenu.cpp testbase.cpp) target_link_libraries(testlistpopupmenu ${LIBS}) find_package(LLVM CONFIG) find_package(Clang CONFIG) if(NOT Clang_FOUND) find_package(CLANG QUIET) endif() if(LLVM_FOUND AND (Clang_FOUND OR CLANG_FOUND) AND LLVM_VERSION VERSION_LESS "8.0.0") message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}") message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}") message(STATUS "Found CLANG ${CLANG_PACKAGE_VERSION}") include_directories(${LLVM_INCLUDE_DIRS}) add_definitions(${LLVM_DEFINITIONS}) include_directories(${CLANG_INCLUDE_DIRS}) add_definitions(${CLANG_DEFINITIONS}) # Now build our tools add_executable(testllvm testllvm.cpp) # Find the libraries that correspond to the LLVM components # that we wish to use if(LLVM_VERSION_MAJOR STREQUAL "7") set(llvm_libs LLVM) else() llvm_map_components_to_libnames(llvm_libs support core irreader analysis) endif() # Link against LLVM libraries target_link_libraries(testllvm ${llvm_libs} clangFrontend clangTooling clangBasic) add_executable(testllvmparser testllvmparser.cpp) if(NOT LLVM_VERSION_MAJOR STREQUAL "7") llvm_map_components_to_libnames(llvm_libs support) endif() target_link_libraries(testllvmparser ${llvm_libs} clangFrontend clangTooling clangAST clangBasic ${LIBS}) ecm_mark_nongui_executable(testllvm testllvmparser) endif() add_custom_target(check COMMAND ${CMAKE_BUILD_TOOL} test) diff --git a/unittests/testassociation.cpp b/unittests/testassociation.cpp new file mode 100644 index 000000000..28d9b0ba8 --- /dev/null +++ b/unittests/testassociation.cpp @@ -0,0 +1,205 @@ +/* + Copyright 2019 Ralf Habacker + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 2 of + the License or (at your option) version 3 or any later version + accepted by the membership of KDE e.V. (or its successor approved + by the membership of KDE e.V.), which shall act as a proxy + defined in Section 14 of version 3 of the license. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#include "testassociation.h" + +// app include +#include "association.h" +#include "package.h" +#include "stereotype.h" +#include "uml.h" +#include "umldoc.h" +#include "umlobject.h" +#include "umlrole.h" + +//----------------------------------------------------------------------------- + +void TestAssociation::test_equal() +{ + UMLPackage parent("Test Parent"); + UMLObject o1(nullptr, "objectA"); + UMLObject o2(nullptr, "objectB"); + UMLAssociation a(Uml::AssociationType::Association, &o1, &o2); + a.setUMLPackage(&parent); + UMLAssociation b(Uml::AssociationType::Association, &o1, &o2); + b.setUMLPackage(&parent); + UMLAssociation c(Uml::AssociationType::Association, &o1, nullptr); + c.setUMLPackage(&parent); + UMLAssociation d(Uml::AssociationType::Association, nullptr, &o2); + d.setUMLPackage(&parent); + //QCOMPARE(a, b); + QCOMPARE(a == c, false); + QCOMPARE(b == c, false); + QCOMPARE(c == d, false); +} + +void TestAssociation::test_toString() +{ + UMLObject o1(nullptr, "objectA"); + UMLObject o2(nullptr, "objectB"); + UMLAssociation a(Uml::AssociationType::Association, &o1, nullptr); + QString ar = QString(QLatin1String("objectA: %1 null")).arg(Uml::AssociationType::toStringI18n(Uml::AssociationType::Association)); + QCOMPARE(a.toString(), ar); + UMLAssociation b(Uml::AssociationType::Association, nullptr, &o2); + QString br = QString(QLatin1String("null %1 objectB:")).arg(Uml::AssociationType::toStringI18n(Uml::AssociationType::Association)); + QCOMPARE(b.toString(), br); + UMLAssociation c(Uml::AssociationType::Association, &o1, &o2); + QString cr = QString(QLatin1String("objectA: %1 objectB:")).arg(Uml::AssociationType::toStringI18n(Uml::AssociationType::Association)); + QCOMPARE(c.toString(), cr); +} + +void TestAssociation::test_UMLRole() +{ + UMLObject o1(nullptr, "objectA"); + UMLObject o2(nullptr, "objectB"); + UMLAssociation a(Uml::AssociationType::Association, &o1, &o2); + QCOMPARE(a.getUMLRole(Uml::RoleType::A)->object(), &o1); + QCOMPARE(a.getUMLRole(Uml::RoleType::B)->object(), &o2); +} + +void TestAssociation::test_associationType() +{ + UMLAssociation a(Uml::AssociationType::Association); + QCOMPARE(a.getAssocType(), Uml::AssociationType::Association); + a.setAssociationType(Uml::AssociationType::Aggregation); + QCOMPARE(a.getAssocType(), Uml::AssociationType::Aggregation); +} + +void TestAssociation::test_objectID() +{ + UMLObject o1(nullptr, "objectA"); + UMLObject o2(nullptr, "objectB"); + UMLAssociation a(Uml::AssociationType::Association, &o1, &o2); + QCOMPARE(a.getObjectId(Uml::RoleType::A), o1.id()); + QCOMPARE(a.getObjectId(Uml::RoleType::B), o2.id()); +} + +void TestAssociation::test_visibility() +{ + UMLObject o1(nullptr, "objectA"); + UMLObject o2(nullptr, "objectB"); + UMLAssociation a(Uml::AssociationType::Association, &o1, &o2); + QVERIFY(a.visibility(Uml::RoleType::A) == Uml::Visibility::Public); + QVERIFY(a.visibility(Uml::RoleType::B) == Uml::Visibility::Public); + a.setVisibility(Uml::Visibility::Protected, Uml::RoleType::A); + a.setVisibility(Uml::Visibility::Protected, Uml::RoleType::B); + QVERIFY(a.visibility(Uml::RoleType::A) == Uml::Visibility::Protected); + QVERIFY(a.visibility(Uml::RoleType::B) == Uml::Visibility::Protected); + a.setVisibility(Uml::Visibility::Private, Uml::RoleType::A); + a.setVisibility(Uml::Visibility::Private, Uml::RoleType::B); + QVERIFY(a.visibility(Uml::RoleType::A) == Uml::Visibility::Private); + QVERIFY(a.visibility(Uml::RoleType::B) == Uml::Visibility::Private); + a.setVisibility(Uml::Visibility::Implementation, Uml::RoleType::A); + a.setVisibility(Uml::Visibility::Implementation, Uml::RoleType::B); + QVERIFY(a.visibility(Uml::RoleType::A) == Uml::Visibility::Implementation); + QVERIFY(a.visibility(Uml::RoleType::B) == Uml::Visibility::Implementation); + UMLAssociation b(Uml::AssociationType::Association); +} + +void TestAssociation::test_changeability() +{ + UMLObject o1(nullptr, "objectA"); + UMLObject o2(nullptr, "objectB"); + UMLAssociation a(Uml::AssociationType::Association, &o1, &o2); + QVERIFY(a.visibility(Uml::RoleType::A) == Uml::Visibility::Public); + QVERIFY(a.visibility(Uml::RoleType::B) == Uml::Visibility::Public); + a.setVisibility(Uml::Visibility::Protected, Uml::RoleType::A); + a.setVisibility(Uml::Visibility::Protected, Uml::RoleType::B); + QVERIFY(a.visibility(Uml::RoleType::A) == Uml::Visibility::Protected); + QVERIFY(a.visibility(Uml::RoleType::B) == Uml::Visibility::Protected); + a.setVisibility(Uml::Visibility::Private, Uml::RoleType::A); + a.setVisibility(Uml::Visibility::Private, Uml::RoleType::B); + QVERIFY(a.visibility(Uml::RoleType::A) == Uml::Visibility::Private); + QVERIFY(a.visibility(Uml::RoleType::B) == Uml::Visibility::Private); + a.setVisibility(Uml::Visibility::Implementation, Uml::RoleType::A); + a.setVisibility(Uml::Visibility::Implementation, Uml::RoleType::B); + QVERIFY(a.visibility(Uml::RoleType::A) == Uml::Visibility::Implementation); + QVERIFY(a.visibility(Uml::RoleType::B) == Uml::Visibility::Implementation); + UMLAssociation b(Uml::AssociationType::Association); + QVERIFY(b.visibility(Uml::RoleType::A) == Uml::Visibility::Public); + QVERIFY(b.visibility(Uml::RoleType::B) == Uml::Visibility::Public); +} + +void TestAssociation::test_multiplicity() +{ + UMLObject o1(nullptr, "objectA"); + UMLObject o2(nullptr, "objectB"); + UMLAssociation a(Uml::AssociationType::Association, &o1, &o2); + QCOMPARE(a.getMultiplicity(Uml::RoleType::A), QString()); + QCOMPARE(a.getMultiplicity(Uml::RoleType::B), QString()); + a.setMultiplicity("1", Uml::RoleType::A); + a.setMultiplicity("2", Uml::RoleType::B); + QCOMPARE(a.getMultiplicity(Uml::RoleType::A), QLatin1String("1")); + QCOMPARE(a.getMultiplicity(Uml::RoleType::B), QLatin1String("2")); +} + +void TestAssociation::test_roleName() +{ + UMLObject o1(nullptr, "objectA"); + UMLObject o2(nullptr, "objectB"); + UMLAssociation a(Uml::AssociationType::Association, &o1, &o2); + QCOMPARE(a.getRoleName(Uml::RoleType::A), QString()); + QCOMPARE(a.getRoleName(Uml::RoleType::B), QString()); + a.setRoleName("test1", Uml::RoleType::A); + a.setRoleName("test2", Uml::RoleType::B); + QCOMPARE(a.getRoleName(Uml::RoleType::A), QLatin1String("test1")); + QCOMPARE(a.getRoleName(Uml::RoleType::B), QLatin1String("test2")); +} + +void TestAssociation::test_roleDoc() +{ + UMLObject o1(nullptr, "objectA"); + UMLObject o2(nullptr, "objectB"); + UMLAssociation a(Uml::AssociationType::Association, &o1, &o2); + QCOMPARE(a.getRoleDoc(Uml::RoleType::A), QString()); + QCOMPARE(a.getRoleDoc(Uml::RoleType::B), QString()); + a.setRoleDoc("test1", Uml::RoleType::A); + a.setRoleDoc("test2", Uml::RoleType::B); + QCOMPARE(a.getRoleDoc(Uml::RoleType::A), QLatin1String("test1")); + QCOMPARE(a.getRoleDoc(Uml::RoleType::B), QLatin1String("test2")); +} + +void TestAssociation::resolveRef() +{ + UMLPackage parent("Test Parent"); + UMLStereotype *stereotype1 = UMLApp::app()->document()->createStereotype("test1"); + UMLStereotype *stereotype2 = UMLApp::app()->document()->createStereotype("test2"); + + UMLObject o1(nullptr, "objectA"); + UMLObject o2(nullptr, "objectB"); + UMLAssociation a(Uml::AssociationType::Association, &o1, &o2); + // no resolve + a.setUMLPackage(&parent); + QCOMPARE(a.resolveRef(), true); + + // secondary + a.getUMLRole(Uml::RoleType::A)->setSecondaryId(Uml::ID::toString(stereotype1->id())); + a.getUMLRole(Uml::RoleType::B)->setSecondaryId(Uml::ID::toString(stereotype2->id())); + QCOMPARE(a.resolveRef(), true); + + // secondary fallback + a.getUMLRole(Uml::RoleType::A)->setSecondaryId(QLatin1String("")); + a.getUMLRole(Uml::RoleType::A)->setSecondaryFallback(Uml::ID::toString(stereotype1->id())); + a.getUMLRole(Uml::RoleType::B)->setSecondaryId(QLatin1String("")); + a.getUMLRole(Uml::RoleType::B)->setSecondaryFallback(Uml::ID::toString(stereotype2->id())); + QCOMPARE(a.resolveRef(), true); +} + +QTEST_MAIN(TestAssociation) diff --git a/unittests/testassociation.h b/unittests/testassociation.h new file mode 100644 index 000000000..875b01f68 --- /dev/null +++ b/unittests/testassociation.h @@ -0,0 +1,43 @@ +/* + Copyright 2019 Ralf Habacker + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 2 of + the License or (at your option) version 3 or any later version + accepted by the membership of KDE e.V. (or its successor approved + by the membership of KDE e.V.), which shall act as a proxy + defined in Section 14 of version 3 of the license. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#ifndef TESTASSOCIATION_H +#define TESTASSOCIATION_H + +#include "testbase.h" + +class TestAssociation : public TestBase +{ + Q_OBJECT +private slots: + void test_equal(); + void test_toString(); + void test_UMLRole(); + void test_associationType(); + void test_objectID(); + void test_visibility(); + void test_changeability(); + void test_multiplicity(); + void test_roleName(); + void test_roleDoc(); + void resolveRef(); +}; + +#endif // TESTASSOCIATION_H