This repository has been archived by the owner on May 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#69 Implement UserScripts and IUserScriptFactory
- Loading branch information
1 parent
ebc3e79
commit 09754ff
Showing
7 changed files
with
251 additions
and
16 deletions.
There are no files selected for viewing
13 changes: 13 additions & 0 deletions
13
lib/MellowPlayer/Application/UserScripts/IUserScriptFactory.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#pragma once | ||
|
||
namespace MellowPlayer::Application | ||
{ | ||
class IUserScript; | ||
|
||
class IUserScriptFactory | ||
{ | ||
public: | ||
virtual ~IUserScriptFactory() = default; | ||
virtual IUserScript* create() const = 0; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
lib/MellowPlayer/Infrastructure/UserScripts/UserScriptFactory.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#include "UserScriptFactory.hpp" | ||
#include "UserScript.hpp" | ||
|
||
using namespace MellowPlayer::Application; | ||
using namespace MellowPlayer::Infrastructure; | ||
|
||
IUserScript* MellowPlayer::Infrastructure::UserScriptFactory::create() const | ||
{ | ||
return new UserScript; | ||
} |
13 changes: 13 additions & 0 deletions
13
lib/MellowPlayer/Infrastructure/UserScripts/UserScriptFactory.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#pragma once | ||
|
||
#include <MellowPlayer/Application/UserScripts/IUserScriptFactory.hpp> | ||
|
||
namespace MellowPlayer::Infrastructure | ||
{ | ||
class UserScriptFactory: public Application::IUserScriptFactory | ||
{ | ||
public: | ||
Application::IUserScript* create() const override; | ||
|
||
}; | ||
} |
125 changes: 125 additions & 0 deletions
125
tests/UnitTests/Application/UserScripts/UserScriptsTests.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
#include <catch.hpp> | ||
#include <fakeit.hpp> | ||
#include <MellowPlayer/Application/UserScripts/IUserScript.hpp> | ||
#include <MellowPlayer/Application/UserScripts/IUserScriptFactory.hpp> | ||
#include <MellowPlayer/Application/UserScripts/UserScripts.hpp> | ||
#include <MellowPlayer/Application/Settings/ISettingsProvider.hpp> | ||
#include <Mocks/SettingsProviderMock.hpp> | ||
|
||
using namespace fakeit; | ||
using namespace MellowPlayer::Application; | ||
|
||
SCENARIO("UserScriptsTests") | ||
{ | ||
Mock<IUserScript> userScriptMock; | ||
When(Method(userScriptMock, path)).AlwaysReturn("/path"); | ||
When(Method(userScriptMock, code)).AlwaysReturn("code"); | ||
When(Method(userScriptMock, name)).AlwaysReturn("name"); | ||
Fake(Method(userScriptMock, setName)); | ||
When(Method(userScriptMock, load)).AlwaysReturn(true); | ||
When(Method(userScriptMock, import)).AlwaysReturn(true); | ||
Fake(Dtor(userScriptMock)); | ||
IUserScript& userScript = userScriptMock.get(); | ||
|
||
Mock<IUserScriptFactory> factoryMock; | ||
When(Method(factoryMock, create)).AlwaysReturn(&userScript); | ||
|
||
QString serviceName = "fakeService"; | ||
auto settingsProviderMock = SettingsProviderMock::get(); | ||
ISettingsProvider& settingsProvider = settingsProviderMock.get(); | ||
settingsProvider.clear(); | ||
|
||
|
||
GIVEN("empty settings") | ||
{ | ||
UserScripts userScripts(serviceName, factoryMock.get(), settingsProvider); | ||
|
||
WHEN("get count") | ||
{ | ||
THEN("count is zero") | ||
{ | ||
REQUIRE(userScripts.count() == 0); | ||
} | ||
} | ||
|
||
WHEN("add script") | ||
{ | ||
userScripts.add("name", "/path"); | ||
|
||
THEN("factory is called") | ||
{ | ||
Verify(Method(factoryMock, create)).Exactly(1); | ||
|
||
AND_THEN("name is set") | ||
{ | ||
Verify(Method(userScriptMock, setName)).Exactly(1); | ||
|
||
AND_THEN("import is called") | ||
{ | ||
Verify(Method(userScriptMock, import)).Exactly(1); | ||
|
||
AND_THEN("count is 1") | ||
{ | ||
REQUIRE(userScripts.count() == 1); | ||
|
||
AND_THEN("settings are saved") | ||
{ | ||
REQUIRE(settingsProvider.value("fakeService/userScriptPaths", QStringList()).toStringList().count() == 1); | ||
REQUIRE(settingsProvider.value("fakeService/userScriptNames", QStringList()).toStringList().count() == 1); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
AND_WHEN("remove is called") | ||
{ | ||
userScripts.remove("name"); | ||
|
||
THEN("count is zero") | ||
{ | ||
REQUIRE(userScripts.count() == 0); | ||
} | ||
} | ||
} | ||
} | ||
|
||
GIVEN("2 scripts in settings") | ||
{ | ||
QStringList paths; | ||
paths << "/path2"; | ||
paths << "/path2"; | ||
settingsProvider.setValue("fakeService/userScriptPaths", paths); | ||
|
||
QStringList names; | ||
names << "name1"; | ||
names << "name2"; | ||
settingsProvider.setValue("fakeService/userScriptNames", names); | ||
|
||
WHEN("creating a UserScripts instance") | ||
{ | ||
UserScripts userScripts(serviceName, factoryMock.get(), settingsProvider); | ||
|
||
THEN("factory called once") | ||
{ | ||
Verify(Method(factoryMock, create)).Exactly(2); | ||
|
||
AND_THEN("name is set") | ||
{ | ||
Verify(Method(userScriptMock, setName)).Exactly(2); | ||
|
||
AND_THEN("load is called") | ||
{ | ||
Verify(Method(userScriptMock, load)).Exactly(2); | ||
|
||
AND_THEN("count is 2") | ||
{ | ||
REQUIRE(userScripts.count() == 2); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
tests/UnitTests/Infrastructure/UserScripts/UserScriptFactoryTests.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#include <catch.hpp> | ||
#include <MellowPlayer/Infrastructure/UserScripts/UserScriptFactory.hpp> | ||
|
||
using namespace MellowPlayer::Application; | ||
using namespace MellowPlayer::Infrastructure; | ||
|
||
SCENARIO("UserScriptFactoryTests") | ||
{ | ||
GIVEN("an instance of userscript factory") | ||
{ | ||
UserScriptFactory factory; | ||
|
||
WHEN("call create") | ||
{ | ||
auto* userScript = factory.create(); | ||
|
||
THEN("created user script is not null") | ||
{ | ||
REQUIRE(userScript != nullptr); | ||
} | ||
} | ||
} | ||
} |