File: keduvocdocumenttest.cpp

package info (click to toggle)
libkeduvocdocument 4%3A20.12.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 2,284 kB
  • sloc: cpp: 8,230; makefile: 3; sh: 3
file content (126 lines) | stat: -rw-r--r-- 4,996 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/***************************************************************************
 *   Copyright (C) 2016 by Hartmut Riesenbeck <hartmut.riesenbeck@gmx.de>  *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.          *
 ***************************************************************************/

#include "keduvocdocument.h"
#include <QTemporaryFile>
#include <QTest>

namespace KEduVocDocumentTests {
/** @file
 * \brief Various KEduVocDocument tests
 * This test class contends various tests for public KEduVocDocument methods.
 * They are used to proof expected behavior before and after apply changes to the tested methods.
 * @author Hartmut Riesenbeck <hartmut.riesenbeck@gmx.de>
 */

class KEduVocDocumentTest : public QObject
{
    Q_OBJECT

private slots:
    void initTestCase();
    void testDetectFileType();
    void testOpen();

private:
    void createTmpTestFileOfType(KEduVocDocument::FileType fileType);
    QString tmpTestFileName() const;
    QUrl tmpTestFileUrl() const;

private:
    QTemporaryFile m_tempTestFile;
    KEduVocDocument m_documentTemplate;
};

void KEduVocDocumentTest::initTestCase()
{
    if (!m_tempTestFile.open()) {
        QFAIL("Temporary test file could not be created.");
        return;
    }
    m_tempTestFile.close();

    KEduVocIdentifier lang;
    lang.setName(QStringLiteral("KEduVocDocument Language Name"));
    lang.setLocale(QStringLiteral("en"));

    m_documentTemplate.setAuthor(QStringLiteral("KEduVocDocument Tests"));
    m_documentTemplate.appendIdentifier(lang);
    m_documentTemplate.setGenerator(QStringLiteral("KEduVocDocument Unit Tests"));
}

void KEduVocDocumentTest::testDetectFileType()
{
    KEduVocDocument::FileType resultFileType(KEduVocDocument::KvdNone);

    createTmpTestFileOfType(KEduVocDocument::Kvtml);
    resultFileType = KEduVocDocument::detectFileType(tmpTestFileName());
    QCOMPARE(resultFileType, KEduVocDocument::Kvtml);

    createTmpTestFileOfType(KEduVocDocument::Csv);
    resultFileType = KEduVocDocument::detectFileType(tmpTestFileName());
    QCOMPARE(resultFileType, KEduVocDocument::Csv);
}

void KEduVocDocumentTest::testOpen()
{
    KEduVocDocument::ErrorCode openResult(KEduVocDocument::NoError);

    createTmpTestFileOfType(KEduVocDocument::Kvtml);
    KEduVocDocument docKvtml;
    openResult = docKvtml.open(tmpTestFileUrl(), KEduVocDocument::FileOpenReadOnly);
    QCOMPARE(openResult, KEduVocDocument::NoError);

    createTmpTestFileOfType(KEduVocDocument::Csv);
    KEduVocDocument docCsv;
    openResult = docCsv.open(tmpTestFileUrl(), KEduVocDocument::FileOpenReadOnly);
    QCOMPARE(openResult, KEduVocDocument::NoError);
}

void KEduVocDocumentTest::createTmpTestFileOfType(KEduVocDocument::FileType fileType)
{
    KEduVocDocument::ErrorCode result = m_documentTemplate.saveAs(tmpTestFileUrl(), fileType);
    if (result != KEduVocDocument::NoError) {
        QStringList errorStrings = QStringList() << QStringLiteral("NoError") << QStringLiteral("Unknown") << QStringLiteral("InvalidXml")
            << QStringLiteral("FileTypeUnknown") << QStringLiteral("FileCannotWrite") << QStringLiteral("FileWriterFailed") << QStringLiteral("FileCannotRead")
            << QStringLiteral("FileReaderFailed")<< QStringLiteral("FileDoesNotExist") << QStringLiteral("FileLocked") << QStringLiteral("FileCannotLock")
            << QStringLiteral("FileIsReadOnly");
        QString failText =QStringLiteral("Temporary test file could not be saved. (Error code = %1)");
        failText = failText.arg(errorStrings.value(result));
        QFAIL(failText.toLocal8Bit().constData());
    }
}

QString KEduVocDocumentTest::tmpTestFileName() const
{
    return m_tempTestFile.fileName();
}

QUrl KEduVocDocumentTest::tmpTestFileUrl() const
{
    return QUrl::fromLocalFile(m_tempTestFile.fileName());
}

}

QTEST_MAIN(KEduVocDocumentTests::KEduVocDocumentTest)

#include "keduvocdocumenttest.moc"