mirror of
https://github.com/zoriya/octokit.net.git
synced 2026-06-07 12:26:18 +00:00
Deserializer should handle nullable StringEnum<T> (#1760)
* add deserializer tests for nullable enums, StringEnum and nullable StringEnum properties * Fix deserializing nullable structs by using the underlying type when nullable * StringEnum<T> should behave like an enum, returning default(T) when it is uninitialised/null/blank * Don't allow null to be passed into StringEnum ctor - if it needs to be null then it should be declared as nullable * fix expected json * move logic to determine if property is a StringEnum<T> into helper function * serializer needs to treat StringEnum<T> specially by serializing the enum value according to existing serializer strategy (parameter attributes and so on) * remove fallback to default(T) * add test to assert ctor throws exception when null passed in * remove test for default(T) fallback behaviour * Fix exception in serializer test - StringEnum property must be initialized otherwise an exception is thrown when attempting to serialize * Dont allow empty strings either
This commit is contained in:
@@ -7,6 +7,14 @@ namespace Octokit.Tests.Models
|
||||
{
|
||||
public class TheCtor
|
||||
{
|
||||
[Fact]
|
||||
public void ShouldThrowForNullOrEmptyStringValues()
|
||||
{
|
||||
Assert.Throws<ArgumentNullException>(() => new StringEnum<AccountType>(null));
|
||||
|
||||
Assert.Throws<ArgumentException>(() => new StringEnum<AccountType>(""));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ShouldSetValue()
|
||||
{
|
||||
@@ -42,16 +50,12 @@ namespace Octokit.Tests.Models
|
||||
}
|
||||
}
|
||||
|
||||
public class TheParsedValueProperty
|
||||
public class TheValueProperty
|
||||
{
|
||||
[Theory]
|
||||
[InlineData("")]
|
||||
[InlineData(null)]
|
||||
[InlineData("Cow")]
|
||||
public void ShouldThrowForInvalidValue(string value)
|
||||
[Fact]
|
||||
public void ShouldThrowForInvalidValue()
|
||||
{
|
||||
var stringEnum = new StringEnum<AccountType>(value);
|
||||
|
||||
var stringEnum = new StringEnum<AccountType>("Cow");
|
||||
Assert.Throws<ArgumentException>(() => stringEnum.Value);
|
||||
}
|
||||
|
||||
@@ -96,13 +100,10 @@ namespace Octokit.Tests.Models
|
||||
Assert.True(result);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("")]
|
||||
[InlineData(null)]
|
||||
[InlineData("Cow")]
|
||||
public void ShouldReturnFalseForInvalidValue(string value)
|
||||
[Fact]
|
||||
public void ShouldReturnFalseForInvalidValue()
|
||||
{
|
||||
var stringEnum = new StringEnum<AccountType>(value);
|
||||
var stringEnum = new StringEnum<AccountType>("Cow");
|
||||
|
||||
AccountType type;
|
||||
var result = stringEnum.TryParse(out type);
|
||||
|
||||
Reference in New Issue
Block a user