-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
191 additions
and
45 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,30 @@ | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Runtime.CompilerServices; | ||
|
||
namespace DivertR.Internal | ||
{ | ||
internal class RedirectTracker | ||
{ | ||
private readonly ConditionalWeakTable<object, IRedirect> _redirectTable = new(); | ||
|
||
public void AddRedirect<TTarget>(Redirect<TTarget> redirect, [DisallowNull] TTarget proxy) where TTarget : class? | ||
{ | ||
_redirectTable.Add(proxy, redirect); | ||
} | ||
|
||
public Redirect<TTarget> GetRedirect<TTarget>([DisallowNull] TTarget proxy) where TTarget : class? | ||
{ | ||
if (!_redirectTable.TryGetValue(proxy, out var redirect)) | ||
{ | ||
throw new DiverterException("Redirect not found"); | ||
} | ||
|
||
if (redirect is not Redirect<TTarget> redirectOf) | ||
{ | ||
throw new DiverterException($"Redirect target type: {redirect.RedirectId.Type} does not match proxy type: {typeof(TTarget)}"); | ||
} | ||
|
||
return redirectOf; | ||
} | ||
} | ||
} |
This file was deleted.
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
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,90 @@ | ||
using DivertR.UnitTests.Model; | ||
using Shouldly; | ||
using Xunit; | ||
|
||
namespace DivertR.UnitTests; | ||
|
||
public class RedirectStaticTests | ||
{ | ||
[Fact] | ||
public void GivenProxy_WhenProxyMethodCalled_ThenRelayToRoot() | ||
{ | ||
// ARRANGE | ||
var proxy = Redirect.Proxy<IFoo>(new Foo("test")); | ||
|
||
// ACT | ||
var callResult = proxy.Name; | ||
|
||
// ASSERT | ||
callResult.ShouldBe("test"); | ||
} | ||
|
||
[Fact] | ||
public void GivenProxyWithNoRoot_WhenProxyCalled_ShouldReturnDefault() | ||
{ | ||
// ARRANGE | ||
var proxy = Redirect.Proxy<IFoo>(); | ||
|
||
// ACT | ||
var callResult = proxy.Name; | ||
|
||
// ASSERT | ||
callResult.ShouldBeNull(); | ||
} | ||
|
||
[Fact] | ||
public void GivenProxyWithVia_WhenMockCalled_ThenRedirects() | ||
{ | ||
// ARRANGE | ||
var proxy = Redirect.Proxy<IFoo>(); | ||
var redirect = Redirect.Of(proxy); | ||
|
||
redirect | ||
.To(x => x.Echo(Is<string>.Any)) | ||
.Via<(string input, __)>(call => $"{call.Args.input} redirected"); | ||
|
||
// ACT | ||
var result = proxy.Echo("test"); | ||
|
||
// ASSERT | ||
result.ShouldBe("test redirected"); | ||
} | ||
|
||
[Fact] | ||
public void GivenProxyAsObject_WhenRedirectOf_ThenThrowsDiverterException() | ||
{ | ||
// ARRANGE | ||
object fooProxy = Redirect.Proxy<IFoo>(); | ||
|
||
// ACT | ||
var testAction = () => Redirect.Of(fooProxy); | ||
|
||
// ASSERT | ||
testAction.ShouldThrow<DiverterException>(); | ||
} | ||
|
||
[Fact] | ||
public void GivenNonProxyObject_WhenRedirectOfObject_ThenThrowsDiverterException() | ||
{ | ||
// ARRANGE | ||
IFoo foo = new Foo(); | ||
|
||
// ACT | ||
var testAction = () => Redirect.Of(foo); | ||
|
||
// ASSERT | ||
testAction.ShouldThrow<DiverterException>(); | ||
} | ||
|
||
[Fact] | ||
public void GivenDispatchProxyFactory_WhenSpyOnClassType_ThenThrowsDiverterException() | ||
{ | ||
// ARRANGE | ||
|
||
// ACT | ||
var testAction = () => Redirect.Proxy<Foo>(); | ||
|
||
// ASSERT | ||
testAction.ShouldThrow<DiverterException>(); | ||
} | ||
} |