Commit e6b0ccac authored by Andreas Cord-Landwehr's avatar Andreas Cord-Landwehr
Browse files

Introduce LanguageStub

parent 73ec6200
/*
* Copyright 2019 Andreas Cord-Landwehr <cordlandwehr@kde.org>
*
* 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) version 3 or any later version
* accepted by the membership of KDE e.V. (or its successor approved
* by the membership of KDE e.V.), which shall act as a proxy
* defined in Section 14 of version 3 of the license.
*
* 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, see <http://www.gnu.org/licenses/>.
*/
#include "languagestub.h"
// define one virtual method out of line to pin LanguageStub to this translation unit
LanguageStub::~LanguageStub() = default;
/*
* Copyright 2019 Andreas Cord-Landwehr <cordlandwehr@kde.org>
*
* 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) version 3 or any later version
* accepted by the membership of KDE e.V. (or its successor approved
* by the membership of KDE e.V.), which shall act as a proxy
* defined in Section 14 of version 3 of the license.
*
* 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, see <http://www.gnu.org/licenses/>.
*/
#ifndef LANGUAGESTUB_H
#define LANGUAGESTUB_H
#include "src/core/ilanguage.h"
#include <QObject>
#include <QVector>
class Phoneme;
class PhonemeGroup;
class LanguageStub : public ILanguage
{
public:
LanguageStub(QString id)
: m_id(id)
{
}
~LanguageStub() override;
QString id() const override
{
return m_id;
}
QString title() const override
{
return m_title;
}
void setTitle(QString title)
{
m_title = title;
emit titleChanged();
}
QString i18nTitle() const override
{
return "i18n title";
}
QVector<std::shared_ptr<Phoneme>> phonemes() const override
{
return QVector<std::shared_ptr<Phoneme>>();
}
QVector<std::shared_ptr<PhonemeGroup>> phonemeGroups() const override
{
return QVector<std::shared_ptr<PhonemeGroup>>();
}
private:
QString m_id{ "UNKNOWN_ID" };
QString m_title{ "title" };
};
#endif
......@@ -22,21 +22,14 @@
#include "core/language.h"
#include "core/unit.h"
#include "core/phrase.h"
#include "core/phoneme.h"
#include "core/resources/courseparser.h"
#include <QTest>
#include <QDebug>
#include <QTemporaryFile>
#include <QDirIterator>
#include <QDebug>
#include <QUrl>
#include <QIODevice>
#include <QFile>
#include <QXmlSchema>
#include <QXmlSchemaValidator>
#include <QDomDocument>
#include <QStandardPaths>
TestLanguageFiles::TestLanguageFiles()
{
}
......@@ -51,14 +44,6 @@ void TestLanguageFiles::cleanup()
// TODO cleanup after test run
}
void TestLanguageFiles::languageSchemeValidationTest()
{
QUrl languageFile = QUrl::fromLocalFile(":/artikulate/schemes/language.xsd");
QXmlSchema languageSchema;
QVERIFY(languageSchema.load(languageFile));
QVERIFY(languageSchema.isValid());
}
void TestLanguageFiles::checkIdUniqueness()
{
QDirIterator iter(QDir(":/artikulate/languages/"));
......@@ -66,34 +51,11 @@ void TestLanguageFiles::checkIdUniqueness()
const QString &file = iter.next();
qDebug() << "File being parsed: " << file;
QStringList idList;
const QUrl &languageFile = QUrl::fromLocalFile(file);
QVERIFY(languageFile.isLocalFile());
QXmlSchema schema = CourseParser::loadXmlSchema(QStringLiteral("language"));
QVERIFY(schema.isValid());
QDomDocument document = CourseParser::loadDomDocument(languageFile, schema);
QVERIFY(!document.isNull());
QDomElement root(document.documentElement());
std::unique_ptr<Language> language(new Language);
language->setFile(languageFile);
language->setId(root.firstChildElement(QStringLiteral("id")).text());
language->setTitle(root.firstChildElement(QStringLiteral("title")).text());
// create phoneme groups
for (QDomElement groupNode = root.firstChildElement(QStringLiteral("phonemeGroups")).firstChildElement();
!groupNode.isNull();
groupNode = groupNode.nextSiblingElement())
{
for (QDomElement phonemeNode = groupNode.firstChildElement(QStringLiteral("phonemes")).firstChildElement();
!phonemeNode.isNull();
phonemeNode = phonemeNode.nextSiblingElement())
{
QString id = phonemeNode.firstChildElement(QStringLiteral("id")).text();
qDebug() << "ID: " << id;
QVERIFY2(!idList.contains(id),"Phoneme ID used more than once in the tested file");
idList.append(id);
}
auto language = Language::create(QUrl::fromLocalFile(file));
for (auto phoneme : language->phonemes()) {
QVERIFY2(!idList.contains(phoneme->id()), "Phoneme ID used more than once in the tested file");
idList.append(phoneme->id());
}
}
}
......
/*
* Copyright 2013 Oindrila Gupta <oindrila.gupta92@gmail.com>
* Copyright 2019 Andreas Cord-Landwehr <cordlandwehr@kde.org>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
......@@ -23,10 +24,6 @@
#include <QObject>
class QXmlSchema;
class QDomDocument;
class QUrl;
class TestLanguageFiles : public QObject
{
Q_OBJECT
......@@ -45,11 +42,6 @@ private slots:
*/
void cleanup();
/**
* Test if language XSD specification is valid.
*/
void languageSchemeValidationTest();
/**
* Test if id of each phoneme is unique in every language file.
*/
......
......@@ -53,6 +53,7 @@ ecm_mark_as_test(test_resourcerepository)
set(TestTrainingSession_SRCS
trainingsession/test_trainingsession.cpp
../mocks/coursestub.cpp
../mocks/languagestub.cpp
)
add_executable(test_trainingsession ${TestTrainingSession_SRCS})
target_link_libraries(test_trainingsession
......@@ -67,6 +68,7 @@ ecm_mark_as_test(test_trainingsession)
set(TestEditorSession_SRCS
editorsession/test_editorsession.cpp
../mocks/editablerepositorystub.cpp
../mocks/languagestub.cpp
)
add_executable(test_editorsession ${TestEditorSession_SRCS})
target_link_libraries(test_editorsession
......@@ -81,6 +83,7 @@ ecm_mark_as_test(test_editorsession)
set(TestCourseResource_SRCS
courseresource/test_courseresource.cpp
../mocks/resourcerepositorystub.cpp
../mocks/languagestub.cpp
)
qt5_add_resources(TestCourseResource_SRCS ../../data/languages.qrc)
add_executable(test_courseresource ${TestCourseResource_SRCS} )
......@@ -96,6 +99,7 @@ ecm_mark_as_test(test_courseresource)
set(TestSkeletonResource_SRCS
skeletonresource/test_skeletonresource.cpp
../mocks/resourcerepositorystub.cpp
../mocks/languagestub.cpp
)
qt5_add_resources(TestSkeletonResource_SRCS ../../data/languages.qrc)
add_executable(test_skeletonresource ${TestSkeletonResource_SRCS} )
......@@ -111,6 +115,7 @@ ecm_mark_as_test(test_skeletonresource)
set(TestEditableCourseResource_SRCS
editablecourseresource/test_editablecourseresource.cpp
../mocks/resourcerepositorystub.cpp
../mocks/languagestub.cpp
)
qt5_add_resources(TestEditableCourseResource_SRCS ../../data/languages.qrc)
qt5_add_resources(TestEditableCourseResource_SRCS ../testdata/testdata.qrc)
......@@ -128,6 +133,7 @@ set(TestCourseModel_SRCS
coursemodel/test_coursemodel.cpp
../mocks/resourcerepositorystub.cpp
../mocks/coursestub.cpp
../mocks/languagestub.cpp
)
qt5_add_resources(TestCourseModel_SRCS ../../data/languages.qrc)
qt5_add_resources(TestCourseModel_SRCS ../testdata/testdata.qrc)
......
......@@ -24,6 +24,7 @@
#include "src/models/coursemodel.h"
#include "../mocks/resourcerepositorystub.h"
#include "../mocks/coursestub.h"
#include "../mocks/languagestub.h"
#include <QTest>
#include <QSignalSpy>
......@@ -41,8 +42,7 @@ void TestCourseModel::cleanup()
void TestCourseModel::testInit()
{
// boilerplate
std::shared_ptr<ILanguage> language(new Language);
std::static_pointer_cast<Language>(language)->setId("de");
std::shared_ptr<ILanguage> language(new LanguageStub("de"));
std::vector<std::shared_ptr<ILanguage>> languages;
languages.push_back(language);
auto course = CourseStub::create(language, QVector<std::shared_ptr<Unit>>({}));
......@@ -58,8 +58,7 @@ void TestCourseModel::testInit()
void TestCourseModel::testAddRemoveOperations()
{
// boilerplate
std::shared_ptr<ILanguage> language(new Language);
std::static_pointer_cast<Language>(language)->setId("de");
std::shared_ptr<ILanguage> language(new LanguageStub("de"));
std::vector<std::shared_ptr<ILanguage>> languages;
languages.push_back(language);
ResourceRepositoryStub repository(languages, {});
......@@ -98,8 +97,7 @@ void TestCourseModel::testAddRemoveOperations()
void TestCourseModel::testDataChangedSignals()
{
// boilerplate
std::shared_ptr<ILanguage> language(new Language);
std::static_pointer_cast<Language>(language)->setId("de");
std::shared_ptr<ILanguage> language(new LanguageStub("de"));
std::vector<std::shared_ptr<ILanguage>> languages;
languages.push_back(language);
auto course = CourseStub::create(language, QVector<std::shared_ptr<Unit>>({}));
......@@ -123,7 +121,7 @@ void TestCourseModel::testDataChangedSignals()
QCOMPARE(spyUpdate.count(), 0);
repository.removeCourse(course);
std::static_pointer_cast<CourseStub>(course)->setTitle("TitleSwitchedAgain");
// QCOMPARE(spyUpdate.count(), 0);
QCOMPARE(spyUpdate.count(), 0);
}
}
......
......@@ -25,7 +25,7 @@
#include "core/phrase.h"
#include "core/phonemegroup.h"
#include "core/resources/courseresource.h"
#include <memory>
#include "../mocks/languagestub.h"
#include <QTest>
#include <QDebug>
#include <QTemporaryFile>
......@@ -50,8 +50,7 @@ void TestCourseResource::cleanup()
void TestCourseResource::loadCourseResource()
{
std::shared_ptr<ILanguage> language(new Language);
std::static_pointer_cast<Language>(language)->setId("de");
std::shared_ptr<ILanguage> language(new LanguageStub("de"));
auto group = std::static_pointer_cast<Language>(language)->addPhonemeGroup("id", "title");
group->addPhoneme("g", "G");
group->addPhoneme("u", "U");
......@@ -95,8 +94,7 @@ void TestCourseResource::loadCourseResource()
void TestCourseResource::unitAddAndRemoveHandling()
{
// boilerplate
std::shared_ptr<ILanguage> language(new Language);
std::static_pointer_cast<Language>(language)->setId("de");
std::shared_ptr<ILanguage> language(new LanguageStub("de"));
ResourceRepositoryStub repository({language});
const QString courseDirectory = "data/courses/de/";
......@@ -122,8 +120,7 @@ void TestCourseResource::unitAddAndRemoveHandling()
void TestCourseResource::coursePropertyChanges()
{
// boilerplate
std::shared_ptr<ILanguage> language(new Language);
std::static_pointer_cast<Language>(language)->setId("de");
std::shared_ptr<ILanguage> language(new LanguageStub("de"));
ResourceRepositoryStub repository({language});
const QString courseDirectory = "data/courses/de/";
......
......@@ -25,6 +25,7 @@
#include "core/phrase.h"
#include "core/resources/courseparser.h"
#include "core/resources/editablecourseresource.h"
#include "../mocks/languagestub.h"
#include <memory>
#include <QTest>
......@@ -51,8 +52,7 @@ void TestEditableCourseResource::cleanup()
void TestEditableCourseResource::loadCourseResource()
{
std::shared_ptr<ILanguage> language(new Language);
std::static_pointer_cast<Language>(language)->setId("de");
std::shared_ptr<ILanguage> language(new LanguageStub("de"));
ResourceRepositoryStub repository({language});
auto course = EditableCourseResource::create(QUrl::fromLocalFile(":/courses/de.xml"), &repository);
......@@ -88,8 +88,7 @@ void TestEditableCourseResource::loadCourseResource()
void TestEditableCourseResource::unitAddAndRemoveHandling()
{
// boilerplate
std::shared_ptr<ILanguage> language(new Language);
std::static_pointer_cast<Language>(language)->setId("de");
std::shared_ptr<ILanguage> language(new LanguageStub("de"));
ResourceRepositoryStub repository({language});
auto course = EditableCourseResource::create(QUrl::fromLocalFile(":/courses/de.xml"), &repository);
......@@ -112,8 +111,7 @@ void TestEditableCourseResource::unitAddAndRemoveHandling()
void TestEditableCourseResource::coursePropertyChanges()
{
// boilerplate
std::shared_ptr<ILanguage> language(new Language);
std::static_pointer_cast<Language>(language)->setId("de");
std::shared_ptr<ILanguage> language(new LanguageStub("de"));
ResourceRepositoryStub repository({language});
auto course = CourseResource::create(QUrl::fromLocalFile(":/courses/de.xml"), &repository);
......@@ -181,8 +179,7 @@ void TestEditableCourseResource::coursePropertyChanges()
void TestEditableCourseResource::fileLoadSaveCompleteness()
{
// boilerplate
std::shared_ptr<ILanguage> language(new Language);
std::static_pointer_cast<Language>(language)->setId("de");
std::shared_ptr<ILanguage> language(new LanguageStub("de"));
ResourceRepositoryStub repository({language});
auto course = EditableCourseResource::create(QUrl::fromLocalFile(":/courses/de.xml"), &repository);
......
......@@ -27,13 +27,14 @@
#include "src/core/language.h"
#include "src/core/resources/skeletonresource.h"
#include "src/core/unit.h"
#include "../mocks/languagestub.h"
#include <QTest>
#include <QSignalSpy>
class EditableCourseStub : public IEditableCourse
{
public:
EditableCourseStub(std::shared_ptr<Language> language, QVector<std::shared_ptr<Unit>> units)
EditableCourseStub(std::shared_ptr<ILanguage> language, QVector<std::shared_ptr<Unit>> units)
: IEditableCourse()
, m_language(language)
, m_units(units)
......@@ -153,10 +154,8 @@ void TestEditorSession::cleanup()
void TestEditorSession::createEditorSession()
{
auto languageGerman = std::make_shared<Language>();
languageGerman->setId("de");
auto languageEnglish = std::make_shared<Language>();
languageEnglish->setId("en");
auto languageGerman = std::make_shared<LanguageStub>("de");
auto languageEnglish = std::make_shared<LanguageStub>("en");
std::shared_ptr<IEditableCourse> course(new EditableCourseStub(languageGerman, QVector<std::shared_ptr<Unit>>()));
course->setLanguage(languageGerman);
......@@ -176,10 +175,8 @@ void TestEditorSession::createEditorSession()
void TestEditorSession::nonSkeletonSwitchingBehavior()
{
auto languageGerman = std::make_shared<Language>();
languageGerman->setId("de");
auto languageEnglish = std::make_shared<Language>();
languageEnglish->setId("en");
auto languageGerman = std::make_shared<LanguageStub>("de");
auto languageEnglish = std::make_shared<LanguageStub>("en");
std::shared_ptr<IEditableCourse> courseGerman(new EditableCourseStub(languageGerman, QVector<std::shared_ptr<Unit>>()));
courseGerman->setId("course-german");
std::shared_ptr<IEditableCourse> courseEnglish(new EditableCourseStub(languageEnglish, QVector<std::shared_ptr<Unit>>()));
......@@ -211,10 +208,8 @@ void TestEditorSession::nonSkeletonSwitchingBehavior()
void TestEditorSession::skeletonSwitchingBehavior()
{
auto languageGerman = std::make_shared<Language>();
languageGerman->setId("de");
auto languageEnglish = std::make_shared<Language>();
languageEnglish->setId("en");
auto languageGerman = std::make_shared<LanguageStub>("de");
auto languageEnglish = std::make_shared<LanguageStub>("en");
std::shared_ptr<IEditableCourse> courseGermanA(new EditableCourseStub(languageGerman, QVector<std::shared_ptr<Unit>>()));
courseGermanA->setId("course-german");
courseGermanA->setForeignId("testskeletonA");
......@@ -258,8 +253,7 @@ void TestEditorSession::skeletonSwitchingBehavior()
void TestEditorSession::iterateCourse()
{
// language
auto language = std::make_shared<Language>();
language->setId("de");
auto language = std::make_shared<LanguageStub>("de");
// course
std::shared_ptr<Unit> unitA(new Unit);
......
......@@ -24,6 +24,7 @@
#include "core/unit.h"
#include "core/phrase.h"
#include "core/resources/skeletonresource.h"
#include "../mocks/languagestub.h"
#include <QTest>
#include <QDebug>
......@@ -49,8 +50,7 @@ void TestSkeletonResource::cleanup()
void TestSkeletonResource::loadSkeletonResource()
{
std::shared_ptr<Language> language(new Language);
language->setId("de");
std::shared_ptr<ILanguage> language(new LanguageStub("de"));
ResourceRepositoryStub repository({language});
const QString courseDirectory = "data/contributorrepository/skeletons/";
const QString courseFile = courseDirectory + "skeleton.xml";
......@@ -87,8 +87,7 @@ void TestSkeletonResource::loadSkeletonResource()
void TestSkeletonResource::unitAddAndRemoveHandling()
{
// boilerplate
std::shared_ptr<Language> language(new Language);
language->setId("de");
std::shared_ptr<ILanguage> language(new LanguageStub("de"));
ResourceRepositoryStub repository({language});
const QString courseDirectory = "data/contributorrepository/skeletons/";
const QString courseFile = courseDirectory + "skeleton.xml";
......@@ -112,8 +111,7 @@ void TestSkeletonResource::unitAddAndRemoveHandling()
void TestSkeletonResource::coursePropertyChanges()
{
// boilerplate
std::shared_ptr<Language> language(new Language);
language->setId("de");
std::shared_ptr<ILanguage> language(new LanguageStub("de"));
ResourceRepositoryStub repository({language});
const QString courseDirectory = "data/contributorrepository/skeletons/";
const QString courseFile = courseDirectory + "skeleton.xml";
......@@ -153,8 +151,7 @@ void TestSkeletonResource::coursePropertyChanges()
void TestSkeletonResource::fileLoadSaveCompleteness()
{
// boilerplate
std::shared_ptr<Language> language(new Language);
language->setId("de");
std::shared_ptr<ILanguage> language(new LanguageStub("de"));
ResourceRepositoryStub repository({language});
const QString courseDirectory = "data/contributorrepository/skeletons/";
const QString courseFile = courseDirectory + "skeleton.xml";
......
......@@ -25,6 +25,7 @@
#include "src/core/unit.h"
#include "src/core/trainingaction.h"
#include "../mocks/coursestub.h"
#include "../mocks/languagestub.h"
#include "liblearnerprofile/src/profilemanager.h"
#include <QTest>
#include <QSignalSpy>
......@@ -44,7 +45,7 @@ void TestTrainingSession::cleanup()
void TestTrainingSession::createTrainingSessionWithoutUnits()
{
auto language = std::make_shared<Language>();
auto language = std::make_shared<LanguageStub>("de");
CourseStub course(language, QVector<std::shared_ptr<Unit>>());
LearnerProfile::ProfileManager manager;
TrainingSession session(&manager);
......@@ -54,7 +55,7 @@ void TestTrainingSession::createTrainingSessionWithoutUnits()
void TestTrainingSession::createTrainingSessionWithEmptySounds()
{
auto language = std::make_shared<Language>();
auto language = std::make_shared<LanguageStub>("de");
std::shared_ptr<Unit> unitA(new Unit);
std::shared_ptr<Unit> unitB(new Unit);
Phrase *phraseA1 = new Phrase;
......@@ -85,7 +86,7 @@ void TestTrainingSession::createTrainingSessionWithEmptySounds()
void TestTrainingSession::createTrainingSessionWithEmptyUnits()
{
auto language = std::make_shared<Language>();
auto language = std::make_shared<LanguageStub>("de");
std::shared_ptr<Unit> unitA(new Unit);
std::shared_ptr<Unit> unitB(new Unit);
CourseStub course(language, QVector<std::shared_ptr<Unit>>({unitA, unitB}));
......@@ -97,7 +98,7 @@ void TestTrainingSession::createTrainingSessionWithEmptyUnits()
void TestTrainingSession::createTrainingSessionWithUnitsAndPhrases()
{
auto language = std::make_shared<Language>();
auto language = std::make_shared<LanguageStub>("de");
std::shared_ptr<Unit> unit(new Unit);
Phrase *firstPhrase = new Phrase();
Phrase *secondPhrase = new Phrase();
......@@ -113,7 +114,7 @@ void TestTrainingSession::createTrainingSessionWithUnitsAndPhrases()
void TestTrainingSession::iterateCourse()
{
auto language = std::make_shared<Language>();
auto language = std::make_shared<LanguageStub>("de");
std::shared_ptr<Unit> unitA(new Unit);
std::shared_ptr<Unit> unitB(new Unit);
Phrase *phraseA1 = new Phrase;
......
......@@ -45,16 +45,16 @@ public:
static std::shared_ptr<Language> create(QUrl file);
~Language() override;
QString id() const;
QString id() const override;
void setId(const QString &id);
QString title() const;
QString title() const override;
void seti18nTitle(const QString &title);
QString i18nTitle() const;
QString i18nTitle() const override;
void setTitle(const QString &title);
QUrl file() const;
void setFile(const QUrl &file);
QVector<std::shared_ptr<Phoneme>> phonemes() const;
QVector<std::shared_ptr<PhonemeGroup>> phonemeGroups() const;
QVector<std::shared_ptr<Phoneme>> phonemes() const override;
QVector<std::shared_ptr<PhonemeGroup>> phonemeGroups() const override;
std::shared_ptr<PhonemeGroup> addPhonemeGroup(const QString &identifier, const QString &title);
Q_SIGNALS:
......@@ -64,7 +64,7 @@ Q_SIGNALS:
void phonemesChanged();
void phonemeGroupsChanged();
public: // TODO
protected:
explicit Language();
private:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment