• 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
testcsvparser.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  TestCSVParser.cpp - K Desktop Planetarium
3  -------------------
4  begin : 2012/23/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 /*
19  * Justification for not testing CombineQuoteParts separately:
20  * Changing the structure of KSParser would solve this (by removing the
21  * segments to be tested into separate classes) but would unnecessarily
22  * complicate things resulting in a large number of small classes.
23  * This is good from an OOD perspective, but would make the code unmanageable
24 */
25 
26 #include "testcsvparser.h"
27 
28 #include <QDir>
29 #include <ktemporaryfile.h>
30 
31 TestCSVParser::TestCSVParser(): QObject() {
32 }
33 
34 void TestCSVParser::initTestCase() {
35  /*
36  * Justification for doing this instead of simply creating a file:
37  * To add/change tests, we'll need to modify 2 places. The file and this class.
38  * So we write the file from the class.
39  */
40  test_cases_.append("\n");
41  test_cases_.append(QString(","
42  "isn't,"
43  "it,"
44  "\"amusing\","
45  "how,"
46  "3,"
47  "\"isn't, pi\","
48  "and,"
49  "\"\","
50  "-3.141,"
51  "isn't,"
52  "either\n"));
53  test_cases_.append(QString(","
54  "isn't,"
55  "it,"
56  "\"amusing\","
57  "how,"
58  "3,"
59  "\"isn't\"(, )\"pi\","
60  "and,"
61  "\"\","
62  "-3.141,"
63  "isn't,"
64  "either\n"));
65  test_cases_.append(QString(","
66  "isn't,"
67  "it,"
68  "\"amusing\","
69  "how,"
70  "3,"
71  "\"isn't, pi\","
72  "and,"
73  "\"\",")); // less than required fields
74  test_cases_.append(QString(","
75  "isn't,"
76  "it,"
77  "\"amusing\","
78  "how,"
79  "3,"
80  "\"isn't, pi\","
81  "and,"
82  "\"," // no matching "
83  "-3.141,"
84  "isn't,"
85  "either\n"));
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();
94  QTextStream out_stream(&temp_file);
95  foreach(const QString &test_case, test_cases_)
96  out_stream << test_case;
97  temp_file.close();
98 
99  //Building the sequence to be used. Includes all available types.
100  sequence_.clear();
101  sequence_.append(qMakePair(QString("field1"), KSParser::D_QSTRING));
102  sequence_.append(qMakePair(QString("field2"), KSParser::D_QSTRING));
103  sequence_.append(qMakePair(QString("field3"), KSParser::D_QSTRING));
104  sequence_.append(qMakePair(QString("field4"), KSParser::D_QSTRING));
105  sequence_.append(qMakePair(QString("field5"), KSParser::D_QSTRING));
106  sequence_.append(qMakePair(QString("field6"), KSParser::D_INT));
107  sequence_.append(qMakePair(QString("field7"), KSParser::D_QSTRING));
108  sequence_.append(qMakePair(QString("field8"), KSParser::D_QSTRING));
109  sequence_.append(qMakePair(QString("field9"), KSParser::D_QSTRING));
110  sequence_.append(qMakePair(QString("field10"), KSParser::D_FLOAT));
111  sequence_.append(qMakePair(QString("field11"), KSParser::D_QSTRING));
112  sequence_.append(qMakePair(QString("field12"), KSParser::D_QSTRING));
113 
114  test_parser_ = new KSParser(test_file_name_, '#', sequence_);
115 }
116 
117 TestCSVParser::~TestCSVParser()
118 {
119 }
120 
121 void TestCSVParser::cleanupTestCase() {
122  delete test_parser_;
123 }
124 
125 
126  /*
127  * The following tests checks for the following cases for CSV files
128  * 1. Mixed inputs (See test case for description)
129  * 1b. Quoteception
130  * 2. Empty Row
131  * 3. No row (only a newline character)
132  * 4. Truncated row
133  * 5. Row with no matching quote
134  * 6. Attempt to read missing file
135  *
136  */
137 
138 
139 void TestCSVParser::CSVMixedInputs() {
140  /*
141  * Test 1. Includes input of the form:
142  *
143  * It starts with a newline char which should be skipped by virtue
144  * of the design of the parser
145  *
146  * Then a row with the following types of inputs:
147  * 1. empty column
148  * 2. simple single word
149  * 3. single word in quotes
150  * 4. multiple words with , in quotes
151  * 5. integer
152  * 6. float
153  */
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"));
168 }
169 
170 void TestCSVParser::CSVQuotesInQuotes() {
171  /*
172  * Test 1b. Identical to 1 except quotes in quotes
173  * in Field 7
174  *
175  */
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"));
190 }
191 
192 
193 void TestCSVParser::CSVEmptyRow() {
194  /* Test 2. Row with less rows than required (to be skipped)
195  * Test 3. Row with truncated \" i.e. no matching "
196  * (should be skipped)
197  */
198  /*
199  * Test 4. Attempt to read an empty but valid row
200  *
201  * Also includes test for:
202  * 1. missing integer
203  * 2. missing float
204  * 3. missing string
205  */
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(""));
220 }
221 
222 void TestCSVParser::CSVNoRow() {
223  /*
224  * Test 3. Attempt to read a newline char instead of a row
225  * The parser is designed to skip an empty row so we can
226  * test this for a boundary case. i.e. newline at the end.
227  */
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"));
242 }
243 
244 void TestCSVParser::CSVIgnoreHasNextRow() {
245  QHash<QString, QVariant> row_content;
246  for (int times = 0; times < 20; times++) {
247  row_content = test_parser_->ReadNextRow();
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"));
260  }
261 }
262 
263 
264 void TestCSVParser::CSVReadMissingFile() {
265  /*
266  * Test 6. Attempt to read a missing file repeatedly
267  */
268  QFile::remove(test_file_name_);
269 
270  KSParser missing_parser(test_file_name_, '#', sequence_);
271  QHash<QString, QVariant> row_content = missing_parser.ReadNextRow();
272 
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"));
287  }
288 }
289 
290 
291 QTEST_MAIN(TestCSVParser)
292 
293 #include "testcsvparser.moc"
KSParser
Generic class for text file parsers used in KStars.
Definition: ksparser.h:49
QObject
TestCSVParser
Definition: testcsvparser.h:27
KSParser::D_FLOAT
Definition: ksparser.h:70
KSParser::D_INT
Definition: ksparser.h:69
QTextStream
testcsvparser.h
TestCSVParser::~TestCSVParser
~TestCSVParser()
Definition: testcsvparser.cpp:117
TestCSVParser::TestCSVParser
TestCSVParser()
Definition: testcsvparser.cpp:31
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