Skip to content

Commit

Permalink
Add start of qx-common-io tests
Browse files Browse the repository at this point in the history
  • Loading branch information
oblivioncth committed Aug 2, 2023
1 parent 695b08c commit 0db75b1
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 0 deletions.
1 change: 1 addition & 0 deletions tests/io/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
add_subdirectory(qx_common_io)
21 changes: 21 additions & 0 deletions tests/io/qx_common_io/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
include(OB/Test)

ob_add_basic_standard_test(
TARGET_PREFIX "${TESTS_TARGET_PREFIX}"
TARGET_VAR test_target
LINKS
${TESTS_COMMON_TARGET}
Qx::Io
)

# Bundle test data
file(GLOB_RECURSE text_data
RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}
"data/*.*"
)

qt_add_resources(${test_target} "tst_qx_common_io_data"
PREFIX "/"
FILES
${text_data}
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
0123456789
01Jishy
SaltSalivaMan
test456789
0123456789
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Jishy
SaltSalivaMan
test
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
0123456789
0123456789
0123456789
0123456789
0123456789
87 changes: 87 additions & 0 deletions tests/io/qx_common_io/tst_qx_common_io.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// Qt Includes
#include <QtTest>

// Qx Includes
#include <qx/io/qx-common-io.h>

// Test Includes
//#include <qx_test_common.h>

class tst_qx_common_io : public QObject
{
Q_OBJECT

private:
QTemporaryDir mWriteDir;

public:
tst_qx_common_io();

private slots:
// Init
// void initTestCase();
// void initTestCase_data();
// void cleanupTestCase();
// void init()
// void cleanup();

// Test cases
void writeStringToFile_data();
void writeStringToFile();
};

// Setup
tst_qx_common_io::tst_qx_common_io()
{
QVERIFY(mWriteDir.isValid());
}

// Cases
void tst_qx_common_io::writeStringToFile_data()
{
QTest::addColumn<QFile*>("file");
QTest::addColumn<QString>("input");
QTest::addColumn<QString>("expected");

// TODO: Make this more generic with helper functions once more test files are added

// Overwrite
QString overwritePath = mWriteDir.filePath("overwrite_file.txt");
QVERIFY(QFile::copy(u":/data/writeStringToFile/overwrite_original.txt"_s, overwritePath));
QFile::setPermissions(overwritePath, QFile::WriteOwner); // Required since copying from a file marked read-only

QFile inputFile(u":/data/writeStringToFile/overwrite_input.txt"_s);
QVERIFY(inputFile.open(QIODevice::ReadOnly | QIODevice::Text));
QString input = inputFile.readAll();
inputFile.close();

QFile expectedFile(u":/data/writeStringToFile/overwrite_expected.txt"_s);
QVERIFY(expectedFile.open(QIODevice::ReadOnly | QIODevice::Text));
QString expected = expectedFile.readAll();
expectedFile.close();

QTest::newRow("Overwrite") << new QFile(overwritePath, this) << input << expected;
}

void tst_qx_common_io::writeStringToFile()
{
// Fetch data from test table
QFETCH(QFile*, file);
QFETCH(QString, input);
QFETCH(QString, expected);

// Write to file
// TODO: Handle different arguments for writeStringToFile in _data
Qx::IoOpReport rp = Qx::writeStringToFile(*file, input, Qx::Overwrite, Qx::TextPos(1,2));
QVERIFY(!rp.isFailure());

// Get file's new contents and compare
QVERIFY(file->open(QIODevice::ReadOnly | QIODevice::Text));
QTextStream ts(file); // Handles proper conversion of \r\n to \n
QString newData = ts.readAll();
QCOMPARE(newData, expected);
file->close();
}

QTEST_APPLESS_MAIN(tst_qx_common_io)
#include "tst_qx_common_io.moc"

0 comments on commit 0db75b1

Please sign in to comment.