Skip to content

Commit

Permalink
Enum conversion tests
Browse files Browse the repository at this point in the history
  • Loading branch information
occluder committed Jun 12, 2024
1 parent c4beae3 commit d88b00b
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
41 changes: 40 additions & 1 deletion MiniTwitch.Helix.Test/Converters/EnumConverterTest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,44 @@
namespace MiniTwitch.Helix.Test.Converters;
using MiniTwitch.Helix.Internal.Json;

namespace MiniTwitch.Helix.Test.Converters;

public class EnumConverterTest
{
[Fact]
public void Convert()
{
var e = EnumConverter<TestEnum>.ReadEnum("Forsaan");
Assert.Equal(TestEnum.Forsaan, e);
}

[Fact]
public void Convert_From_SnakeCase()
{
var e = EnumConverter<TestEnum>.ReadEnum("i_eat_rocks");
Assert.Equal(TestEnum.IEatRocks, e);
}

[Fact]
public void Convert_From_Null()
{
var e = EnumConverter<TestEnum>.ReadEnum(null);
Assert.Equal(default(TestEnum), e);
}

[Fact]
public void Convert_From_Undefined()
{
var e = EnumConverter<TestEnum>.ReadEnum("my life is doge");
Assert.Equal(default(TestEnum), e);
}

enum TestEnum
{
Unknown,
Forshon,
Forsen,
Forsaan,
IEatRocks,
XD
}
}
2 changes: 1 addition & 1 deletion MiniTwitch.Helix/Internal/Json/Converters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ internal static TEnum ReadEnum(string? enumString)
return (TEnum)enumMember!;
}

throw new JsonException();
return default!;
}

public override void Write(Utf8JsonWriter writer, TEnum value, JsonSerializerOptions options)
Expand Down

0 comments on commit d88b00b

Please sign in to comment.