Skip to content

Commit

Permalink
Added support for nullable structs in type generator (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
MilleBo authored Oct 17, 2019
1 parent a8074c5 commit ae45d10
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
28 changes: 28 additions & 0 deletions src/Testura.Code.Tests/Generators/Common/TypeGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ public class TypeGeneratorTests
{
private enum MyCustomEnum { Stuff }

private struct MyStruct
{
}

private class MyClass
{
}

[TestCase(typeof(int), "int")]
[TestCase(typeof(double), "double")]
[TestCase(typeof(float), "float")]
Expand All @@ -25,6 +33,7 @@ private enum MyCustomEnum { Stuff }
[TestCase(typeof(char), "char")]
[TestCase(typeof(bool), "bool")]
[TestCase(typeof(MyCustomEnum), "MyCustomEnum")]
[TestCase(typeof(Guid), "Guid")]
public void Create_WhenCreatingPredefinedTypes_ShouldGenerateCorrectCode(Type type, string expected)
{
Assert.AreEqual(expected, TypeGenerator.Create(type).ToString());
Expand All @@ -43,6 +52,7 @@ public void Create_WhenCreatingPredefinedTypes_ShouldGenerateCorrectCode(Type ty
[TestCase(typeof(char?), "char?")]
[TestCase(typeof(bool?), "bool?")]
[TestCase(typeof(MyCustomEnum?), "MyCustomEnum?")]
[TestCase(typeof(Guid?), "Guid?")]
public void Create_WhenCreatingPredefinedTypesAsNullable_ShouldGenerateCorrectCode(Type type, string expected)
{
Assert.AreEqual(expected, TypeGenerator.Create(type).ToString());
Expand All @@ -60,6 +70,24 @@ public void Create_WhenCreatingWithNoPredfinedType_ShouldGenerateCode()
Assert.AreEqual("List", TypeGenerator.Create(typeof(List)).ToString());
}

[Test]
public void Create_WhenCreatingWithClass_ShouldGenerateCode()
{
Assert.AreEqual("MyClass", TypeGenerator.Create(typeof(MyClass)).ToString());
}

[Test]
public void Create_WhenCreatingWithStruct_ShouldGenerateCode()
{
Assert.AreEqual("MyStruct", TypeGenerator.Create(typeof(MyStruct)).ToString());
}

[Test]
public void Create_WhenCreatingWithNullableStruct_ShouldGenerateCode()
{
Assert.AreEqual("MyStruct?", TypeGenerator.Create(typeof(MyStruct?)).ToString());
}

[Test]
public void Create_WhenCreatingWithNoPredfinedGenericType_ShouldGenerateCode()
{
Expand Down
3 changes: 2 additions & 1 deletion src/Testura.Code/Generators/Common/TypeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ private static TypeSyntax CheckPredefinedTypes(Type type)
{
TypeSyntax typeSyntax = null;
var isNullable = false;

if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>))
{
type = type.GetGenericArguments()[0];
Expand Down Expand Up @@ -140,7 +141,7 @@ private static TypeSyntax CheckPredefinedTypes(Type type)
typeSyntax = IdentifierName("DateTimeOffset");
}

if (type.IsEnum)
if (type.IsEnum || (typeSyntax == null && type.IsValueType))
{
typeSyntax = IdentifierName(type.Name);
}
Expand Down

0 comments on commit ae45d10

Please sign in to comment.