Skip to content

Commit

Permalink
feat(DependencyInjector): rename RestoreDefaultDependency to ResetPro…
Browse files Browse the repository at this point in the history
…videdInstance

BREAKING CHANGE: DependencyInjector's RestoreDefaultDependency method has been renamed to ResetProvidedInstance
  • Loading branch information
jonisavo committed Jul 11, 2022
1 parent 03da01f commit 4be20c7
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 50 deletions.
2 changes: 1 addition & 1 deletion Assets/UIComponents.Tests/AssetPathAttributeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public void OneTimeSetUp()
public void TearDown()
{
_resolver.ClearReceivedCalls();
DependencyInjector.RestoreDefaultDependency<UIComponentWithAssetPaths, IAssetResolver>();
DependencyInjector.ResetProvidedInstance<UIComponentWithAssetPaths, IAssetResolver>();
}

[Test]
Expand Down
6 changes: 3 additions & 3 deletions Assets/UIComponents.Tests/DependencyInjectorStaticTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@ public void Removes_The_Injector_From_The_Dependency_Injector()
}

[TestFixture]
public class RestoreDefaultDependency
public class ResetProvidedInstance
{
[Test]
public void Restores_The_Default_Dependency_Instance()
public void Resets_The_Dependency_Instance()
{
var component = new UIComponentWithDependency();

DependencyInjector.ClearDependency<UIComponentWithDependency, IDependency>();
DependencyInjector.RestoreDefaultDependency<UIComponentWithDependency, IDependency>();
DependencyInjector.ResetProvidedInstance<UIComponentWithDependency, IDependency>();

Assert.That(component.GetDependency(), Is.InstanceOf<DependencyProvider>());
}
Expand Down
27 changes: 5 additions & 22 deletions Assets/UIComponents.Tests/DependencyInjectorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,46 +128,29 @@ public void Does_Not_Throw_If_Dependency_Does_Not_Exist()
}

[TestFixture]
public class RestoreDefaultDependency
public class ResetProvidedInstance
{
[SetUp]
public void SetUp()
{
DependencyInjector.Container.Clear();
}

[Test]
public void Restores_Default_Dependency()
{
var dependencyAttribute =
new DependencyAttribute(typeof(IDependency), typeof(DependencyOne));

var injector = new DependencyInjector(new[] {dependencyAttribute});

injector.SetDependency<IDependency>(new DependencyTwo());

Assert.That(injector.Provide<IDependency>(), Is.InstanceOf<DependencyTwo>());

injector.RestoreDefaultDependency<IDependency>();

Assert.That(injector.Provide<IDependency>(), Is.InstanceOf<DependencyOne>());
}

[Test]
public void Throws_If_No_Default_Dependency_Exists()
{
var injector = new DependencyInjector();

Assert.Throws<InvalidOperationException>(
() => injector.RestoreDefaultDependency<IDependency>()
() => injector.ResetProvidedInstance<IDependency>()
);

var initialDependency = new DependencyOne();

injector.SetDependency<IDependency>(initialDependency);

Assert.DoesNotThrow(
() => injector.RestoreDefaultDependency<IDependency>()
() => injector.ResetProvidedInstance<IDependency>()
);
}

Expand All @@ -180,7 +163,7 @@ public void Restores_Singleton_Instance()

injector.SetDependency<IDependency>(singletonInstance);
injector.SetDependency<IDependency>(new DependencyTwo());
injector.RestoreDefaultDependency<IDependency>();
injector.ResetProvidedInstance<IDependency>();

Assert.That(injector.Provide<IDependency>(), Is.SameAs(singletonInstance));
}
Expand All @@ -193,7 +176,7 @@ public void Creates_New_Transient_Instance()

injector.SetDependency<IDependency>(transientInstance, Scope.Transient);
injector.SetDependency<IDependency>(new DependencyTwo());
injector.RestoreDefaultDependency<IDependency>();
injector.ResetProvidedInstance<IDependency>();

