-
Notifications
You must be signed in to change notification settings - Fork 749
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
f30f750
commit bec8793
Showing
4 changed files
with
168 additions
and
0 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
TechTalk.SpecFlow/Assist/ValueRetrievers/NullValueRetriever.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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<>)); | ||
} | ||
} | ||
} |
105 changes: 105 additions & 0 deletions
105
...TechTalk.SpecFlow.RuntimeTests/AssistTests/ValueRetrieverTests/NullValueRetrieverTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
...echTalk.SpecFlow.RuntimeTests/AssistTests/ValueRetrieverTests/ValueRetrieverTestsUtils.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters