29 #include <ktemporaryfile.h> 
   34 void TestCSVParser::initTestCase() {
 
   40   test_cases_.append(
"\n");
 
   41   test_cases_.append(QString(
"," 
   53     test_cases_.append(QString(
"," 
   59                          "\"isn't\"(, )\"pi\"," 
   65   test_cases_.append(QString(
"," 
   74   test_cases_.append(QString(
"," 
   86   test_cases_.append(
",,,,,,,,,,,\n");
 
   87   test_cases_.append(
"\n");
 
   88   KTemporaryFile temp_file;
 
   89   temp_file.setPrefix(QDir::tempPath() + 
"/");
 
   90   temp_file.setSuffix(
".txt");
 
   91   temp_file.setAutoRemove(
false);
 
   92   QVERIFY(temp_file.open());
 
   93   test_file_name_ = temp_file.fileName();
 
   95   foreach(
const QString &test_case, test_cases_)
 
   96     out_stream << test_case;
 
  114   test_parser_ = 
new KSParser(test_file_name_, 
'#', sequence_);
 
  121 void TestCSVParser::cleanupTestCase() {
 
  139 void TestCSVParser::CSVMixedInputs() {
 
  154   QHash<QString, QVariant> row_content = test_parser_->
ReadNextRow();
 
  155   qDebug() << row_content[
"field1"];
 
  156   QCOMPARE(row_content[
"field1"].toString(), QString(
""));
 
  157   QCOMPARE(row_content[
"field2"].toString(), QString(
"isn't"));
 
  158   QCOMPARE(row_content[
"field3"].toString(), QString(
"it"));
 
  159   QCOMPARE(row_content[
"field4"].toString(), QString(
"amusing"));
 
  160   QCOMPARE(row_content[
"field5"].toString(), QString(
"how"));
 
  161   QCOMPARE(row_content[
"field6"].toInt(), 3);
 
  162   QCOMPARE(row_content[
"field7"].toString(), QString(
"isn't, pi"));
 
  163   QCOMPARE(row_content[
"field8"].toString(), QString(
"and"));
 
  164   QCOMPARE(row_content[
"field9"].toString(), QString(
""));
 
  165   QVERIFY(row_content[
"field10"].toFloat() + 3.141 < 0.1);
 
  166   QCOMPARE(row_content[
"field11"].toString(), QString(
"isn't"));
 
  167   QCOMPARE(row_content[
"field12"].toString(), QString(
"either"));
 
  170 void TestCSVParser::CSVQuotesInQuotes() {
 
  176   QHash<QString, QVariant> row_content = test_parser_->
ReadNextRow();
 
  177   qDebug() << row_content[
"field7"];
 
  178   QCOMPARE(row_content[
"field1"].toString(), QString(
""));
 
  179   QCOMPARE(row_content[
"field2"].toString(), QString(
"isn't"));
 
  180   QCOMPARE(row_content[
"field3"].toString(), QString(
"it"));
 
  181   QCOMPARE(row_content[
"field4"].toString(), QString(
"amusing"));
 
  182   QCOMPARE(row_content[
"field5"].toString(), QString(
"how"));
 
  183   QCOMPARE(row_content[
"field6"].toInt(), 3);
 
  184   QCOMPARE(row_content[
"field7"].toString(), QString(
"isn't\"(, )\"pi"));
 
  185   QCOMPARE(row_content[
"field8"].toString(), QString(
"and"));
 
  186   QCOMPARE(row_content[
"field9"].toString(), QString(
""));
 
  187   QVERIFY(row_content[
"field10"].toFloat() + 3.141 < 0.1);
 
  188   QCOMPARE(row_content[
"field11"].toString(), QString(
"isn't"));
 
  189   QCOMPARE(row_content[
"field12"].toString(), QString(
"either"));
 
  193 void TestCSVParser::CSVEmptyRow() {
 
  206   QHash<QString, QVariant> row_content = test_parser_->
ReadNextRow();
 
  207   qDebug() << row_content[
"field1"];
 
  208   QCOMPARE(row_content[
"field1"].toString(), QString(
""));
 
  209   QCOMPARE(row_content[
"field2"].toString(), QString(
""));
 
  210   QCOMPARE(row_content[
"field3"].toString(), QString(
""));
 
  211   QCOMPARE(row_content[
"field4"].toString(), QString(
""));
 
  212   QCOMPARE(row_content[
"field5"].toString(), QString(
""));
 
  213   QCOMPARE(row_content[
"field6"].toInt(), 0);
 
  214   QCOMPARE(row_content[
"field7"].toString(), QString(
""));
 
  215   QCOMPARE(row_content[
"field8"].toString(), QString(
""));
 
  216   QCOMPARE(row_content[
"field9"].toString(), QString(
""));
 
  217   QCOMPARE(row_content[
"field10"].toFloat(), 
float(0.0));
 
  218   QCOMPARE(row_content[
"field11"].toString(), QString(
""));
 
  219   QCOMPARE(row_content[
"field12"].toString(), QString(
""));
 
  222 void TestCSVParser::CSVNoRow() {
 
  228   QHash<QString, QVariant> row_content = test_parser_->
ReadNextRow();
 
  229   qDebug() << row_content[
"field1"];
 
  230   QCOMPARE(row_content[
"field1"].toString(), QString(
"Null"));
 
  231   QCOMPARE(row_content[
"field2"].toString(), QString(
"Null"));
 
  232   QCOMPARE(row_content[
"field3"].toString(), QString(
"Null"));
 
  233   QCOMPARE(row_content[
"field4"].toString(), QString(
"Null"));
 
  234   QCOMPARE(row_content[
"field5"].toString(), QString(
"Null"));
 
  235   QCOMPARE(row_content[
"field6"].toInt(), 0);
 
  236   QCOMPARE(row_content[
"field7"].toString(), QString(
"Null"));
 
  237   QCOMPARE(row_content[
"field8"].toString(), QString(
"Null"));
 
  238   QCOMPARE(row_content[
"field9"].toString(), QString(
"Null"));
 
  239   QCOMPARE(row_content[
"field10"].toFloat(), 
float(0.0));
 
  240   QCOMPARE(row_content[
"field11"].toString(), QString(
"Null"));
 
  241   QCOMPARE(row_content[
"field12"].toString(), QString(
"Null"));
 
  244 void TestCSVParser::CSVIgnoreHasNextRow() {
 
  245   QHash<QString, QVariant> row_content;
 
  246   for (
int times = 0; times < 20; times++) {
 
  248     QCOMPARE(row_content[
"field1"].toString(), QString(
"Null"));
 
  249     QCOMPARE(row_content[
"field2"].toString(), QString(
"Null"));
 
  250     QCOMPARE(row_content[
"field3"].toString(), QString(
"Null"));
 
  251     QCOMPARE(row_content[
"field4"].toString(), QString(
"Null"));
 
  252     QCOMPARE(row_content[
"field5"].toString(), QString(
"Null"));
 
  253     QCOMPARE(row_content[
"field6"].toInt(), 0);
 
  254     QCOMPARE(row_content[
"field7"].toString(), QString(
"Null"));
 
  255     QCOMPARE(row_content[
"field8"].toString(), QString(
"Null"));
 
  256     QCOMPARE(row_content[
"field9"].toString(), QString(
"Null"));
 
  257     QCOMPARE(row_content[
"field10"].toFloat(), 
float(0.0));
 
  258     QCOMPARE(row_content[
"field11"].toString(), QString(
"Null"));
 
  259     QCOMPARE(row_content[
"field12"].toString(), QString(
"Null"));
 
  264 void TestCSVParser::CSVReadMissingFile() {
 
  268   QFile::remove(test_file_name_);
 
  270   KSParser missing_parser(test_file_name_, 
'#', sequence_);
 
  271   QHash<QString, QVariant> row_content = missing_parser.ReadNextRow();
 
  273   for (
int times = 0; times < 20; times++) {
 
  274     row_content = missing_parser.ReadNextRow();
 
  275     QCOMPARE(row_content[
"field1"].toString(), QString(
"Null"));
 
  276     QCOMPARE(row_content[
"field2"].toString(), QString(
"Null"));
 
  277     QCOMPARE(row_content[
"field3"].toString(), QString(
"Null"));
 
  278     QCOMPARE(row_content[
"field4"].toString(), QString(
"Null"));
 
  279     QCOMPARE(row_content[
"field5"].toString(), QString(
"Null"));
 
  280     QCOMPARE(row_content[
"field6"].toInt(), 0);
 
  281     QCOMPARE(row_content[
"field7"].toString(), QString(
"Null"));
 
  282     QCOMPARE(row_content[
"field8"].toString(), QString(
"Null"));
 
  283     QCOMPARE(row_content[
"field9"].toString(), QString(
"Null"));
 
  284     QCOMPARE(row_content[
"field10"].toFloat(), 
float(0.0));
 
  285     QCOMPARE(row_content[
"field11"].toString(), QString(
"Null"));
 
  286     QCOMPARE(row_content[
"field12"].toString(), QString(
"Null"));
 
  293 #include "testcsvparser.moc" 
Generic class for text file parsers used in KStars. 
 
QHash< QString, QVariant > ReadNextRow()
Generic function used to read the next row of a text file.