Skip to content

Commit f410485

Browse files
committed
Adding unit tests for the CMDP
Details: - Adding tests for the already implemented CMDP methods - Extracting the common parts from the MDP tests that can be used for the CMDP and future protocols and storing them into the test_protocol_common.h - Updating the MDP unit tests
1 parent 1592ee4 commit f410485

File tree

4 files changed

+159
-33
lines changed

4 files changed

+159
-33
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
//==============================================================================//
2+
// //
3+
// RDB Diplomaterv Monitor //
4+
// A monitor program for the RDB Diplomaterv project //
5+
// Copyright (C) 2018 András Gergő Kocsis //
6+
// //
7+
// This program is free software: you can redistribute it and/or modify //
8+
// it under the terms of the GNU General Public License as published by //
9+
// the Free Software Foundation, either version 3 of the License, or //
10+
// (at your option) any later version. //
11+
// //
12+
// This program is distributed in the hope that it will be useful, //
13+
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
14+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
15+
// GNU General Public License for more details. //
16+
// //
17+
// You should have received a copy of the GNU General Public License //
18+
// along with this program. If not, see <https://www.gnu.org/licenses/>. //
19+
// //
20+
//==============================================================================//
21+
22+
23+
24+
#include <fstream>
25+
26+
#include <gtest/gtest.h>
27+
#include <gmock/gmock-matchers.h>
28+
29+
#include <QCoreApplication>
30+
#include <QString>
31+
#include <QDir>
32+
33+
#include "../application/sources/global.hpp"
34+
#include "../application/sources/continous_measurement_data_protocol.hpp"
35+
#include "test_protocol_common.h"
36+
37+
38+
39+
class TestContinousMeasurementDataProtocol : public ::testing::Test,
40+
public testing::WithParamInterface<TestProtocolParameter>
41+
{
42+
protected:
43+
ContinousMeasurementDataProtocol test_cmdp_processor;
44+
std::string expected_protocol_name = "Continous Measurement Data Protocol CMDP";
45+
std::vector<DiagramSpecialized> processed_diagrams;
46+
// Empty string as the CMDP does not support file handling
47+
std::string expected_file_type;
48+
};
49+
50+
TEST_F(TestContinousMeasurementDataProtocol, GetProtocolName)
51+
{
52+
EXPECT_EQ(test_cmdp_processor.GetProtocolName(), expected_protocol_name);
53+
}
54+
55+
TEST_F(TestContinousMeasurementDataProtocol, CanThisFileBeProcessed)
56+
{
57+
std::string filename_to_test = std::string("myfile.") + expected_file_type;
58+
EXPECT_FALSE(test_cmdp_processor.CanThisFileBeProcessed(filename_to_test));
59+
filename_to_test = std::string("mymdpfile.") + std::string("txt");
60+
EXPECT_FALSE(test_cmdp_processor.CanThisFileBeProcessed(filename_to_test));
61+
}
62+
63+
TEST_F(TestContinousMeasurementDataProtocol, GetSupportedFileType)
64+
{
65+
EXPECT_EQ(test_cmdp_processor.GetSupportedFileType(), expected_file_type);
66+
}
67+
68+
TEST_F(TestContinousMeasurementDataProtocol, ProcessData_EmptyStream)
69+
{
70+
std::ifstream empty_stream;
71+
processed_diagrams = test_cmdp_processor.ProcessData(empty_stream);
72+
EXPECT_EQ(processed_diagrams.size(), std::size_t(0));
73+
}
74+
75+
TEST_F(TestContinousMeasurementDataProtocol, ExportData)
76+
{
77+
std::stringstream exported_data;
78+
EXPECT_ANY_THROW(exported_data = test_cmdp_processor.ExportData(std::vector<DiagramSpecialized>()));
79+
EXPECT_EQ(exported_data.str(), std::string());
80+
}

tests/sources/test_measurement_data_protocol.cpp

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,31 +21,18 @@
2121

2222

2323

24-
#include <fstream>
25-
26-
#include <gtest/gtest.h>
27-
#include <gmock/gmock-matchers.h>
28-
2924
#include <QCoreApplication>
3025
#include <QString>
3126
#include <QDir>
3227

3328
#include "../application/sources/global.hpp"
3429
#include "../application/sources/measurement_data_protocol.hpp"
30+
#include "test_protocol_common.h"
3531

3632

3733

38-
struct TestMeasurementDataProtocolParameter
39-
{
40-
TestMeasurementDataProtocolParameter(const QString& new_file_name,
41-
const int& new_expected_correct_diagrams) : file_name(new_file_name),
42-
expected_correct_diagrams(new_expected_correct_diagrams) {}
43-
QString file_name;
44-
int expected_correct_diagrams;
45-
};
46-
4734
class TestMeasurementDataProtocol : public ::testing::Test,
48-
public testing::WithParamInterface<TestMeasurementDataProtocolParameter>
35+
public testing::WithParamInterface<TestProtocolParameter>
4936
{
5037
protected:
5138
std::ifstream ReadTestFileContent(const QString& file_name)
@@ -70,17 +57,24 @@ class TestMeasurementDataProtocol : public ::testing::Test,
7057
QString test_files_path = QDir(QCoreApplication::applicationDirPath()).filePath("test_files");
7158
};
7259

