Skip to content

Commit

Permalink
Fix enum retriever for nullable enums and empty value (#1941)
Browse files Browse the repository at this point in the history
* Added tests

* fix

* changelog

* update changelog

Co-authored-by: Andreas Willich <[email protected]>
  • Loading branch information
304NotModified and SabotageAndi committed May 7, 2020
1 parent 16580dd commit 2602a90
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 4 deletions.
12 changes: 11 additions & 1 deletion TechTalk.SpecFlow/Assist/ValueRetrievers/EnumValueRetriever.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ public class EnumValueRetriever : IValueRetriever
{
public object GetValue(string value, Type enumType)
{
if (string.IsNullOrEmpty(value) && IsNullable(enumType))
{
return null;
}

CheckThatTheValueIsAnEnum(value, enumType);

return ConvertTheStringToAnEnum(value, enumType);
Expand All @@ -20,11 +25,16 @@ public object Retrieve(KeyValuePair<string, string> keyValuePair, Type targetTyp

public bool CanRetrieve(KeyValuePair<string, string> keyValuePair, Type targetType, Type propertyType)
{
if (propertyType.IsGenericType && propertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
if (IsNullable(propertyType))
return typeof(Enum).IsAssignableFrom(propertyType.GetGenericArguments()[0]);
return propertyType.IsEnum;
}

private static bool IsNullable(Type propertyType)
{
return propertyType.IsGenericType && propertyType.GetGenericTypeDefinition() == typeof(Nullable<>);
}

private object ConvertTheStringToAnEnum(string value, Type enumType)
{
return StepArgumentTypeConverter.ConvertToAnEnum(GetTheEnumType(enumType), PrepareValue(value));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using Xunit;
using System;
using Xunit;
using FluentAssertions;
using TechTalk.SpecFlow.Assist;

namespace TechTalk.SpecFlow.RuntimeTests.AssistTests.SituationalTests
{

public class NullableEnumTests
{
public enum TestEnum
Expand All @@ -28,5 +29,35 @@ public void The_value_should_be_set_if_it_is_in_the_table()
var test = table.CreateInstance<TestEntity>();
test.TestProperty.Should().Be(TestEnum.Value2);
}

[Fact]
public void The_value_should_be_NULL_if_it_is_not_filled_in_the_table()
{
var table = new Table("Field", "Value");
table.AddRow("TestProperty", "");

var test = table.CreateInstance<TestEntity>();
test.TestProperty.Should().BeNull();
}

[Fact]
public void The_value_should_be_NULL_if_it_is_not_in_the_table()
{
var table = new Table("Field", "Value");

var test = table.CreateInstance<TestEntity>();
test.TestProperty.Should().BeNull();
}

[Fact]
public void There_should_be_an_error_if_in_the_table_is_no_valid_Enum_value()
{
var table = new Table("Field", "Value");
table.AddRow("TestProperty", "NotAnEnumValue");

Action x = () => { table.CreateInstance<TestEntity>(); };

x.Should().Throw<InvalidOperationException>();
}
}
}
}
4 changes: 4 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ Changes:
+ Performance and memory improvements for `table.CreateSet` and `table.CreateInstance`
+ Changed all specflow.org hyperlinks to https (docs, generated code, packages)

Fixes:
+ Empty value for nullable enum should not throw an exception


Fixes:
+ add auto-generated comment for StyleCop to ignore the Assemblyhook files
Expand All @@ -26,6 +29,7 @@ Fixes:
+ Revert "Replace NUnit.Framework.DescriptionAttribute to TestName in NUnit generator #1225" because of problems with the NUnit Test Adapter
+ Remove files with invalid characters from the FeatureFiles list (provided by msbuild)


Features:
+ Created VerifyAllColumnsBound check for `table.CreateSet` and `table.CreateInstance`

Expand Down

0 comments on commit 2602a90

Please sign in to comment.