-
-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add It.IsNotNull and It.IsNotOneOf
#627
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 all commits
Commits
Show all changes
2 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
Some comments aren't visible on the classic Files Changed page.
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,28 @@ | ||
| using System.Diagnostics; | ||
| using Mockolate.Internals; | ||
| using Mockolate.Parameters; | ||
|
|
||
| namespace Mockolate; | ||
|
|
||
| #pragma warning disable S3453 // This class can't be instantiated; make its constructor 'public'. | ||
| #pragma warning disable S3218 // Inner class members should not shadow outer class "static" or type members | ||
| public partial class It | ||
| { | ||
| /// <summary> | ||
| /// Matches any parameter that is not <see langword="null" />. | ||
| /// </summary> | ||
| public static IParameter<T> IsNotNull<T>(string? toString = null) | ||
| => new NotNullParameterMatch<T>(toString); | ||
|
|
||
| [DebuggerNonUserCode] | ||
| private sealed class NotNullParameterMatch<T>(string? toString) : TypedMatch<T> | ||
| { | ||
| /// <inheritdoc cref="TypedMatch{T}.Matches(T)" /> | ||
| protected override bool Matches(T value) => value is not null; | ||
|
|
||
| /// <inheritdoc cref="object.ToString()" /> | ||
| public override string ToString() => toString ?? $"It.IsNotNull<{typeof(T).FormatType()}>()"; | ||
| } | ||
| } | ||
| #pragma warning restore S3218 // Inner class members should not shadow outer class "static" or type members | ||
| #pragma warning restore S3453 // This class can't be instantiated; make its constructor 'public'. |
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,68 @@ | ||
| using System.Collections.Generic; | ||
| using System.Diagnostics; | ||
| using System.Linq; | ||
| using System.Runtime.CompilerServices; | ||
| using Mockolate.Parameters; | ||
|
|
||
| namespace Mockolate; | ||
|
|
||
| #pragma warning disable S3453 // This class can't be instantiated; make its constructor 'public'. | ||
| #pragma warning disable S3218 // Inner class members should not shadow outer class "static" or type members | ||
| public partial class It | ||
| { | ||
| /// <summary> | ||
| /// Matches a parameter that is not equal to one of the <paramref name="values" />. | ||
| /// </summary> | ||
| public static IIsNotOneOfParameter<T> IsNotOneOf<T>(params IEnumerable<T> values) | ||
| => new ParameterIsNotOneOfMatch<T>(values.ToArray()); | ||
|
|
||
| /// <summary> | ||
| /// An <see cref="IParameter{T}" /> used for equality comparison of a collection of alternatives. | ||
| /// </summary> | ||
| public interface IIsNotOneOfParameter<out T> : IParameter<T> | ||
| { | ||
| /// <summary> | ||
| /// Use the specified comparer to determine equality. | ||
| /// </summary> | ||
| IIsNotOneOfParameter<T> Using(IEqualityComparer<T> comparer, | ||
| [CallerArgumentExpression(nameof(comparer))] | ||
| string doNotPopulateThisValue = ""); | ||
| } | ||
|
|
||
| [DebuggerNonUserCode] | ||
| private sealed class ParameterIsNotOneOfMatch<T>(T[] values) : TypedMatch<T>, IIsNotOneOfParameter<T> | ||
| { | ||
| private IEqualityComparer<T>? _comparer; | ||
| private string? _comparerExpression; | ||
|
|
||
| /// <inheritdoc cref="IIsParameter{T}.Using(IEqualityComparer{T}, string)" /> | ||
| public IIsNotOneOfParameter<T> Using(IEqualityComparer<T> comparer, string doNotPopulateThisValue = "") | ||
| { | ||
| _comparer = comparer; | ||
| _comparerExpression = doNotPopulateThisValue; | ||
| return this; | ||
| } | ||
|
|
||
| /// <inheritdoc cref="TypedMatch{T}.Matches(T)" /> | ||
| protected override bool Matches(T value) | ||
| { | ||
| IEqualityComparer<T> comparer = _comparer ?? EqualityComparer<T>.Default; | ||
| return values.All(v => !comparer.Equals(value, v)); | ||
| } | ||
|
|
||
| /// <inheritdoc cref="object.ToString()" /> | ||
| public override string ToString() | ||
| { | ||
| string result = | ||
| $"It.IsNotOneOf({string.Join(", ", values.Select(v => v is string ? $"\"{v}\"" : v?.ToString() ?? "null"))})"; | ||
| if (_comparer is not null) | ||
| { | ||
| result += $".Using({_comparerExpression})"; | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
| } | ||
| } | ||
| #pragma warning restore S3218 // Inner class members should not shadow outer class "static" or type members | ||
| #pragma warning restore S3453 // This class can't be instantiated; make its constructor 'public'. | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| using Mockolate.Parameters; | ||
|
|
||
| namespace Mockolate.Tests; | ||
|
|
||
| public sealed partial class ItTests | ||
| { | ||
| public sealed class IsNotNullTests | ||
| { | ||
| [Theory] | ||
| [InlineData(null, 0)] | ||
| [InlineData(1, 1)] | ||
| public async Task NotNull_ShouldMatchWhenNotNull(int? value, int expectedCount) | ||
| { | ||
| IMyServiceWithNullable sut = IMyServiceWithNullable.CreateMock(); | ||
| sut.Mock.Setup.DoSomething(It.IsNotNull<int?>(), It.Is(true)); | ||
|
|
||
| sut.DoSomething(value, true); | ||
|
|
||
| await That(sut.Mock.Verify.DoSomething(It.IsNotNull<int?>(), It.Is(true))).Exactly(expectedCount); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task ToString_ShouldReturnExpectedValue() | ||
| { | ||
| IParameter<string> sut = It.IsNotNull<string>(); | ||
| string expectedValue = "It.IsNotNull<string>()"; | ||
|
|
||
| string? result = sut.ToString(); | ||
|
|
||
| await That(result).IsEqualTo(expectedValue); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task ToString_WithCustomValue_ShouldReturnCustomValue() | ||
| { | ||
| IParameter<string> sut = It.IsNotNull<string>("custom"); | ||
| string expectedValue = "custom"; | ||
|
|
||
| string? result = sut.ToString(); | ||
|
|
||
| await That(result).IsEqualTo(expectedValue); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData(null, false)] | ||
| [InlineData("", true)] | ||
| [InlineData("foo", true)] | ||
| public async Task WithValue_Nullable_ShouldMatchWhenNotNull(string? value, bool expectMatch) | ||
| { | ||
| IParameter<string?> sut = It.IsNotNull<string?>(); | ||
|
|
||
| bool result = ((IParameter)sut).Matches(value); | ||
|
|
||
| await That(result).IsEqualTo(expectMatch); | ||
| } | ||
| } | ||
| } |
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,117 @@ | ||
| using Mockolate.Parameters; | ||
|
|
||
| namespace Mockolate.Tests; | ||
|
|
||
| public sealed partial class ItTests | ||
| { | ||
| public sealed class IsNotOneOfTests | ||
| { | ||
| [Theory] | ||
| [InlineData(1, true)] | ||
| [InlineData(4, true)] | ||
| [InlineData(5, false)] | ||
| [InlineData(6, false)] | ||
| [InlineData(7, false)] | ||
| [InlineData(8, true)] | ||
| [InlineData(-5, true)] | ||
| [InlineData(42, true)] | ||
| public async Task ShouldMatchWhenNotEqualToAny(int value, bool expectMatch) | ||
| { | ||
| IParameter<int> sut = It.IsNotOneOf(5, 6, 7); | ||
|
|
||
| bool result = ((IParameter)sut).Matches(value); | ||
|
|
||
| await That(result).IsEqualTo(expectMatch); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task ShouldSupportCovarianceInSetup() | ||
| { | ||
| IMyServiceIsNotOneOf sut = IMyServiceIsNotOneOf.CreateMock(); | ||
| MyBaseIsNotOneOf value1 = new MyImplementationIsNotOneOf(); | ||
| MyBaseIsNotOneOf value2 = new MyImplementationIsNotOneOf(); | ||
| MyBaseIsNotOneOf other = new MyOtherImplementationIsNotOneOf(); | ||
| sut.Mock.Setup.DoSomething(It.IsNotOneOf(value1, value2)) | ||
| .Returns(3); | ||
|
|
||
| int result1 = sut.DoSomething(other); | ||
| int result2 = sut.DoSomething(value1); | ||
|
|
||
| await That(result1).IsEqualTo(3); | ||
| await That(result2).IsEqualTo(0); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task ShouldSupportCovarianceInVerify() | ||
| { | ||
| IMyServiceIsNotOneOf sut = IMyServiceIsNotOneOf.CreateMock(); | ||
| MyImplementationIsNotOneOf value1 = new(); | ||
| MyImplementationIsNotOneOf value2 = new(); | ||
| MyImplementationIsNotOneOf other = new(); | ||
|
|
||
| sut.DoSomething(other); | ||
|
|
||
| // 'other' is not in [value1, value2], so the match is found once | ||
| await That(sut.Mock.Verify.DoSomething(It.IsNotOneOf(value1, value2))).Once(); | ||
| // 'other' IS in [other], so no match | ||
| await That(sut.Mock.Verify.DoSomething(It.IsNotOneOf(other))).Never(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task ToString_Using_ShouldReturnExpectedValue() | ||
| { | ||
| IParameter<int> sut = It.IsNotOneOf(3, 5).Using(new AllEqualComparer()); | ||
| string expectedValue = "It.IsNotOneOf(3, 5).Using(new AllEqualComparer())"; | ||
|
|
||
| string? result = sut.ToString(); | ||
|
|
||
| await That(result).IsEqualTo(expectedValue); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task ToString_WithNullableIntValues_ShouldReturnExpectedValue() | ||
| { | ||
| IParameter<int?> sut = It.IsNotOneOf<int?>(3, null, 5); | ||
| string expectedValue = "It.IsNotOneOf(3, null, 5)"; | ||
|
|
||
| string? result = sut.ToString(); | ||
|
|
||
| await That(result).IsEqualTo(expectedValue); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task ToString_WithStringValues_ShouldReturnExpectedValue() | ||
| { | ||
| IParameter<string> sut = It.IsNotOneOf("foo", "bar"); | ||
| string expectedValue = "It.IsNotOneOf(\"foo\", \"bar\")"; | ||
|
|
||
| string? result = sut.ToString(); | ||
|
|
||
| await That(result).IsEqualTo(expectedValue); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData(1)] | ||
| [InlineData(5)] | ||
| [InlineData(-42)] | ||
| public async Task WithComparer_ShouldUseComparer(int value) | ||
| { | ||
| IParameter<int> sut = It.IsNotOneOf(4, 5, 6).Using(new AllEqualComparer()); | ||
|
|
||
| bool result = ((IParameter)sut).Matches(value); | ||
|
|
||
| await That(result).IsFalse(); | ||
| } | ||
|
|
||
| public abstract class MyBaseIsNotOneOf; | ||
|
|
||
| public class MyImplementationIsNotOneOf : MyBaseIsNotOneOf; | ||
|
|
||
| public class MyOtherImplementationIsNotOneOf : MyBaseIsNotOneOf; | ||
|
|
||
| public interface IMyServiceIsNotOneOf | ||
| { | ||
| int DoSomething(MyBaseIsNotOneOf value); | ||
| } | ||
| } | ||
| } |
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.