Skip to content

Commit 20e0d0d

Browse files
author
Barnabás Domozi
committed
Add tests for efferent coupling at module level.
1 parent f7b0b0a commit 20e0d0d

File tree

9 files changed

+126
-10
lines changed

9 files changed

+126
-10
lines changed

plugins/cpp_metrics/test/sources/parser/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ add_library(CppMetricsTestProject STATIC
66
typemccabe.cpp
77
lackofcohesion.cpp
88
bumpyroad.cpp
9-
afferentcoupling.cpp)
9+
afferentcoupling.cpp
10+
modulemetrics.cpp)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#ifndef CC_CPP_MODULE_METRICS_TEST_A1
2+
#define CC_CPP_MODULE_METRICS_TEST_A1
3+
4+
#include "./a2.h"
5+
6+
namespace CC_CPP_MODULE_METRICS_TEST
7+
{
8+
class A1 {
9+
A2 a2;
10+
};
11+
}
12+
13+
#endif
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#ifndef CC_CPP_MODULE_METRICS_TEST_A2
2+
#define CC_CPP_MODULE_METRICS_TEST_A2
3+
4+
#include "../module_b/b1.h"
5+
6+
namespace CC_CPP_MODULE_METRICS_TEST
7+
{
8+
class A2 {
9+
B1 b1;
10+
};
11+
}
12+
13+
#endif
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#ifndef CC_CPP_MODULE_METRICS_TEST_B1
2+
#define CC_CPP_MODULE_METRICS_TEST_B1
3+
4+
#include "./b2.h"
5+
6+
namespace CC_CPP_MODULE_METRICS_TEST
7+
{
8+
class B1 {
9+
B2 b2;
10+
};
11+
}
12+
13+
#endif
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#ifndef CC_CPP_MODULE_METRICS_TEST_B2
2+
#define CC_CPP_MODULE_METRICS_TEST_B2
3+
4+
namespace CC_CPP_MODULE_METRICS_TEST
5+
{
6+
class B2 {};
7+
}
8+
9+
#endif
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#ifndef CC_CPP_MODULE_METRICS_TEST_C1
2+
#define CC_CPP_MODULE_METRICS_TEST_C1
3+
4+
#include "../module_b/b1.h"
5+
#include "./c2.h"
6+
7+
namespace CC_CPP_MODULE_METRICS_TEST
8+
{
9+
class C1 {
10+
B1 b1;
11+
C2 c2;
12+
};
13+
}
14+
15+
#endif
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#ifndef CC_CPP_MODULE_METRICS_TEST_C2
2+
#define CC_CPP_MODULE_METRICS_TEST_C2
3+
4+
#include "../module_a/a2.h"
5+
6+
namespace CC_CPP_MODULE_METRICS_TEST
7+
{
8+
class C2 {
9+
A2 a2;
10+
};
11+
}
12+
13+
#endif
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#include "./module_a/a1.h"
2+
#include "./module_a/a2.h"
3+
#include "./module_b/b1.h"
4+
#include "./module_b/b2.h"
5+
#include "./module_c/c1.h"
6+
#include "./module_c/c2.h"

plugins/cpp_metrics/test/src/cppmetricsparsertest.cpp

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
#include <model/cppfunction-odb.hxx>
1414
#include <model/cppastnodemetrics.h>
1515
#include <model/cppastnodemetrics-odb.hxx>
16+
#include <model/cppfilemetrics.h>
17+
#include <model/cppfilemetrics-odb.hxx>
1618

1719
#include <util/dbutil.h>
1820
#include <util/odbtransaction.h>
@@ -21,6 +23,8 @@ using namespace cc;
2123

2224
extern char* dbConnectionString;
2325

26+
typedef std::pair<std::string, unsigned int> StringUintParam;
27+
2428
class CppMetricsParserTest : public ::testing::Test
2529
{
2630
public:
@@ -62,14 +66,12 @@ bool CppMetricsParserTest::queryRecordMetric(
6266

6367
// McCabe
6468

65-
typedef std::pair<std::string, unsigned int> McCabeParam;
66-
6769
class ParameterizedMcCabeTest
6870
: public CppMetricsParserTest,
69-
public ::testing::WithParamInterface<McCabeParam>
71+
public ::testing::WithParamInterface<StringUintParam>
7072
{};
7173

72-
std::vector<McCabeParam> paramMcCabe = {
74+
std::vector<StringUintParam> paramMcCabe = {
7375
{"conditionals", 8},
7476
{"loops1", 6},
7577
{"loops2", 6},
@@ -173,10 +175,10 @@ INSTANTIATE_TEST_SUITE_P(
173175

174176
class ParameterizedTypeMcCabeTest
175177
: public CppMetricsParserTest,
176-
public ::testing::WithParamInterface<McCabeParam>
178+
public ::testing::WithParamInterface<StringUintParam>
177179
{};
178180

179-
std::vector<McCabeParam> paramTypeMcCabe = {
181+
std::vector<StringUintParam> paramTypeMcCabe = {
180182
{"Empty", 0},
181183
{"ClassMethodsInside", 16},
182184
{"ClassMethodsOutside", 44},
@@ -271,13 +273,12 @@ INSTANTIATE_TEST_SUITE_P(
271273

272274
// Afferent coupling
273275

274-
typedef std::pair<std::string, unsigned int> AfferentParam;
275276
class ParameterizedAfferentCouplingTest
276277
: public CppMetricsParserTest,
277-
public ::testing::WithParamInterface<AfferentParam>
278+
public ::testing::WithParamInterface<StringUintParam>
278279
{};
279280

280-
std::vector<AfferentParam> paramAfferent = {
281+
std::vector<StringUintParam> paramAfferent = {
281282
{"A", 1},
282283
{"A2", 1},
283284
{"A3", 1},
@@ -320,3 +321,35 @@ INSTANTIATE_TEST_SUITE_P(
320321
ParameterizedAfferentCouplingTest,
321322
::testing::ValuesIn(paramAfferent)
322323
);
324+
325+
// Efferent coupling at module level
326+
327+
class ParameterizedEfferentModuleCouplingTest
328+
: public CppMetricsParserTest,
329+
public ::testing::WithParamInterface<StringUintParam>
330+
{};
331+
332+
std::vector<StringUintParam> paramEfferentModule = {
333+
{"%/test/sources/parser/module_a", 1},
334+
{"%/test/sources/parser/module_b", 0},
335+
{"%/test/sources/parser/module_c", 2},
336+
};
337+
338+
TEST_P(ParameterizedEfferentModuleCouplingTest, ModuleEfferentTest) {
339+
_transaction([&, this]() {
340+
341+
typedef odb::query<model::CppModuleMetricsForPathView> CppModuleMetricsQuery;
342+
343+
const auto metric = _db->query_value<model::CppModuleMetricsForPathView>(
344+
CppModuleMetricsQuery::CppFileMetrics::type == model::CppFileMetrics::Type::EFFERENT_MODULE &&
345+
CppModuleMetricsQuery::File::path.like(GetParam().first));
346+
347+
EXPECT_EQ(GetParam().second, metric.value);
348+
});
349+
}
350+
351+
INSTANTIATE_TEST_SUITE_P(
352+
ParameterizedEfferentModuleCouplingTestSuite,
353+
ParameterizedEfferentModuleCouplingTest,
354+
::testing::ValuesIn(paramEfferentModule)
355+
);

0 commit comments

Comments
 (0)