Skip to content

Commit

Permalink
Update StrEnum to v2.0.0. Switch to parsing members by value only. (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
dmytro-khmara authored Jul 30, 2022
1 parent 4c392b0 commit 40fc723
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,6 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>

<PackageReference Include="StrEnum" Version="[1.5.0,2.0.0)" />
<PackageReference Include="StrEnum" Version="[2.0.0,3.0.0)" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class StringEnumValueConverter<TEnum> : ValueConverter<StringEnum<TEnum>,
where TEnum : StringEnum<TEnum>, new()
{
public StringEnumValueConverter(ConverterMappingHints? mappingHints = null)
: base(@enum => (string)@enum, value => StringEnum<TEnum>.Parse(value, false), mappingHints)
: base(@enum => (string)@enum, value => StringEnum<TEnum>.Parse(value, false, MatchBy.ValueOnly), mappingHints)
{
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.6.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
<PackageReference Include="StrEnum" Version="1.5.0" />
<PackageReference Include="StrEnum" Version="2.0.1" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using FluentAssertions;
using Xunit;

Expand All @@ -16,12 +17,12 @@ public class Country : StringEnum<Country>
new[]
{
new object?[] { "UKR", Country.Ukraine},
new object?[] { "SouthAfrica", Country.SouthAfrica}
new object?[] { "ZAF", Country.SouthAfrica}
};

[Theory]
[MemberData(nameof(FromStringTestCases))]
public void ConvertFromString_ShouldConvertToStringEnumByNameOrValue(string value, Country expectedMember)
public void ConvertFromString_ShouldConvertToStringEnumByValue(string value, Country expectedMember)
{
var converter = new StringEnumValueConverter<Country>();

Expand All @@ -30,6 +31,16 @@ public void ConvertFromString_ShouldConvertToStringEnumByNameOrValue(string valu
actualCountry.Should().Be(expectedMember);
}

[Fact]
public void ConvertFromString_GivenMemberName_ShouldThrowAnException()
{
var converter = new StringEnumValueConverter<Country>();

var convert = () => converter.ConvertFromProvider("SouthAfrica");

convert.Should().Throw<ArgumentException>();
}

[Fact]
public void ConvertToString_ShouldConvertMemberValueToString()
{
Expand Down

0 comments on commit 40fc723

Please sign in to comment.