Assert.That(injector.Provide<IDependency>(), Is.InstanceOf<DependencyOne>());
Assert.That(injector.Provide<IDependency>(), Is.Not.SameAs(transientInstance));
Expand Down
8 changes: 4 additions & 4 deletions Assets/UIComponents.Tests/LayoutAttributeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ public void OneTimeSetUp()
[OneTimeTearDown]
public void OneTimeTearDown()
{
DependencyInjector.RestoreDefaultDependency<UIComponentWithLayout, IAssetResolver>();
DependencyInjector.RestoreDefaultDependency<InheritedComponentWithoutAttribute, IAssetResolver>();
DependencyInjector.RestoreDefaultDependency<InheritedComponentWithAttribute, IAssetResolver>();
DependencyInjector.RestoreDefaultDependency<UIComponentWithNullLayout, IAssetResolver>();
DependencyInjector.ResetProvidedInstance<UIComponentWithLayout, IAssetResolver>();
DependencyInjector.ResetProvidedInstance<InheritedComponentWithoutAttribute, IAssetResolver>();
DependencyInjector.ResetProvidedInstance<InheritedComponentWithAttribute, IAssetResolver>();
DependencyInjector.ResetProvidedInstance<UIComponentWithNullLayout, IAssetResolver>();
}

[TearDown]
Expand Down
4 changes: 2 additions & 2 deletions Assets/UIComponents.Tests/PathAttributeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ public void OneTimeSetUp()
[OneTimeTearDown]
public void OneTimeTearDown()
{
DependencyInjector.RestoreDefaultDependency<UIComponentWithValidAssetPath, IAssetResolver>();
DependencyInjector.RestoreDefaultDependency<UIComponentWithInvalidAssetPath, IAssetResolver>();
DependencyInjector.ResetProvidedInstance<UIComponentWithValidAssetPath, IAssetResolver>();
DependencyInjector.ResetProvidedInstance<UIComponentWithInvalidAssetPath, IAssetResolver>();
}

[Test]
Expand Down
4 changes: 2 additions & 2 deletions Assets/UIComponents.Tests/StylesheetAttributeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ public void OneTimeSetUp()
[OneTimeTearDown]
public void OneTimeTearDown()
{
DependencyInjector.RestoreDefaultDependency<UIComponentWithTwoStylesheets, IAssetResolver>();
DependencyInjector.RestoreDefaultDependency<InheritedComponent, IAssetResolver>();
DependencyInjector.ResetProvidedInstance<UIComponentWithTwoStylesheets, IAssetResolver>();
DependencyInjector.ResetProvidedInstance<InheritedComponent, IAssetResolver>();
}

[TearDown]
Expand Down
2 changes: 1 addition & 1 deletion Assets/UIComponents.Tests/UIComponentNoAttributesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public void OneTimeSetUp()
[OneTimeTearDown]
public void OneTimeTearDown()
{
DependencyInjector.RestoreDefaultDependency<UIComponentNoAttributes, IAssetResolver>();
DependencyInjector.ResetProvidedInstance<UIComponentNoAttributes, IAssetResolver>();
}

[TearDown]
Expand Down
19 changes: 10 additions & 9 deletions Assets/UIComponents/Core/DependencyInjection/DependencyInjector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,9 @@ public class DependencyInjector
}

/// <summary>
/// Restores the default dependency, which is the one
/// set by <see cref="DependencyAttribute"/>.
/// Resets the provided instance of a dependency.
/// If a singleton instance exists, it is restored.
/// Otherwise, a new instance of the dependency is created.
/// </summary>
/// <remarks>
/// Can be used in unit tests to clear
Expand All @@ -73,15 +74,15 @@ public class DependencyInjector
/// <typeparam name="TConsumer">Consumer type</typeparam>
/// <typeparam name="TDependency">Dependency type</typeparam>
/// <exception cref="InvalidOperationException">
/// Thrown if no default dependency type exists
/// Thrown if no configured dependency exists
/// </exception>
public static void RestoreDefaultDependency<TConsumer, TDependency>()
public static void ResetProvidedInstance<TConsumer, TDependency>()
where TConsumer : class
where TDependency : class
{
var injector = GetInjector(typeof(TConsumer));

injector.RestoreDefaultDependency<TDependency>();
injector.ResetProvidedInstance<TDependency>();
}

