From adf07ce4ee7903fcbadf97ff4d041e87a3645685 Mon Sep 17 00:00:00 2001 From: Antony Cherepanov Date: Tue, 27 Sep 2016 13:03:27 +0300 Subject: [PATCH] Fix CheckFile(): check suffix of the file --- src/sources/filechecker.h | 2 +- tests/data/test.file.dots.csv | 2 ++ tests/testreader.cpp | 23 +++++++++++++++++++++++ tests/testreader.h | 2 ++ tests/testwriter.cpp | 33 ++++++++++++++++++++++++++++++++- tests/testwriter.h | 2 ++ 6 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 tests/data/test.file.dots.csv diff --git a/src/sources/filechecker.h b/src/sources/filechecker.h index 062ecc0..1237f3a 100644 --- a/src/sources/filechecker.h +++ b/src/sources/filechecker.h @@ -14,7 +14,7 @@ namespace QtCSV inline bool CheckFile(const QString& filePath) { QFileInfo fileInfo(filePath); - if ( fileInfo.isAbsolute() && "csv" == fileInfo.completeSuffix() ) + if ( fileInfo.isAbsolute() && "csv" == fileInfo.suffix() ) { return true; } diff --git a/tests/data/test.file.dots.csv b/tests/data/test.file.dots.csv new file mode 100644 index 0000000..27f4074 --- /dev/null +++ b/tests/data/test.file.dots.csv @@ -0,0 +1,2 @@ +one,two,three +one_element diff --git a/tests/testreader.cpp b/tests/testreader.cpp index eeb24b5..13dc542 100644 --- a/tests/testreader.cpp +++ b/tests/testreader.cpp @@ -82,6 +82,24 @@ void TestReader::testReadFileWithCommas() } } +void TestReader::testReadFileWithDotsInName() +{ + const QString path = getPathToFileTestDotsInName(); + QList data = QtCSV::Reader::readToList(path); + + QVERIFY2(false == data.isEmpty(), "Failed to read file content"); + + QList expected; + expected << (QStringList() << "one" << "two" << "three"); + expected << (QStringList() << "one_element"); + + QVERIFY2(expected.size() == data.size(), "Wrong number of rows"); + for (int i = 0; i < data.size(); ++i) + { + QVERIFY2(expected.at(i) == data.at(i), "Wrong row data"); + } +} + void TestReader::testReadFileWithCommasToStringData() { const QString path = getPathToFileTestComma(); @@ -321,6 +339,11 @@ QString TestReader::getPathToFileTestComma() const return getPathToFolderWithTestFiles() + "test-comma.csv"; } +QString TestReader::getPathToFileTestDotsInName() const +{ + return getPathToFolderWithTestFiles() + "test.file.dots.csv"; +} + QString TestReader::getPathToFileTestSemicolon() const { return getPathToFolderWithTestFiles() + "test-semicolon.csv"; diff --git a/tests/testreader.h b/tests/testreader.h index 357edb3..f3de599 100644 --- a/tests/testreader.h +++ b/tests/testreader.h @@ -15,6 +15,7 @@ private Q_SLOTS: void testReadToListInvalidArgs(); void testReadToDataInvalidArgs(); void testReadFileWithCommas(); + void testReadFileWithDotsInName(); void testReadFileWithCommasToStringData(); void testReadFileWithCommasToVariantData(); void testReadFileWithSemicolons(); @@ -31,6 +32,7 @@ private Q_SLOTS: private: QString getPathToFolderWithTestFiles() const; QString getPathToFileTestComma() const; + QString getPathToFileTestDotsInName() const; QString getPathToFileTestSemicolon() const; QString getPathToFileTestDataTextDelimDQuotes() const; QString getPathToFileTestDataTextDelimQuotes() const; diff --git a/tests/testwriter.cpp b/tests/testwriter.cpp index 1ba9eab..49e121e 100644 --- a/tests/testwriter.cpp +++ b/tests/testwriter.cpp @@ -17,10 +17,17 @@ TestWriter::TestWriter() void TestWriter::cleanup() { - if ( false == QFile::remove(getFilePath()) ) + if ( QFile::exists(getFilePath()) && + false == QFile::remove(getFilePath()) ) { qDebug() << "Can't remove file:" << getFilePath(); } + + if ( QFile::exists(getFilePathWithDotsInName()) && + false == QFile::remove(getFilePathWithDotsInName()) ) + { + qDebug() << "Can't remove file:" << getFilePathWithDotsInName(); + } } QString TestWriter::getFilePath() const @@ -28,6 +35,11 @@ QString TestWriter::getFilePath() const return QDir::currentPath() + "/test-file.csv"; } +QString TestWriter::getFilePathWithDotsInName() const +{ + return QDir::currentPath() + "/test.file.dots.csv"; +} + void TestWriter::testWriteInvalidArgs() { QVERIFY2(false == QtCSV::Writer::write(QString(), QtCSV::StringData()), @@ -101,6 +113,25 @@ void TestWriter::testWriteFromVariantData() "Wrong values in third row"); } +void TestWriter::testWriteToFileWithDotsInName() +{ + QStringList strList; + strList << "one" << "two" << "three"; + + QtCSV::StringData strData; + strData.addRow(strList); + + bool writeResult = + QtCSV::Writer::write(getFilePathWithDotsInName(), strData); + QVERIFY2(true == writeResult, "Failed to write to file"); + + QList data = + QtCSV::Reader::readToList(getFilePathWithDotsInName()); + QVERIFY2(false == data.isEmpty(), "Failed to read file content"); + QVERIFY2(1 == data.size(), "Wrong number of rows"); + QVERIFY2(strList == data.at(0), "Wrong data"); +} + void TestWriter::testWriteAppendMode() { QStringList strFirstList; diff --git a/tests/testwriter.h b/tests/testwriter.h index 29ee683..c81270e 100644 --- a/tests/testwriter.h +++ b/tests/testwriter.h @@ -18,6 +18,7 @@ private Q_SLOTS: void testWriteInvalidArgs(); void testWriteFromStringData(); void testWriteFromVariantData(); + void testWriteToFileWithDotsInName(); void testWriteAppendMode(); void testWriteWithNotDefaultSeparator(); void testWriteWithHeader(); @@ -29,6 +30,7 @@ private Q_SLOTS: private: QString getFilePath() const; + QString getFilePathWithDotsInName() const; QtCSV::StringData getTestStringData(const int &symbolsInRow, const int &rowsNumber); };