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

Ensure that every unit has a course parent set

parent 3a313191
......@@ -77,6 +77,7 @@ void TestCourseResource::loadCourseResource()
QCOMPARE(unit->id(), "1");
QCOMPARE(unit->title(), QStringLiteral("Auf der Straße"));
QCOMPARE(unit->foreignId(), "{dd60f04a-eb37-44b7-9787-67aaf7d3578d}");
QCOMPARE(unit->course(), course.get());
QCOMPARE(unit->phraseList().count(), 3);
// note: this test takes the silent assumption that phrases are added to the list in same
......
......@@ -72,6 +72,7 @@ void TestEditableCourseResource::loadCourseResource()
QCOMPARE(unit->id(), "1");
QCOMPARE(unit->title(), QStringLiteral("Auf der Straße"));
QCOMPARE(unit->foreignId(), "{dd60f04a-eb37-44b7-9787-67aaf7d3578d}");
QCOMPARE(unit->course(), course.get());
QCOMPARE(unit->phraseList().count(), 3);
// note: this test takes the silent assumption that phrases are added to the list in same
......
......@@ -70,6 +70,9 @@ void TestSkeletonResource::loadSkeletonResource()
QCOMPARE(unit->id(), "{11111111-b885-4833-97ff-27cb1ca2f543}");
QCOMPARE(unit->title(), QStringLiteral("Numbers"));
QCOMPARE(unit->phraseList().count(), 2);
QVERIFY(unit->course() != nullptr);
QCOMPARE(unit->course(), skeleton.get());
// note: this test takes the silent assumption that phrases are added to the list in same
// order as they are defined in the file. This assumption should be made explicit or dropped
const auto firstPhrase = unit->phraseList().first();
......@@ -102,10 +105,11 @@ void TestSkeletonResource::unitAddAndRemoveHandling()
QSignalSpy spyAdded(skeleton.get(), SIGNAL(unitAdded()));
QCOMPARE(spyAboutToBeAdded.count(), 0);
QCOMPARE(spyAdded.count(), 0);
skeleton->addUnit(std::move(unit));
auto sharedUnit = skeleton->addUnit(std::move(unit));
QCOMPARE(skeleton->units().count(), initialUnitNumber + 1);
QCOMPARE(spyAboutToBeAdded.count(), 1);
QCOMPARE(spyAdded.count(), 1);
QCOMPARE(sharedUnit->course(), skeleton.get());
}
void TestSkeletonResource::coursePropertyChanges()
......
......@@ -287,9 +287,10 @@ void EditorSession::switchToNextPhrase()
bool EditorSession::hasPreviousPhrase() const
{
if (!m_course || !m_unit || !m_phrase) {
if (!m_unit || !m_phrase) {
return false;
}
Q_ASSERT(m_unit->course() != nullptr);
const int phraseIndex = m_phrase->unit()->phraseList().indexOf(m_phrase);
int unitIndex = -1;
......@@ -307,9 +308,10 @@ bool EditorSession::hasPreviousPhrase() const
bool EditorSession::hasNextPhrase() const
{
if (!m_course || !m_unit || !m_phrase) {
if (!m_unit || !m_phrase) {
return false;
}
Q_ASSERT(m_unit->course() != nullptr);
const int phraseIndex = m_phrase->unit()->phraseList().indexOf(m_phrase);
int unitIndex = -1;
......
......@@ -109,16 +109,21 @@ QVector<std::shared_ptr<Unit>> SkeletonResourcePrivate::units()
}
auto units = CourseParser::parseUnits(m_path);
for (auto &unit : units) {
Q_ASSERT(m_self.lock() != nullptr);
unit->setCourse(m_self.lock().get());
m_units.append(std::move(unit));
}
m_unitsParsed = true;
return m_units;
}
std::shared_ptr<Unit> SkeletonResourcePrivate::appendUnit(std::shared_ptr<Unit> unit) {
std::shared_ptr<Unit> SkeletonResourcePrivate::appendUnit(std::shared_ptr<Unit> unit)
{
units(); // ensure that units are parsed
m_units.append(unit);
m_modified = true;
Q_ASSERT(m_self.lock() != nullptr);
unit->setCourse(m_self.lock().get());
return m_units.last();
}
......
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