Skip to content

Commit

Permalink
Adding unit tests for the CMDP
Browse files Browse the repository at this point in the history
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
  • Loading branch information
KGergo88 committed Dec 28, 2020
1 parent 1592ee4 commit f410485
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 33 deletions.
80 changes: 80 additions & 0 deletions tests/sources/test_continous_measurement_data_protocol.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
//==============================================================================//
// //
// RDB Diplomaterv Monitor //
// A monitor program for the RDB Diplomaterv project //
// Copyright (C) 2018 András Gergő Kocsis //
// //
// 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 3 of the License, or //
// (at your option) any later version. //
// //
// 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 <https://www.gnu.org/licenses/>. //
// //
//==============================================================================//



#include <fstream>

#include <gtest/gtest.h>
#include <gmock/gmock-matchers.h>

#include <QCoreApplication>
#include <QString>
#include <QDir>

#include "../application/sources/global.hpp"
#include "../application/sources/continous_measurement_data_protocol.hpp"
#include "test_protocol_common.h"



class TestContinousMeasurementDataProtocol : public ::testing::Test,
public testing::WithParamInterface<TestProtocolParameter>
{
protected:
ContinousMeasurementDataProtocol test_cmdp_processor;
std::string expected_protocol_name = "Continous Measurement Data Protocol CMDP";
std::vector<DiagramSpecialized> processed_diagrams;
// Empty string as the CMDP does not support file handling
std::string expected_file_type;
};

TEST_F(TestContinousMeasurementDataProtocol, GetProtocolName)
{
EXPECT_EQ(test_cmdp_processor.GetProtocolName(), expected_protocol_name);
}

TEST_F(TestContinousMeasurementDataProtocol, CanThisFileBeProcessed)
{
std::string filename_to_test = std::string("myfile.") + expected_file_type;
EXPECT_FALSE(test_cmdp_processor.CanThisFileBeProcessed(filename_to_test));
filename_to_test = std::string("mymdpfile.") + std::string("txt");
EXPECT_FALSE(test_cmdp_processor.CanThisFileBeProcessed(filename_to_test));
}

TEST_F(TestContinousMeasurementDataProtocol, GetSupportedFileType)
{
EXPECT_EQ(test_cmdp_processor.GetSupportedFileType(), expected_file_type);
}

TEST_F(TestContinousMeasurementDataProtocol, ProcessData_EmptyStream)
{
std::ifstream empty_stream;
processed_diagrams = test_cmdp_processor.ProcessData(empty_stream);
EXPECT_EQ(processed_diagrams.size(), std::size_t(0));
}

TEST_F(TestContinousMeasurementDataProtocol, ExportData)
{
std::stringstream exported_data;
EXPECT_ANY_THROW(exported_data = test_cmdp_processor.ExportData(std::vector<DiagramSpecialized>()));
EXPECT_EQ(exported_data.str(), std::string());
}
36 changes: 15 additions & 21 deletions tests/sources/test_measurement_data_protocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,18 @@



#include <fstream>

#include <gtest/gtest.h>
#include <gmock/gmock-matchers.h>

#include <QCoreApplication>
#include <QString>
#include <QDir>

#include "../application/sources/global.hpp"
#include "../application/sources/measurement_data_protocol.hpp"
#include "test_protocol_common.h"



struct TestMeasurementDataProtocolParameter
{
TestMeasurementDataProtocolParameter(const QString& new_file_name,
const int& new_expected_correct_diagrams) : file_name(new_file_name),
expected_correct_diagrams(new_expected_correct_diagrams) {}
QString file_name;
int expected_correct_diagrams;
};

class TestMeasurementDataProtocol : public ::testing::Test,
public testing::WithParamInterface<TestMeasurementDataProtocolParameter>
public testing::WithParamInterface<TestProtocolParameter>
{
protected:
std::ifstream ReadTestFileContent(const QString& file_name)
Expand All @@ -70,17 +57,24 @@ class TestMeasurementDataProtocol : public ::testing::Test,
QString test_files_path = QDir(QCoreApplication::applicationDirPath()).filePath("test_files");
};

TEST_F(TestMeasurementDataProtocol, ConstructorAndDataProcessingInterface)
TEST_F(TestMeasurementDataProtocol, GetProtocolName)
{
EXPECT_EQ(test_mdp_processor.GetProtocolName(), expected_protocol_name);
EXPECT_EQ(test_mdp_processor.GetSupportedFileType(), expected_file_type);
}

