• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeedu API Reference
  • KDE Home
  • Contact Us
 

kstars

  • sources
  • kde-4.12
  • kdeedu
  • kstars
  • Tests
testfwparser.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  TestFWParser.cpp - K Desktop Planetarium
3  -------------------
4  begin : 2012/24/07
5  copyright : (C) 2012 by Rishab Arora
6  email : ra.rishab@gmail.com
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #include "testfwparser.h"
19 
20 #include <QDir>
21 #include <ktemporaryfile.h>
22 
23 TestFWParser::TestFWParser(): QObject() {
24 }
25 
26 void TestFWParser::initTestCase() {
27  test_cases_.append(
28  "this is an exam ple of 256 cases being tested -3.14 times\n");
29  test_cases_.append(
30  " \n");
31  test_cases_.append("this is an ex\n\n");
32 
33  KTemporaryFile temp_file;
34  temp_file.setPrefix(QDir::tempPath() + "/");
35  temp_file.setSuffix(".txt");
36  temp_file.setAutoRemove(false);
37  QVERIFY(temp_file.open());
38  test_file_name_ = temp_file.fileName();
39  QTextStream out_stream(&temp_file);
40  foreach(const QString &test_case, test_cases_)
41  out_stream << test_case;
42  temp_file.close();
43 
44  //Building the sequence to be used. Includes all available types.
45  sequence_.clear();
46  sequence_.append(qMakePair(QString("field1"), KSParser::D_QSTRING));
47  sequence_.append(qMakePair(QString("field2"), KSParser::D_QSTRING));
48  sequence_.append(qMakePair(QString("field3"), KSParser::D_QSTRING));
49  sequence_.append(qMakePair(QString("field4"), KSParser::D_QSTRING));
50  sequence_.append(qMakePair(QString("field5"), KSParser::D_QSTRING));
51  sequence_.append(qMakePair(QString("field6"), KSParser::D_INT));
52  sequence_.append(qMakePair(QString("field7"), KSParser::D_QSTRING));
53  sequence_.append(qMakePair(QString("field8"), KSParser::D_QSTRING));
54  sequence_.append(qMakePair(QString("field9"), KSParser::D_QSTRING));
55  sequence_.append(qMakePair(QString("field10"), KSParser::D_FLOAT));
56  sequence_.append(qMakePair(QString("field11"), KSParser::D_QSTRING));
57  sequence_.append(qMakePair(QString("field12"), KSParser::D_QSTRING));
58  widths_.append(5);
59  widths_.append(3);
60  widths_.append(3);
61  widths_.append(9);
62  widths_.append(3);
63  widths_.append(4);
64  widths_.append(6);
65  widths_.append(6);
66  widths_.append(7);
67  widths_.append(6);
68  widths_.append(6); //'repeatedly' doesn't need a width
69 
70  test_parser_ = new KSParser(test_file_name_, '#', sequence_, widths_);
71 }
72 
73 TestFWParser::~TestFWParser()
74 {
75 }
76 
77 void TestFWParser::cleanupTestCase() {
78  delete test_parser_;
79 }
80 
81 void TestFWParser::MixedInputs() {
82  /*
83  * Test 1: Checks all conversions are working as expected
84  */
85  QHash<QString, QVariant> row_content = test_parser_->ReadNextRow();
86 
87  QCOMPARE(row_content["field1"].toString(), QString("this"));
88  QCOMPARE(row_content["field2"].toString(), QString("is"));
89  QCOMPARE(row_content["field3"].toString(), QString("an"));
90  QCOMPARE(row_content["field4"].toString(), QString("exam ple"));
91  QCOMPARE(row_content["field5"].toString(), QString("of"));
92  QCOMPARE(row_content["field6"].toInt(), 256);
93  QCOMPARE(row_content["field7"].toString(), QString("cases"));
94  QCOMPARE(row_content["field8"].toString(), QString("being"));
95  QCOMPARE(row_content["field9"].toString(), QString("tested"));
96  QVERIFY(row_content["field10"].toFloat() + 3.141 < 0.1);
97  QCOMPARE(row_content["field11"].toString(), QString(""));
98  QCOMPARE(row_content["field12"].toString(), QString("times"));
99 }
100 
101 void TestFWParser::OnlySpaceRow() {
102  /*
103  * Test 2: Checks what happens in case of reading an empty space row
104  */
105  QHash<QString, QVariant> row_content = test_parser_->ReadNextRow();
106 
107  QCOMPARE(row_content["field1"].toString(), QString(""));
108  QCOMPARE(row_content["field2"].toString(), QString(""));
109  QCOMPARE(row_content["field3"].toString(), QString(""));
110  QCOMPARE(row_content["field4"].toString(), QString(""));
111  QCOMPARE(row_content["field5"].toString(), QString(""));
112  QCOMPARE(row_content["field6"].toInt(), 0);
113  QCOMPARE(row_content["field7"].toString(), QString(""));
114  QCOMPARE(row_content["field8"].toString(), QString(""));
115  QCOMPARE(row_content["field9"].toString(), QString(""));
116  QCOMPARE(row_content["field10"].toFloat(), float(0.0));
117  QCOMPARE(row_content["field11"].toString(), QString(""));
118  QCOMPARE(row_content["field12"].toString(), QString(""));
119 }
120 
121 void TestFWParser::NoRow() {
122  /*
123  * Test 3:
124  * This test also tests what happens if we have a partial row or a
125  * truncated row. It is simply skipped.
126  *
127  * It then reaches a point where the file ends.
128  * We attempt reading a file after EOF 20 times
129  */
130  QHash<QString, QVariant> row_content;
131  qDebug() << row_content["field12"];
132 
133  for (int times = 0; times < 20; times++) {
134  row_content = test_parser_->ReadNextRow();
135  QCOMPARE(row_content["field1"].toString(), QString("Null"));
136  QCOMPARE(row_content["field2"].toString(), QString("Null"));
137  QCOMPARE(row_content["field3"].toString(), QString("Null"));
138  QCOMPARE(row_content["field4"].toString(), QString("Null"));
139  QCOMPARE(row_content["field5"].toString(), QString("Null"));
140  QCOMPARE(row_content["field6"].toInt(), 0);
141  QCOMPARE(row_content["field7"].toString(), QString("Null"));
142  QCOMPARE(row_content["field8"].toString(), QString("Null"));
143  QCOMPARE(row_content["field9"].toString(), QString("Null"));
144  QCOMPARE(row_content["field10"].toFloat(), float(0.0));
145  QCOMPARE(row_content["field11"].toString(), QString("Null"));
146  QCOMPARE(row_content["field12"].toString(), QString("Null"));
147  }
148 }
149 
150 void TestFWParser::FWReadMissingFile()
151 {
152  /*
153  * Test 4:
154  * This tests how the parser reacts if there is no file with the
155  * given path.
156  */
157  QFile::remove(test_file_name_);
158 
159  KSParser missing_parser(test_file_name_, '#', sequence_, widths_);
160  QHash<QString, QVariant> row_content = missing_parser.ReadNextRow();
161 
162  for (int times = 0; times < 20; times++) {
163  row_content = missing_parser.ReadNextRow();
164  QCOMPARE(row_content["field1"].toString(), QString("Null"));
165  QCOMPARE(row_content["field2"].toString(), QString("Null"));
166  QCOMPARE(row_content["field3"].toString(), QString("Null"));
167  QCOMPARE(row_content["field4"].toString(), QString("Null"));
168  QCOMPARE(row_content["field5"].toString(), QString("Null"));
169  QCOMPARE(row_content["field6"].toInt(), 0);
170  QCOMPARE(row_content["field7"].toString(), QString("Null"));
171  QCOMPARE(row_content["field8"].toString(), QString("Null"));
172  QCOMPARE(row_content["field9"].toString(), QString("Null"));
173  QCOMPARE(row_content["field10"].toFloat(), float(0.0));
174  QCOMPARE(row_content["field11"].toString(), QString("Null"));
175  QCOMPARE(row_content["field12"].toString(), QString("Null"));
176  }
177 }
178 
179 
180 
181 QTEST_MAIN(TestFWParser)
182 
183 #include "testfwparser.moc"
184 
KSParser
Generic class for text file parsers used in KStars.
Definition: ksparser.h:49
TestFWParser
Definition: testfwparser.h:26
QObject
TestFWParser::~TestFWParser
~TestFWParser()
Definition: testfwparser.cpp:73
testfwparser.h
KSParser::D_FLOAT
Definition: ksparser.h:70
TestFWParser::TestFWParser
TestFWParser()
Definition: testfwparser.cpp:23
KSParser::D_INT
Definition: ksparser.h:69
QTextStream
KSParser::ReadNextRow
QHash< QString, QVariant > ReadNextRow()
Generic function used to read the next row of a text file.
Definition: ksparser.cpp:56
KSParser::D_QSTRING
Definition: ksparser.h:68
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:36:21 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kstars

Skip menu "kstars"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdeedu API Reference

Skip menu "kdeedu API Reference"
  • Analitza
  •     lib
  • kalgebra
  • kalzium
  •   libscience
  • kanagram
  • kig
  •   lib
  • klettres
  • kstars
  • libkdeedu
  •   keduvocdocument
  • marble
  • parley
  • rocs
  •   App
  •   RocsCore
  •   VisualEditor
  •   stepcore

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal