-
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 all commits
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,29 +153,22 @@ 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> | ||
/// Test caching a nullable, where the providing type is within the osu.Framework assembly. | ||
/// </summary> | ||
[TestCase(null)] | ||
[TestCase(10)] | ||
public void TestCacheNullableInternal(int? testValue) | ||
[Test] | ||
public void TestAttemptCacheNullableInternal() | ||
{ | ||
var provider = new CachedNullableProvider(); | ||
provider.SetValue(null); | ||
|
||
provider.SetValue(testValue); | ||
|
||
var dependencies = DependencyActivator.MergeDependencies(provider, new DependencyContainer()); | ||
|
||
Assert.AreEqual(testValue, dependencies.GetValue<int?>()); | ||
Assert.Throws<NullDependencyException>(() => DependencyActivator.MergeDependencies(provider, new DependencyContainer())); | ||
} | ||
|
||
[Test] | ||
|
@@ -460,9 +457,7 @@ private class Provider22 : IDependencyInjectionCandidate | |
public object Provided1 | ||
{ | ||
get => null; | ||
set | ||
{ | ||
} | ||
set { } | ||
} | ||
} | ||
|
||
|
@@ -471,9 +466,7 @@ private class Provider23 : IDependencyInjectionCandidate | |
[Cached] | ||
public object Provided1 | ||
{ | ||
set | ||
{ | ||
} | ||
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.