From fa16baba13897c3ebcf91bb674163dcb07c9a52f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Barnab=C3=A1s=20Domozi?= Date: Wed, 29 Jan 2025 14:22:53 +0100 Subject: [PATCH] Added afferent coupling parser tests. --- .../test/sources/parser/CMakeLists.txt | 3 +- .../test/sources/parser/afferentcoupling.cpp | 176 ++++++++++++++++++ .../test/src/cppmetricsparsertest.cpp | 52 ++++++ 3 files changed, 230 insertions(+), 1 deletion(-) create mode 100644 plugins/cpp_metrics/test/sources/parser/afferentcoupling.cpp diff --git a/plugins/cpp_metrics/test/sources/parser/CMakeLists.txt b/plugins/cpp_metrics/test/sources/parser/CMakeLists.txt index 6b790120b..650aa24c0 100644 --- a/plugins/cpp_metrics/test/sources/parser/CMakeLists.txt +++ b/plugins/cpp_metrics/test/sources/parser/CMakeLists.txt @@ -5,4 +5,5 @@ add_library(CppMetricsTestProject STATIC functionmccabe.cpp typemccabe.cpp lackofcohesion.cpp - bumpyroad.cpp) + bumpyroad.cpp + afferentcoupling.cpp) diff --git a/plugins/cpp_metrics/test/sources/parser/afferentcoupling.cpp b/plugins/cpp_metrics/test/sources/parser/afferentcoupling.cpp new file mode 100644 index 000000000..61601122e --- /dev/null +++ b/plugins/cpp_metrics/test/sources/parser/afferentcoupling.cpp @@ -0,0 +1,176 @@ +#include +#include + +// Member types +class A {}; +class A_D { + A a; +}; + +class A2 {}; +class A2_D { + A2* a2; +}; + +class A3 {}; +class A3_D { + A3& a3; +}; + +class A4 {}; +class A4_D { + std::vector a4_vec; +}; + +class A5 {}; +class A5_D { + std::unique_ptr a5_ptr; +}; +// ========== + +// Function parameters +class B {}; +class B_D { + void foo(B b); +}; + +class B2 {}; +class B2_D { + void foo(B2* b); +}; + +class B3 {}; +class B3_D { + void foo(B3& b); +}; + +class B4 {}; +class B4_D { + void foo(std::vector& b4_vec); +}; + +class B5 {}; +class B5_D { + void foo(std::unique_ptr& b5_ptr); +}; + +class B6 {}; +class B6_D { + void foo(int, bool, B6* b); +}; + +class B7 {}; +class B7_D { + void foo(int, bool, B7* b); +}; + +class B8 {}; +class B8_D { + void foo(int, bool); + void foo(int, bool, B8* b); +}; +// ========== + +// Function local variables +class C {}; +class C_D { + void foo() + { + C c; + } +}; + +class C2 {}; +class C2_D { + void foo() + { + auto c2 = C2(); + } +}; + +class C3 {}; +class C3_D { + void foo() + { + std::vector c3_vec; + } +}; +// ========== + +// Out-of-class member functions +class D {}; +class D_D { + void foo(); +}; + +void D_D::foo() +{ + D d; +} + +class D2 {}; +class D2_D { + void foo(); +}; + +void D2_D::foo() +{ + std::unique_ptr d2_ptr; +} +// ========== + +// Multiple usage of the same type +class E {}; +class E_D { + E* e; + + void foo() + { + E e; + } + + void bar(std::vector e_vec); +}; +// ========== + +// Inheritance +class F {}; +class F_D : public F {}; +// ========== + +// Multi inheritance +class G {}; +class G_C {}; +class G_D : public G_C, public G {}; +// ========== + +// Multiple dependant types +class H {}; +class H_D1 { + H h; +}; + +class H_D2 { + void foo() + { + std::vector h_vec; + } +}; + +// ---------- + +class H2 {}; +class H2_D1 { + H2 h2; + void bar(std::unique_ptr

h2_ptr); +}; + +class H2_D2 { + H2* h2_ptr; + + void foo() + { + std::vector

h2_vec; + } +}; +// ========== diff --git a/plugins/cpp_metrics/test/src/cppmetricsparsertest.cpp b/plugins/cpp_metrics/test/src/cppmetricsparsertest.cpp index 37da8caa4..677cb4486 100644 --- a/plugins/cpp_metrics/test/src/cppmetricsparsertest.cpp +++ b/plugins/cpp_metrics/test/src/cppmetricsparsertest.cpp @@ -268,3 +268,55 @@ INSTANTIATE_TEST_SUITE_P( ParameterizedLackOfCohesionTest, ::testing::ValuesIn(paramLackOfCohesion) ); + +// Afferent coupling + +typedef std::pair AfferentParam; +class ParameterizedAfferentCouplingTest + : public CppMetricsParserTest, + public ::testing::WithParamInterface +{}; + +std::vector paramAfferent = { + {"A", 1}, + {"A2", 1}, + {"A3", 1}, + {"A4", 1}, + {"A5", 1}, + {"B", 1}, + {"B2", 1}, + {"B3", 1}, + {"B4", 1}, + {"B5", 1}, + {"B6", 1}, + {"B7", 1}, + {"B8", 1}, + {"C", 1}, + {"C2", 1}, + {"C3", 1}, + {"E", 1}, + {"F", 1}, + {"G", 1}, + {"H", 2}, + {"H2", 2}, +}; + +TEST_P(ParameterizedAfferentCouplingTest, TypeAfferentTest) { + _transaction([&, this]() { + + const auto record = _db->query_value( + odb::query::qualifiedName == GetParam().first); + + const auto metric = _db->query_value( + odb::query::astNodeId == record.astNodeId && + odb::query::type == model::CppAstNodeMetrics::AFFERENT_TYPE); + + EXPECT_EQ(GetParam().second, metric.value); + }); +} + +INSTANTIATE_TEST_SUITE_P( + ParameterizedAfferentCouplingTestSuite, + ParameterizedAfferentCouplingTest, + ::testing::ValuesIn(paramAfferent) +);