Skip to content

Commit

Permalink
add in #1928 removed NullValueRetriever back (#2067)
Browse files Browse the repository at this point in the history
  • Loading branch information
SabotageAndi committed Jul 24, 2020
1 parent f30f750 commit bec8793
Show file tree
Hide file tree
Showing 4 changed files with 168 additions and 0 deletions.
33 changes: 33 additions & 0 deletions TechTalk.SpecFlow/Assist/ValueRetrievers/NullValueRetriever.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;
using System.Collections.Generic;

namespace TechTalk.SpecFlow.Assist.ValueRetrievers
{
public class NullValueRetriever : IValueRetriever
{
private readonly string _nullText;

public NullValueRetriever(string nullText)
{
_nullText = nullText;
}

public bool CanRetrieve(KeyValuePair<string, string> keyValuePair, Type targetType, Type propertyType)
{
return IsNullableType(propertyType) &&
keyValuePair.Value != null &&
string.Compare(keyValuePair.Value.Trim(), _nullText, StringComparison.InvariantCultureIgnoreCase) == 0;
}

public object Retrieve(KeyValuePair<string, string> keyValuePair, Type targetType, Type propertyType)
{
return null;
}

private static bool IsNullableType(Type type)
{
return !type.IsValueType ||
(type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
using System;
using FluentAssertions;
using TechTalk.SpecFlow.Assist.ValueRetrievers;
using Xunit;

namespace TechTalk.SpecFlow.RuntimeTests.AssistTests.ValueRetrieverTests
{
public class NullValueRetrieverTests
{
private const string TestNullValue = "MyNullValue";

[Theory]
[InlineData("MyNullValue")]
[InlineData(" \tMyNullValue\t ")]
[InlineData("MyNuLLValue")]
[InlineData("\tMyNuLLValue\t")]
public void Can_retrieve_null_value(string value)
{
var retriever = CreateTestee();

var actual = retriever.CanRetrieve<object>(value);

actual.Should().BeTrue();
}

[Theory]
[InlineData(typeof(object))]
[InlineData(typeof(bool?))]
public void Can_retrieve_nullable_types(Type propertyType)
{
var retriever = CreateTestee();

var actual = retriever.CanRetrieve(TestNullValue, propertyType);

actual.Should().BeTrue();
}

[Theory]
[InlineData(null)]
[InlineData("")]
[InlineData("Any string")]
[InlineData("30.0")]
[InlineData("true")]
[InlineData(" \t ")]
public void Cannot_retrieve_non_null_values(string value)
{
var retriever = CreateTestee();

var actual = retriever.CanRetrieve<object>(value);

actual.Should().BeFalse();
}

[Theory]
[InlineData(typeof(bool))]
[InlineData(typeof(Guid))]
[InlineData(typeof(StringSplitOptions))]
public void Cannot_retrieve_non_nullable_types(Type propertyType)
{
var retriever = CreateTestee();

var actual = retriever.CanRetrieve(TestNullValue, propertyType);

actual.Should().BeFalse();
}

[Theory]
[InlineData(null)]
[InlineData("")]
[InlineData("Any string")]
[InlineData("30.0")]
[InlineData("true")]
[InlineData(" \t ")]
[InlineData(" \tMyNullValue\t ")]
[InlineData("MyNuLLValue")]
public void Retrieves_null_for_any_value(string value)
{
var retriever = CreateTestee();

var actual = retriever.GetValue<object>(value);

actual.Should().BeNull();
}

[Theory]
[InlineData(typeof(object))]
[InlineData(typeof(bool?))]
[InlineData(typeof(bool))]
[InlineData(typeof(Guid))]
[InlineData(typeof(StringSplitOptions))]
public void Retrieves_null_for_any_type(Type propertyType)
{
var retriever = CreateTestee();

var actual = retriever.GetValue("Any value", propertyType);

actual.Should().BeNull();
}

private NullValueRetriever CreateTestee()
{
return new NullValueRetriever(TestNullValue);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using System.Collections.Generic;
using TechTalk.SpecFlow.Assist;

namespace TechTalk.SpecFlow.RuntimeTests.AssistTests.ValueRetrieverTests
{
internal static class ValueRetrieverTestsUtils
{
internal static bool CanRetrieve<TProperty>(this IValueRetriever retriever, string value)
{
return retriever.CanRetrieve(value, typeof(TProperty));
}

internal static bool CanRetrieve(this IValueRetriever retriever, string value, Type propertyType)
{
return retriever.CanRetrieve(new KeyValuePair<string, string>("Value", value), typeof(object), propertyType);
}

internal static TProperty GetValue<TProperty>(this IValueRetriever retriever, string value)
{
return (TProperty)retriever.GetValue(value, typeof(TProperty));
}

internal static object GetValue(this IValueRetriever retriever, string value, Type propertyType)
{
return retriever.Retrieve(new KeyValuePair<string, string>("Value", value), typeof(object), propertyType);
}
}
}
1 change: 1 addition & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Fixes:
+ Fix step definition attribute support for the Visual Studio Extension #2065

Changes:
+ Add NullValueRetriever back again (was accidential removed with https://github.com/SpecFlowOSS/SpecFlow/pull/1928) - https://github.com/SpecFlowOSS/SpecFlow/pull/2067
+ Add a new `TestRunEnd` extension point for plugins

3.3.57
Expand Down

0 comments on commit bec8793

Please sign in to comment.