-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding service dependency injection feature and additional `TryGetSer…
…vice` simplified call. (#111)
- Loading branch information
1 parent
b072245
commit 170fc5e
Showing
11 changed files
with
299 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// Copyright (c) Reality Collective. All rights reserved. | ||
// Licensed under the MIT License. See LICENSE in the project root for license information. | ||
|
||
namespace RealityCollective.ServiceFramework.Tests.Interfaces | ||
{ | ||
public interface ITestDependencyService1 : ITestService { } | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,7 @@ | ||
// Copyright (c) Reality Collective. All rights reserved. | ||
// Licensed under the MIT License. See LICENSE in the project root for license information. | ||
|
||
namespace RealityCollective.ServiceFramework.Tests.Interfaces | ||
{ | ||
public interface ITestDependencyService2 : ITestService { } | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,30 @@ | ||
// Copyright (c) Reality Collective. All rights reserved. | ||
// Licensed under the MIT License. See LICENSE in the project root for license information. | ||
|
||
using RealityCollective.ServiceFramework.Definitions; | ||
using RealityCollective.ServiceFramework.Services; | ||
using RealityCollective.ServiceFramework.Tests.Interfaces; | ||
using UnityEngine; | ||
|
||
namespace RealityCollective.ServiceFramework.Tests.Services | ||
{ | ||
public class DependencyTestService1 : BaseServiceWithConstructor, ITestDependencyService1 | ||
{ | ||
public const string TestName = "Dependency Test Service 1"; | ||
public ITestService1 testService1; | ||
|
||
public DependencyTestService1(string name, uint priority, BaseProfile profile, ITestService1 testService1) | ||
: base(name, priority) | ||
{ | ||
this.testService1 = testService1; | ||
} | ||
|
||
public override void Initialize() | ||
{ | ||
//base.Initialize(); | ||
Debug.Log($"{TestName} is Initialised"); | ||
} | ||
|
||
public override bool RegisterServiceModules => false; | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,32 @@ | ||
// Copyright (c) Reality Collective. All rights reserved. | ||
// Licensed under the MIT License. See LICENSE in the project root for license information. | ||
|
||
using RealityCollective.ServiceFramework.Definitions; | ||
using RealityCollective.ServiceFramework.Services; | ||
using RealityCollective.ServiceFramework.Tests.Interfaces; | ||
using UnityEngine; | ||
|
||
namespace RealityCollective.ServiceFramework.Tests.Services | ||
{ | ||
public class DependencyTestService2 : BaseServiceWithConstructor, ITestDependencyService2 | ||
{ | ||
public const string TestName = "Dependency Test Service 2"; | ||
public ITestService1 testService1; | ||
public ITestDependencyService1 testService2; | ||
|
||
public DependencyTestService2(string name, uint priority, BaseProfile profile, ITestService1 testService1, ITestDependencyService1 testService2) | ||
: base(name, priority) | ||
{ | ||
this.testService1 = testService1; | ||
this.testService2 = testService2; | ||
} | ||
|
||
public override void Initialize() | ||
{ | ||
//base.Initialize(); | ||
Debug.Log($"{TestName} is Initialised"); | ||
} | ||
|
||
public override bool RegisterServiceModules => false; | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,107 @@ | ||
// Copyright (c) Reality Collective. All rights reserved. | ||
|
||
using NUnit.Framework; | ||
using RealityCollective.ServiceFramework.Definitions; | ||
using RealityCollective.ServiceFramework.Definitions.Platforms; | ||
using RealityCollective.ServiceFramework.Interfaces; | ||
using RealityCollective.ServiceFramework.Services; | ||
using RealityCollective.ServiceFramework.Tests.Interfaces; | ||
using RealityCollective.ServiceFramework.Tests.Services; | ||
using RealityCollective.ServiceFramework.Tests.Utilities; | ||
using System.Text.RegularExpressions; | ||
using UnityEngine; | ||
using UnityEngine.TestTools; | ||
|
||
namespace RealityCollective.ServiceFramework.Tests.N_ServiceDependency | ||
{ | ||
internal class ServiceDependencyTests | ||
{ | ||
private ServiceManager testServiceManager; | ||
|
||
#region Service Retrieval | ||
|
||
[Test] | ||
public void Test_10_01_ParentServiceRegistration() | ||
{ | ||
TestUtilities.InitializeServiceManagerScene(ref testServiceManager); | ||
|
||
var activeServiceCount = testServiceManager.ActiveServices.Count; | ||
|
||
var config = new ServiceConfiguration<ITestService1>(typeof(TestService1), TestService1.TestName, 1, AllPlatforms.Platforms, null); | ||
var serviceResult = testServiceManager.TryCreateAndRegisterService<ITestService1>(config, out ITestService1 testService1); | ||
|
||
// Tests | ||
Assert.IsTrue(serviceResult, "Test service was not registered"); | ||
Assert.IsNotNull(testService1, "Test service instance was not returned"); | ||
Assert.IsTrue(testService1.ServiceGuid != System.Guid.Empty, "No GUID generated for the test service"); | ||
Assert.AreEqual(activeServiceCount + 1, testServiceManager.ActiveServices.Count, "More or less services found than was expected"); | ||
} | ||
|
||
[Test] | ||
public void Test_10_02_DependentServiceRegistration() | ||
{ | ||
TestUtilities.InitializeServiceManagerScene(ref testServiceManager); | ||
|
||
var activeServiceCount = testServiceManager.ActiveServices.Count; | ||
|
||
var config = new ServiceConfiguration<ITestService1>(typeof(TestService1), TestService1.TestName, 1, AllPlatforms.Platforms, null); | ||
testServiceManager.TryCreateAndRegisterService<ITestService1>(config, out ITestService1 testService1); | ||
|
||
var config2 = new ServiceConfiguration<ITestDependencyService1>(typeof(DependencyTestService1), "Dependency Service", 1, AllPlatforms.Platforms, null); | ||
var serviceResult2 = testServiceManager.TryCreateAndRegisterService<ITestDependencyService1>(config2, out ITestDependencyService1 testService2); | ||
|
||
// Tests | ||
Assert.IsTrue(serviceResult2, "Test service 2 was not registered"); | ||
Assert.IsNotNull(testService2, "Test service 2 instance was not returned"); | ||
Assert.IsTrue(testService2.ServiceGuid == System.Guid.Empty, "GUID found for the second test service when none configured"); | ||
Assert.AreEqual(activeServiceCount + 2, testServiceManager.ActiveServices.Count, "More or less services found than was expected"); | ||
Assert.IsNotNull((testService2 as DependencyTestService1)?.testService1, "Dependent service was not injected with parent service"); | ||
} | ||
|
||
[Test] | ||
public void Test_10_03_DependentServiceRegistrationMissingDependency() | ||
{ | ||
TestUtilities.InitializeServiceManagerScene(ref testServiceManager); | ||
|
||
var activeServiceCount = testServiceManager.ActiveServices.Count; | ||
|
||
var config2 = new ServiceConfiguration<ITestDependencyService1>(typeof(DependencyTestService1), "Dependency Service", 1, AllPlatforms.Platforms, null); | ||
var serviceResult2 = testServiceManager.TryCreateAndRegisterService<ITestDependencyService1>(config2, out ITestDependencyService1 testService2); | ||
LogAssert.Expect(LogType.Error, new Regex("Failed to find an ITestService1 service to inject into testService1!")); | ||
LogAssert.Expect(LogType.Error, new Regex("Failed to register the DependencyTestService1 service due to missing dependencies. Ensure all dependencies are registered prior to registering this service.")); | ||
|
||
// Tests | ||
Assert.IsFalse(serviceResult2, "Test service 2 was registered, when it should not have been"); | ||
Assert.IsNull(testService2, "Test service 2 instance was returned"); | ||
Assert.AreEqual(activeServiceCount, testServiceManager.ActiveServices.Count, "More or less services found than was expected"); | ||
Assert.IsNull((testService2 as DependencyTestService1)?.testService1, "Dependent service was not injected with parent service"); | ||
} | ||
|
||
[Test] | ||
public void Test_10_04_MultipleDependentServiceRegistration() | ||
{ | ||
TestUtilities.InitializeServiceManagerScene(ref testServiceManager); | ||
|
||
var activeServiceCount = testServiceManager.ActiveServices.Count; | ||
|
||
var config = new ServiceConfiguration<ITestService1>(typeof(TestService1), TestService1.TestName, 1, AllPlatforms.Platforms, null); | ||
testServiceManager.TryCreateAndRegisterService<ITestService1>(config, out ITestService1 testService1); | ||
|
||
var config2 = new ServiceConfiguration<ITestDependencyService1>(typeof(DependencyTestService1), "Dependency Service", 1, AllPlatforms.Platforms, null); | ||
testServiceManager.TryCreateAndRegisterService<ITestDependencyService1>(config2, out ITestDependencyService1 testService2); | ||
|
||
var config3 = new ServiceConfiguration<ITestDependencyService2>(typeof(DependencyTestService2), "Dependency Service", 1, AllPlatforms.Platforms, null); | ||
var serviceResult3 = testServiceManager.TryCreateAndRegisterService<ITestDependencyService2>(config3, out ITestDependencyService2 testService3); | ||
|
||
// Tests | ||
Assert.IsTrue(serviceResult3, "Test service 3 was not registered"); | ||
Assert.IsNotNull(testService3, "Test service 3 instance was not returned"); | ||
Assert.IsTrue(testService3.ServiceGuid == System.Guid.Empty, "GUID found for the second test service when none configured"); | ||
Assert.AreEqual(activeServiceCount + 3, testServiceManager.ActiveServices.Count, "More or less services found than was expected"); | ||
Assert.IsNotNull((testService3 as DependencyTestService2)?.testService1, "Dependent service was not injected with parent service"); | ||
Assert.IsNotNull((testService3 as DependencyTestService2)?.testService2, "Dependent service was not injected with parent service"); | ||
} | ||
|
||
#endregion Service Retrieval | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.