LibKEduVocDocument

dummyreader.h
1/*
2 * SPDX-FileCopyrightText: 2014 Andreas Xavier <andxav at zoho dot com>
3 * SPDX-License-Identifier: GPL-2.0-or-later
4 */
5
6#ifndef DUMMYREADER_H
7#define DUMMYREADER_H
8
9#include "readerbase.h"
10
11#include <QXmlStreamReader>
12
13#include <QDebug>
14
15class QIODevice;
16
17/**
18 * @brief A dummy reader to be used in unit tests
19 * @details DummyReader always returns the error message corresponding to the
20 * file generated with makeDummyString()
21 *
22 * */
24{
25public:
26 /** @brief Given an error produce a file that the dummy reader can detect and return the error.
27 @param error The error to return
28 @param msg the message to return
29 @return string of the file that will generate the error */
30 static QString makeDummyString(KEduVocDocument::ErrorCode error, const QString &msg = QStringLiteral("Dummy Reader Error"))
31 // Note: This should be defined in the cpp file, but I was having linker problems
32 {
33 QString out;
34 QXmlStreamWriter stream(&out);
35 stream.setAutoFormatting(true);
36 stream.writeStartDocument();
37 stream.writeStartElement(mTag());
38 stream.writeTextElement(makeErrorTag(error), msg);
39 stream.writeEndElement(); // m_tag
40 stream.writeEndDocument();
41 qDebug() << "The file" << out;
42 return out;
43 }
44
45 /** constructor
46 @param dev device to parse*/
47 explicit DummyReader(QIODevice &dev);
48 /**destructor*/
49 ~DummyReader() override {};
50
51 /** @brief Can this reader parse this file
52 *
53 Read a small portion of the header of the file
54 and decide if it is a suitable type.
55 @return true if parsable
56 */
57 bool isParsable() Q_DECL_OVERRIDE;
58
59 /** @brief returns the KEduVocDocument::FileType that this reader handles
60 @return KEduVocDocument::FileType handled
61 */
62 KEduVocDocument::FileType fileTypeHandled() Q_DECL_OVERRIDE;
63
64 /** @brief Parse file and write into doc
65 @param doc to be written
66 @return error status of the read.*/
67 KEduVocDocument::ErrorCode read(KEduVocDocument &doc) Q_DECL_OVERRIDE;
68
69 /** an error message.
70 @return the error message
71 */
72 QString errorMessage() const Q_DECL_OVERRIDE;
73
74private:
75 /**
76 @brief XML tag to identify a dummy file
77 @return tagname that indicates this is a dummy document
78 */
79 static QString mTag()
80 // Note: This should be defined in the cpp file, but I was having linker problems
81 {
82 return QStringLiteral("kvocdocdummyreadertestelement");
83 }
84
85 /**
86 @brief Make error into a tabname
87 @param err Error code to convert
88 @return tagname
89 */
90 static QString makeErrorTag(KEduVocDocument::ErrorCode err)
91 // Note: This should be defined in the cpp file, but I was having linker problems
92 {
93 return "errnum-" + QString::number(int(err));
94 }
95
96 KEduVocDocument::ErrorCode m_error; ///< The error code to always return;
97 QString m_errorMessage; ///< The error message
98 QIODevice &m_dev; ///< input device
99};
100
101#endif // DUMMYREADER_H
A dummy reader to be used in unit tests.
Definition dummyreader.h:24
KEduVocDocument::FileType fileTypeHandled() override
returns the KEduVocDocument::FileType that this reader handles
KEduVocDocument::ErrorCode read(KEduVocDocument &doc) override
Parse file and write into doc.
DummyReader(QIODevice &dev)
constructor
bool isParsable() override
Can this reader parse this file.
QString errorMessage() const override
an error message.
static QString makeDummyString(KEduVocDocument::ErrorCode error, const QString &msg=QStringLiteral("Dummy Reader Error"))
Given an error produce a file that the dummy reader can detect and return the error.
Definition dummyreader.h:30
~DummyReader() override
destructor
Definition dummyreader.h:49
The primary entry point to the hierarchy of objects describing vocabularies.
ErrorCode
the return code when opening/saving
a base class for readers of various lexicon formats
Definition readerbase.h:20
QString number(double n, char format, int precision)
Error error() const const
void setAutoFormatting(bool enable)
void writeEndDocument()
void writeEndElement()
void writeStartDocument()
void writeStartElement(QAnyStringView namespaceUri, QAnyStringView name)
void writeTextElement(QAnyStringView namespaceUri, QAnyStringView name, QAnyStringView text)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 22 2024 12:06:56 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.