Improve code for mocked reply

* Fix missing include
* Avoid useless cast
* Convert to current coding style
This commit is contained in:
Martchus 2024-03-01 23:59:28 +01:00
parent 0db27b14b0
commit 9ecb9dc6c3
1 changed files with 14 additions and 14 deletions

View File

@ -8,12 +8,12 @@
#include <QTimer> #include <QTimer>
#include <QUrlQuery> #include <QUrlQuery>
#include <algorithm>
#include <cstdlib> #include <cstdlib>
#include <iostream> #include <iostream>
#include <limits> #include <limits>
#include <string> #include <string>
using namespace std;
using namespace CppUtilities; using namespace CppUtilities;
using namespace CppUtilities::EscapeCodes; using namespace CppUtilities::EscapeCodes;
@ -24,21 +24,21 @@ namespace Data {
*/ */
namespace TestData { namespace TestData {
static bool initialized = false; static bool initialized = false;
static string config, status, folderStats, deviceStats, errors, folderStatus, folderStatus2, folderStatus3, pullErrors, connections, version, empty; static std::string config, status, folderStats, deviceStats, errors, folderStatus, folderStatus2, folderStatus3, pullErrors, connections, version, empty;
static string events[7]; static std::string events[7];
} // namespace TestData } // namespace TestData
/*! /*!
* \brief Returns the contents of the specified file and exits with an error message if an error occurs. * \brief Returns the contents of the specified file and exits with an error message if an error occurs.
*/ */
static string readMockFile(const string &filePath) static std::string readMockFile(const std::string &filePath)
{ {
try { try {
return readFile(filePath); return readFile(filePath);
} catch (const std::ios_base::failure &failure) { } catch (const std::ios_base::failure &failure) {
cerr << Phrases::Error << "An IO error occurred when reading mock config file \"" << filePath << "\": " << failure.what() std::cerr << Phrases::Error << "An IO error occurred when reading mock config file \"" << filePath << "\": " << failure.what()
<< Phrases::EndFlush; << Phrases::EndFlush;
exit(-2); std::exit(-2);
} }
} }
@ -64,7 +64,7 @@ void setupTestData()
const char *const fileNames[] = { "config", "status", "folderstats", "devicestats", "errors", "folderstatus-01", "folderstatus-02", const char *const fileNames[] = { "config", "status", "folderstats", "devicestats", "errors", "folderstatus-01", "folderstatus-02",
"folderstatus-03", "pullerrors-01", "connections", "version", "empty" }; "folderstatus-03", "pullerrors-01", "connections", "version", "empty" };
const char *const *fileName = fileNames; const char *const *fileName = fileNames;
for (string *testDataVariable : { &config, &status, &folderStats, &deviceStats, &errors, &folderStatus, &folderStatus2, &folderStatus3, for (auto *const testDataVariable : { &config, &status, &folderStats, &deviceStats, &errors, &folderStatus, &folderStatus2, &folderStatus3,
&pullErrors, &connections, &version, &empty }) { &pullErrors, &connections, &version, &empty }) {
*testDataVariable = readMockFile(testApp.testFilePath(argsToString("mocks/", *fileName, ".json"))); *testDataVariable = readMockFile(testApp.testFilePath(argsToString("mocks/", *fileName, ".json")));
++fileName; ++fileName;
@ -72,7 +72,7 @@ void setupTestData()
// read mock files for Event-API // read mock files for Event-API
unsigned int index = 1; unsigned int index = 1;
for (string &event : events) { for (auto &event : events) {
const char *const pad = index < 10 ? "0" : ""; const char *const pad = index < 10 ? "0" : "";
event = readMockFile(testApp.testFilePath(argsToString("mocks/events-", pad, index, ".json"))); event = readMockFile(testApp.testFilePath(argsToString("mocks/events-", pad, index, ".json")));
++index; ++index;
@ -81,7 +81,7 @@ void setupTestData()
initialized = true; initialized = true;
} }
MockedReply::MockedReply(const string &buffer, int delay, QObject *parent) MockedReply::MockedReply(const std::string &buffer, int delay, QObject *parent)
: QNetworkReply(parent) : QNetworkReply(parent)
, m_buffer(buffer) , m_buffer(buffer)
, m_pos(buffer.data()) , m_pos(buffer.data())
@ -124,11 +124,11 @@ qint64 MockedReply::readData(char *data, qint64 maxlen)
if (!m_bytesLeft) { if (!m_bytesLeft) {
return -1; return -1;
} }
const qint64 bytesToRead = static_cast<int>(min<qint64>(m_bytesLeft, maxlen)); const auto bytesToRead = std::min<qint64>(m_bytesLeft, maxlen);
if (!bytesToRead) { if (!bytesToRead) {
return 0; return 0;
} }
copy(m_pos, m_pos + bytesToRead, data); std::copy(m_pos, m_pos + bytesToRead, data);
m_pos += bytesToRead; m_pos += bytesToRead;
m_bytesLeft -= bytesToRead; m_bytesLeft -= bytesToRead;
return bytesToRead; return bytesToRead;
@ -146,8 +146,8 @@ MockedReply *MockedReply::forRequest(const QString &method, const QString &path,
url.setQuery(query); url.setQuery(query);
// find the correct buffer for the request // find the correct buffer for the request
static const string emptyBuffer; static const auto emptyBuffer = std::string();
const string *buffer = &emptyBuffer; const auto *buffer = &emptyBuffer;
int delay = 5; int delay = 5;
{ {
using namespace TestData; using namespace TestData;
@ -182,7 +182,7 @@ MockedReply *MockedReply::forRequest(const QString &method, const QString &path,
buffer = &version; buffer = &version;
} else if (path == QLatin1String("events")) { } else if (path == QLatin1String("events")) {
buffer = &events[s_eventIndex]; buffer = &events[s_eventIndex];
cerr << "mocking: at event index " << s_eventIndex << endl; std::cerr << "mocking: at event index " << s_eventIndex << std::endl;
// "emit" the first event almost immediately and further events each 2.5 seconds // "emit" the first event almost immediately and further events each 2.5 seconds
switch (s_eventIndex) { switch (s_eventIndex) {
case 0: case 0: