File indexing completed on 2024-04-28 15:11:57
0001 /* 0002 SPDX-FileCopyrightText: 2012 Rishab Arora <ra.rishab@gmail.com> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include "testfwparser.h" 0008 0009 #include <QDir> 0010 #include <QTemporaryFile> 0011 0012 TestFWParser::TestFWParser() : QObject() 0013 { 0014 } 0015 0016 void TestFWParser::initTestCase() 0017 { 0018 test_cases_.append("this is an exam ple of 256 cases being tested -3.14 times\n"); 0019 test_cases_.append(" \n"); 0020 test_cases_.append("this is an ex\n\n"); 0021 0022 QTemporaryFile temp_file; 0023 //temp_file.setPrefix(QDir::tempPath() + QDir::separator()); 0024 //temp_file.setSuffix(".txt"); 0025 temp_file.setAutoRemove(false); 0026 QVERIFY(temp_file.open()); 0027 test_file_name_ = temp_file.fileName(); 0028 QTextStream out_stream(&temp_file); 0029 foreach (const QString &test_case, test_cases_) 0030 out_stream << test_case; 0031 temp_file.close(); 0032 0033 //Building the sequence to be used. Includes all available types. 0034 sequence_.clear(); 0035 sequence_.append(qMakePair(QString("field1"), KSParser::D_QSTRING)); 0036 sequence_.append(qMakePair(QString("field2"), KSParser::D_QSTRING)); 0037 sequence_.append(qMakePair(QString("field3"), KSParser::D_QSTRING)); 0038 sequence_.append(qMakePair(QString("field4"), KSParser::D_QSTRING)); 0039 sequence_.append(qMakePair(QString("field5"), KSParser::D_QSTRING)); 0040 sequence_.append(qMakePair(QString("field6"), KSParser::D_INT)); 0041 sequence_.append(qMakePair(QString("field7"), KSParser::D_QSTRING)); 0042 sequence_.append(qMakePair(QString("field8"), KSParser::D_QSTRING)); 0043 sequence_.append(qMakePair(QString("field9"), KSParser::D_QSTRING)); 0044 sequence_.append(qMakePair(QString("field10"), KSParser::D_FLOAT)); 0045 sequence_.append(qMakePair(QString("field11"), KSParser::D_QSTRING)); 0046 sequence_.append(qMakePair(QString("field12"), KSParser::D_QSTRING)); 0047 widths_.append(5); 0048 widths_.append(3); 0049 widths_.append(3); 0050 widths_.append(9); 0051 widths_.append(3); 0052 widths_.append(4); 0053 widths_.append(6); 0054 widths_.append(6); 0055 widths_.append(7); 0056 widths_.append(6); 0057 widths_.append(6); //'repeatedly' doesn't need a width 0058 0059 test_parser_ = new KSParser(test_file_name_, '#', sequence_, widths_); 0060 } 0061 0062 void TestFWParser::cleanupTestCase() 0063 { 0064 delete test_parser_; 0065 } 0066 0067 void TestFWParser::MixedInputs() 0068 { 0069 /* 0070 * Test 1: Checks all conversions are working as expected 0071 */ 0072 QHash<QString, QVariant> row_content = test_parser_->ReadNextRow(); 0073 0074 QCOMPARE(row_content["field1"].toString(), QString("this")); 0075 QCOMPARE(row_content["field2"].toString(), QString("is")); 0076 QCOMPARE(row_content["field3"].toString(), QString("an")); 0077 QCOMPARE(row_content["field4"].toString(), QString("exam ple")); 0078 QCOMPARE(row_content["field5"].toString(), QString("of")); 0079 QCOMPARE(row_content["field6"].toInt(), 256); 0080 QCOMPARE(row_content["field7"].toString(), QString("cases")); 0081 QCOMPARE(row_content["field8"].toString(), QString("being")); 0082 QCOMPARE(row_content["field9"].toString(), QString("tested")); 0083 QVERIFY(row_content["field10"].toFloat() + 3.141 < 0.1); 0084 QCOMPARE(row_content["field11"].toString(), QString("")); 0085 QCOMPARE(row_content["field12"].toString(), QString("times")); 0086 } 0087 0088 void TestFWParser::OnlySpaceRow() 0089 { 0090 /* 0091 * Test 2: Checks what happens in case of reading an empty space row 0092 */ 0093 QHash<QString, QVariant> row_content = test_parser_->ReadNextRow(); 0094 0095 QCOMPARE(row_content["field1"].toString(), QString("")); 0096 QCOMPARE(row_content["field2"].toString(), QString("")); 0097 QCOMPARE(row_content["field3"].toString(), QString("")); 0098 QCOMPARE(row_content["field4"].toString(), QString("")); 0099 QCOMPARE(row_content["field5"].toString(), QString("")); 0100 QCOMPARE(row_content["field6"].toInt(), 0); 0101 QCOMPARE(row_content["field7"].toString(), QString("")); 0102 QCOMPARE(row_content["field8"].toString(), QString("")); 0103 QCOMPARE(row_content["field9"].toString(), QString("")); 0104 QCOMPARE(row_content["field10"].toFloat(), float(0.0)); 0105 QCOMPARE(row_content["field11"].toString(), QString("")); 0106 QCOMPARE(row_content["field12"].toString(), QString("")); 0107 } 0108 0109 void TestFWParser::NoRow() 0110 { 0111 /* 0112 * Test 3: 0113 * This test also tests what happens if we have a partial row or a 0114 * truncated row. It is simply skipped. 0115 * 0116 * It then reaches a point where the file ends. 0117 * We attempt reading a file after EOF 20 times 0118 */ 0119 QHash<QString, QVariant> row_content; 0120 qDebug() << row_content["field12"]; 0121 0122 for (int times = 0; times < 20; times++) 0123 { 0124 row_content = test_parser_->ReadNextRow(); 0125 QCOMPARE(row_content["field1"].toString(), QString("Null")); 0126 QCOMPARE(row_content["field2"].toString(), QString("Null")); 0127 QCOMPARE(row_content["field3"].toString(), QString("Null")); 0128 QCOMPARE(row_content["field4"].toString(), QString("Null")); 0129 QCOMPARE(row_content["field5"].toString(), QString("Null")); 0130 QCOMPARE(row_content["field6"].toInt(), 0); 0131 QCOMPARE(row_content["field7"].toString(), QString("Null")); 0132 QCOMPARE(row_content["field8"].toString(), QString("Null")); 0133 QCOMPARE(row_content["field9"].toString(), QString("Null")); 0134 QCOMPARE(row_content["field10"].toFloat(), float(0.0)); 0135 QCOMPARE(row_content["field11"].toString(), QString("Null")); 0136 QCOMPARE(row_content["field12"].toString(), QString("Null")); 0137 } 0138 } 0139 0140 void TestFWParser::FWReadMissingFile() 0141 { 0142 /* 0143 * Test 4: 0144 * This tests how the parser reacts if there is no file with the 0145 * given path. 0146 */ 0147 QFile::remove(test_file_name_); 0148 0149 KSParser missing_parser(test_file_name_, '#', sequence_, widths_); 0150 QHash<QString, QVariant> row_content = missing_parser.ReadNextRow(); 0151 0152 for (int times = 0; times < 20; times++) 0153 { 0154 row_content = missing_parser.ReadNextRow(); 0155 QCOMPARE(row_content["field1"].toString(), QString("Null")); 0156 QCOMPARE(row_content["field2"].toString(), QString("Null")); 0157 QCOMPARE(row_content["field3"].toString(), QString("Null")); 0158 QCOMPARE(row_content["field4"].toString(), QString("Null")); 0159 QCOMPARE(row_content["field5"].toString(), QString("Null")); 0160 QCOMPARE(row_content["field6"].toInt(), 0); 0161 QCOMPARE(row_content["field7"].toString(), QString("Null")); 0162 QCOMPARE(row_content["field8"].toString(), QString("Null")); 0163 QCOMPARE(row_content["field9"].toString(), QString("Null")); 0164 QCOMPARE(row_content["field10"].toFloat(), float(0.0)); 0165 QCOMPARE(row_content["field11"].toString(), QString("Null")); 0166 QCOMPARE(row_content["field12"].toString(), QString("Null")); 0167 } 0168 } 0169 0170 QTEST_GUILESS_MAIN(TestFWParser)