Hotwheels-Cluster 1.2
Creation of Cluster APP for SEA:ME project.
 
Loading...
Searching...
No Matches
test_MileageFileHandler.cpp
Go to the documentation of this file.
1
15
18#include <gmock/gmock.h>
19#include <gtest/gtest.h>
20
21using ::testing::_;
22using ::testing::Return;
23
31class MileageFileHandlerTest : public ::testing::Test
32{
33protected:
35 QString testFilePath = "test_mileage.txt";
36
38
39 void SetUp() override { initFileHandler(); }
40
41 void TearDown() override { delete mileageFileHandler; }
42
44 {
47 [this](QFile &file, QIODevice::OpenMode mode) {
48 return mockFileController.open(file, mode);
49 },
50 [this](QFile &file) { return mockFileController.read(file); },
51 [this](QFile &file, const QString &data) {
52 return mockFileController.write(file, data);
53 },
54 [this](const QString &path) { return mockFileController.exists(path); });
55 };
56};
57
65TEST_F(MileageFileHandlerTest, EnsureFileExists_FileDoesNotExist_CreatesFileWithZeroMileage)
66{
67 // Ensure file does NOT exist
68 EXPECT_CALL(mockFileController, exists(testFilePath)).WillOnce(Return(false));
69
70 // Expect file to open for writing
71 EXPECT_CALL(mockFileController, open(_, QIODevice::WriteOnly | QIODevice::Text))
72 .WillOnce(Return(true));
73
74 // Expect writing "0.0" to file
75 EXPECT_CALL(mockFileController, write(_, QString("0.0"))).WillOnce(Return(true));
76
77 // Manually trigger ensureFileExists()
78 MileageFileHandler *tmpHandler = new MileageFileHandler(
79 testFilePath,
80 [this](QFile &file, QIODevice::OpenMode mode) {
81 return mockFileController.open(file, mode);
82 },
83 [this](QFile &file) { return mockFileController.read(file); },
84 [this](QFile &file, const QString &data) { return mockFileController.write(file, data); },
85 [this](const QString &path) { return mockFileController.exists(path); });
86
87 delete tmpHandler;
88}
89
97TEST_F(MileageFileHandlerTest, ReadMileage_ValidNumber_ReturnsParsedValue)
98{
99 EXPECT_CALL(mockFileController, open(testing::_, QIODevice::ReadOnly | QIODevice::Text))
100 .WillOnce(Return(true)); // Simulate file opening
101
102 EXPECT_CALL(mockFileController, read(testing::_))
103 .WillOnce(Return("123.45")); // Simulate reading mileage
104
105 double mileage = mileageFileHandler->readMileage();
106 EXPECT_DOUBLE_EQ(mileage, 123.45);
107}
108
116TEST_F(MileageFileHandlerTest, ReadMileage_InvalidNumber_ReturnsZero)
117{
118 EXPECT_CALL(mockFileController, open(testing::_, QIODevice::ReadOnly | QIODevice::Text))
119 .WillOnce(Return(true)); // Simulate file opening
120
121 EXPECT_CALL(mockFileController, read(testing::_))
122 .WillOnce(Return("INVALID")); // Simulate reading invalid mileage
123
124 double mileage = mileageFileHandler->readMileage();
125 EXPECT_DOUBLE_EQ(mileage, 0.0);
126}
127
135TEST_F(MileageFileHandlerTest, WriteMileage_ValidNumber_WritesToFile)
136{
137 EXPECT_CALL(mockFileController, open(testing::_, QIODevice::WriteOnly | QIODevice::Text))
138 .WillOnce(Return(true)); // Simulate file opening
139
140 EXPECT_CALL(mockFileController, write(testing::_, QString("789.12")))
141 .WillOnce(Return(true)); // Simulate successful write
142
143 mileageFileHandler->writeMileage(789.12);
144}
Definition of the MileageFileHandler class.
File containing Mock classes to test the controller of the File module.
Test fixture for testing the MileageFileHandler class.
MockFileController mockFileController
MileageFileHandler * mileageFileHandler
Class that manages the mileage file.
double readMileage() const override
Reads the mileage from the file.
Class to emulate the behavior of the File controller.
TEST_F(MileageFileHandlerTest, EnsureFileExists_FileDoesNotExist_CreatesFileWithZeroMileage)
Ensures that the mileage file handler creates the file if it does not exist.