-
Notifications
You must be signed in to change notification settings - Fork 268
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create IAutoValueProvider factory with all supported providers
Now it's possible to customize list of the auto-value providers by injecting a customized dependency to the SubstituteFactory.
- Loading branch information
Showing
12 changed files
with
117 additions
and
48 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,22 @@ | ||
using System; | ||
using NSubstitute.Routing; | ||
|
||
namespace NSubstitute.Core | ||
{ | ||
public class CallRouterFactory : ICallRouterFactory | ||
{ | ||
private readonly SequenceNumberGenerator _sequenceNumberGenerator; | ||
private readonly IThreadLocalContext _threadLocalContext; | ||
private readonly IRouteFactory _routeFactory; | ||
private readonly ICallSpecificationFactory _callSpecificationFactory; | ||
private readonly ICallInfoFactory _callInfoFactory; | ||
|
||
public CallRouterFactory(SequenceNumberGenerator sequenceNumberGenerator, | ||
IRouteFactory routeFactory, | ||
ICallSpecificationFactory callSpecificationFactory, | ||
ICallInfoFactory callInfoFactory) | ||
public CallRouterFactory(IThreadLocalContext threadLocalContext, IRouteFactory routeFactory) | ||
{ | ||
_sequenceNumberGenerator = sequenceNumberGenerator; | ||
_routeFactory = routeFactory; | ||
_callSpecificationFactory = callSpecificationFactory; | ||
_callInfoFactory = callInfoFactory; | ||
_threadLocalContext = threadLocalContext ?? throw new ArgumentNullException(nameof(threadLocalContext)); | ||
_routeFactory = routeFactory ?? throw new ArgumentNullException(nameof(routeFactory)); | ||
} | ||
|
||
public ICallRouter Create(SubstituteConfig config, IThreadLocalContext threadContext, ISubstituteFactory substituteFactory) | ||
public ICallRouter Create(ISubstituteState substituteState) | ||
{ | ||
var substituteState = new SubstituteState(config, | ||
_sequenceNumberGenerator, | ||
substituteFactory, | ||
_callSpecificationFactory, | ||
_callInfoFactory); | ||
|
||
return new CallRouter(substituteState, threadContext, _routeFactory); | ||
return new CallRouter(substituteState, _threadLocalContext, _routeFactory); | ||
} | ||
} | ||
} |
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,7 @@ | ||
namespace NSubstitute.Core | ||
{ | ||
public interface ISubstituteStateFactory | ||
{ | ||
ISubstituteState Create(SubstituteConfig config, ISubstituteFactory substituteFactory); | ||
} | ||
} |
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,34 @@ | ||
using NSubstitute.Routing.AutoValues; | ||
|
||
namespace NSubstitute.Core | ||
{ | ||
public class SubstituteStateFactory : ISubstituteStateFactory | ||
{ | ||
private readonly SequenceNumberGenerator _sequenceNumberGenerator; | ||
private readonly ICallSpecificationFactory _callSpecificationFactory; | ||
private readonly ICallInfoFactory _callInfoFactory; | ||
private readonly IAutoValueProvidersFactory _autoValueProvidersFactory; | ||
|
||
public SubstituteStateFactory(SequenceNumberGenerator sequenceNumberGenerator, | ||
ICallSpecificationFactory callSpecificationFactory, | ||
ICallInfoFactory callInfoFactory, | ||
IAutoValueProvidersFactory autoValueProvidersFactory) | ||
{ | ||
_sequenceNumberGenerator = sequenceNumberGenerator; | ||
_callSpecificationFactory = callSpecificationFactory; | ||
_callInfoFactory = callInfoFactory; | ||
_autoValueProvidersFactory = autoValueProvidersFactory; | ||
} | ||
|
||
public ISubstituteState Create(SubstituteConfig config, ISubstituteFactory substituteFactory) | ||
{ | ||
var autoValueProviders = _autoValueProvidersFactory.CreateProviders(substituteFactory); | ||
|
||
return new SubstituteState(config, | ||
_sequenceNumberGenerator, | ||
_callSpecificationFactory, | ||
_callInfoFactory, | ||
autoValueProviders); | ||
} | ||
} | ||
} |
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
30 changes: 30 additions & 0 deletions
30
src/NSubstitute/Routing/AutoValues/AutoValueProvidersFactory.cs
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; | ||
using System.Collections.Generic; | ||
using System.Threading; | ||
using NSubstitute.Core; | ||
|
||
namespace NSubstitute.Routing.AutoValues | ||
{ | ||
public class AutoValueProvidersFactory : IAutoValueProvidersFactory | ||
{ | ||
public IReadOnlyCollection<IAutoValueProvider> CreateProviders(ISubstituteFactory substituteFactory) | ||
{ | ||
IAutoValueProvider[] result = null; | ||
var lazyResult = new Lazy<IReadOnlyCollection<IAutoValueProvider>>( | ||
() => result ?? throw new InvalidOperationException("Value was not constructed yet."), | ||
LazyThreadSafetyMode.None); | ||
|
||
result = new IAutoValueProvider[] | ||
{ | ||
new AutoObservableProvider(lazyResult), | ||
new AutoQueryableProvider(), | ||
new AutoSubstituteProvider(substituteFactory), | ||
new AutoStringProvider(), | ||
new AutoArrayProvider(), | ||
new AutoTaskProvider(lazyResult) | ||
}; | ||
|
||
return result; | ||
} | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/NSubstitute/Routing/AutoValues/IAutoValueProvidersFactory.cs
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,10 @@ | ||
using System.Collections.Generic; | ||
using NSubstitute.Core; | ||
|
||
namespace NSubstitute.Routing.AutoValues | ||
{ | ||
public interface IAutoValueProvidersFactory | ||
{ | ||
IReadOnlyCollection<IAutoValueProvider> CreateProviders(ISubstituteFactory substituteFactory); | ||
} | ||
} |