-
-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add Is{Not}EquatableTo
#745
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
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,74 @@ | ||
| using System; | ||
| using aweXpect.Core; | ||
| using aweXpect.Core.Constraints; | ||
| using aweXpect.Helpers; | ||
| using aweXpect.Results; | ||
|
|
||
| namespace aweXpect; | ||
|
|
||
| public static partial class ThatGeneric | ||
| { | ||
| /// <summary> | ||
| /// Verifies that the subject is equal to the <paramref name="expected" /> value | ||
| /// using the <see cref="IEquatable{T}.Equals(T)" /> method. | ||
| /// </summary> | ||
| public static AndOrResult<TEquatable, IThat<TEquatable>> IsEquatableTo<T, TEquatable>( | ||
| this IThat<TEquatable> source, | ||
| T expected) | ||
| where TEquatable : IEquatable<T> | ||
| => new(source.Get().ExpectationBuilder.AddConstraint((it, grammars) | ||
| => new IsEquatableToConstraint<T, TEquatable>(it, grammars, expected)), | ||
| source); | ||
|
|
||
| /// <summary> | ||
| /// Verifies that the subject is not equal to the <paramref name="unexpected" /> value | ||
| /// using the <see cref="IEquatable{T}.Equals(T)" /> method. | ||
| /// </summary> | ||
| public static AndOrResult<TEquatable, IThat<TEquatable>> IsNotEquatableTo<T, TEquatable>( | ||
| this IThat<TEquatable> source, | ||
| T unexpected) | ||
| where TEquatable : IEquatable<T> | ||
| => new(source.Get().ExpectationBuilder.AddConstraint((it, grammars) | ||
| => new IsEquatableToConstraint<T, TEquatable>(it, grammars, unexpected).Invert()), | ||
| source); | ||
|
|
||
| private sealed class IsEquatableToConstraint<T, TEquatable>( | ||
| string it, | ||
| ExpectationGrammars grammars, | ||
| T expected) | ||
| : ConstraintResult.WithValue<IEquatable<T>>(grammars), | ||
| IValueConstraint<TEquatable> | ||
| where TEquatable : IEquatable<T> | ||
| { | ||
| public ConstraintResult IsMetBy(TEquatable actual) | ||
| { | ||
| Actual = actual; | ||
| Outcome = actual.Equals(expected) ? Outcome.Success : Outcome.Failure; | ||
| return this; | ||
| } | ||
|
|
||
| protected override void AppendNormalExpectation(StringBuilder stringBuilder, string? indentation = null) | ||
| { | ||
| stringBuilder.Append("is equatable to "); | ||
| Formatter.Format(stringBuilder, expected); | ||
| } | ||
|
|
||
| protected override void AppendNormalResult(StringBuilder stringBuilder, string? indentation = null) | ||
| { | ||
| stringBuilder.Append(it).Append(" was "); | ||
| Formatter.Format(stringBuilder, Actual); | ||
| } | ||
|
|
||
| protected override void AppendNegatedExpectation(StringBuilder stringBuilder, string? indentation = null) | ||
| { | ||
| stringBuilder.Append("is not equatable to "); | ||
| Formatter.Format(stringBuilder, expected); | ||
| } | ||
|
|
||
| protected override void AppendNegatedResult(StringBuilder stringBuilder, string? indentation = null) | ||
| { | ||
| stringBuilder.Append(it).Append(" was in "); | ||
| Formatter.Format(stringBuilder, Actual); | ||
| } | ||
| } | ||
| } | ||
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
150 changes: 150 additions & 0 deletions
150
Tests/aweXpect.Tests/ThatGeneric.IsEquatableTo.Tests.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,150 @@ | ||
| namespace aweXpect.Tests; | ||
|
|
||
| public sealed partial class ThatGeneric | ||
| { | ||
| public sealed class IsEquatableTo | ||
| { | ||
| public sealed class Tests | ||
| { | ||
| [Fact] | ||
| public async Task WhenComparingToMatchingLong_ShouldSucceed() | ||
| { | ||
| Wrapper subject = new(1); | ||
|
|
||
| async Task Act() | ||
| => await That(subject).IsEquatableTo(1L); | ||
|
|
||
| await That(Act).DoesNotThrow(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task WhenComparingToMatchingWrapper_ShouldSucceed() | ||
| { | ||
| Wrapper subject = new(1); | ||
| Wrapper expected = new(1); | ||
|
|
||
| async Task Act() | ||
| => await That(subject).IsEquatableTo(expected); | ||
|
|
||
| await That(Act).DoesNotThrow(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task WhenComparingToNotMatchingLong_ShouldFail() | ||
| { | ||
| Wrapper subject = new(1); | ||
|
|
||
| async Task Act() | ||
| => await That(subject).IsEquatableTo(2L); | ||
|
|
||
| await That(Act).Throws<XunitException>() | ||
| .WithMessage(""" | ||
| Expected that subject | ||
| is equatable to 2, | ||
| but it was ThatGeneric.IsEquatableTo.Wrapper { | ||
| Value = 1 | ||
| } | ||
| """); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task WhenComparingToNotMatchingWrapper_ShouldFail() | ||
| { | ||
| Wrapper subject = new(1); | ||
| Wrapper expected = new(3); | ||
|
|
||
| async Task Act() | ||
| => await That(subject).IsEquatableTo(expected); | ||
|
|
||
| await That(Act).Throws<XunitException>() | ||
| .WithMessage(""" | ||
| Expected that subject | ||
| is equatable to ThatGeneric.IsEquatableTo.Wrapper { | ||
| Value = 3 | ||
| }, | ||
| but it was ThatGeneric.IsEquatableTo.Wrapper { | ||
| Value = 1 | ||
| } | ||
| """); | ||
| } | ||
| } | ||
|
|
||
| public sealed class NegatedTests | ||
| { | ||
| [Fact] | ||
| public async Task WhenComparingToMatchingLong_ShouldFail() | ||
| { | ||
| Wrapper subject = new(1); | ||
|
|
||
| async Task Act() | ||
| => await That(subject).DoesNotComplyWith(it => it.IsEquatableTo(1L)); | ||
|
|
||
| await That(Act).Throws<XunitException>() | ||
| .WithMessage(""" | ||
| Expected that subject | ||
| is not equatable to 1, | ||
| but it was in ThatGeneric.IsEquatableTo.Wrapper { | ||
| Value = 1 | ||
| } | ||
| """); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task WhenComparingToMatchingWrapper_ShouldFail() | ||
| { | ||
| Wrapper subject = new(1); | ||
| Wrapper expected = new(1); | ||
|
|
||
| async Task Act() | ||
| => await That(subject).DoesNotComplyWith(it => it.IsEquatableTo(expected)); | ||
|
|
||
| await That(Act).Throws<XunitException>() | ||
| .WithMessage(""" | ||
| Expected that subject | ||
| is not equatable to ThatGeneric.IsEquatableTo.Wrapper { | ||
| Value = 1 | ||
| }, | ||
| but it was in ThatGeneric.IsEquatableTo.Wrapper { | ||
| Value = 1 | ||
| } | ||
| """); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task WhenComparingToNotMatchingLong_ShouldSucceed() | ||
| { | ||
| Wrapper subject = new(1); | ||
|
|
||
| async Task Act() | ||
| => await That(subject).DoesNotComplyWith(it => it.IsEquatableTo(2L)); | ||
|
|
||
| await That(Act).DoesNotThrow(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task WhenComparingToNotMatchingWrapper_ShouldSucceed() | ||
| { | ||
| Wrapper subject = new(1); | ||
| Wrapper expected = new(3); | ||
|
|
||
| async Task Act() | ||
| => await That(subject).DoesNotComplyWith(it => it.IsEquatableTo(expected)); | ||
|
|
||
| await That(Act).DoesNotThrow(); | ||
| } | ||
| } | ||
|
|
||
| private readonly struct Wrapper(long value) | ||
| : IEquatable<Wrapper>, | ||
| IEquatable<long> | ||
| { | ||
| public long Value { get; } = value; | ||
|
|
||
| public bool Equals(Wrapper other) | ||
| => Value == other.Value; | ||
|
|
||
| public bool Equals(long other) | ||
| => Value == other; | ||
| } | ||
| } | ||
| } |
150 changes: 150 additions & 0 deletions
150
Tests/aweXpect.Tests/ThatGeneric.IsNotEquatableTo.Tests.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,150 @@ | ||
| namespace aweXpect.Tests; | ||
|
|
||
| public sealed partial class ThatGeneric | ||
| { | ||
| public sealed class IsNotEquatableTo | ||
| { | ||
| public sealed class Tests | ||
| { | ||
| [Fact] | ||
| public async Task WhenComparingToMatchingLong_ShouldFail() | ||
| { | ||
| Wrapper subject = new(1); | ||
|
|
||
| async Task Act() | ||
| => await That(subject).IsNotEquatableTo(1L); | ||
|
|
||
| await That(Act).Throws<XunitException>() | ||
| .WithMessage(""" | ||
| Expected that subject | ||
| is not equatable to 1, | ||
| but it was in ThatGeneric.IsNotEquatableTo.Wrapper { | ||
| Value = 1 | ||
| } | ||
| """); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task WhenComparingToMatchingWrapper_ShouldFail() | ||
| { | ||
| Wrapper subject = new(1); | ||
| Wrapper expected = new(1); | ||
|
|
||
| async Task Act() | ||
| => await That(subject).IsNotEquatableTo(expected); | ||
|
|
||
| await That(Act).Throws<XunitException>() | ||
| .WithMessage(""" | ||
| Expected that subject | ||
| is not equatable to ThatGeneric.IsNotEquatableTo.Wrapper { | ||
| Value = 1 | ||
| }, | ||
| but it was in ThatGeneric.IsNotEquatableTo.Wrapper { | ||
| Value = 1 | ||
| } | ||
| """); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task WhenComparingToNotMatchingLong_ShouldSucceed() | ||
| { | ||
| Wrapper subject = new(1); | ||
|
|
||
| async Task Act() | ||
| => await That(subject).IsNotEquatableTo(2L); | ||
|
|
||
| await That(Act).DoesNotThrow(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task WhenComparingToNotMatchingWrapper_ShouldSucceed() | ||
| { | ||
| Wrapper subject = new(1); | ||
| Wrapper expected = new(3); | ||
|
|
||
| async Task Act() | ||
| => await That(subject).IsNotEquatableTo(expected); | ||
|
|
||
| await That(Act).DoesNotThrow(); | ||
| } | ||
| } | ||
|
|
||
| public sealed class NegatedTests | ||
| { | ||
| [Fact] | ||
| public async Task WhenComparingToMatchingLong_ShouldSucceed() | ||
| { | ||
| Wrapper subject = new(1); | ||
|
|
||
| async Task Act() | ||
| => await That(subject).DoesNotComplyWith(it => it.IsNotEquatableTo(1L)); | ||
|
|
||
| await That(Act).DoesNotThrow(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task WhenComparingToMatchingWrapper_ShouldSucceed() | ||
| { | ||
| Wrapper subject = new(1); | ||
| Wrapper expected = new(1); | ||
|
|
||
| async Task Act() | ||
| => await That(subject).DoesNotComplyWith(it => it.IsNotEquatableTo(expected)); | ||
|
|
||
| await That(Act).DoesNotThrow(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task WhenComparingToNotMatchingLong_ShouldFail() | ||
| { | ||
| Wrapper subject = new(1); | ||
|
|
||
| async Task Act() | ||
| => await That(subject).DoesNotComplyWith(it => it.IsNotEquatableTo(2L)); | ||
|
|
||
| await That(Act).Throws<XunitException>() | ||
| .WithMessage(""" | ||
| Expected that subject | ||
| is equatable to 2, | ||
| but it was ThatGeneric.IsNotEquatableTo.Wrapper { | ||
| Value = 1 | ||
| } | ||
| """); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task WhenComparingToNotMatchingWrapper_ShouldFail() | ||
| { | ||
| Wrapper subject = new(1); | ||
| Wrapper expected = new(3); | ||
|
|
||
| async Task Act() | ||
| => await That(subject).DoesNotComplyWith(it => it.IsNotEquatableTo(expected)); | ||
|
|
||
| await That(Act).Throws<XunitException>() | ||
| .WithMessage(""" | ||
| Expected that subject | ||
| is equatable to ThatGeneric.IsNotEquatableTo.Wrapper { | ||
| Value = 3 | ||
| }, | ||
| but it was ThatGeneric.IsNotEquatableTo.Wrapper { | ||
| Value = 1 | ||
| } | ||
| """); | ||
| } | ||
| } | ||
|
|
||
| private readonly struct Wrapper(long value) | ||
| : IEquatable<Wrapper>, | ||
| IEquatable<long> | ||
| { | ||
| public long Value { get; } = value; | ||
|
|
||
| public bool Equals(Wrapper other) | ||
| => Value == other.Value; | ||
|
|
||
| public bool Equals(long other) | ||
| => Value == other; | ||
| } | ||
| } | ||
| } |
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.