TEST_F(TestMeasurementDataProtocol, CanThisFileBeProcessed)
{
std::string filename_to_test = std::string("myfile.") + expected_file_type;
EXPECT_TRUE(test_mdp_processor.CanThisFileBeProcessed(filename_to_test));
filename_to_test = std::string("mymdpfile.") + std::string("txt");
EXPECT_FALSE(test_mdp_processor.CanThisFileBeProcessed(filename_to_test));
}

TEST_F(TestMeasurementDataProtocol, GetSupportedFileType)
{
EXPECT_EQ(test_mdp_processor.GetSupportedFileType(), expected_file_type);
}

TEST_F(TestMeasurementDataProtocol, ProcessData_ExportData_EmptyStream)
{
std::ifstream empty_stream;
Expand All @@ -106,8 +100,8 @@ TEST_P(TestMeasurementDataProtocol, ProcessData_ExportData)

INSTANTIATE_TEST_SUITE_P(TestMeasurementDataProtocolInstantiation,
TestMeasurementDataProtocol,
testing::Values(TestMeasurementDataProtocolParameter("TEST_1C_0E_MDP.mdp", 1),
TestMeasurementDataProtocolParameter("TEST_2C_0E_MDP.mdp", 2),
TestMeasurementDataProtocolParameter("TEST_1C_1E_MDP_HeadlineError.mdp", 1),
TestMeasurementDataProtocolParameter("TEST_1C_2E_MDP_DatalineError.mdp", 1)
testing::Values(TestProtocolParameter("TEST_1C_0E_MDP.mdp", 1),
TestProtocolParameter("TEST_2C_0E_MDP.mdp", 2),
TestProtocolParameter("TEST_1C_1E_MDP_HeadlineError.mdp", 1),
TestProtocolParameter("TEST_1C_2E_MDP_DatalineError.mdp", 1)
));
47 changes: 47 additions & 0 deletions tests/sources/test_protocol_common.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//==============================================================================//
// //
// RDB Diplomaterv Monitor //
// A monitor program for the RDB Diplomaterv project //
// Copyright (C) 2018 András Gergő Kocsis //
// //
// 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 3 of the License, or //
// (at your option) any later version. //
// //
// 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 <https://www.gnu.org/licenses/>. //
// //
//==============================================================================//



#include <fstream>

#include <gtest/gtest.h>
#include <gmock/gmock-matchers.h>

#include <QString>



#ifndef TEST_PROTOCOL_COMMON_H
#define TEST_PROTOCOL_COMMON_H



struct TestProtocolParameter
{
TestProtocolParameter(const QString& new_file_name,
const int& new_expected_correct_diagrams) : file_name(new_file_name),
expected_correct_diagrams(new_expected_correct_diagrams) {}
QString file_name;
int expected_correct_diagrams;
};

#endif // TEST_PROTOCOL_COMMON_H
29 changes: 17 additions & 12 deletions tests/tests.pro
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,25 @@ win32 {
}

# Source files of the target
SOURCES += \
../application/sources/configuration.cpp \
../application/sources/measurement_data_protocol.cpp \
sources/test_main.cpp \
sources/test_data_point.cpp \
sources/test_data_line.cpp \
sources/test_diagram.cpp \
sources/test_configuration.cpp \
sources/test_diagram_container.cpp \
sources/test_measurement_data_protocol.cpp \
sources/test_ordered_dict.cpp \
sources/test_serial_port.cpp \
SOURCES += \
../application/sources/configuration.cpp \
../application/sources/measurement_data_protocol.cpp \
../application/sources/continous_measurement_data_protocol.cpp \
sources/test_main.cpp \
sources/test_data_point.cpp \
sources/test_data_line.cpp \
sources/test_diagram.cpp \
sources/test_configuration.cpp \
sources/test_diagram_container.cpp \
sources/test_measurement_data_protocol.cpp \
sources/test_continous_measurement_data_protocol.cpp \
sources/test_ordered_dict.cpp \
sources/test_serial_port.cpp \
sources/test_backend.cpp

HEADERS += \
sources/test_protocol_common.h

DISTFILES += \
gtest_dendency.pri \
test_files/TEST_1C_0E_MDP.mdp \
Expand Down

0 comments on commit f410485

Please sign in to comment.