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:
Ryan Gribble
2018-02-16 20:12:44 +10:00
committed by GitHub
parent b35b60f24c
commit 41b4059c11
5 changed files with 151 additions and 41 deletions
+15 -14
View File
@@ -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);