From 1f3dd3867bea5f5380a135dedbf33a2669580977 Mon Sep 17 00:00:00 2001 From: ColinDuquesnoy Date: Sun, 24 Sep 2017 11:43:59 +0200 Subject: [PATCH] #69 Add UserScriptViewModel --- docs/developers/PlantUml/UserScripts.puml | 2 +- .../UserScripts/UserScriptViewModel.cpp | 40 ++++++++++++++ .../UserScripts/UserScriptViewModel.hpp | 36 +++++++++++++ .../UserScripts/FakeUserScript.cpp | 17 ------ .../UserScripts/FakeUserScript.hpp | 13 ++++- .../UserScripts/UserScriptViewModelTests.cpp | 54 +++++++++++++++++++ 6 files changed, 142 insertions(+), 20 deletions(-) create mode 100644 lib/MellowPlayer/Presentation/ViewModels/UserScripts/UserScriptViewModel.cpp create mode 100644 lib/MellowPlayer/Presentation/ViewModels/UserScripts/UserScriptViewModel.hpp delete mode 100644 tests/UnitTests/Application/UserScripts/FakeUserScript.cpp create mode 100644 tests/UnitTests/Presentation/ViewModels/UserScripts/UserScriptViewModelTests.cpp diff --git a/docs/developers/PlantUml/UserScripts.puml b/docs/developers/PlantUml/UserScripts.puml index f3382405..d11acff2 100644 --- a/docs/developers/PlantUml/UserScripts.puml +++ b/docs/developers/PlantUml/UserScripts.puml @@ -66,7 +66,7 @@ namespace MellowPlayer.Presentation { + removeScript(QString name) } class UserScriptViewModel { - - _userScript: IUserScript& + - model_: IUserScript& + code(): QString + name(): QString + setName(QString) diff --git a/lib/MellowPlayer/Presentation/ViewModels/UserScripts/UserScriptViewModel.cpp b/lib/MellowPlayer/Presentation/ViewModels/UserScripts/UserScriptViewModel.cpp new file mode 100644 index 00000000..12375787 --- /dev/null +++ b/lib/MellowPlayer/Presentation/ViewModels/UserScripts/UserScriptViewModel.cpp @@ -0,0 +1,40 @@ +#include "UserScriptViewModel.hpp" +#include + +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_; +} diff --git a/lib/MellowPlayer/Presentation/ViewModels/UserScripts/UserScriptViewModel.hpp b/lib/MellowPlayer/Presentation/ViewModels/UserScripts/UserScriptViewModel.hpp new file mode 100644 index 00000000..17b892a6 --- /dev/null +++ b/lib/MellowPlayer/Presentation/ViewModels/UserScripts/UserScriptViewModel.hpp @@ -0,0 +1,36 @@ +#pragma once + +#include + +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_; + + }; +} diff --git a/tests/UnitTests/Application/UserScripts/FakeUserScript.cpp b/tests/UnitTests/Application/UserScripts/FakeUserScript.cpp deleted file mode 100644 index 7190dfa9..00000000 --- a/tests/UnitTests/Application/UserScripts/FakeUserScript.cpp +++ /dev/null @@ -1,17 +0,0 @@ -#include "FakeUserScript.hpp" - -using namespace MellowPlayer::Application; -using namespace MellowPlayer::Application::Tests; - - -bool FakeUserScript::import(const QString& path) -{ - return load(path); -} - -bool FakeUserScript::load(const QString& path) -{ - UserScriptBase::path_ = path; - Application::UserScriptBase::code_ = FAKE_USER_SCRIPT_CODE; - return true; -} diff --git a/tests/UnitTests/Application/UserScripts/FakeUserScript.hpp b/tests/UnitTests/Application/UserScripts/FakeUserScript.hpp index b1c97a58..8bf8c352 100644 --- a/tests/UnitTests/Application/UserScripts/FakeUserScript.hpp +++ b/tests/UnitTests/Application/UserScripts/FakeUserScript.hpp @@ -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; + } }; } \ No newline at end of file diff --git a/tests/UnitTests/Presentation/ViewModels/UserScripts/UserScriptViewModelTests.cpp b/tests/UnitTests/Presentation/ViewModels/UserScripts/UserScriptViewModelTests.cpp new file mode 100644 index 00000000..984e9291 --- /dev/null +++ b/tests/UnitTests/Presentation/ViewModels/UserScripts/UserScriptViewModelTests.cpp @@ -0,0 +1,54 @@ +#include +#include +#include +#include + +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); + } + } + } + } + + } +}