Skip to content
This repository has been archived by the owner on May 3, 2019. It is now read-only.

Commit

Permalink
#69 Add UserScriptViewModel
Browse files Browse the repository at this point in the history
  • Loading branch information
ColinDuquesnoy committed Sep 24, 2017
1 parent 4a2667a commit 1f3dd38
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 20 deletions.
2 changes: 1 addition & 1 deletion docs/developers/PlantUml/UserScripts.puml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ namespace MellowPlayer.Presentation {
+ removeScript(QString name)
}
class UserScriptViewModel {
- _userScript: IUserScript&
- model_: IUserScript&
+ code(): QString
+ name(): QString
+ setName(QString)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include "UserScriptViewModel.hpp"
#include <MellowPlayer/Application/UserScripts/IUserScript.hpp>

using namespace MellowPlayer::Application;
using namespace MellowPlayer::Presentation;

UserScriptViewModel::UserScriptViewModel(IUserScript& model, QObject* parent):
QObject(parent), model_(model)
{

}

QString UserScriptViewModel::name() const
{
return model_.name();
}

void UserScriptViewModel::setName(const QString& name)
{
Q_UNUSED(name);
if (name != model_.name()) {
model_.setName(name);
emit nameChanged();
}
}

QString UserScriptViewModel::code() const
{
return model_.code();
}

QString UserScriptViewModel::path() const
{
return model_.path();
}

IUserScript& UserScriptViewModel::model()
{
return model_;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#pragma once

#include <QObject>

namespace MellowPlayer::Application
{
class IUserScript;
}

namespace MellowPlayer::Presentation
{
class UserScriptViewModel: public QObject
{
Q_OBJECT
Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
Q_PROPERTY(QString code READ code CONSTANT)
Q_PROPERTY(QString path READ path CONSTANT)
public:
UserScriptViewModel(Application::IUserScript& model, QObject* parent= nullptr);

QString name() const;
void setName(const QString& name);

QString code() const;
QString path() const;

Application::IUserScript& model();

signals:
void nameChanged();

private:
Application::IUserScript& model_;

};
}
17 changes: 0 additions & 17 deletions tests/UnitTests/Application/UserScripts/FakeUserScript.cpp

This file was deleted.

13 changes: 11 additions & 2 deletions tests/UnitTests/Application/UserScripts/FakeUserScript.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,16 @@ namespace MellowPlayer::Application::Tests
class FakeUserScript: public UserScriptBase
{
public:
bool import(const QString& path) override;
bool load(const QString& path) override;
bool import(const QString& path) override
{
return load(path);
}

bool load(const QString& path) override
{
UserScriptBase::path_ = path;
Application::UserScriptBase::code_ = FAKE_USER_SCRIPT_CODE;
return true;
}
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include <catch.hpp>
#include <UnitTests/Application/UserScripts/FakeUserScript.hpp>
#include <MellowPlayer/Presentation/ViewModels/UserScripts/UserScriptViewModel.hpp>
#include <QtTest/QSignalSpy>

using namespace MellowPlayer::Application;
using namespace MellowPlayer::Application::Tests;
using namespace MellowPlayer::Presentation;

SCENARIO("UserScritViewModelTests")
{
GIVEN("a fake user script")
{
FakeUserScript userScript;
userScript.setName("name");
userScript.import("/path");

WHEN("a view model is created") {
UserScriptViewModel viewModel(userScript);
QSignalSpy spy(&viewModel, &UserScriptViewModel::nameChanged);

THEN("code is the same as the user script code")
{
REQUIRE(viewModel.code() == userScript.code());
}

THEN("name is the same as the user script name")
{
REQUIRE(viewModel.name() == userScript.name());
}

THEN("path is the same as the user script path")
{
REQUIRE(viewModel.path() == userScript.path());
}

AND_WHEN("another name is set")
{
viewModel.setName("Another name");

THEN("new name is saved")
{
REQUIRE(viewModel.name() == "Another name");

AND_THEN("nameChanged signal is emitted")
{
REQUIRE(spy.count() == 1);
}
}
}
}

}
}

0 comments on commit 1f3dd38

Please sign in to comment.