From 985f518732bed8f65552a96d05fceba7af37ffcb Mon Sep 17 00:00:00 2001 From: Matthias Lischka Date: Tue, 28 Feb 2017 22:22:19 +0100 Subject: [PATCH 1/4] GetDiscreteEnum for when you want to parse enum not used in suitable class. Generic type constraint to point out the purpose of the GetEnum and GetDiscreteEnum method. --- .../Assist/RowExtensionMethods.cs | 20 ++++-- .../RowExtensionMethodTests_GetEnum.cs | 63 ++++++++++++++----- 2 files changed, 65 insertions(+), 18 deletions(-) diff --git a/TechTalk.SpecFlow/Assist/RowExtensionMethods.cs b/TechTalk.SpecFlow/Assist/RowExtensionMethods.cs index 828ded1fa..f6eaed5ba 100644 --- a/TechTalk.SpecFlow/Assist/RowExtensionMethods.cs +++ b/TechTalk.SpecFlow/Assist/RowExtensionMethods.cs @@ -70,9 +70,21 @@ public static char GetChar(this TableRow row, string id) return Convert.ToChar(row[id]); } - internal static Enum GetEnumFromSingleInstanceRow(this TableRow row) + public static T GetDiscreteEnum(this TableRow row, string id) where T : struct, IConvertible { - return GetTheEnumValue(row[1], row[0]); + var value = row[id].Replace(" ", string.Empty); + T @enum; + if (Enum.TryParse(value, true, out @enum)) + return @enum; + + throw new InvalidOperationException($"No enum with value {value} found in enum {typeof(T).Name}"); + } + + public static T GetDiscreteEnum(this TableRow row, string id, T defaultValue) where T : struct, IConvertible + { + var value = row[id].Replace(" ", string.Empty); + T @enum; + return Enum.TryParse(value, true, out @enum) ? @enum : defaultValue; } public static TEnum GetEnumValue(this TableRow row, string id) @@ -80,12 +92,12 @@ public static TEnum GetEnumValue(this TableRow row, string id) return (TEnum)Enum.Parse(typeof(TEnum), row[id]); } - public static Enum GetEnum(this TableRow row, string id) + public static Enum GetEnum(this TableRow row, string id) where T : class { return GetTheEnumValue(row[id], id); } - private static Enum GetTheEnumValue(string rowValue, string propertyName) + private static Enum GetTheEnumValue(string rowValue, string propertyName) where T : class { var value = rowValue.Replace(" ", string.Empty); diff --git a/Tests/TechTalk.SpecFlow.RuntimeTests/AssistTests/RowExtensionMethodTests_GetEnum.cs b/Tests/TechTalk.SpecFlow.RuntimeTests/AssistTests/RowExtensionMethodTests_GetEnum.cs index caafd8512..effa4d722 100644 --- a/Tests/TechTalk.SpecFlow.RuntimeTests/AssistTests/RowExtensionMethodTests_GetEnum.cs +++ b/Tests/TechTalk.SpecFlow.RuntimeTests/AssistTests/RowExtensionMethodTests_GetEnum.cs @@ -4,7 +4,6 @@ using FluentAssertions; using TechTalk.SpecFlow.Assist; using TechTalk.SpecFlow.RuntimeTests.AssistTests.ExampleEntities; -using RowExtensionMethods = TechTalk.SpecFlow.Assist.RowExtensionMethods; namespace TechTalk.SpecFlow.RuntimeTests.AssistTests { @@ -51,19 +50,55 @@ public void GetEnum_should_return_the_enum_value_from_the_row_even_when_i_mess_u public void GetEnum_throws_exception_when_the_value_is_not_defined_in_any_Enum_in_the_type() { var table = new Table("Sex"); - table.AddRow("NotDefinied"); - - var exceptionThrown = false; - try - { - RowExtensionMethods.GetEnum(table.Rows.First(), "Sex"); - } - catch (InvalidOperationException exception) - { - if (exception.Message == "No enum with value NotDefinied found in type Person") - exceptionThrown = true; - } - exceptionThrown.Should().BeTrue(); + table.AddRow("NotDefined"); + + Action getNotDefinedEnum = () => table.Rows.First().GetEnum("Sex"); + + getNotDefinedEnum.ShouldThrow().WithMessage("No enum with value NotDefined found in type Person"); + } + + [Test] + public void GetDiscreteEnum_should_return_enum_of_my_specified_type() + { + var table = new Table("Header Not Representing Property Of Any Class"); + table.AddRow("Red"); + + var discreteEnum = table.Rows[0].GetDiscreteEnum("Header Not Representing Property Of Any Class"); + + discreteEnum.Should().Be(Colors.Red); + } + + [Test] + public void GetDiscreteEnum_throws_exception_when_the_value_is_not_defined_in_enum() + { + var table = new Table("Header Not Representing Property Of Any Class"); + table.AddRow("NotDefined"); + + Action getDiscreteEnum = () => table.Rows[0].GetDiscreteEnum("Header Not Representing Property Of Any Class"); + + getDiscreteEnum.ShouldThrow().WithMessage("No enum with value NotDefined found in enum Colors"); + } + + [Test] + public void GetDiscreteEnum_should_return_not_default_enum_if_value_matched() + { + var table = new Table("Header Not Representing Property Of Any Class"); + table.AddRow("Red"); + + var discreteEnum = table.Rows[0].GetDiscreteEnum("Header Not Representing Property Of Any Class", Colors.Green); + + discreteEnum.Should().Be(Colors.Red); + } + + [Test] + public void GetDiscreteEnum_should_return_default_enum_if_value_is_not_defined_in_enum() + { + var table = new Table("Header Not Representing Property Of Any Class"); + table.AddRow("NotDefined"); + + var discreteEnum = table.Rows[0].GetDiscreteEnum("Header Not Representing Property Of Any Class", Colors.Green); + + discreteEnum.Should().Be(Colors.Green); } [Fact] From 2daa31189b82285aaa35af29059db77897839e2c Mon Sep 17 00:00:00 2001 From: Andreas Willich Date: Tue, 5 May 2020 15:11:12 +0200 Subject: [PATCH 2/4] switch to xunit --- .../AssistTests/RowExtensionMethodTests_GetEnum.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Tests/TechTalk.SpecFlow.RuntimeTests/AssistTests/RowExtensionMethodTests_GetEnum.cs b/Tests/TechTalk.SpecFlow.RuntimeTests/AssistTests/RowExtensionMethodTests_GetEnum.cs index effa4d722..77c0a8d0a 100644 --- a/Tests/TechTalk.SpecFlow.RuntimeTests/AssistTests/RowExtensionMethodTests_GetEnum.cs +++ b/Tests/TechTalk.SpecFlow.RuntimeTests/AssistTests/RowExtensionMethodTests_GetEnum.cs @@ -57,7 +57,7 @@ public void GetEnum_throws_exception_when_the_value_is_not_defined_in_any_Enum_i getNotDefinedEnum.ShouldThrow().WithMessage("No enum with value NotDefined found in type Person"); } - [Test] + [Fact] public void GetDiscreteEnum_should_return_enum_of_my_specified_type() { var table = new Table("Header Not Representing Property Of Any Class"); @@ -68,7 +68,7 @@ public void GetDiscreteEnum_should_return_enum_of_my_specified_type() discreteEnum.Should().Be(Colors.Red); } - [Test] + [Fact] public void GetDiscreteEnum_throws_exception_when_the_value_is_not_defined_in_enum() { var table = new Table("Header Not Representing Property Of Any Class"); @@ -79,7 +79,7 @@ public void GetDiscreteEnum_throws_exception_when_the_value_is_not_defined_in_en getDiscreteEnum.ShouldThrow().WithMessage("No enum with value NotDefined found in enum Colors"); } - [Test] + [Fact] public void GetDiscreteEnum_should_return_not_default_enum_if_value_matched() { var table = new Table("Header Not Representing Property Of Any Class"); @@ -90,7 +90,7 @@ public void GetDiscreteEnum_should_return_not_default_enum_if_value_matched() discreteEnum.Should().Be(Colors.Red); } - [Test] + [Fact] public void GetDiscreteEnum_should_return_default_enum_if_value_is_not_defined_in_enum() { var table = new Table("Header Not Representing Property Of Any Class"); @@ -152,4 +152,4 @@ public enum ColorsAgain { Red, Green, Blue} public enum ShirtSizes { Small, Medium, Large } } -} \ No newline at end of file +} From a5f799126f59259e4b99d6ef61ebaa2008bb206c Mon Sep 17 00:00:00 2001 From: Andreas Willich Date: Tue, 5 May 2020 15:40:13 +0200 Subject: [PATCH 3/4] adjust to new FluentAssertions --- .../AssistTests/RowExtensionMethodTests_GetEnum.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Tests/TechTalk.SpecFlow.RuntimeTests/AssistTests/RowExtensionMethodTests_GetEnum.cs b/Tests/TechTalk.SpecFlow.RuntimeTests/AssistTests/RowExtensionMethodTests_GetEnum.cs index 77c0a8d0a..838de0db0 100644 --- a/Tests/TechTalk.SpecFlow.RuntimeTests/AssistTests/RowExtensionMethodTests_GetEnum.cs +++ b/Tests/TechTalk.SpecFlow.RuntimeTests/AssistTests/RowExtensionMethodTests_GetEnum.cs @@ -54,7 +54,7 @@ public void GetEnum_throws_exception_when_the_value_is_not_defined_in_any_Enum_i Action getNotDefinedEnum = () => table.Rows.First().GetEnum("Sex"); - getNotDefinedEnum.ShouldThrow().WithMessage("No enum with value NotDefined found in type Person"); + getNotDefinedEnum.Should().Throw().WithMessage("No enum with value NotDefined found in type Person"); } [Fact] @@ -76,7 +76,7 @@ public void GetDiscreteEnum_throws_exception_when_the_value_is_not_defined_in_en Action getDiscreteEnum = () => table.Rows[0].GetDiscreteEnum("Header Not Representing Property Of Any Class"); - getDiscreteEnum.ShouldThrow().WithMessage("No enum with value NotDefined found in enum Colors"); + getDiscreteEnum.Should().Throw().WithMessage("No enum with value NotDefined found in enum Colors"); } [Fact] From c15b1bb1a18985c23233e628999414dedc61190c Mon Sep 17 00:00:00 2001 From: Andreas Willich Date: Wed, 20 May 2020 10:30:36 +0200 Subject: [PATCH 4/4] Update changelog.txt --- changelog.txt | 3 +++ 1 file changed, 3 insertions(+) diff --git a/changelog.txt b/changelog.txt index 1e3418154..ae8e779f0 100644 --- a/changelog.txt +++ b/changelog.txt @@ -8,6 +8,9 @@ Fixes for Development: API Changes: + Now allows override of method categories in NUnit to allow for plugin development on this function ++ (breaking) tableRow.GetEnum(..) has now a class contraint on T ++ (breaking) tableRow.GetTheEnumValue(..) has now a class contraint on T ++ added tableRow.GetDiscreteEnum Changes since 3.1.89