-
Notifications
You must be signed in to change notification settings - Fork 424
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow value types to be cached and injected as dependencies #6159
base: master
Are you sure you want to change the base?
Changes from 1 commit
0a0d4d5
5ee40fd
8ff8f8c
fd23266
4082fe3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -51,11 +51,13 @@ public void TestCacheTypeOverrideParentCache() | |
} | ||
|
||
[Test] | ||
public void TestAttemptToCacheStruct() | ||
public void TestCacheStruct() | ||
{ | ||
var provider = new Provider4(); | ||
|
||
Assert.Throws<ArgumentException>(() => DependencyActivator.MergeDependencies(provider, new DependencyContainer())); | ||
var dependencies = DependencyActivator.MergeDependencies(provider, new DependencyContainer()); | ||
|
||
Assert.IsNotNull(dependencies.Get<int?>()); | ||
} | ||
|
||
[Test] | ||
|
@@ -136,7 +138,9 @@ public void TestCacheStructAsInterface() | |
{ | ||
var provider = new Provider12(); | ||
|
||
Assert.Throws<ArgumentException>(() => DependencyActivator.MergeDependencies(provider, new DependencyContainer())); | ||
var dependencies = DependencyActivator.MergeDependencies(provider, new DependencyContainer()); | ||
|
||
Assert.IsNotNull(dependencies.Get<IProvidedInterface1>()); | ||
} | ||
|
||
/// <summary> | ||
|
@@ -149,13 +153,13 @@ public void TestCacheStructInternal() | |
|
||
var dependencies = DependencyActivator.MergeDependencies(provider, new DependencyContainer()); | ||
|
||
Assert.AreEqual(provider.CachedObject.Value, dependencies.GetValue<CachedStructProvider.Struct>().Value); | ||
Assert.AreEqual(provider.CachedObject.Value, dependencies.Get<CachedStructProvider.Struct>().Value); | ||
} | ||
|
||
[Test] | ||
public void TestGetValueNullInternal() | ||
public void TestGetNullInternal() | ||
{ | ||
Assert.AreEqual(default(int), new DependencyContainer().GetValue<int>()); | ||
Assert.AreEqual(default(int), new DependencyContainer().Get<int>()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This creates a duality in semantics in that a "required value-type dependency" does not practically exist. If you forget to cache one, anyone that tries to resolve it will just get If the argument here is that that is fine and matches the general type semantics of C# in general, that's fine, but should probably be documented better. |
||
} | ||
|
||
/// <summary> | ||
|
@@ -171,7 +175,7 @@ public void TestCacheNullableInternal(int? testValue) | |
|
||
var dependencies = DependencyActivator.MergeDependencies(provider, new DependencyContainer()); | ||
|
||
Assert.AreEqual(testValue, dependencies.GetValue<int?>()); | ||
Assert.AreEqual(testValue, dependencies.Get<int?>()); | ||
} | ||
|
||
[Test] | ||
|
@@ -460,9 +464,7 @@ private class Provider22 : IDependencyInjectionCandidate | |
public object Provided1 | ||
{ | ||
get => null; | ||
set | ||
{ | ||
} | ||
set { } | ||
} | ||
} | ||
|
||
|
@@ -471,9 +473,7 @@ private class Provider23 : IDependencyInjectionCandidate | |
[Cached] | ||
public object Provided1 | ||
{ | ||
set | ||
{ | ||
} | ||
set { } | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
#nullable disable | ||
|
||
using System.Diagnostics.CodeAnalysis; | ||
using System.Threading; | ||
using NUnit.Framework; | ||
using osu.Framework.Allocation; | ||
using osu.Framework.Bindables; | ||
|
@@ -160,7 +161,10 @@ public void TestResolveNullableInternal(int? testValue) | |
[Test] | ||
public void TestResolveStructWithoutNullPermits() | ||
{ | ||
Assert.Throws<DependencyNotRegisteredException>(() => new DependencyContainer().Inject(new Receiver14())); | ||
var receiver = new Receiver14(); | ||
|
||
Assert.DoesNotThrow(() => new DependencyContainer().Inject(receiver)); | ||
Assert.AreEqual(0, receiver.Obj); | ||
Comment on lines
+148
to
+151
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again, this test is basically |
||
} | ||
|
||
[Test] | ||
|
@@ -212,6 +216,26 @@ public void TestResolveNonNullWithNullableReferenceTypes() | |
Assert.DoesNotThrow(() => createDependencies(new Bindable<int>(10)).Inject(receiver)); | ||
} | ||
|
||
[Test] | ||
public void TestResolveDefaultStruct() | ||
{ | ||
var receiver = new Receiver19(); | ||
|
||
createDependencies().Inject(receiver); | ||
|
||
Assert.That(receiver.Token, Is.EqualTo(default(CancellationToken))); | ||
} | ||
|
||
[Test] | ||
public void TestResolveNullStruct() | ||
{ | ||
var receiver = new Receiver20(); | ||
|
||
createDependencies().Inject(receiver); | ||
|
||
Assert.That(receiver.Token, Is.Null); | ||
} | ||
|
||
private DependencyContainer createDependencies(params object[] toCache) | ||
{ | ||
var dependencies = new DependencyContainer(); | ||
|
@@ -342,5 +366,17 @@ private class Receiver18 : IDependencyInjectionCandidate | |
[Resolved] | ||
public Bindable<int> Obj { get; private set; } = null!; | ||
} | ||
|
||
private class Receiver19 : IDependencyInjectionCandidate | ||
{ | ||
[Resolved] | ||
public CancellationToken Token { get; private set; } | ||
} | ||
|
||
private class Receiver20 : IDependencyInjectionCandidate | ||
{ | ||
[Resolved] | ||
public CancellationToken? Token { get; private set; } | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test is pretty much superseded by
TestCacheStruct
it looks like. Probably can be removed.