-
-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add It.IsNot
#632
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
feat: add It.IsNot
#632
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
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,89 @@ | ||
| using System.Collections.Generic; | ||
| using System.Diagnostics; | ||
| 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 <paramref name="value" />. | ||
| /// </summary> | ||
| public static IIsNotParameter<T> IsNot<T>(T value, | ||
| [CallerArgumentExpression(nameof(value))] | ||
| string doNotPopulateThisValue = "") | ||
| => new ParameterEqualsNotMatch<T>(value, doNotPopulateThisValue); | ||
|
|
||
| /// <summary> | ||
| /// An <see cref="IParameter{T}" /> used for equality comparison. | ||
| /// </summary> | ||
| public interface IIsNotParameter<out T> : IParameter<T> | ||
| { | ||
| /// <summary> | ||
| /// Use the specified comparer to determine equality. | ||
| /// </summary> | ||
| IIsNotParameter<T> Using(IEqualityComparer<T> comparer, | ||
| [CallerArgumentExpression(nameof(comparer))] | ||
| string doNotPopulateThisValue = ""); | ||
| } | ||
|
|
||
| [DebuggerNonUserCode] | ||
| private sealed class ParameterEqualsNotMatch<T> : TypedMatch<T>, IIsNotParameter<T> | ||
| { | ||
| private readonly T _value; | ||
| private readonly string _valueExpression; | ||
| private IEqualityComparer<T>? _comparer; | ||
| private string? _comparerExpression; | ||
|
|
||
| public ParameterEqualsNotMatch(T value, string valueExpression) | ||
| { | ||
| _value = value; | ||
| _valueExpression = valueExpression; | ||
| } | ||
|
|
||
| /// <inheritdoc cref="IIsNotParameter{T}.Using(IEqualityComparer{T}, string)" /> | ||
| public IIsNotParameter<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) | ||
| { | ||
| if (_comparer is not null) | ||
| { | ||
| return !_comparer.Equals(value, _value); | ||
| } | ||
|
|
||
| return !EqualityComparer<T>.Default.Equals(value, _value); | ||
| } | ||
|
|
||
| public override bool Matches(object? value) | ||
| { | ||
| if (value is T typedValue) | ||
| { | ||
| return Matches(typedValue); | ||
| } | ||
|
|
||
| return value is not null || Matches(default!); | ||
| } | ||
|
|
||
| /// <inheritdoc cref="object.ToString()" /> | ||
| public override string ToString() | ||
| { | ||
| if (_comparer is not null) | ||
| { | ||
| return $"It.IsNot({_valueExpression}).Using({_comparerExpression})"; | ||
| } | ||
|
|
||
| return $"It.IsNot({_valueExpression})"; | ||
| } | ||
| } | ||
| } | ||
| #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,136 @@ | ||
| using Mockolate.Parameters; | ||
|
|
||
| namespace Mockolate.Tests; | ||
|
|
||
| public sealed partial class ItTests | ||
| { | ||
| public sealed class IsNotTests | ||
| { | ||
| [Fact] | ||
| public async Task ShouldCorrectlyHandleNull() | ||
| { | ||
| IMyService sut = IMyService.CreateMock(); | ||
| MyImplementation value1 = new(); | ||
| sut.Mock.Setup.DoSomething(It.IsNot<IMyBase>(null!)) | ||
| .Returns(3); | ||
|
|
||
| int result1 = sut.DoSomething(value1); | ||
| int result2 = sut.DoSomething(null!); | ||
|
|
||
| await That(result1).IsEqualTo(3); | ||
| await That(result2).IsEqualTo(0); | ||
| } | ||
|
vbreuss marked this conversation as resolved.
|
||
|
|
||
| [Fact] | ||
| public async Task ShouldCorrectlyHandleNullWithCovariance() | ||
| { | ||
| IMyService sut = IMyService.CreateMock(); | ||
| MyOtherImplementation value1 = new(); | ||
| sut.Mock.Setup.DoSomething(It.IsNot<MyImplementation>(null!)) | ||
| .Returns(3); | ||
|
|
||
| int result1 = sut.DoSomething(value1); | ||
| int result2 = sut.DoSomething(null!); | ||
|
|
||
| await That(result1).IsEqualTo(3); | ||
| await That(result2).IsEqualTo(0); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData(1, true)] | ||
| [InlineData(5, false)] | ||
| [InlineData(-5, true)] | ||
| [InlineData(42, true)] | ||
| public async Task ShouldMatchWhenNotEqual(int value, bool expectMatch) | ||
| { | ||
| IParameter<int> sut = It.IsNot(5); | ||
|
|
||
| bool result = ((IParameter)sut).Matches(value); | ||
|
|
||
| await That(result).IsEqualTo(expectMatch); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task ShouldSupportCovarianceInSetup() | ||
| { | ||
| IMyService sut = IMyService.CreateMock(); | ||
| MyImplementation value1 = new(); | ||
| MyOtherImplementation value2 = new(); | ||
| sut.Mock.Setup.DoSomething(It.IsNot(value1)) | ||
| .Returns(3); | ||
|
|
||
| int result1 = sut.DoSomething(value1); | ||
| int result2 = sut.DoSomething(value2); | ||
|
|
||
| await That(result1).IsEqualTo(0); | ||
| await That(result2).IsEqualTo(3); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task ToString_ShouldReturnExpectedValue() | ||
| { | ||
| IParameter<string> sut = It.IsNot("foo"); | ||
| string expectedValue = "It.IsNot(\"foo\")"; | ||
|
|
||
| string? result = sut.ToString(); | ||
|
|
||
| await That(result).IsEqualTo(expectedValue); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task ToString_WithComparer_ShouldReturnExpectedValue() | ||
| { | ||
| IParameter<int> sut = It.IsNot(4).Using(new AllEqualComparer()); | ||
| string expectedValue = "It.IsNot(4).Using(new AllEqualComparer())"; | ||
|
|
||
| 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.IsNot(5).Using(new AllEqualComparer()); | ||
|
|
||
| bool result = ((IParameter)sut).Matches(value); | ||
|
|
||
| await That(result).IsFalse(); | ||
| } | ||
|
|
||
| public interface IMyBase | ||
| { | ||
| int DoWork(); | ||
| } | ||
|
|
||
| public class MyImplementation : IMyBase | ||
| { | ||
| public int Progress { get; private set; } | ||
|
|
||
| public int DoWork() | ||
| { | ||
| Progress++; | ||
| return Progress; | ||
| } | ||
| } | ||
|
|
||
| public class MyOtherImplementation : IMyBase | ||
| { | ||
| public string Output { get; private set; } = ""; | ||
|
|
||
| public int DoWork() | ||
| { | ||
| Output += "did something\n"; | ||
| return 1; | ||
| } | ||
| } | ||
|
|
||
| public interface IMyService | ||
| { | ||
| int DoSomething(IMyBase 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.