Skip to content
Open
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
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
namespace EnumMemberAsString;

public class Person
public class Person(string name, Country country)
{
public string Name { get; set; }
public Country Country { get; set; }

public Person(string name, Country country)
{
Name = name;
Country = country;
}
public string Name { get; set; } = name;
public Country Country { get; set; } = country;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace EnumMemberAsString;

public class Trip
{
public string Name { get; set; } = string.Empty;
public Region Region { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.ComponentModel;
using System.Runtime.Serialization;
using System.Text.Json.Serialization;

namespace EnumMemberAsString;

[JsonConverter(typeof(JsonStringEnumConverter))]
public enum Region
{
[Description("North America")]
[EnumMember(Value = "north-america")]
NorthAmerica,

[JsonStringEnumMemberName("asia-pacific")]
AsiaPacific,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System.ComponentModel;
using System.Reflection;
using System.Text.Json;

public class CustomStringUnitTests
{
[Fact]
public void WhenMemberHasEnumMemberAttribute_ThenSystemTextJsonIgnoresIt()
{
var json = JsonSerializer.Serialize(new Trip { Name = "a", Region = Region.NorthAmerica });

Assert.Equal("{\"Name\":\"a\",\"Region\":\"NorthAmerica\"}", json);
}

[Fact]
public void WhenMemberHasJsonStringEnumMemberName_ThenSystemTextJsonUsesIt()
{
var json = JsonSerializer.Serialize(new Trip { Name = "b", Region = Region.AsiaPacific });

Assert.Equal("{\"Name\":\"b\",\"Region\":\"asia-pacific\"}", json);
}

[Fact]
public void WhenMemberHasDescription_ThenReflectionReturnsIt()
{
var description = typeof(Region)
.GetField(nameof(Region.NorthAmerica))?
.GetCustomAttribute<DescriptionAttribute>()?
.Description;

Assert.Equal("North America", description);
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.10.1" />
<PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="4.0.1">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.1.2">
<PackageReference Include="coverlet.collector" Version="10.1.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,25 @@ public class EnumUnitTests
[Fact]
public void WhenUsingGetName_ThenReturnsString()
{
var usa = Enum.GetName(typeof(Country), Country.USA);
var india = Enum.GetName(typeof(Country), Country.India);
var australia = Enum.GetName(typeof(Country), Country.Australia);
var usa = Enum.GetName<Country>(Country.USA);
var india = Enum.GetName<Country>(Country.India);
var australia = Enum.GetName<Country>(Country.Australia);

Assert.Equal("USA", usa);
Assert.Equal("India", india);
Assert.Equal("Australia", australia);
}

[Fact]
public void WhenEnumTypeIsOnlyKnownAtRuntime_ThenGetNameStillReturnsString()
{
Type enumType = typeof(Country);

var usa = Enum.GetName(enumType, 0);

Assert.Equal("USA", usa);
}

[Fact]
public void WhenUsingToString_ThenReturnsString()
{
Expand All @@ -35,4 +45,26 @@ public void WhenUsingNameof_ThenReturnsString()
Assert.Equal("India", india);
Assert.Equal("Australia", australia);
}
}

[Fact]
public void WhenUsingFormatSpecifiers_ThenReturnsExpectedText()
{
var country = Country.Australia;

Assert.Equal("Australia", country.ToString("G"));
Assert.Equal("2", country.ToString("D"));
Assert.Equal("00000002", country.ToString("X"));
Assert.Equal("Australia", country.ToString("F"));
Assert.Equal("0", Country.USA.ToString("D"));
Assert.Equal("00000000", Country.USA.ToString("X"));
}

[Fact]
public void WhenValueIsNotDefined_ThenGetNameIsNullAndToStringIsTheNumber()
{
var undefined = (Country)20;

Assert.Null(Enum.GetName<Country>(undefined));
Assert.Equal("20", undefined.ToString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ public class InstanceUnitTests
[Fact]
public void WhenUsingGetName_ThenReturnsString()
{
var usa = Enum.GetName(typeof(Country), bob.Country);
var india = Enum.GetName(typeof(Country), alice.Country);
var australia = Enum.GetName(typeof(Country), john.Country);
var usa = Enum.GetName<Country>(bob.Country);
var india = Enum.GetName<Country>(alice.Country);
var australia = Enum.GetName<Country>(john.Country);

Assert.Equal("USA", usa);
Assert.Equal("India", india);
Expand Down
Loading