/// <summary>
Expand Down Expand Up @@ -173,15 +174,15 @@ public void SetDependency<T>([NotNull] T instance, Scope scope = Scope.Singleton
}

/// <summary>
/// Restores the default dependency. If a singleton instance
/// exists, it is restored. Otherwise, a new instance
/// of the dependency is created.
/// Resets the provided instance of a dependency.
/// If a singleton instance exists, it is restored.
/// Otherwise, a new instance of the dependency is created.
/// </summary>
/// <typeparam name="T">Dependency type</typeparam>
/// <exception cref="InvalidOperationException">
/// Thrown if no configured dependency exists
/// </exception>
public void RestoreDefaultDependency<T>() where T : class
public void ResetProvidedInstance<T>() where T : class
{
var dependencyType = typeof(T);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ internal class DiContainer
/// by interested parties.
/// </summary>
internal readonly Dictionary<Type, object> SingletonInstanceDictionary;

public DiContainer()
{
InjectorDictionary =
Expand Down
21 changes: 16 additions & 5 deletions ProjectSettings/ProjectSettings.asset
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ PlayerSettings:
defaultCursor: {fileID: 0}
cursorHotspot: {x: 0, y: 0}
m_SplashScreenBackgroundColor: {r: 0.13725491, g: 0.12156863, b: 0.1254902, a: 1}
m_ShowUnitySplashScreen: 1
m_ShowUnitySplashScreen: 0
m_ShowUnitySplashLogo: 1
m_SplashScreenOverlayOpacity: 1
m_SplashScreenAnimation: 1
Expand Down Expand Up @@ -88,7 +88,7 @@ PlayerSettings:
bakeCollisionMeshes: 0
forceSingleInstance: 0
useFlipModelSwapchain: 1
resizableWindow: 0
resizableWindow: 1
useMacAppStoreValidation: 0
macAppStoreCategory: public.app-category.games
gpuSkinning: 1
Expand All @@ -99,7 +99,7 @@ PlayerSettings:
xboxEnableFitness: 0
visibleInBackground: 1
allowFullscreenSwitch: 1
fullscreenMode: 1
fullscreenMode: 3
xboxSpeechDB: 0
xboxEnableHeadOrientation: 0
xboxEnableGuest: 0
Expand Down Expand Up @@ -163,7 +163,7 @@ PlayerSettings:
AndroidMinSdkVersion: 19
AndroidTargetSdkVersion: 0
AndroidPreferredInstallLocation: 1
aotOptions:
aotOptions: nimt-trampolines=1024
stripEngineCode: 1
iPhoneStrippingLevel: 0
iPhoneScriptCallOptimization: 0
Expand Down Expand Up @@ -570,7 +570,18 @@ PlayerSettings:
ps4AllowPS5Detection: 0
ps4GPU800MHz: 1
ps4attribEyeToEyeDistanceSettingVR: 0
ps4IncludedModules: []
ps4IncludedModules:
- libc.prx
- libSceAudioLatencyEstimation.prx
- libSceFace.prx
- libSceFaceTracker.prx
- libSceFios2.prx
- libSceHand.prx
- libSceHandTracker.prx
- libSceHeadTracker.prx
- libSceJobManager.prx
- libSceNpToolkit2.prx
- libSceS3DConversion.prx
ps4attribVROutputEnabled: 0
monoEnv:
splashScreenBackgroundSourceLandscape: {fileID: 0}
Expand Down

0 comments on commit 4be20c7

Please sign in to comment.