Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion Source/aweXpect.Core/Core/Nodes/AndNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,20 @@ public override void AppendExpectation(StringBuilder stringBuilder, string? inde
private bool Equals(AndNode other) => Current.Equals(other.Current) && _nodes.SequenceEqual(other._nodes);

/// <inheritdoc cref="object.GetHashCode()" />
public override int GetHashCode() => Current.GetHashCode() ^ _nodes.GetHashCode();
public override int GetHashCode()
{
unchecked
{
// ReSharper disable once NonReadonlyMemberInGetHashCode
int hash = 19 * Current.GetHashCode();
foreach (Node node in _nodes.Select(x => x.Item2))
{
hash = (hash * 31) + node.GetHashCode();
}

return hash;
}
}

private static ConstraintResult CombineResults(
ConstraintResult? combinedResult,
Expand Down
6 changes: 5 additions & 1 deletion Source/aweXpect.Core/Core/Nodes/ExpectationNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,5 +155,9 @@ private bool Equals(ExpectationNode other)
}

/// <inheritdoc cref="object.GetHashCode()" />
public override int GetHashCode() => GetType().GetHashCode();
// ReSharper disable NonReadonlyMemberInGetHashCode
public override int GetHashCode()
=> _constraint?.GetType().GetHashCode() ?? 17
+ _inner?.GetHashCode() ?? 0;
// ReSharper restore NonReadonlyMemberInGetHashCode
}
21 changes: 17 additions & 4 deletions Source/aweXpect.Core/Core/Nodes/OrNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,20 @@ public override void AppendExpectation(StringBuilder stringBuilder, string? inde
private bool Equals(OrNode other) => Current.Equals(other.Current) && _nodes.SequenceEqual(other._nodes);

/// <inheritdoc cref="object.GetHashCode()" />
public override int GetHashCode() => Current.GetHashCode() ^ _nodes.GetHashCode();
public override int GetHashCode()
{
unchecked
{
// ReSharper disable once NonReadonlyMemberInGetHashCode
int hash = 19 * Current.GetHashCode();
foreach (Node node in _nodes.Select(x => x.Item2))
{
hash = (hash * 31) + node.GetHashCode();
}

return hash;
}
}

private static ConstraintResult CombineResults(
ConstraintResult? combinedResult,
Expand Down Expand Up @@ -143,9 +156,9 @@ private static Outcome Or(Outcome left, Outcome right)
=> (left, right) switch
{
(Outcome.Failure, Outcome.Failure) => Outcome.Failure,
(_, Outcome.Undecided) => Outcome.Undecided,
(Outcome.Undecided, _) => Outcome.Undecided,
(_, _) => Outcome.Success,
(_, Outcome.Success) => Outcome.Success,
(Outcome.Success, _) => Outcome.Success,
(_, _) => Outcome.Undecided,
Comment thread
vbreuss marked this conversation as resolved.
};

public override void AppendExpectation(StringBuilder stringBuilder, string? indentation = null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ public async Task FromType_WhenNameDoesNotExist_ShouldReturnNull()
await That(exception).IsNull();
}

[Fact]
public async Task Inconclusive_MissingAssemblyName_ShouldThrowNotSupportedException()
{
MyTestFrameworkAdapter adapter = new(MissingAssembly, skipException: new MyException());
_ = adapter.IsAvailable;

await That(() => adapter.Inconclusive("foo")).Throws<NotSupportedException>()
.WithMessage("Failed to create the inconclusive assertion type");
}

[Fact]
public async Task Inconclusive_ValidAssemblyName_ShouldThrowNotSupportedException()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,23 @@ public async Task AppendResult_SpecificException_ShouldAppendExpectedValue()
await That(sb.ToString()).IsEqualTo("it did throw an ArgumentException");
}

[Theory]
[InlineData(Outcome.Failure, Outcome.Success)]
[InlineData(Outcome.Success, Outcome.Failure)]
[InlineData(Outcome.Undecided, Outcome.Undecided)]
public async Task Negate_ShouldNegateInnerOutcome(Outcome innerOutcome, Outcome expectedAfterNegation)
{
DummyConstraintResult inner = new(innerOutcome, "foo");
Exception exception = new("bar");
DummyExpectationBuilder expectationBuilder = new();
MyFromExceptionConstraintResult sut = new(inner, exception, expectationBuilder);

sut.Negate();

await That(sut.Outcome).IsEqualTo(Outcome.Failure);
await That(inner.Outcome).IsEqualTo(expectedAfterNegation);
}

[Fact]
public async Task Outcome_ShouldBeFailure()
{
Expand All @@ -61,7 +78,6 @@ public async Task Outcome_ShouldBeFailure()
await That(sut.Outcome).IsEqualTo(Outcome.Failure);
}


[Fact]
public async Task SetOutcome_ShouldBeForwardedToInner()
{
Expand Down
22 changes: 22 additions & 0 deletions Tests/aweXpect.Core.Tests/Core/ManualExpectationBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,16 @@ public async Task Equals_FirstNull_ShouldBeFalse()
await That(result).IsFalse();
}

[Fact]
public async Task Equals_ObjectNull_ShouldBeFalse()
{
ManualExpectationBuilder<int> sut = new(null);

bool result = sut.Equals(null);

await That(result).IsFalse();
}

[Fact]
public async Task Equals_SecondNull_ShouldBeFalse()
{
Expand Down Expand Up @@ -136,6 +146,18 @@ public async Task GetHashCode_SameConstraint_ShouldBeEqual()
await That(sut1.GetHashCode()).IsEqualTo(sut2.GetHashCode());
}

[Fact]
public async Task GetHashCode_WithParameter_ShouldUseHashCodeFromParameter()
{
ManualExpectationBuilder<int> sut1 = new(null);
sut1.AddConstraint((_, _, _) => new DummyConstraint("foo"));
ManualExpectationBuilder<int> sut2 = new(null);
sut2.ForWhich<int, int>(x => x)
.AddConstraint((_, _, _) => new DummyConstraint("foo"));

await That(sut1.GetHashCode()).IsEqualTo(sut2.GetHashCode(sut1));
}

[Fact]
public async Task IsMet_ShouldThrowNotSupportedException()
{
Expand Down
Loading
Loading