-
-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add WithDefaultValueFor to mock behavior
#377
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,42 @@ | ||
| using System; | ||
|
|
||
| namespace Mockolate; | ||
|
|
||
| /// <summary> | ||
| /// Defines a mechanism for generating default values. | ||
| /// </summary> | ||
| public class DefaultValueFactory | ||
| { | ||
| private readonly Func<Type, object?[], object?>? _generator; | ||
| private readonly Func<Type, bool>? _predicate; | ||
|
|
||
| /// <summary> | ||
| /// This constructor is protected to allow inheritance. | ||
| /// </summary> | ||
| // ReSharper disable once UnusedMember.Global | ||
| protected DefaultValueFactory() | ||
| { | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Creates a new default value factory for types that match the given <paramref name="predicate" />. | ||
| /// </summary> | ||
| public DefaultValueFactory(Func<Type, bool> predicate, Func<Type, object?[], object?> generator) | ||
| { | ||
| _predicate = predicate; | ||
| _generator = generator; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Checks if the factory can generate a value for the specified <paramref name="type" />. | ||
| /// </summary> | ||
| public virtual bool CanGenerateValue(Type type) | ||
| => _predicate?.Invoke(type) ?? false; | ||
|
|
||
| /// <summary> | ||
| /// Generates a default value of the specified <paramref name="type" />, with | ||
| /// the <paramref name="parameters" /> for context. | ||
| /// </summary> | ||
| public virtual object? GenerateValue(Type type, params object?[] parameters) | ||
| => _generator?.Invoke(type, parameters); | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
93 changes: 93 additions & 0 deletions
93
Tests/Mockolate.Tests/MockBehaviorTests.WithDefaultValueForTests.cs
This file contains hidden or 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,93 @@ | ||
| namespace Mockolate.Tests; | ||
|
|
||
| public sealed partial class MockBehaviorTests | ||
| { | ||
| public sealed class WithDefaultValueForTests | ||
| { | ||
| [Fact] | ||
| public async Task MultipleTypedFactories_ShouldSupportAll() | ||
| { | ||
| MockBehavior behavior = MockBehavior.Default | ||
| .WithDefaultValueFor(() => new MySpecialValue(5)) | ||
| .WithDefaultValueFor(() => new MyInheritedSpecialValue(6, 7)); | ||
|
|
||
| IMyServiceForMySpecialValue mockWithBehavior | ||
| = Mock.Create<IMyServiceForMySpecialValue>(behavior); | ||
|
|
||
| await That(mockWithBehavior.Value.Value).IsEqualTo(5); | ||
| await That(mockWithBehavior.InheritedValue) | ||
| .For(x => x.Value, it => it.IsEqualTo(6)).And | ||
| .For(x => x.OtherValue, it => it.IsEqualTo(7)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task WithExplicitDefaultValueFactory_ShouldApplyThisFactory() | ||
| { | ||
| DefaultValueFactory defaultValueFactory = new( | ||
| t => typeof(MySpecialValue).IsAssignableFrom(t), | ||
| (_, _) => new MyInheritedSpecialValue(6, 7)); | ||
| MockBehavior behavior = MockBehavior.Default | ||
| .WithDefaultValueFor(defaultValueFactory); | ||
|
|
||
| IMyServiceForMySpecialValue mockWithBehavior | ||
| = Mock.Create<IMyServiceForMySpecialValue>(behavior); | ||
|
|
||
| await That(mockWithBehavior.Value.Value).IsEqualTo(6); | ||
| await That(mockWithBehavior.InheritedValue) | ||
| .For(x => x.Value, it => it.IsEqualTo(6)).And | ||
| .For(x => x.OtherValue, it => it.IsEqualTo(7)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task WithTypedFactory_ShouldRequireExactTypeMatch() | ||
| { | ||
| MockBehavior behavior1 = MockBehavior.Default | ||
| .WithDefaultValueFor(() => new MySpecialValue(5)); | ||
| MockBehavior behavior2 = MockBehavior.Default | ||
| .WithDefaultValueFor(() => new MyInheritedSpecialValue(6, 7)); | ||
|
|
||
| IMyServiceForMySpecialValue mockWithBehavior1 | ||
| = Mock.Create<IMyServiceForMySpecialValue>(behavior1); | ||
| IMyServiceForMySpecialValue mockWithBehavior2 | ||
| = Mock.Create<IMyServiceForMySpecialValue>(behavior2); | ||
|
|
||
| await That(mockWithBehavior1.InheritedValue).IsNull(); | ||
| await That(mockWithBehavior1.Value.Value).IsEqualTo(5); | ||
| await That(mockWithBehavior2.InheritedValue) | ||
| .For(x => x.Value, it => it.IsEqualTo(6)).And | ||
| .For(x => x.OtherValue, it => it.IsEqualTo(7)); | ||
| await That(mockWithBehavior2.Value).IsNull(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task WithTypedFactory_ShouldUseDefaultValueFactoryFromBehavior() | ||
| { | ||
| MockBehavior behavior = MockBehavior.Default | ||
| .WithDefaultValueFor(() => new MySpecialValue(5)); | ||
|
|
||
| IMyServiceForMySpecialValue mockWithDefaultBehavior | ||
| = Mock.Create<IMyServiceForMySpecialValue>(); | ||
| IMyServiceForMySpecialValue mockWithCustomBehavior | ||
| = Mock.Create<IMyServiceForMySpecialValue>(behavior); | ||
|
|
||
| await That(mockWithDefaultBehavior.Value).IsNull(); | ||
| await That(mockWithCustomBehavior.Value.Value).IsEqualTo(5); | ||
| } | ||
|
|
||
| internal class MySpecialValue(int value) | ||
| { | ||
| public int Value { get; } = value; | ||
| } | ||
|
|
||
| internal class MyInheritedSpecialValue(int value, int otherValue) : MySpecialValue(value) | ||
| { | ||
| public int OtherValue { get; } = otherValue; | ||
| } | ||
|
|
||
| internal interface IMyServiceForMySpecialValue | ||
| { | ||
| MySpecialValue Value { get; } | ||
| MyInheritedSpecialValue InheritedValue { get; } | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.