-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce IOwned<out T> and IAsyncOwned<out T> (#158)
Introduce `IOwned<out T>` and `IAsyncOwned<out T>`. These can be resolved directly. Also allow passing a null disposeActions to Owned/AsyncOwned.
- Loading branch information
Showing
7 changed files
with
313 additions
and
16 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
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
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,55 @@ | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
|
||
namespace StrongInject.Generator.Tests.Unit | ||
{ | ||
public static class OwnedTests | ||
{ | ||
[Fact] | ||
public static void OwnedCanBeImplicitlyCastToWiderT() | ||
{ | ||
var narrowerType = new Owned<string>("", dispose: () => { }); | ||
IOwned<object> widerType = narrowerType; | ||
|
||
// This should survive any refactoring of the types or APIs. | ||
// (E.g. Replacing IOwned with owned.Cast<object>() returning an Owned<object> wrapper would not be acceptable.) | ||
Assert.Same(narrowerType, widerType); | ||
} | ||
|
||
[Fact] | ||
public static void AsyncOwnedCanBeImplicitlyCastToWiderT() | ||
{ | ||
var narrowerType = new AsyncOwned<string>("", dispose: () => ValueTask.CompletedTask); | ||
IAsyncOwned<object> widerType = narrowerType; | ||
|
||
// This should survive any refactoring of the types or APIs. | ||
// (E.g. Replacing IOwned with owned.Cast<object>() returning an Owned<object> wrapper would not be acceptable.) | ||
Assert.Same(narrowerType, widerType); | ||
} | ||
|
||
[Fact] | ||
public static void OwnedCanBeUsedWithNullDisposeAction() | ||
{ | ||
var owned = new Owned<string>("", dispose: null); | ||
|
||
// If a check is ever added in the future to return null or throw if Value is accessed after disposal, | ||
// the null action should not cause Owned<T> to think that disposal has already happened. | ||
Assert.Equal("", owned.Value); | ||
|
||
owned.Dispose(); | ||
} | ||
|
||
[Fact] | ||
public static void AsyncOwnedCanBeUsedWithNullDisposeAction() | ||
{ | ||
var owned = new AsyncOwned<string>("", dispose: null); | ||
|
||
// If a check is ever added in the future to return null or throw if Value is accessed after disposal, | ||
// the null action should not cause Owned<T> to think that disposal has already happened. | ||
Assert.Equal("", owned.Value); | ||
|
||
// This should complete instantly since there is nothing to do. | ||
Assert.True(owned.DisposeAsync().IsCompletedSuccessfully); | ||
} | ||
} | ||
} |
Oops, something went wrong.