73-
TEST_F(TestMeasurementDataProtocol, ConstructorAndDataProcessingInterface)
60+
TEST_F(TestMeasurementDataProtocol, GetProtocolName)
7461
{
7562
EXPECT_EQ(test_mdp_processor.GetProtocolName(), expected_protocol_name);
76-
EXPECT_EQ(test_mdp_processor.GetSupportedFileType(), expected_file_type);
63+
}
7764

65+
TEST_F(TestMeasurementDataProtocol, CanThisFileBeProcessed)
66+
{
7867
std::string filename_to_test = std::string("myfile.") + expected_file_type;
7968
EXPECT_TRUE(test_mdp_processor.CanThisFileBeProcessed(filename_to_test));
8069
filename_to_test = std::string("mymdpfile.") + std::string("txt");
8170
EXPECT_FALSE(test_mdp_processor.CanThisFileBeProcessed(filename_to_test));
8271
}
8372

73+
TEST_F(TestMeasurementDataProtocol, GetSupportedFileType)
74+
{
75+
EXPECT_EQ(test_mdp_processor.GetSupportedFileType(), expected_file_type);
76+
}
77+
8478
TEST_F(TestMeasurementDataProtocol, ProcessData_ExportData_EmptyStream)
8579
{
8680
std::ifstream empty_stream;
@@ -106,8 +100,8 @@ TEST_P(TestMeasurementDataProtocol, ProcessData_ExportData)
106100

107101
INSTANTIATE_TEST_SUITE_P(TestMeasurementDataProtocolInstantiation,
108102
TestMeasurementDataProtocol,
109-
testing::Values(TestMeasurementDataProtocolParameter("TEST_1C_0E_MDP.mdp", 1),
110-
TestMeasurementDataProtocolParameter("TEST_2C_0E_MDP.mdp", 2),
111-
TestMeasurementDataProtocolParameter("TEST_1C_1E_MDP_HeadlineError.mdp", 1),
112-
TestMeasurementDataProtocolParameter("TEST_1C_2E_MDP_DatalineError.mdp", 1)
103+
testing::Values(TestProtocolParameter("TEST_1C_0E_MDP.mdp", 1),
104+
TestProtocolParameter("TEST_2C_0E_MDP.mdp", 2),
105+
TestProtocolParameter("TEST_1C_1E_MDP_HeadlineError.mdp", 1),
106+
TestProtocolParameter("TEST_1C_2E_MDP_DatalineError.mdp", 1)
113107
));

tests/sources/test_protocol_common.h

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//==============================================================================//
2+
// //
3+
// RDB Diplomaterv Monitor //
4+
// A monitor program for the RDB Diplomaterv project //
5+
// Copyright (C) 2018 András Gergő Kocsis //
6+
// //
7+
// This program is free software: you can redistribute it and/or modify //
8+
// it under the terms of the GNU General Public License as published by //
9+
// the Free Software Foundation, either version 3 of the License, or //
10+
// (at your option) any later version. //
11+
// //
12+
// This program is distributed in the hope that it will be useful, //
13+
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
14+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
15+
// GNU General Public License for more details. //
16+
// //
17+
// You should have received a copy of the GNU General Public License //
18+
// along with this program. If not, see <https://www.gnu.org/licenses/>. //
19+
// //
20+
//==============================================================================//
21+
22+
23+
24+
#include <fstream>
25+
26+
#include <gtest/gtest.h>
27+
#include <gmock/gmock-matchers.h>
28+
29+
#include <QString>
30+
31+
32+
33+
#ifndef TEST_PROTOCOL_COMMON_H
34+
#define TEST_PROTOCOL_COMMON_H
35+
36+
37+
38+
struct TestProtocolParameter
39+
{
40+
TestProtocolParameter(const QString& new_file_name,
41+
const int& new_expected_correct_diagrams) : file_name(new_file_name),
42+
expected_correct_diagrams(new_expected_correct_diagrams) {}
43+
QString file_name;
44+
int expected_correct_diagrams;
45+
};
46+
47+
#endif // TEST_PROTOCOL_COMMON_H

tests/tests.pro

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -60,20 +60,25 @@ win32 {
6060
}
6161

6262
# Source files of the target
63-
SOURCES += \
64-
../application/sources/configuration.cpp \
65-
../application/sources/measurement_data_protocol.cpp \
66-
sources/test_main.cpp \
67-
sources/test_data_point.cpp \
68-
sources/test_data_line.cpp \
69-
sources/test_diagram.cpp \
70-
sources/test_configuration.cpp \
71-
sources/test_diagram_container.cpp \
72-
sources/test_measurement_data_protocol.cpp \
73-
sources/test_ordered_dict.cpp \
74-
sources/test_serial_port.cpp \
63+
SOURCES += \
64+
../application/sources/configuration.cpp \
65+
../application/sources/measurement_data_protocol.cpp \
66+
../application/sources/continous_measurement_data_protocol.cpp \
67+
sources/test_main.cpp \
68+
sources/test_data_point.cpp \
69+
sources/test_data_line.cpp \
70+
sources/test_diagram.cpp \
71+
sources/test_configuration.cpp \
72+
sources/test_diagram_container.cpp \
73+
sources/test_measurement_data_protocol.cpp \
74+
sources/test_continous_measurement_data_protocol.cpp \
75+
sources/test_ordered_dict.cpp \
76+
sources/test_serial_port.cpp \
7577
sources/test_backend.cpp
7678

79+
HEADERS += \
80+
sources/test_protocol_common.h
81+
7782
DISTFILES += \
7883
gtest_dendency.pri \
7984
test_files/TEST_1C_0E_MDP.mdp \

0 commit comments

Comments
 (0)