From 3a5757a35cea016c109554d87509ccf454fd8886 Mon Sep 17 00:00:00 2001 From: Stuart Ferguson Date: Tue, 28 Nov 2023 12:01:37 +0000 Subject: [PATCH] Added new specflow extensions and unit tests --- .../Shared.EventStoreContext.Tests.csproj | 2 +- .../Shared.IntegrationTesting.Tests.csproj | 2 +- .../GlobalUsings.cs | 1 + ...Shared.IntegrationTesting.UnitTests.csproj | 29 +++++ .../UnitTest1.cs | 118 ++++++++++++++++++ .../SpecflowTableHelper.cs | 44 ++----- Shared.sln | 9 +- 7 files changed, 167 insertions(+), 38 deletions(-) create mode 100644 Shared.IntegrationTesting.UnitTests/GlobalUsings.cs create mode 100644 Shared.IntegrationTesting.UnitTests/Shared.IntegrationTesting.UnitTests.csproj create mode 100644 Shared.IntegrationTesting.UnitTests/UnitTest1.cs diff --git a/Shared.EventStoreContext.Tests/Shared.EventStoreContext.Tests.csproj b/Shared.EventStoreContext.Tests/Shared.EventStoreContext.Tests.csproj index bf31d1ba..2648fbf1 100644 --- a/Shared.EventStoreContext.Tests/Shared.EventStoreContext.Tests.csproj +++ b/Shared.EventStoreContext.Tests/Shared.EventStoreContext.Tests.csproj @@ -4,7 +4,7 @@ net7.0 enable enable - Full + None false true diff --git a/Shared.IntegrationTesting.Tests/Shared.IntegrationTesting.Tests.csproj b/Shared.IntegrationTesting.Tests/Shared.IntegrationTesting.Tests.csproj index 4ba3ee24..81aa7191 100644 --- a/Shared.IntegrationTesting.Tests/Shared.IntegrationTesting.Tests.csproj +++ b/Shared.IntegrationTesting.Tests/Shared.IntegrationTesting.Tests.csproj @@ -4,7 +4,7 @@ net7.0 enable enable - Full + None false diff --git a/Shared.IntegrationTesting.UnitTests/GlobalUsings.cs b/Shared.IntegrationTesting.UnitTests/GlobalUsings.cs new file mode 100644 index 00000000..8c927eb7 --- /dev/null +++ b/Shared.IntegrationTesting.UnitTests/GlobalUsings.cs @@ -0,0 +1 @@ +global using Xunit; \ No newline at end of file diff --git a/Shared.IntegrationTesting.UnitTests/Shared.IntegrationTesting.UnitTests.csproj b/Shared.IntegrationTesting.UnitTests/Shared.IntegrationTesting.UnitTests.csproj new file mode 100644 index 00000000..6a27241e --- /dev/null +++ b/Shared.IntegrationTesting.UnitTests/Shared.IntegrationTesting.UnitTests.csproj @@ -0,0 +1,29 @@ + + + + net7.0 + enable + enable + None + false + true + + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + + + + + diff --git a/Shared.IntegrationTesting.UnitTests/UnitTest1.cs b/Shared.IntegrationTesting.UnitTests/UnitTest1.cs new file mode 100644 index 00000000..b0289b43 --- /dev/null +++ b/Shared.IntegrationTesting.UnitTests/UnitTest1.cs @@ -0,0 +1,118 @@ +namespace Shared.IntegrationTesting.UnitTests +{ + using Shouldly; + using TechTalk.SpecFlow; + + public class SpecflowTableHelperTests + { + [Theory] + [InlineData("Field1", "true", true)] + [InlineData("Field1", "false", false)] + public void SpecflowTableHelper_GetBooleanValue_ExpectedValueIsReturned(String header, String value, Boolean expectedValue) + { + List headers = new List(); + headers.Add(header); + Table table = new Table(headers.ToArray()); + table.AddRow(value); + Boolean actual = SpecflowTableHelper.GetBooleanValue(table.Rows.First(), header); + actual.ShouldBe(expectedValue); + } + + [Theory] + [MemberData(nameof(GetUserChoiceTestData))] + public void SpecflowTableHelper_GetDateForDateString_ExpectedValueIsReturned(String value, DateTime expectedDate) + { + + DateTime today = new DateTime(2023, 11, 27); + DateTime actual = SpecflowTableHelper.GetDateForDateString(value, today); + actual.ShouldBe(expectedDate); + } + + public static IEnumerable GetUserChoiceTestData() + { + yield return new object[] { "TODAY", new DateTime(2023, 11, 27) }; + yield return new object[] { "YESTERDAY", new DateTime(2023, 11, 26) }; + yield return new object[] { "LASTWEEK", new DateTime(2023, 11, 20) }; + yield return new object[] { "LASTMONTH", new DateTime(2023, 10, 27) }; + yield return new object[] { "LASTYEAR", new DateTime(2022, 11, 27) }; + yield return new object[] { "TOMORROW", new DateTime(2023, 11, 28) }; + yield return new object[] { "2023-11-01", new DateTime(2023, 11, 1) }; + } + + [Theory] + [InlineData("Field1", "-1.00", -1.00)] + [InlineData("Field1", "0.00", 0.00)] + [InlineData("Field1", "1.00", 1.00)] + [InlineData("Field1", "-1.23", -1.23)] + [InlineData("Field1", "1.23", 1.23)] + public void SpecflowTableHelper_GetDecimalValue_ExpectedValueIsReturned(String header, String value, Decimal expectedValue) + { + List headers = new List(); + headers.Add(header); + Table table = new Table(headers.ToArray()); + table.AddRow(value); + Decimal actual = SpecflowTableHelper.GetDecimalValue(table.Rows.First(), header); + actual.ShouldBe(expectedValue); + } + + [Theory] + [InlineData("Field1", "-1", -1.00)] + [InlineData("Field1", "0", 0.00)] + [InlineData("Field1", "1", 1.00)] + public void SpecflowTableHelper_GetIntValue_ExpectedValueIsReturned(String header, String value, Int32 expectedValue) + { + List headers = new List(); + headers.Add(header); + Table table = new Table(headers.ToArray()); + table.AddRow(value); + Int32 actual = SpecflowTableHelper.GetIntValue(table.Rows.First(), header); + actual.ShouldBe(expectedValue); + } + + [Theory] + [InlineData("Field1", "-1", -1.00)] + [InlineData("Field1", "0", 0.00)] + [InlineData("Field1", "1", 1.00)] + public void SpecflowTableHelper_GetShortValue_ExpectedValueIsReturned(String header, String value, Int16 expectedValue) + { + List headers = new List(); + headers.Add(header); + Table table = new Table(headers.ToArray()); + table.AddRow(value); + Int16 actual = SpecflowTableHelper.GetShortValue(table.Rows.First(), header); + actual.ShouldBe(expectedValue); + } + + [Fact] + public void SpecflowTableHelper_GetStringRowValue_ExpectedValueIsReturned() + { + String header = "Field1"; + String expectedValue = "TestStringValue"; + + List headers = new List(); + headers.Add(header); + Table table = new Table(headers.ToArray()); + table.AddRow(expectedValue); + + String? actual = SpecflowTableHelper.GetStringRowValue(table.Rows.First(), header); + actual.ShouldBe(expectedValue); + } + + [Theory] + [InlineData("Field1", "Ordinal", StringComparison.Ordinal)] + [InlineData("Field1", "OrdinalIgnoreCase", StringComparison.OrdinalIgnoreCase)] + [InlineData("Field1", "InvariantCulture", StringComparison.InvariantCulture)] + [InlineData("Field1", "InvariantCultureIgnoreCase", StringComparison.InvariantCultureIgnoreCase)] + [InlineData("Field1", "CurrentCulture", StringComparison.CurrentCulture)] + [InlineData("Field1", "CurrentCultureIgnoreCase", StringComparison.CurrentCultureIgnoreCase)] + public void SpecflowTableHelper_GetEnumValue_ExpectedResultIsReturned(String header, String value, StringComparison expectedValue) + { + List headers = new List(); + headers.Add(header); + Table table = new Table(headers.ToArray()); + table.AddRow(value); + StringComparison actual = SpecflowTableHelper.GetEnumValue(table.Rows.First(), header); + actual.ShouldBe(expectedValue); + } + } +} \ No newline at end of file diff --git a/Shared.IntegrationTesting/SpecflowTableHelper.cs b/Shared.IntegrationTesting/SpecflowTableHelper.cs index 7d5caaf5..02c468f0 100644 --- a/Shared.IntegrationTesting/SpecflowTableHelper.cs +++ b/Shared.IntegrationTesting/SpecflowTableHelper.cs @@ -10,12 +10,6 @@ public static class SpecflowTableHelper { #region Methods - /// - /// Gets the boolean value. - /// - /// The row. - /// The key. - /// public static Boolean GetBooleanValue(TableRow row, String key) { @@ -24,12 +18,6 @@ public static Boolean GetBooleanValue(TableRow row, return bool.TryParse(field, out Boolean value) && value; } - /// - /// Gets the date for date string. - /// - /// The date string. - /// The today. - /// public static DateTime GetDateForDateString(String dateString, DateTime today) { @@ -52,12 +40,6 @@ public static DateTime GetDateForDateString(String dateString, } } - /// - /// Gets the decimal value. - /// - /// The row. - /// The key. - /// public static Decimal GetDecimalValue(TableRow row, String key) { @@ -66,12 +48,6 @@ public static Decimal GetDecimalValue(TableRow row, return decimal.TryParse(field, out Decimal value) ? value : -1; } - /// - /// Gets the int value. - /// - /// The row. - /// The key. - /// public static Int32 GetIntValue(TableRow row, String key) { @@ -80,12 +56,6 @@ public static Int32 GetIntValue(TableRow row, return int.TryParse(field, out Int32 value) ? value : -1; } - /// - /// Gets the short value. - /// - /// The row. - /// The key. - /// public static Int16 GetShortValue(TableRow row, String key) { @@ -99,17 +69,21 @@ public static Int16 GetShortValue(TableRow row, return -1; } - /// - /// Gets the string row value. - /// - /// The row. - /// The key. /// public static String GetStringRowValue(TableRow row, String key) { return row.TryGetValue(key, out String value) ? value : ""; } + + public static T GetEnumValue(TableRow row, + String key) where T : struct + { + String field = SpecflowTableHelper.GetStringRowValue(row, key); + + return Enum.Parse(field, true); + } + #endregion } diff --git a/Shared.sln b/Shared.sln index d8684d1e..f04bab8c 100644 --- a/Shared.sln +++ b/Shared.sln @@ -33,7 +33,9 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution .github\workflows\sonarcloud.yml = .github\workflows\sonarcloud.yml EndProjectSection EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Shared.EventStoreContext.Tests", "Shared.EventStoreContext.Tests\Shared.EventStoreContext.Tests.csproj", "{A7909DC1-12F9-42F8-B665-E8A2C63B1481}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Shared.EventStoreContext.Tests", "Shared.EventStoreContext.Tests\Shared.EventStoreContext.Tests.csproj", "{A7909DC1-12F9-42F8-B665-E8A2C63B1481}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Shared.IntegrationTesting.UnitTests", "Shared.IntegrationTesting.UnitTests\Shared.IntegrationTesting.UnitTests.csproj", "{5E23CBBB-8484-4F7F-81D9-308957783695}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -81,6 +83,10 @@ Global {A7909DC1-12F9-42F8-B665-E8A2C63B1481}.Debug|Any CPU.Build.0 = Debug|Any CPU {A7909DC1-12F9-42F8-B665-E8A2C63B1481}.Release|Any CPU.ActiveCfg = Release|Any CPU {A7909DC1-12F9-42F8-B665-E8A2C63B1481}.Release|Any CPU.Build.0 = Release|Any CPU + {5E23CBBB-8484-4F7F-81D9-308957783695}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {5E23CBBB-8484-4F7F-81D9-308957783695}.Debug|Any CPU.Build.0 = Debug|Any CPU + {5E23CBBB-8484-4F7F-81D9-308957783695}.Release|Any CPU.ActiveCfg = Release|Any CPU + {5E23CBBB-8484-4F7F-81D9-308957783695}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -96,6 +102,7 @@ Global {81AF1FCA-837F-4CEA-9E8E-AD38F510930A} = {C641A0E0-A26E-4B16-89E2-DD6E33509C6E} {77D4A6AC-1DEF-4F6D-88C8-35763A1FD9B5} = {C641A0E0-A26E-4B16-89E2-DD6E33509C6E} {A7909DC1-12F9-42F8-B665-E8A2C63B1481} = {C641A0E0-A26E-4B16-89E2-DD6E33509C6E} + {5E23CBBB-8484-4F7F-81D9-308957783695} = {C641A0E0-A26E-4B16-89E2-DD6E33509C6E} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {34032CEC-68CD-41EA-8707-D450689AEA46}