Skip to content
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

Fix auto conversion to enums #2261

Merged
merged 1 commit into from
Aug 16, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions Src/FluentAssertions/Equivalency/Steps/AutoConversionStep.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ private static bool TryChangeType(object subject, Type expectationType, out obje
{
if (expectationType.IsEnum)
{
if (Enum.IsDefined(expectationType, subject))
if (subject is sbyte or byte or short or ushort or int or uint or long or ulong)
{
conversionResult = Enum.ToObject(expectationType, subject);
return true;
return Enum.IsDefined(expectationType, conversionResult);
}

return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,36 @@ public void Numbers_can_be_converted_to_enums()
{
// Arrange
var expectation = new { Property = EnumFour.Three };
var subject = new { Property = 3 };
var subject = new { Property = 3UL };

// Act / Assert
subject.Should().BeEquivalentTo(expectation, options => options.WithAutoConversion());
}

[Fact]
public void Enums_are_not_converted_to_enums_of_different_type()
{
// Arrange
var expectation = new { Property = EnumTwo.Two };
var subject = new { Property = EnumThree.Two };

// Act / Assert
subject.Should().BeEquivalentTo(expectation, options => options.WithAutoConversion());
}

[Fact]
public void Strings_are_not_converted_to_enums()
{
// Arrange
var expectation = new { Property = EnumTwo.Two };
var subject = new { Property = "Two" };

// Act / Assert
var act = () => subject.Should().BeEquivalentTo(expectation, options => options.WithAutoConversion());

act.Should().Throw<XunitException>();
}

[Fact]
public void Numbers_that_are_out_of_range_cannot_be_converted_to_enums()
{
Expand Down
2 changes: 2 additions & 0 deletions docs/_pages/releases.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ sidebar:
### Fixes
* `because` and `becauseArgs` were not included in the error message when collections of enums were not equivalent - [#2214](https://github.com/fluentassertions/fluentassertions/pull/2214)
* Improve caller identification for tests written in Visual Basic - [#2254](https://github.com/fluentassertions/fluentassertions/pull/2254)
* Improved auto conversion to enums for objects of different integral type - [#2261](https://github.com/fluentassertions/fluentassertions/pull/2261)
* Fixed exceptions when trying to auto convert strings or enums of different type to enums- [#2261](https://github.com/fluentassertions/fluentassertions/pull/2261)

## 6.11.0

Expand Down