Skip to content

Commit

Permalink
Fix issue when an empty ArraySegment is a member of a class (#2511)
Browse files Browse the repository at this point in the history
* Fix issue when an empty ArraySegmnet is a member of a class.

With latest, it crashes when trying to access the array members.

* Fix PR comments.

* Fix Qodana complaints

* Update releases.md

* Address review comments

---------

Co-authored-by: Shahar Prish <shaharp@microsoft.com>
  • Loading branch information
ITaluone and ShaharPrishMSFT committed Dec 15, 2023
1 parent d4261b6 commit af4fce2
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,17 @@ internal static object[] ToArray(object value)
{
return ((IEnumerable)value).Cast<object>().ToArray();
}
catch (InvalidOperationException) when (value.GetType().Name.Equals("ImmutableArray`1", StringComparison.Ordinal))
catch (InvalidOperationException) when (IsIgnorableArrayLikeType(value))
{
// This is probably a default ImmutableArray<T>
// This is probably a default ImmutableArray<T> or an empty ArraySegment.
return Array.Empty<object>();
}
}

private static bool IsIgnorableArrayLikeType(object value)
{
var type = value.GetType();
return type.Name.Equals("ImmutableArray`1", StringComparison.Ordinal) ||
(type.IsGenericType && type.GetGenericTypeDefinition() == typeof(ArraySegment<>));
}
}
16 changes: 16 additions & 0 deletions Tests/FluentAssertions.Equivalency.Specs/BasicSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -645,4 +645,20 @@ public void When_the_graph_contains_guids_it_should_properly_format_them()
// Assert
act.Should().Throw<XunitException>().WithMessage("Expected property actual[0].Id*to be *-*, but found *-*");
}

[Fact]
public void Empty_array_segments_can_be_compared_for_equivalency()
{
// Arrange
var actual = new ClassWithArraySegment();
var expected = new ClassWithArraySegment();

// Act / Assert
actual.Should().BeEquivalentTo(expected);
}

private class ClassWithArraySegment
{
public ArraySegment<byte> Segment { get; set; }
}
}
2 changes: 1 addition & 1 deletion docs/_pages/releases.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ sidebar:
* Improved the failure message for `[Not]HaveExplicitProperty` when wrapped in an `AssertionScope` and not implementing the interface - [#2403](https://github.com/fluentassertions/fluentassertions/pull/2403)
* Improved the failure message for `[Not]HaveExplicitMethod` when wrapped in an `AssertionScope` and not implementing the interface - [#2403](https://github.com/fluentassertions/fluentassertions/pull/2403)
* Changed `BeEquivalentTo` to exclude `private protected` members from the comparison - [#2417](https://github.com/fluentassertions/fluentassertions/pull/2417)

* Fixed using `BeEquivalentTo` on an empty `ArraySegment` - [#2445](https://github.com/fluentassertions/fluentassertions/pull/2445), [#2511](https://github.com/fluentassertions/fluentassertions/pull/2511)

### Breaking Changes (for users)
* Moved support for `DataSet`, `DataTable`, `DataRow` and `DataColumn` into a new package `FluentAssertions.DataSet` - [#2267](https://github.com/fluentassertions/fluentassertions/pull/2267)
Expand Down
1 change: 1 addition & 0 deletions qodana.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ exclude:
- name: ConvertIfStatementToConditionalTernaryExpression
- name: InvertIf
- name: SimilarAnonymousTypeNearby
- name: UnusedMember.Local
paths:
- Tests
- name: All
Expand Down

0 comments on commit af4fce2

Please sign in to comment.