From 5b8d93d674e254d265b70559b67bf2f071dbbdb5 Mon Sep 17 00:00:00 2001 From: Martin Smith Date: Sun, 12 Jan 2025 00:52:11 +0000 Subject: [PATCH] file-scoped namespaces --- src/DNX.Extensions/Arrays/ArrayExtensions.cs | 27 +- .../Arrays/ArrayExtensionsTests.cs | 163 +++- .../Arrays/ByteArrayExtensionsTests.cs | 53 +- .../Assemblies/AssemblyExtensionsTests.cs | 101 ++- .../StringComparisonEqualityComparerTests.cs | 169 +++-- .../Conversion/ConvertExtensionsTest.cs | 441 ++++++----- .../Conversion/GuidExtensionsTests.cs | 73 +- .../DateTimes/DateTimeExtensionsTests.cs | 529 +++++++------ .../Dictionaries/DictionaryExtensionsTests.cs | 195 +++-- .../Enums/EnumExtensionsTests.cs | 85 ++- .../Execution/RunSafelyTests.cs | 593 ++++++++------- .../IO/DirectoryInfoExtensionsTests.cs | 449 ++++++----- .../IO/FileInfoExtensionsTests.cs | 235 +++--- .../Linq/EnumerableExtensionsTests.cs | 707 +++++++++--------- 14 files changed, 1959 insertions(+), 1861 deletions(-) diff --git a/src/DNX.Extensions/Arrays/ArrayExtensions.cs b/src/DNX.Extensions/Arrays/ArrayExtensions.cs index 221a35b..87bdf06 100644 --- a/src/DNX.Extensions/Arrays/ArrayExtensions.cs +++ b/src/DNX.Extensions/Arrays/ArrayExtensions.cs @@ -30,7 +30,7 @@ public static T[] PadLeft(this T[] input, int length) return paddedArray; } - public static T[] ShiftLeft(this T[] input) + public static T[] ShiftLeft(this T[] input, int by = 1, T fillValue = default) { if (input == null) { @@ -38,7 +38,30 @@ public static T[] ShiftLeft(this T[] input) } var shiftedArray = new T[input.Length]; - Array.Copy(input, 1, shiftedArray, 0, input.Length - 1); + Array.Copy(input, by, shiftedArray, 0, input.Length - by); + + for (var x = input.Length - by; x < input.Length; ++x) + { + shiftedArray[x] = fillValue; + } + + return shiftedArray; + } + + public static T[] ShiftRight(this T[] input, int by = 1, T fillValue = default) + { + if (input == null) + { + return []; + } + + var shiftedArray = new T[input.Length]; + Array.Copy(input, 0, shiftedArray, by, input.Length - by); + + for (var x = 0; x < by; ++x) + { + shiftedArray[x] = fillValue; + } return shiftedArray; } diff --git a/tests/DNX.Extensions.Tests/Arrays/ArrayExtensionsTests.cs b/tests/DNX.Extensions.Tests/Arrays/ArrayExtensionsTests.cs index caf1e79..eb944ba 100644 --- a/tests/DNX.Extensions.Tests/Arrays/ArrayExtensionsTests.cs +++ b/tests/DNX.Extensions.Tests/Arrays/ArrayExtensionsTests.cs @@ -5,79 +5,166 @@ #pragma warning disable IDE0290 -namespace DNX.Extensions.Tests.Arrays +namespace DNX.Extensions.Tests.Arrays; + +public class ArrayExtensionsTests(ITestOutputHelper outputHelper) { - public class ArrayExtensionsTests(ITestOutputHelper outputHelper) + [Theory] + [InlineData("2,3,4", false)] + [InlineData("", true)] + [InlineData(null, true)] + public void Test_IsNullOrEmpty(string byteArray, bool isEmpty) + { + var bytes = byteArray?.Split(",", StringSplitOptions.RemoveEmptyEntries) + .Select(x => Convert.ToByte(x)) + .ToArray(); + + bytes.IsNullOrEmpty().Should().Be(isEmpty); + } + + [Fact] + public void Test_PadLeft_when_greater_than_existing_array_length() { - [Theory] - [InlineData("2,3,4", false)] - [InlineData("", true)] - [InlineData(null, true)] - public void Test_IsNullOrEmpty(string byteArray, bool isEmpty) + var values = "1,2,3,4,5,6".Split(",").ToArray(); + + var result = values.PadLeft(8); + + values.Length.Should().Be(6); + result.Length.Should().Be(8); + + for (var i = 1; i <= result.Length; ++i) { - var bytes = byteArray?.Split(",", StringSplitOptions.RemoveEmptyEntries) - .Select(x => Convert.ToByte(x)) - .ToArray(); + outputHelper.WriteLine($"Index: {i}"); + result[^i].Should().Be(i > values.Length ? default : values[^i]); + } + } + + [Fact] + public void Test_PadLeft_when_less_than_existing_array_length() + { + var values = "1,2,3,4,5,6".Split(",").ToArray(); - bytes.IsNullOrEmpty().Should().Be(isEmpty); + // Act + var result = values.PadLeft(4); + + // Assert + values.Length.Should().Be(6); + result.Length.Should().Be(4); + + for (var i = 1; i <= result.Length; ++i) + { + outputHelper.WriteLine($"Index: {i}"); + result[^i].Should().Be(values[^i]); } + } + public class ShiftLeft + { [Fact] - public void Test_PadLeft_when_greater_than_existing_array_length() + public void Test_ShiftLeft_defaults_populated_array() { var values = "1,2,3,4,5,6".Split(",").ToArray(); + string[] expected = ["2", "3", "4", "5", "6", null]; + + // Act + var result = values.ShiftLeft(); + + // Assert + result.Length.Should().Be(values.Length); + result.Should().BeEquivalentTo(expected); + } - var result = values.PadLeft(8); + [Fact] + public void Test_ShiftLeft_by_number_populated_array() + { + var values = "1,2,3,4,5,6".Split(",").ToArray(); + string[] expected = ["4", "5", "6", null, null, null]; - values.Length.Should().Be(6); - result.Length.Should().Be(8); + // Act + var result = values.ShiftLeft(by: 3); - for (var i = 1; i <= result.Length; ++i) - { - outputHelper.WriteLine($"Index: {i}"); - result[^i].Should().Be(i > values.Length ? default : values[^i]); - } + // Assert + result.Length.Should().Be(values.Length); + result.Should().BeEquivalentTo(expected); } [Fact] - public void Test_PadLeft_when_less_than_existing_array_length() + public void Test_ShiftLeft_with_fill_value_populated_array() { var values = "1,2,3,4,5,6".Split(",").ToArray(); + string[] expected = ["3", "4", "5", "6", "0", "0"]; + + // Act + var result = values.ShiftLeft(by: 2, fillValue: "0"); + + // Assert + result.Length.Should().Be(values.Length); + result.Should().BeEquivalentTo(expected); + } - var result = values.PadLeft(4); + [Fact] + public void Test_ShiftLeft_null_array() + { + string[] values = null; - values.Length.Should().Be(6); - result.Length.Should().Be(4); + var result = values.ShiftLeft(); - for (var i = 1; i <= result.Length; ++i) - { - outputHelper.WriteLine($"Index: {i}"); - result[^i].Should().Be(values[^i]); - } + result.Should().NotBeNull(); + result.Length.Should().Be(0); } + } + + public class ShiftRight + { [Fact] - public void Test_ShiftLeft_populated_array() + public void Test_ShiftRight_defaults_populated_array() { var values = "1,2,3,4,5,6".Split(",").ToArray(); + string[] expected = [null, "1", "2", "3", "4", "5"]; - var result = values.ShiftLeft(); + // Act + var result = values.ShiftRight(); + // Assert result.Length.Should().Be(values.Length); + result.Should().BeEquivalentTo(expected); + } - for (var i = 0; i < result.Length; ++i) - { - outputHelper.WriteLine($"Index: {i}"); - result[i].Should().Be(i >= values.Length - 1 ? default : values[i + 1]); - } + [Fact] + public void Test_ShiftRight_by_number_populated_array() + { + var values = "1,2,3,4,5,6".Split(",").ToArray(); + string[] expected = [null, null, null, "1", "2", "3"]; + + // Act + var result = values.ShiftRight(by: 3); + + // Assert + result.Length.Should().Be(values.Length); + result.Should().BeEquivalentTo(expected); } [Fact] - public void Test_ShiftLeft_null_array() + public void Test_ShiftRight_with_fill_value_populated_array() + { + var values = "1,2,3,4,5,6".Split(",").ToArray(); + string[] expected = ["0", "0", "1", "2", "3", "4"]; + + // Act + var result = values.ShiftRight(by: 2, fillValue: "0"); + + // Assert + result.Length.Should().Be(values.Length); + result.Should().BeEquivalentTo(expected); + } + + [Fact] + public void Test_ShiftRight_null_array() { string[] values = null; - var result = values.ShiftLeft(); + var result = values.ShiftRight(); result.Should().NotBeNull(); result.Length.Should().Be(0); diff --git a/tests/DNX.Extensions.Tests/Arrays/ByteArrayExtensionsTests.cs b/tests/DNX.Extensions.Tests/Arrays/ByteArrayExtensionsTests.cs index 985bcf5..aee1a24 100644 --- a/tests/DNX.Extensions.Tests/Arrays/ByteArrayExtensionsTests.cs +++ b/tests/DNX.Extensions.Tests/Arrays/ByteArrayExtensionsTests.cs @@ -2,39 +2,38 @@ using FluentAssertions; using Xunit; -namespace DNX.Extensions.Tests.Arrays +namespace DNX.Extensions.Tests.Arrays; + +public class ByteArrayExtensionsTests { - public class ByteArrayExtensionsTests + [Theory] + [InlineData("65,66,67,68,69,70", "ABCDEF")] + [InlineData("97,98,99,100,101,102", "abcdef")] + [InlineData("", "")] + [InlineData(null, "")] + public void Test_GetAsciiString(string byteText, string expectedResult) { - [Theory] - [InlineData("65,66,67,68,69,70", "ABCDEF")] - [InlineData("97,98,99,100,101,102", "abcdef")] - [InlineData("", "")] - [InlineData(null, "")] - public void Test_GetAsciiString(string byteText, string expectedResult) - { - var bytes = byteText == null - ? [] - : byteText.Split(",", StringSplitOptions.RemoveEmptyEntries).Select(x => Convert.ToByte(x)).ToArray(); + var bytes = byteText == null + ? [] + : byteText.Split(",", StringSplitOptions.RemoveEmptyEntries).Select(x => Convert.ToByte(x)).ToArray(); - var result = bytes.GetAsciiString(); + var result = bytes.GetAsciiString(); - result.Should().Be(expectedResult); - } + result.Should().Be(expectedResult); + } - [Theory] - [InlineData("1,2,3,4,5,6", "010203040506")] - [InlineData("", "")] - [InlineData(null, "")] - public void Test_ToHexString(string byteText, string expectedResult) - { - var bytes = byteText == null - ? [] - : byteText.Split(",", StringSplitOptions.RemoveEmptyEntries).Select(x => Convert.ToByte(x)).ToArray(); + [Theory] + [InlineData("1,2,3,4,5,6", "010203040506")] + [InlineData("", "")] + [InlineData(null, "")] + public void Test_ToHexString(string byteText, string expectedResult) + { + var bytes = byteText == null + ? [] + : byteText.Split(",", StringSplitOptions.RemoveEmptyEntries).Select(x => Convert.ToByte(x)).ToArray(); - var result = bytes.ToHexString(); + var result = bytes.ToHexString(); - result.Should().Be(expectedResult); - } + result.Should().Be(expectedResult); } } diff --git a/tests/DNX.Extensions.Tests/Assemblies/AssemblyExtensionsTests.cs b/tests/DNX.Extensions.Tests/Assemblies/AssemblyExtensionsTests.cs index 49111ab..22c18fa 100644 --- a/tests/DNX.Extensions.Tests/Assemblies/AssemblyExtensionsTests.cs +++ b/tests/DNX.Extensions.Tests/Assemblies/AssemblyExtensionsTests.cs @@ -5,57 +5,56 @@ using Xunit; using Xunit.Abstractions; -namespace DNX.Extensions.Tests.Assemblies +namespace DNX.Extensions.Tests.Assemblies; + +public class AssemblyExtensionsTests(ITestOutputHelper testOutputHelper) { - public class AssemblyExtensionsTests(ITestOutputHelper testOutputHelper) + [Fact] + public void GetEmbeddedResourceText_can_read_resource_successfully() + { + // Arrange + var name = "TestData.SampleData.json"; + + // Act + var result = Assembly.GetExecutingAssembly().GetEmbeddedResourceText(name); + + testOutputHelper.WriteLine("Result: {0}", result); + + // Assert + result.Should().NotBeNull(); + } + + [Fact] + public void GetEmbeddedResourceText_throws_on_unknown_resource_name() + { + // Arrange + var name = $"{Guid.NewGuid()}.json"; + + // Act + var ex = Assert.Throws( + () => Assembly.GetExecutingAssembly().GetEmbeddedResourceText(name) + ); + + testOutputHelper.WriteLine("Exception Message: {0}", ex?.Message); + + // Assert + ex.Should().NotBeNull(); + ex.Message.Should().Contain(name); + } + + [Fact] + public void GetEmbeddedResourceText_can_read_resource_with_specific_namespace_successfully() { - [Fact] - public void GetEmbeddedResourceText_can_read_resource_successfully() - { - // Arrange - var name = "TestData.SampleData.json"; - - // Act - var result = Assembly.GetExecutingAssembly().GetEmbeddedResourceText(name); - - testOutputHelper.WriteLine("Result: {0}", result); - - // Assert - result.Should().NotBeNull(); - } - - [Fact] - public void GetEmbeddedResourceText_throws_on_unknown_resource_name() - { - // Arrange - var name = $"{Guid.NewGuid()}.json"; - - // Act - var ex = Assert.Throws( - () => Assembly.GetExecutingAssembly().GetEmbeddedResourceText(name) - ); - - testOutputHelper.WriteLine("Exception Message: {0}", ex?.Message); - - // Assert - ex.Should().NotBeNull(); - ex.Message.Should().Contain(name); - } - - [Fact] - public void GetEmbeddedResourceText_can_read_resource_with_specific_namespace_successfully() - { - // Arrange - var name = "SampleData.json"; - var nameSpace = $"DNX.Extensions.Tests.TestData"; - - // Act - var result = Assembly.GetExecutingAssembly().GetEmbeddedResourceText(name, nameSpace); - - testOutputHelper.WriteLine("Result: {0}", result); - - // Assert - result.Should().NotBeNull(); - } + // Arrange + var name = "SampleData.json"; + var nameSpace = $"DNX.Extensions.Tests.TestData"; + + // Act + var result = Assembly.GetExecutingAssembly().GetEmbeddedResourceText(name, nameSpace); + + testOutputHelper.WriteLine("Result: {0}", result); + + // Assert + result.Should().NotBeNull(); } -} +} \ No newline at end of file diff --git a/tests/DNX.Extensions.Tests/Comparers/StringComparisonEqualityComparerTests.cs b/tests/DNX.Extensions.Tests/Comparers/StringComparisonEqualityComparerTests.cs index f1161ba..d4724a9 100644 --- a/tests/DNX.Extensions.Tests/Comparers/StringComparisonEqualityComparerTests.cs +++ b/tests/DNX.Extensions.Tests/Comparers/StringComparisonEqualityComparerTests.cs @@ -2,104 +2,103 @@ using FluentAssertions; using Xunit; -namespace DNX.Extensions.Tests.Comparers +namespace DNX.Extensions.Tests.Comparers; + +public class StringComparisonEqualityComparerTests { - public class StringComparisonEqualityComparerTests - { - private StringComparison _comparisonMethod = StringComparison.CurrentCultureIgnoreCase; - private StringComparisonEqualityComparer Sut => new(_comparisonMethod); + private StringComparison _comparisonMethod = StringComparison.CurrentCultureIgnoreCase; + private StringComparisonEqualityComparer Sut => new(_comparisonMethod); - [Fact] - public void DefaultConstructor_works_as_expected() - { - var sut = new StringComparisonEqualityComparer(); + [Fact] + public void DefaultConstructor_works_as_expected() + { + var sut = new StringComparisonEqualityComparer(); - sut.StringComparisonMethod.Should().Be(StringComparison.CurrentCulture); - } + sut.StringComparisonMethod.Should().Be(StringComparison.CurrentCulture); + } - [Theory] - [MemberData(nameof(StringComparisonValues_Data))] - public void Constructor_for_StringComparison_works_as_expected(StringComparison stringComparison) - { - var sut = new StringComparisonEqualityComparer(stringComparison); + [Theory] + [MemberData(nameof(StringComparisonValues_Data))] + public void Constructor_for_StringComparison_works_as_expected(StringComparison stringComparison) + { + var sut = new StringComparisonEqualityComparer(stringComparison); - sut.StringComparisonMethod.Should().Be(stringComparison); - } + sut.StringComparisonMethod.Should().Be(stringComparison); + } - [Theory] - [MemberData(nameof(Equals_Data))] - public void Equals_compares_as_expected(string x, string y, StringComparison stringComparison, bool expectedResult) - { - // Arrange - _comparisonMethod = stringComparison; + [Theory] + [MemberData(nameof(Equals_Data))] + public void Equals_compares_as_expected(string x, string y, StringComparison stringComparison, bool expectedResult) + { + // Arrange + _comparisonMethod = stringComparison; - // Act - var result = Sut.Equals(x, y); + // Act + var result = Sut.Equals(x, y); - // Assert - result.Should().Be(expectedResult); - } + // Assert + result.Should().Be(expectedResult); + } - [Theory] - [MemberData(nameof(Equals_Data))] - public void GetHashCode_compares_as_expected(string x, string y, StringComparison stringComparison, bool expectedResult) - { - // Arrange - _comparisonMethod = stringComparison; + [Theory] + [MemberData(nameof(Equals_Data))] + public void GetHashCode_compares_as_expected(string x, string y, StringComparison stringComparison, bool expectedResult) + { + // Arrange + _comparisonMethod = stringComparison; - // Act - var resultX = Sut.GetHashCode(x); - var resultY = Sut.GetHashCode(y); + // Act + var resultX = Sut.GetHashCode(x); + var resultY = Sut.GetHashCode(y); - // Assert - (resultX == resultY).Should().Be(expectedResult); - } + // Assert + (resultX == resultY).Should().Be(expectedResult); + } - #region TestData + #region TestData - public static TheoryData StringComparisonValues_Data() - { - return new TheoryData( - Enum.GetValues(typeof(StringComparison)) - .Cast() - ); - } + public static TheoryData StringComparisonValues_Data() + { + return new TheoryData( + Enum.GetValues(typeof(StringComparison)) + .Cast() + ); + } - public static TheoryData Equals_Data() + public static TheoryData Equals_Data() + { + return new TheoryData { - return new TheoryData - { - { null, null, StringComparison.CurrentCulture, true }, - { "", "", StringComparison.CurrentCulture, true }, - { "ClearBank", "", StringComparison.CurrentCulture, false }, - { "ClearBank", "", StringComparison.CurrentCulture, false }, - { "", "ClearBank", StringComparison.CurrentCulture, false }, - { "ClearBank", null, StringComparison.CurrentCulture, false }, - { null, "ClearBank", StringComparison.CurrentCulture, false }, - - { "Clear", "Bank", StringComparison.CurrentCulture, false }, - { "Clear", "Bank", StringComparison.CurrentCultureIgnoreCase, false }, - { "Clear", "Bank", StringComparison.InvariantCulture, false }, - { "Clear", "Bank", StringComparison.InvariantCultureIgnoreCase, false }, - { "Clear", "Bank", StringComparison.Ordinal, false }, - { "Clear", "Bank", StringComparison.OrdinalIgnoreCase, false }, - - { "ClearBank", "ClearBank", StringComparison.CurrentCulture, true }, - { "ClearBank", "ClearBank", StringComparison.CurrentCultureIgnoreCase, true }, - { "ClearBank", "ClearBank", StringComparison.InvariantCulture, true }, - { "ClearBank", "ClearBank", StringComparison.InvariantCultureIgnoreCase, true }, - { "ClearBank", "ClearBank", StringComparison.Ordinal, true }, - { "ClearBank", "ClearBank", StringComparison.OrdinalIgnoreCase, true }, - - { "ClearBank", "CLEARBANK", StringComparison.CurrentCulture, false }, - { "ClearBank", "CLEARBANK", StringComparison.CurrentCultureIgnoreCase, true }, - { "ClearBank", "CLEARBANK", StringComparison.InvariantCulture, false }, - { "ClearBank", "CLEARBANK", StringComparison.InvariantCultureIgnoreCase, true }, - { "ClearBank", "CLEARBANK", StringComparison.Ordinal, false }, - { "ClearBank", "CLEARBANK", StringComparison.OrdinalIgnoreCase, true }, - }; - } - - #endregion + { null, null, StringComparison.CurrentCulture, true }, + { "", "", StringComparison.CurrentCulture, true }, + { "ClearBank", "", StringComparison.CurrentCulture, false }, + { "ClearBank", "", StringComparison.CurrentCulture, false }, + { "", "ClearBank", StringComparison.CurrentCulture, false }, + { "ClearBank", null, StringComparison.CurrentCulture, false }, + { null, "ClearBank", StringComparison.CurrentCulture, false }, + + { "Clear", "Bank", StringComparison.CurrentCulture, false }, + { "Clear", "Bank", StringComparison.CurrentCultureIgnoreCase, false }, + { "Clear", "Bank", StringComparison.InvariantCulture, false }, + { "Clear", "Bank", StringComparison.InvariantCultureIgnoreCase, false }, + { "Clear", "Bank", StringComparison.Ordinal, false }, + { "Clear", "Bank", StringComparison.OrdinalIgnoreCase, false }, + + { "ClearBank", "ClearBank", StringComparison.CurrentCulture, true }, + { "ClearBank", "ClearBank", StringComparison.CurrentCultureIgnoreCase, true }, + { "ClearBank", "ClearBank", StringComparison.InvariantCulture, true }, + { "ClearBank", "ClearBank", StringComparison.InvariantCultureIgnoreCase, true }, + { "ClearBank", "ClearBank", StringComparison.Ordinal, true }, + { "ClearBank", "ClearBank", StringComparison.OrdinalIgnoreCase, true }, + + { "ClearBank", "CLEARBANK", StringComparison.CurrentCulture, false }, + { "ClearBank", "CLEARBANK", StringComparison.CurrentCultureIgnoreCase, true }, + { "ClearBank", "CLEARBANK", StringComparison.InvariantCulture, false }, + { "ClearBank", "CLEARBANK", StringComparison.InvariantCultureIgnoreCase, true }, + { "ClearBank", "CLEARBANK", StringComparison.Ordinal, false }, + { "ClearBank", "CLEARBANK", StringComparison.OrdinalIgnoreCase, true }, + }; } -} + + #endregion +} \ No newline at end of file diff --git a/tests/DNX.Extensions.Tests/Conversion/ConvertExtensionsTest.cs b/tests/DNX.Extensions.Tests/Conversion/ConvertExtensionsTest.cs index aa5600c..4f5cbbf 100644 --- a/tests/DNX.Extensions.Tests/Conversion/ConvertExtensionsTest.cs +++ b/tests/DNX.Extensions.Tests/Conversion/ConvertExtensionsTest.cs @@ -2,228 +2,227 @@ using FluentAssertions; using Xunit; -namespace DNX.Extensions.Tests.Conversion +namespace DNX.Extensions.Tests.Conversion; + +public class ConvertExtensionsTests { - public class ConvertExtensionsTests + public enum Numbers + { + Zero = 0, + One, + Two, + Three, + Four, + Five + } + + public class TestClass1 { } + public class TestClass2 : TestClass1 { } + public class TestClass3 { } + + public class ToStringOrDefault + { + [Theory] + [InlineData(1, "1")] + [InlineData(12.34, "12.34")] + [InlineData(true, "True")] + [InlineData((string)null, "")] + public void ToStringOrDefault_without_override_can_convert_successfully(object instance, string expected) + { + var result = instance.ToStringOrDefault(); + + result.Should().Be(expected); + } + + [Theory] + [InlineData(1, "bob", "1")] + [InlineData(12.34, "bob", "12.34")] + [InlineData(true, "bob", "True")] + [InlineData((string)null, "bob", "bob")] + public void ToStringOrDefault_with_override_can_convert_successfully(object instance, string defaultValue, string expected) + { + var result = instance.ToStringOrDefault(defaultValue); + + result.Should().Be(expected); + } + } + + public class ToBoolean + { + [Theory] + [InlineData("true", true)] + [InlineData("TRUE", true)] + [InlineData("TrUe", true)] + [InlineData("false", false)] + [InlineData("FALSE", false)] + [InlineData("FaLsE", false)] + [InlineData("NotABoolean", false)] + public void ToBoolean_without_override_can_convert_successfully(string text, bool expected) + { + var result = text.ToBoolean(); + + result.Should().Be(expected); + } + + [Theory] + [InlineData("true", false, true)] + [InlineData("TRUE", false, true)] + [InlineData("TrUe", false, true)] + [InlineData("false", true, false)] + [InlineData("FALSE", true, false)] + [InlineData("FaLsE", true, false)] + [InlineData("NotABoolean", false, false)] + [InlineData("NotABoolean", true, true)] + public void ToBoolean_with_override_can_convert_successfully(string text, bool defaultValue, bool expected) + { + var result = text.ToBoolean(defaultValue); + + result.Should().Be(expected); + } + } + + public class ToInt32 + { + [Theory] + [InlineData("160", 160)] + [InlineData("0", 0)] + [InlineData("-1", -1)] + [InlineData("2147483647", 2147483647)] + [InlineData("2147483648", 0)] + [InlineData("NotAnInt32", 0)] + public void ToInt32_without_override_can_convert_successfully(string text, int expected) + { + var result = text.ToInt32(); + + result.Should().Be(expected); + } + + [Theory] + [InlineData("160", 42, 160)] + [InlineData("0", 57, 0)] + [InlineData("-1", 5, -1)] + [InlineData("2147483647", 12345, 2147483647)] + [InlineData("2147483648", 12345, 12345)] + [InlineData("NotAnInt32", 0, 0)] + [InlineData("NotAnInt32", 222, 222)] + public void ToInt32_with_override_can_convert_successfully(string text, int defaultValue, int expected) + { + var result = text.ToInt32(defaultValue); + + result.Should().Be(expected); + } + } + + public class ToEnum { - public enum Numbers - { - Zero = 0, - One, - Two, - Three, - Four, - Five - } - - public class TestClass1 { } - public class TestClass2 : TestClass1 { } - public class TestClass3 { } - - public class ToStringOrDefault - { - [Theory] - [InlineData(1, "1")] - [InlineData(12.34, "12.34")] - [InlineData(true, "True")] - [InlineData((string)null, "")] - public void ToStringOrDefault_without_override_can_convert_successfully(object instance, string expected) - { - var result = instance.ToStringOrDefault(); - - result.Should().Be(expected); - } - - [Theory] - [InlineData(1, "bob", "1")] - [InlineData(12.34, "bob", "12.34")] - [InlineData(true, "bob", "True")] - [InlineData((string)null, "bob", "bob")] - public void ToStringOrDefault_with_override_can_convert_successfully(object instance, string defaultValue, string expected) - { - var result = instance.ToStringOrDefault(defaultValue); - - result.Should().Be(expected); - } - } - - public class ToBoolean - { - [Theory] - [InlineData("true", true)] - [InlineData("TRUE", true)] - [InlineData("TrUe", true)] - [InlineData("false", false)] - [InlineData("FALSE", false)] - [InlineData("FaLsE", false)] - [InlineData("NotABoolean", false)] - public void ToBoolean_without_override_can_convert_successfully(string text, bool expected) - { - var result = text.ToBoolean(); - - result.Should().Be(expected); - } - - [Theory] - [InlineData("true", false, true)] - [InlineData("TRUE", false, true)] - [InlineData("TrUe", false, true)] - [InlineData("false", true, false)] - [InlineData("FALSE", true, false)] - [InlineData("FaLsE", true, false)] - [InlineData("NotABoolean", false, false)] - [InlineData("NotABoolean", true, true)] - public void ToBoolean_with_override_can_convert_successfully(string text, bool defaultValue, bool expected) - { - var result = text.ToBoolean(defaultValue); - - result.Should().Be(expected); - } - } - - public class ToInt32 - { - [Theory] - [InlineData("160", 160)] - [InlineData("0", 0)] - [InlineData("-1", -1)] - [InlineData("2147483647", 2147483647)] - [InlineData("2147483648", 0)] - [InlineData("NotAnInt32", 0)] - public void ToInt32_without_override_can_convert_successfully(string text, int expected) - { - var result = text.ToInt32(); - - result.Should().Be(expected); - } - - [Theory] - [InlineData("160", 42, 160)] - [InlineData("0", 57, 0)] - [InlineData("-1", 5, -1)] - [InlineData("2147483647", 12345, 2147483647)] - [InlineData("2147483648", 12345, 12345)] - [InlineData("NotAnInt32", 0, 0)] - [InlineData("NotAnInt32", 222, 222)] - public void ToInt32_with_override_can_convert_successfully(string text, int defaultValue, int expected) - { - var result = text.ToInt32(defaultValue); - - result.Should().Be(expected); - } - } - - public class ToEnum - { - [Theory] - [InlineData("One", Numbers.One)] - [InlineData("FOUR", Numbers.Four)] - [InlineData("THREE", Numbers.Three)] - [InlineData("BOB", Numbers.Zero)] - public void ToEnum_without_override_can_convert_Numbers_enum_successfully(string text, Numbers expected) - { - var result = text.ToEnum(); - - result.Should().Be(expected); - ; - } - - [Theory] - [InlineData("One", Numbers.Five, Numbers.One)] - [InlineData("FOUR", Numbers.Five, Numbers.Four)] - [InlineData("THREE", Numbers.Five, Numbers.Three)] - [InlineData("BOB", Numbers.Five, Numbers.Five)] - public void ToEnum_with_override_can_convert_Numbers_enum_successfully(string text, Numbers defaultValue, Numbers expected) - { - var result = text.ToEnum(defaultValue); - - result.Should().Be(expected); - ; - } - } - - public class ToGuid - { - [Theory] - [InlineData("034B1998-E8C7-4DC0-B0EF-E4D606166756", "034B1998-E8C7-4DC0-B0EF-E4D606166756")] - [InlineData("034B1998-E8C7-4DC0-B0EF-E4D606166756", "034b1998-e8c7-4dc0-b0ef-e4d606166756")] - [InlineData("bob", "00000000-0000-0000-0000-000000000000")] - public void ToGuid_without_default_can_convert_successfully(string text, string expected) - { - var result = text.ToGuid(); - - result.ToString().Should().BeEquivalentTo(expected); - } - - [Theory] - [InlineData("034B1998-E8C7-4DC0-B0EF-E4D606166756", "00000000-0000-0000-0000-000000000000", "034B1998-E8C7-4DC0-B0EF-E4D606166756")] - [InlineData("034B1998-E8C7-4DC0-B0EF-E4D606166756", "00000000-0000-0000-0000-000000000000", "034b1998-e8c7-4dc0-b0ef-e4d606166756")] - [InlineData("034B1998-E8C7-4DC0-B0EF-E4D606166756", "E223D5C2-61C5-4FD9-AABE-F8761B4EDCA6", "034b1998-e8c7-4dc0-b0ef-e4d606166756")] - [InlineData("bob", "E223D5C2-61C5-4FD9-AABE-F8761B4EDCA6", "E223D5C2-61C5-4FD9-AABE-F8761B4EDCA6")] - public void ToGuid_with_default_can_convert_successfully(string text, string defaultValue, string expected) - { - var result = text.ToGuid(Guid.Parse(defaultValue)); - - result.ToString().Should().BeEquivalentTo(expected); - } - } - - public class To - { - [Fact] - public void To_without_default_for_null_class_instance_can_convert_object_successfully() - { - var instance = (TestClass2)null; - var result = instance.To(); - result.Should().BeNull(); - } - - [Fact] - public void To_without_default_for_related_class_can_convert_object_successfully() - { - var instance = new TestClass2(); - var result = instance.To(); - result.Should().NotBeNull(); - result.Should().Be(instance); - } - - [Fact] - public void To_without_default_for_unrelated_class_can_convert_object_successfully() - { - var instance = new TestClass3(); - var result = instance.To(); - result.Should().BeNull(); - } - - [Fact] - public void To_with_default_for_null_class_instance_can_convert_object_successfully() - { - var instance = (TestClass2)null; - var defaultValue = new TestClass1(); - var result = instance.To(defaultValue); - result.Should().NotBeNull(); - result.Should().Be(defaultValue); - } - - [Fact] - public void To_with_default_for_related_class_can_convert_object_successfully() - { - var defaultValue = new TestClass1(); - var instance = new TestClass2(); - var result = instance.To(defaultValue); - result.Should().NotBeNull(); - result.Should().Be(instance); - } - - [Fact] - public void To_with_default_for_unrelated_class_can_convert_object_successfully() - { - var defaultValue = new TestClass1(); - var instance = new TestClass3(); - var result = instance.To(defaultValue); - result.Should().NotBeNull(); - result.Should().Be(defaultValue); - } + [Theory] + [InlineData("One", Numbers.One)] + [InlineData("FOUR", Numbers.Four)] + [InlineData("THREE", Numbers.Three)] + [InlineData("BOB", Numbers.Zero)] + public void ToEnum_without_override_can_convert_Numbers_enum_successfully(string text, Numbers expected) + { + var result = text.ToEnum(); + + result.Should().Be(expected); + ; + } + + [Theory] + [InlineData("One", Numbers.Five, Numbers.One)] + [InlineData("FOUR", Numbers.Five, Numbers.Four)] + [InlineData("THREE", Numbers.Five, Numbers.Three)] + [InlineData("BOB", Numbers.Five, Numbers.Five)] + public void ToEnum_with_override_can_convert_Numbers_enum_successfully(string text, Numbers defaultValue, Numbers expected) + { + var result = text.ToEnum(defaultValue); + + result.Should().Be(expected); + ; + } + } + + public class ToGuid + { + [Theory] + [InlineData("034B1998-E8C7-4DC0-B0EF-E4D606166756", "034B1998-E8C7-4DC0-B0EF-E4D606166756")] + [InlineData("034B1998-E8C7-4DC0-B0EF-E4D606166756", "034b1998-e8c7-4dc0-b0ef-e4d606166756")] + [InlineData("bob", "00000000-0000-0000-0000-000000000000")] + public void ToGuid_without_default_can_convert_successfully(string text, string expected) + { + var result = text.ToGuid(); + + result.ToString().Should().BeEquivalentTo(expected); + } + + [Theory] + [InlineData("034B1998-E8C7-4DC0-B0EF-E4D606166756", "00000000-0000-0000-0000-000000000000", "034B1998-E8C7-4DC0-B0EF-E4D606166756")] + [InlineData("034B1998-E8C7-4DC0-B0EF-E4D606166756", "00000000-0000-0000-0000-000000000000", "034b1998-e8c7-4dc0-b0ef-e4d606166756")] + [InlineData("034B1998-E8C7-4DC0-B0EF-E4D606166756", "E223D5C2-61C5-4FD9-AABE-F8761B4EDCA6", "034b1998-e8c7-4dc0-b0ef-e4d606166756")] + [InlineData("bob", "E223D5C2-61C5-4FD9-AABE-F8761B4EDCA6", "E223D5C2-61C5-4FD9-AABE-F8761B4EDCA6")] + public void ToGuid_with_default_can_convert_successfully(string text, string defaultValue, string expected) + { + var result = text.ToGuid(Guid.Parse(defaultValue)); + + result.ToString().Should().BeEquivalentTo(expected); + } + } + + public class To + { + [Fact] + public void To_without_default_for_null_class_instance_can_convert_object_successfully() + { + var instance = (TestClass2)null; + var result = instance.To(); + result.Should().BeNull(); + } + + [Fact] + public void To_without_default_for_related_class_can_convert_object_successfully() + { + var instance = new TestClass2(); + var result = instance.To(); + result.Should().NotBeNull(); + result.Should().Be(instance); + } + + [Fact] + public void To_without_default_for_unrelated_class_can_convert_object_successfully() + { + var instance = new TestClass3(); + var result = instance.To(); + result.Should().BeNull(); + } + + [Fact] + public void To_with_default_for_null_class_instance_can_convert_object_successfully() + { + var instance = (TestClass2)null; + var defaultValue = new TestClass1(); + var result = instance.To(defaultValue); + result.Should().NotBeNull(); + result.Should().Be(defaultValue); + } + + [Fact] + public void To_with_default_for_related_class_can_convert_object_successfully() + { + var defaultValue = new TestClass1(); + var instance = new TestClass2(); + var result = instance.To(defaultValue); + result.Should().NotBeNull(); + result.Should().Be(instance); + } + + [Fact] + public void To_with_default_for_unrelated_class_can_convert_object_successfully() + { + var defaultValue = new TestClass1(); + var instance = new TestClass3(); + var result = instance.To(defaultValue); + result.Should().NotBeNull(); + result.Should().Be(defaultValue); } } -} +} \ No newline at end of file diff --git a/tests/DNX.Extensions.Tests/Conversion/GuidExtensionsTests.cs b/tests/DNX.Extensions.Tests/Conversion/GuidExtensionsTests.cs index 877a6db..53a37d7 100644 --- a/tests/DNX.Extensions.Tests/Conversion/GuidExtensionsTests.cs +++ b/tests/DNX.Extensions.Tests/Conversion/GuidExtensionsTests.cs @@ -3,46 +3,45 @@ using Xunit; using Xunit.Abstractions; -namespace DNX.Extensions.Tests.Conversion +namespace DNX.Extensions.Tests.Conversion; + +public class GuidExtensionsTests(ITestOutputHelper outputHelper) { - public class GuidExtensionsTests(ITestOutputHelper outputHelper) + [Theory] + [InlineData("ABC")] + [InlineData("abc")] + [InlineData("")] + [InlineData(null)] + [InlineData("497111F7-1511-49A8-8DB2-31B5E40953CB")] + public void ToDeterministicGuid_can_create_a_guid_from_any_string(string text) { - [Theory] - [InlineData("ABC")] - [InlineData("abc")] - [InlineData("")] - [InlineData(null)] - [InlineData("497111F7-1511-49A8-8DB2-31B5E40953CB")] - public void ToDeterministicGuid_can_create_a_guid_from_any_string(string text) - { - // Act - var result = text.ToDeterministicGuid(); - outputHelper.WriteLine($"Text: {text} = {result}"); + // Act + var result = text.ToDeterministicGuid(); + outputHelper.WriteLine($"Text: {text} = {result}"); - // Assert - result.Should().NotBe(Guid.Empty); - result.ToString().Should().NotBe(text); - } + // Assert + result.Should().NotBe(Guid.Empty); + result.ToString().Should().NotBe(text); + } - [Theory] - [InlineData("ABC", "d2bd2f90-dfb1-4f0c-70b4-a5d23525e932")] - [InlineData("abc", "98500190-d23c-b04f-d696-3f7d28e17f72")] - [InlineData("", "d98c1dd4-008f-04b2-e980-0998ecf8427e")] - [InlineData(null, "d98c1dd4-008f-04b2-e980-0998ecf8427e")] - [InlineData("497111F7-1511-49A8-8DB2-31B5E40953CB", "811f4c34-f777-5718-e90f-5b03aee2b92f")] - [InlineData("Strings can be any length, and can be much longer than a standard Guid length", "151991b0-80dc-5902-d525-3a2f3c901477")] - public void ToDeterministicGuid_will_always_generate_a_predictable_result(string text, string expected) - { - // Act - var result = text.ToDeterministicGuid(); - var result2 = text.ToDeterministicGuid(); - outputHelper.WriteLine($"Text: {text} = {result}"); + [Theory] + [InlineData("ABC", "d2bd2f90-dfb1-4f0c-70b4-a5d23525e932")] + [InlineData("abc", "98500190-d23c-b04f-d696-3f7d28e17f72")] + [InlineData("", "d98c1dd4-008f-04b2-e980-0998ecf8427e")] + [InlineData(null, "d98c1dd4-008f-04b2-e980-0998ecf8427e")] + [InlineData("497111F7-1511-49A8-8DB2-31B5E40953CB", "811f4c34-f777-5718-e90f-5b03aee2b92f")] + [InlineData("Strings can be any length, and can be much longer than a standard Guid length", "151991b0-80dc-5902-d525-3a2f3c901477")] + public void ToDeterministicGuid_will_always_generate_a_predictable_result(string text, string expected) + { + // Act + var result = text.ToDeterministicGuid(); + var result2 = text.ToDeterministicGuid(); + outputHelper.WriteLine($"Text: {text} = {result}"); - // Assert - result.Should().NotBe(Guid.Empty); - result.ToString().Should().NotBe(text); - result.Should().Be(result2); - result.ToString().Should().Be(expected); - } + // Assert + result.Should().NotBe(Guid.Empty); + result.ToString().Should().NotBe(text); + result.Should().Be(result2); + result.ToString().Should().Be(expected); } -} +} \ No newline at end of file diff --git a/tests/DNX.Extensions.Tests/DateTimes/DateTimeExtensionsTests.cs b/tests/DNX.Extensions.Tests/DateTimes/DateTimeExtensionsTests.cs index 7d0588c..8573e89 100644 --- a/tests/DNX.Extensions.Tests/DateTimes/DateTimeExtensionsTests.cs +++ b/tests/DNX.Extensions.Tests/DateTimes/DateTimeExtensionsTests.cs @@ -2,312 +2,311 @@ using FluentAssertions; using Xunit; -namespace DNX.Extensions.Tests.DateTimes +namespace DNX.Extensions.Tests.DateTimes; + +public class DateTimeExtensionsTests { - public class DateTimeExtensionsTests - { - private static readonly Random Randomizer = new(); + private static readonly Random Randomizer = new(); - [Fact] - public void Unix_Epoch_returns_the_correct_value() - { - // Arrange - - // Act - var epoch = DateTimeExtensions.UnixEpoch; - - // Assert - epoch.Kind.Should().Be(DateTimeKind.Utc); - epoch.Year.Should().Be(1970); - epoch.Month.Should().Be(1); - epoch.Day.Should().Be(1); - epoch.Hour.Should().Be(0); - epoch.Minute.Should().Be(0); - epoch.Second.Should().Be(0); - epoch.Millisecond.Should().Be(0); - } + [Fact] + public void Unix_Epoch_returns_the_correct_value() + { + // Arrange + + // Act + var epoch = DateTimeExtensions.UnixEpoch; + + // Assert + epoch.Kind.Should().Be(DateTimeKind.Utc); + epoch.Year.Should().Be(1970); + epoch.Month.Should().Be(1); + epoch.Day.Should().Be(1); + epoch.Hour.Should().Be(0); + epoch.Minute.Should().Be(0); + epoch.Second.Should().Be(0); + epoch.Millisecond.Should().Be(0); + } - [Fact] - public void ParseDateAsUtc_can_parse_a_date() - { - // Arrange - var theDateTime = DateTime.Now; - var dateTimeString = theDateTime.ToLongDateString() + " " + theDateTime.ToLongTimeString(); - - // Act - var parsedDateTime = dateTimeString.ParseDateAsUtc(); - - // Assert - theDateTime.Kind.Should().Be(DateTimeKind.Local); - parsedDateTime.Kind.Should().Be(DateTimeKind.Utc); - parsedDateTime.Year.Should().Be(theDateTime.Year); - parsedDateTime.Month.Should().Be(theDateTime.Month); - parsedDateTime.Day.Should().Be(theDateTime.Day); - parsedDateTime.Hour.Should().Be(theDateTime.Hour); - parsedDateTime.Minute.Should().Be(theDateTime.Minute); - parsedDateTime.Second.Should().Be(theDateTime.Second); - parsedDateTime.Millisecond.Should().Be(0); - } + [Fact] + public void ParseDateAsUtc_can_parse_a_date() + { + // Arrange + var theDateTime = DateTime.Now; + var dateTimeString = theDateTime.ToLongDateString() + " " + theDateTime.ToLongTimeString(); + + // Act + var parsedDateTime = dateTimeString.ParseDateAsUtc(); + + // Assert + theDateTime.Kind.Should().Be(DateTimeKind.Local); + parsedDateTime.Kind.Should().Be(DateTimeKind.Utc); + parsedDateTime.Year.Should().Be(theDateTime.Year); + parsedDateTime.Month.Should().Be(theDateTime.Month); + parsedDateTime.Day.Should().Be(theDateTime.Day); + parsedDateTime.Hour.Should().Be(theDateTime.Hour); + parsedDateTime.Minute.Should().Be(theDateTime.Minute); + parsedDateTime.Second.Should().Be(theDateTime.Second); + parsedDateTime.Millisecond.Should().Be(0); + } - [Fact] - public void ParseDateAsUtc_with_default_value_can_parse_a_date() - { - // Arrange - var defaultDateTime = DateTime.Now.Subtract(TimeSpan.FromDays(60)); - var theDateTime = DateTime.Now; - var dateTimeString = theDateTime.ToLongDateString() + " " + theDateTime.ToLongTimeString(); - - // Act - var parsedDateTime = dateTimeString.ParseDateAsUtc(defaultDateTime); - - // Assert - parsedDateTime.Kind.Should().Be(DateTimeKind.Utc); - parsedDateTime.Year.Should().Be(theDateTime.Year); - parsedDateTime.Month.Should().Be(theDateTime.Month); - parsedDateTime.Day.Should().Be(theDateTime.Day); - parsedDateTime.Hour.Should().Be(theDateTime.Hour); - parsedDateTime.Minute.Should().Be(theDateTime.Minute); - parsedDateTime.Second.Should().Be(theDateTime.Second); - parsedDateTime.Millisecond.Should().Be(0); - } + [Fact] + public void ParseDateAsUtc_with_default_value_can_parse_a_date() + { + // Arrange + var defaultDateTime = DateTime.Now.Subtract(TimeSpan.FromDays(60)); + var theDateTime = DateTime.Now; + var dateTimeString = theDateTime.ToLongDateString() + " " + theDateTime.ToLongTimeString(); + + // Act + var parsedDateTime = dateTimeString.ParseDateAsUtc(defaultDateTime); + + // Assert + parsedDateTime.Kind.Should().Be(DateTimeKind.Utc); + parsedDateTime.Year.Should().Be(theDateTime.Year); + parsedDateTime.Month.Should().Be(theDateTime.Month); + parsedDateTime.Day.Should().Be(theDateTime.Day); + parsedDateTime.Hour.Should().Be(theDateTime.Hour); + parsedDateTime.Minute.Should().Be(theDateTime.Minute); + parsedDateTime.Second.Should().Be(theDateTime.Second); + parsedDateTime.Millisecond.Should().Be(0); + } - [Fact] - public void ParseDateAsUtc_with_default_value_returns_default_value_when_it_fails_to_parse_a_date() - { - // Arrange - var defaultDateTime = DateTime.Now; - var dateTimeString = "Not a datetime string"; - - // Act - var parsedDateTime = dateTimeString.ParseDateAsUtc(defaultDateTime); - - // Assert - parsedDateTime.Kind.Should().Be(DateTimeKind.Utc); - parsedDateTime.Year.Should().Be(defaultDateTime.Year); - parsedDateTime.Month.Should().Be(defaultDateTime.Month); - parsedDateTime.Day.Should().Be(defaultDateTime.Day); - parsedDateTime.Hour.Should().Be(defaultDateTime.Hour); - parsedDateTime.Minute.Should().Be(defaultDateTime.Minute); - parsedDateTime.Second.Should().Be(defaultDateTime.Second); - parsedDateTime.Millisecond.Should().Be(defaultDateTime.Millisecond); - } + [Fact] + public void ParseDateAsUtc_with_default_value_returns_default_value_when_it_fails_to_parse_a_date() + { + // Arrange + var defaultDateTime = DateTime.Now; + var dateTimeString = "Not a datetime string"; + + // Act + var parsedDateTime = dateTimeString.ParseDateAsUtc(defaultDateTime); + + // Assert + parsedDateTime.Kind.Should().Be(DateTimeKind.Utc); + parsedDateTime.Year.Should().Be(defaultDateTime.Year); + parsedDateTime.Month.Should().Be(defaultDateTime.Month); + parsedDateTime.Day.Should().Be(defaultDateTime.Day); + parsedDateTime.Hour.Should().Be(defaultDateTime.Hour); + parsedDateTime.Minute.Should().Be(defaultDateTime.Minute); + parsedDateTime.Second.Should().Be(defaultDateTime.Second); + parsedDateTime.Millisecond.Should().Be(defaultDateTime.Millisecond); + } - [Fact] - public void ParseDateAsUtc_can_parse_a_date_using_a_format_provider() - { - // Arrange - var formatProvider = new CustomDateTimeFormatProvider(); - - var theDateTime = DateTime.Now; - var dateTimeString = theDateTime.ToString(CustomDateTimeFormatProvider.FormatString); - - // Act - var parsedDateTime = dateTimeString.ParseDateAsUtc(formatProvider); - - // Assert - theDateTime.Kind.Should().Be(DateTimeKind.Local); - parsedDateTime.Kind.Should().Be(DateTimeKind.Utc); - parsedDateTime.Year.Should().Be(theDateTime.Year); - parsedDateTime.Month.Should().Be(theDateTime.Month); - parsedDateTime.Day.Should().Be(theDateTime.Day); - parsedDateTime.Hour.Should().Be(theDateTime.Hour); - parsedDateTime.Minute.Should().Be(theDateTime.Minute); - parsedDateTime.Second.Should().Be(theDateTime.Second); - parsedDateTime.Millisecond.Should().Be(0); - } + [Fact] + public void ParseDateAsUtc_can_parse_a_date_using_a_format_provider() + { + // Arrange + var formatProvider = new CustomDateTimeFormatProvider(); + + var theDateTime = DateTime.Now; + var dateTimeString = theDateTime.ToString(CustomDateTimeFormatProvider.FormatString); + + // Act + var parsedDateTime = dateTimeString.ParseDateAsUtc(formatProvider); + + // Assert + theDateTime.Kind.Should().Be(DateTimeKind.Local); + parsedDateTime.Kind.Should().Be(DateTimeKind.Utc); + parsedDateTime.Year.Should().Be(theDateTime.Year); + parsedDateTime.Month.Should().Be(theDateTime.Month); + parsedDateTime.Day.Should().Be(theDateTime.Day); + parsedDateTime.Hour.Should().Be(theDateTime.Hour); + parsedDateTime.Minute.Should().Be(theDateTime.Minute); + parsedDateTime.Second.Should().Be(theDateTime.Second); + parsedDateTime.Millisecond.Should().Be(0); + } - [Fact] - public void ParseDateAsUtc_with_default_value_returns_can_parse_a_date_using_a_format_provider() - { - // Arrange - var formatProvider = new CustomDateTimeFormatProvider(); - - var defaultDateTime = DateTime.Now.Subtract(TimeSpan.FromDays(60)); - var theDateTime = DateTime.Now; - var dateTimeString = theDateTime.ToString(CustomDateTimeFormatProvider.FormatString); - - // Act - var parsedDateTime = dateTimeString.ParseDateAsUtc(formatProvider, defaultDateTime); - - // Assert - parsedDateTime.Kind.Should().Be(DateTimeKind.Utc); - parsedDateTime.Year.Should().Be(theDateTime.Year); - parsedDateTime.Month.Should().Be(theDateTime.Month); - parsedDateTime.Day.Should().Be(theDateTime.Day); - parsedDateTime.Hour.Should().Be(theDateTime.Hour); - parsedDateTime.Minute.Should().Be(theDateTime.Minute); - parsedDateTime.Second.Should().Be(theDateTime.Second); - parsedDateTime.Millisecond.Should().Be(0); - } + [Fact] + public void ParseDateAsUtc_with_default_value_returns_can_parse_a_date_using_a_format_provider() + { + // Arrange + var formatProvider = new CustomDateTimeFormatProvider(); + + var defaultDateTime = DateTime.Now.Subtract(TimeSpan.FromDays(60)); + var theDateTime = DateTime.Now; + var dateTimeString = theDateTime.ToString(CustomDateTimeFormatProvider.FormatString); + + // Act + var parsedDateTime = dateTimeString.ParseDateAsUtc(formatProvider, defaultDateTime); + + // Assert + parsedDateTime.Kind.Should().Be(DateTimeKind.Utc); + parsedDateTime.Year.Should().Be(theDateTime.Year); + parsedDateTime.Month.Should().Be(theDateTime.Month); + parsedDateTime.Day.Should().Be(theDateTime.Day); + parsedDateTime.Hour.Should().Be(theDateTime.Hour); + parsedDateTime.Minute.Should().Be(theDateTime.Minute); + parsedDateTime.Second.Should().Be(theDateTime.Second); + parsedDateTime.Millisecond.Should().Be(0); + } - [Fact] - public void ParseDateAsUtc_with_default_value_returns_default_value_when_it_fails_to_parse_a_date_using_a_format_provider() - { - // Arrange - var formatProvider = new CustomDateTimeFormatProvider(); - - var defaultDateTime = DateTime.Now; - var dateTimeString = "Not a datetime string"; - - // Act - var parsedDateTime = dateTimeString.ParseDateAsUtc(formatProvider, defaultDateTime); - - // Assert - parsedDateTime.Kind.Should().Be(DateTimeKind.Utc); - parsedDateTime.Year.Should().Be(defaultDateTime.Year); - parsedDateTime.Month.Should().Be(defaultDateTime.Month); - parsedDateTime.Day.Should().Be(defaultDateTime.Day); - parsedDateTime.Hour.Should().Be(defaultDateTime.Hour); - parsedDateTime.Minute.Should().Be(defaultDateTime.Minute); - parsedDateTime.Second.Should().Be(defaultDateTime.Second); - parsedDateTime.Millisecond.Should().Be(defaultDateTime.Millisecond); - } + [Fact] + public void ParseDateAsUtc_with_default_value_returns_default_value_when_it_fails_to_parse_a_date_using_a_format_provider() + { + // Arrange + var formatProvider = new CustomDateTimeFormatProvider(); + + var defaultDateTime = DateTime.Now; + var dateTimeString = "Not a datetime string"; + + // Act + var parsedDateTime = dateTimeString.ParseDateAsUtc(formatProvider, defaultDateTime); + + // Assert + parsedDateTime.Kind.Should().Be(DateTimeKind.Utc); + parsedDateTime.Year.Should().Be(defaultDateTime.Year); + parsedDateTime.Month.Should().Be(defaultDateTime.Month); + parsedDateTime.Day.Should().Be(defaultDateTime.Day); + parsedDateTime.Hour.Should().Be(defaultDateTime.Hour); + parsedDateTime.Minute.Should().Be(defaultDateTime.Minute); + parsedDateTime.Second.Should().Be(defaultDateTime.Second); + parsedDateTime.Millisecond.Should().Be(defaultDateTime.Millisecond); + } - [Theory] - [MemberData(nameof(ResetDateTime_Data))] - public void SetYear_can_operate_as_expected(DateTime dateTime) - { - var year = Randomizer.Next(1, 9999); - var month = dateTime.Month; - var day = dateTime.Day; + [Theory] + [MemberData(nameof(ResetDateTime_Data))] + public void SetYear_can_operate_as_expected(DateTime dateTime) + { + var year = Randomizer.Next(1, 9999); + var month = dateTime.Month; + var day = dateTime.Day; - var result = dateTime.SetYear(year); + var result = dateTime.SetYear(year); - result.Year.Should().Be(year); - result.Month.Should().Be(month); - result.Day.Should().Be(day); - } + result.Year.Should().Be(year); + result.Month.Should().Be(month); + result.Day.Should().Be(day); + } - [Theory] - [MemberData(nameof(ResetDateTime_Data))] - public void SetMonth_can_operate_as_expected(DateTime dateTime) - { - var year = dateTime.Year; - var month = Randomizer.Next(1, 12); - var day = dateTime.Day; + [Theory] + [MemberData(nameof(ResetDateTime_Data))] + public void SetMonth_can_operate_as_expected(DateTime dateTime) + { + var year = dateTime.Year; + var month = Randomizer.Next(1, 12); + var day = dateTime.Day; - var result = dateTime.SetMonth(month); + var result = dateTime.SetMonth(month); - result.Year.Should().Be(year); - result.Month.Should().Be(month); - result.Day.Should().Be(day); - } + result.Year.Should().Be(year); + result.Month.Should().Be(month); + result.Day.Should().Be(day); + } - [Theory] - [MemberData(nameof(ResetDateTime_Data))] - public void SetDay_can_operate_as_expected(DateTime dateTime) - { - var year = dateTime.Year; - var month = dateTime.Month; - var day = Randomizer.Next(1, 28); + [Theory] + [MemberData(nameof(ResetDateTime_Data))] + public void SetDay_can_operate_as_expected(DateTime dateTime) + { + var year = dateTime.Year; + var month = dateTime.Month; + var day = Randomizer.Next(1, 28); - var result = dateTime.SetDay(day); + var result = dateTime.SetDay(day); - result.Year.Should().Be(year); - result.Month.Should().Be(month); - result.Day.Should().Be(day); - } + result.Year.Should().Be(year); + result.Month.Should().Be(month); + result.Day.Should().Be(day); + } - [Theory] - [MemberData(nameof(ResetDateTime_Data))] - public void ResetHours_can_operate_as_expected(DateTime dateTime) - { - var result = dateTime.ResetHours(); + [Theory] + [MemberData(nameof(ResetDateTime_Data))] + public void ResetHours_can_operate_as_expected(DateTime dateTime) + { + var result = dateTime.ResetHours(); - result.Hour.Should().Be(0); - } + result.Hour.Should().Be(0); + } - [Theory] - [MemberData(nameof(ResetDateTime_Data))] - public void ResetMinutes_can_operate_as_expected(DateTime dateTime) - { - var result = dateTime.ResetMinutes(); + [Theory] + [MemberData(nameof(ResetDateTime_Data))] + public void ResetMinutes_can_operate_as_expected(DateTime dateTime) + { + var result = dateTime.ResetMinutes(); - result.Minute.Should().Be(0); - } + result.Minute.Should().Be(0); + } - [Theory] - [MemberData(nameof(ResetDateTime_Data))] - public void ResetSeconds_can_operate_as_expected(DateTime dateTime) - { - var result = dateTime.ResetSeconds(); + [Theory] + [MemberData(nameof(ResetDateTime_Data))] + public void ResetSeconds_can_operate_as_expected(DateTime dateTime) + { + var result = dateTime.ResetSeconds(); - result.Second.Should().Be(0); - } + result.Second.Should().Be(0); + } - [Theory] - [MemberData(nameof(ResetDateTime_Data))] - public void ResetMilliseconds_can_operate_as_expected(DateTime dateTime) - { - var result = dateTime.ResetMilliseconds(); + [Theory] + [MemberData(nameof(ResetDateTime_Data))] + public void ResetMilliseconds_can_operate_as_expected(DateTime dateTime) + { + var result = dateTime.ResetMilliseconds(); - result.Millisecond.Should().Be(0); - } + result.Millisecond.Should().Be(0); + } - [Theory] - [MemberData(nameof(ResetDateTime_Data))] - public void SetHours_can_operate_as_expected(DateTime dateTime) - { - var value = Randomizer.Next(1, 24); + [Theory] + [MemberData(nameof(ResetDateTime_Data))] + public void SetHours_can_operate_as_expected(DateTime dateTime) + { + var value = Randomizer.Next(1, 24); - var result = dateTime.SetHours(value); + var result = dateTime.SetHours(value); - result.Hour.Should().Be(value); - } + result.Hour.Should().Be(value); + } - [Theory] - [MemberData(nameof(ResetDateTime_Data))] - public void SetMinutes_can_operate_as_expected(DateTime dateTime) - { - var value = Randomizer.Next(1, 60); + [Theory] + [MemberData(nameof(ResetDateTime_Data))] + public void SetMinutes_can_operate_as_expected(DateTime dateTime) + { + var value = Randomizer.Next(1, 60); - var result = dateTime.SetMinutes(value); + var result = dateTime.SetMinutes(value); - result.Minute.Should().Be(value); - } + result.Minute.Should().Be(value); + } - [Theory] - [MemberData(nameof(ResetDateTime_Data))] - public void SetSeconds_can_operate_as_expected(DateTime dateTime) - { - var value = Randomizer.Next(1, 60); + [Theory] + [MemberData(nameof(ResetDateTime_Data))] + public void SetSeconds_can_operate_as_expected(DateTime dateTime) + { + var value = Randomizer.Next(1, 60); - var result = dateTime.SetSeconds(value); + var result = dateTime.SetSeconds(value); - result.Second.Should().Be(value); - } + result.Second.Should().Be(value); + } - [Theory] - [MemberData(nameof(ResetDateTime_Data))] - public void SetMilliseconds_can_operate_as_expected(DateTime dateTime) - { - var value = Randomizer.Next(1, 999); + [Theory] + [MemberData(nameof(ResetDateTime_Data))] + public void SetMilliseconds_can_operate_as_expected(DateTime dateTime) + { + var value = Randomizer.Next(1, 999); - var result = dateTime.SetMilliseconds(value); + var result = dateTime.SetMilliseconds(value); - result.Millisecond.Should().Be(value); - } + result.Millisecond.Should().Be(value); + } - public static TheoryData ResetDateTime_Data() + public static TheoryData ResetDateTime_Data() + { + var data = new TheoryData { - var data = new TheoryData - { - { DateTime.UtcNow }, - { DateTime.UnixEpoch }, - { DateTime.Parse("2021-11-05 20:53:44.12345") }, - { DateTime.Parse("2021-11-05 20:53:44") }, - { DateTime.Parse("2021-11-05 20:53") }, - { DateTime.Parse("2021-11-05") }, - }; - - if (DateTime.Now.Hour != DateTime.UtcNow.Hour) - { - data.Add(DateTime.Now); - } - - return data; + { DateTime.UtcNow }, + { DateTime.UnixEpoch }, + { DateTime.Parse("2021-11-05 20:53:44.12345") }, + { DateTime.Parse("2021-11-05 20:53:44") }, + { DateTime.Parse("2021-11-05 20:53") }, + { DateTime.Parse("2021-11-05") }, + }; + + if (DateTime.Now.Hour != DateTime.UtcNow.Hour) + { + data.Add(DateTime.Now); } + + return data; } } diff --git a/tests/DNX.Extensions.Tests/Dictionaries/DictionaryExtensionsTests.cs b/tests/DNX.Extensions.Tests/Dictionaries/DictionaryExtensionsTests.cs index 9c02f5a..80edfc0 100644 --- a/tests/DNX.Extensions.Tests/Dictionaries/DictionaryExtensionsTests.cs +++ b/tests/DNX.Extensions.Tests/Dictionaries/DictionaryExtensionsTests.cs @@ -2,104 +2,103 @@ using FluentAssertions; using Xunit; -namespace DNX.Extensions.Tests.Dictionaries +namespace DNX.Extensions.Tests.Dictionaries; + +public class DictionaryExtensionsTests { - public class DictionaryExtensionsTests + [Theory] + [InlineData("Sunday", 0)] + [InlineData("Monday", 1)] + [InlineData("Thursday", 4)] + [InlineData("Saturday", 6)] + [InlineData("NotADay", 0)] + public void Can_get_dictionary_value_safely_or_default(string key, int expectedValue) + { + // Arrange + var dict = Enum.GetNames(typeof(DayOfWeek)) + .ToDictionary( + x => x, + x => (int)(Enum.Parse(x)) + ); + + // Act + var value = dict.Get(key); + + // Assert + value.Should().Be(expectedValue); + } + + [Theory] + [InlineData("Sunday", 999, 0)] + [InlineData("Monday", 999, 1)] + [InlineData("NotADay", 999, 999)] + [InlineData("NotADay", 0, 0)] + public void Can_get_dictionary_value_safely_overriding_default(string key, int defaultValue, int expectedValue) { - [Theory] - [InlineData("Sunday", 0)] - [InlineData("Monday", 1)] - [InlineData("Thursday", 4)] - [InlineData("Saturday", 6)] - [InlineData("NotADay", 0)] - public void Can_get_dictionary_value_safely_or_default(string key, int expectedValue) - { - // Arrange - var dict = Enum.GetNames(typeof(DayOfWeek)) - .ToDictionary( - x => x, - x => (int)(Enum.Parse(x)) - ); - - // Act - var value = dict.Get(key); - - // Assert - value.Should().Be(expectedValue); - } - - [Theory] - [InlineData("Sunday", 999, 0)] - [InlineData("Monday", 999, 1)] - [InlineData("NotADay", 999, 999)] - [InlineData("NotADay", 0, 0)] - public void Can_get_dictionary_value_safely_overriding_default(string key, int defaultValue, int expectedValue) - { - // Arrange - var dict = Enum.GetNames(typeof(DayOfWeek)) - .ToDictionary( - x => x, - x => (int)(Enum.Parse(x)) - ); - - // Act - var value = dict.Get(key, defaultValue); - - // Assert - value.Should().Be(expectedValue); - } - - [Fact] - public void Can_split_well_formed_string_into_string_dictionary() - { - // Arrange - var text = "Liverpool=1|Southend United=7"; - - // Act - var result = text.ToStringDictionary(); - - // Assert - result.Should().NotBeNull(); - result.Should().BeOfType(typeof(Dictionary)); - result.Keys.Count.Should().Be(2); - result.ContainsKey("Liverpool").Should().Be(true); - result["Liverpool"].Should().Be("1"); - result.ContainsKey("Southend United").Should().Be(true); - result["Southend United"].Should().Be("7"); - } - - [Fact] - public void Null_string_is_handled_into_string_dictionary() - { - // Arrange - const string text = null; - - // Act - var result = text.ToStringDictionary(); - - // Assert - result.Should().NotBeNull(); - result.Should().BeOfType(typeof(Dictionary)); - result.Keys.Count.Should().Be(0); - } - - [Fact] - public void Can_split_well_formed_string_into_string_object_dictionary() - { - // Arrange - var text = "Liverpool=1|Southend United=7"; - - // Act - var result = text.ToStringObjectDictionary(); - - // Assert - result.Should().NotBeNull(); - result.Should().BeOfType(typeof(Dictionary)); - result.Keys.Count.Should().Be(2); - result.ContainsKey("Liverpool").Should().Be(true); - result["Liverpool"].Should().Be("1"); - result.ContainsKey("Southend United").Should().Be(true); - result["Southend United"].Should().Be("7"); - } + // Arrange + var dict = Enum.GetNames(typeof(DayOfWeek)) + .ToDictionary( + x => x, + x => (int)(Enum.Parse(x)) + ); + + // Act + var value = dict.Get(key, defaultValue); + + // Assert + value.Should().Be(expectedValue); + } + + [Fact] + public void Can_split_well_formed_string_into_string_dictionary() + { + // Arrange + var text = "Liverpool=1|Southend United=7"; + + // Act + var result = text.ToStringDictionary(); + + // Assert + result.Should().NotBeNull(); + result.Should().BeOfType(typeof(Dictionary)); + result.Keys.Count.Should().Be(2); + result.ContainsKey("Liverpool").Should().Be(true); + result["Liverpool"].Should().Be("1"); + result.ContainsKey("Southend United").Should().Be(true); + result["Southend United"].Should().Be("7"); + } + + [Fact] + public void Null_string_is_handled_into_string_dictionary() + { + // Arrange + const string text = null; + + // Act + var result = text.ToStringDictionary(); + + // Assert + result.Should().NotBeNull(); + result.Should().BeOfType(typeof(Dictionary)); + result.Keys.Count.Should().Be(0); + } + + [Fact] + public void Can_split_well_formed_string_into_string_object_dictionary() + { + // Arrange + var text = "Liverpool=1|Southend United=7"; + + // Act + var result = text.ToStringObjectDictionary(); + + // Assert + result.Should().NotBeNull(); + result.Should().BeOfType(typeof(Dictionary)); + result.Keys.Count.Should().Be(2); + result.ContainsKey("Liverpool").Should().Be(true); + result["Liverpool"].Should().Be("1"); + result.ContainsKey("Southend United").Should().Be(true); + result["Southend United"].Should().Be("7"); } -} +} \ No newline at end of file diff --git a/tests/DNX.Extensions.Tests/Enums/EnumExtensionsTests.cs b/tests/DNX.Extensions.Tests/Enums/EnumExtensionsTests.cs index 7d56819..fedee9b 100644 --- a/tests/DNX.Extensions.Tests/Enums/EnumExtensionsTests.cs +++ b/tests/DNX.Extensions.Tests/Enums/EnumExtensionsTests.cs @@ -3,55 +3,54 @@ using FluentAssertions; using Xunit; -namespace DNX.Extensions.Tests.Enums +namespace DNX.Extensions.Tests.Enums; + +public enum MyType { - public enum MyType - { - One = 1, + One = 1, - [Description("Number 2")] - Two = 2, + [Description("Number 2")] + Two = 2, - [Description] - Three = 3, + [Description] + Three = 3, - [Description(null)] - Four = 4, + [Description(null)] + Four = 4, - [Description("")] - Five = 5 + [Description("")] + Five = 5 +} + +public class EnumExtensionsTests +{ + [Theory] + [InlineData(MyType.One, null)] + [InlineData(MyType.Two, "Number 2")] + [InlineData(MyType.Three, "")] + [InlineData(MyType.Four, null)] + [InlineData(MyType.Five, "")] + public void GetDescription_can_retrieve_value_correctly(MyType myType, string expectedResult) + { + // Act + var result = myType.GetDescription(); + + // Assert + result.Should().Be(expectedResult, $"{myType} has description: {result}"); } - public class EnumExtensionsTests + [Theory] + [InlineData(MyType.One, "One")] + [InlineData(MyType.Two, "Number 2")] + [InlineData(MyType.Three, "")] + [InlineData(MyType.Four, "Four")] + [InlineData(MyType.Five, "")] + public void GetDescriptionOrName_can_retrieve_value_correctly(MyType myType, string expectedResult) { - [Theory] - [InlineData(MyType.One, null)] - [InlineData(MyType.Two, "Number 2")] - [InlineData(MyType.Three, "")] - [InlineData(MyType.Four, null)] - [InlineData(MyType.Five, "")] - public void GetDescription_can_retrieve_value_correctly(MyType myType, string expectedResult) - { - // Act - var result = myType.GetDescription(); - - // Assert - result.Should().Be(expectedResult, $"{myType} has description: {result}"); - } - - [Theory] - [InlineData(MyType.One, "One")] - [InlineData(MyType.Two, "Number 2")] - [InlineData(MyType.Three, "")] - [InlineData(MyType.Four, "Four")] - [InlineData(MyType.Five, "")] - public void GetDescriptionOrName_can_retrieve_value_correctly(MyType myType, string expectedResult) - { - // Act - var result = myType.GetDescriptionOrName(); - - // Assert - result.Should().Be(expectedResult, $"{myType} has description: {result}"); - } + // Act + var result = myType.GetDescriptionOrName(); + + // Assert + result.Should().Be(expectedResult, $"{myType} has description: {result}"); } -} +} \ No newline at end of file diff --git a/tests/DNX.Extensions.Tests/Execution/RunSafelyTests.cs b/tests/DNX.Extensions.Tests/Execution/RunSafelyTests.cs index bbfa8db..658d9ae 100644 --- a/tests/DNX.Extensions.Tests/Execution/RunSafelyTests.cs +++ b/tests/DNX.Extensions.Tests/Execution/RunSafelyTests.cs @@ -4,315 +4,314 @@ // ReSharper disable InconsistentNaming -namespace DNX.Extensions.Tests.Execution +namespace DNX.Extensions.Tests.Execution; + +public class RunSafelyTests { - public class RunSafelyTests + public class Execute_Tests + { + [Fact] + public void Can_run_simple_action_that_succeeds() + { + // Arrange + var guid = Guid.NewGuid(); + var value = Guid.Empty; + + // Act + RunSafely.Execute(() => value = guid); + + // Assert + value.Should().Be(guid); + } + + [Fact] + public void Can_handle_simple_action_that_fails() + { + // Act + RunSafely.Execute(() => throw new Exception(nameof(Can_handle_simple_action_that_fails))); + } + + [Fact] + public void Can_handle_simple_action_that_fails_and_extract_exception() + { + // Arrange + var value = int.MaxValue; + var guid = Guid.NewGuid(); + var message = ""; + + // Act + RunSafely.Execute(() => throw new Exception(guid.ToString()), ex => message = ex.Message); + + // Assert + value.Should().Be(int.MaxValue); + message.Should().NotBeNull(); + message.Should().NotBeNull(guid.ToString()); + } + + [Fact] + public void Can_handle_simple_func_that_fails() + { + // Arrange + var dividend = 1000; + var divisor = 0; + var value = 0; + + // Act + RunSafely.Execute(() => value = dividend / divisor); + + // Assert + value.Should().Be(0); + } + + [Fact] + public void Can_handle_simple_func_that_fails_and_extract_exception() + { + // Arrange + var dividend = 1000; + var divisor = 0; + var value = 0; + var message = ""; + + // Act + RunSafely.Execute(() => value = dividend / divisor, ex => message = ex.Message); + + // Assert + value.Should().Be(0); + message.Should().NotBeNullOrEmpty(); + message.Should().Contain("divide by zero"); + } + } + + public class ExecuteT_Tests { - public class Execute_Tests + [Fact] + public void Can_run_simple_func_that_succeeds() + { + // Arrange + var guid = Guid.NewGuid(); + + // Act + var value = RunSafely.Execute(() => guid); + + // Assert + value.Should().Be(guid); + } + + [Fact] + public void Can_handle_simple_func_that_fails() { - [Fact] - public void Can_run_simple_action_that_succeeds() - { - // Arrange - var guid = Guid.NewGuid(); - var value = Guid.Empty; - - // Act - RunSafely.Execute(() => value = guid); - - // Assert - value.Should().Be(guid); - } - - [Fact] - public void Can_handle_simple_action_that_fails() - { - // Act - RunSafely.Execute(() => throw new Exception(nameof(Can_handle_simple_action_that_fails))); - } - - [Fact] - public void Can_handle_simple_action_that_fails_and_extract_exception() - { - // Arrange - var value = int.MaxValue; - var guid = Guid.NewGuid(); - var message = ""; - - // Act - RunSafely.Execute(() => throw new Exception(guid.ToString()), ex => message = ex.Message); - - // Assert - value.Should().Be(int.MaxValue); - message.Should().NotBeNull(); - message.Should().NotBeNull(guid.ToString()); - } - - [Fact] - public void Can_handle_simple_func_that_fails() - { - // Arrange - var dividend = 1000; - var divisor = 0; - var value = 0; - - // Act - RunSafely.Execute(() => value = dividend / divisor); - - // Assert - value.Should().Be(0); - } - - [Fact] - public void Can_handle_simple_func_that_fails_and_extract_exception() - { - // Arrange - var dividend = 1000; - var divisor = 0; - var value = 0; - var message = ""; - - // Act - RunSafely.Execute(() => value = dividend / divisor, ex => message = ex.Message); - - // Assert - value.Should().Be(0); - message.Should().NotBeNullOrEmpty(); - message.Should().Contain("divide by zero"); - } + // Arrange + var dividend = 1000; + var divisor = 0; + + // Act + var value = RunSafely.Execute(() => dividend / divisor); + + // Assert + value.Should().Be(default); } - public class ExecuteT_Tests + [Fact] + public void Can_handle_simple_func_with_default_that_fails() { - [Fact] - public void Can_run_simple_func_that_succeeds() - { - // Arrange - var guid = Guid.NewGuid(); - - // Act - var value = RunSafely.Execute(() => guid); - - // Assert - value.Should().Be(guid); - } - - [Fact] - public void Can_handle_simple_func_that_fails() - { - // Arrange - var dividend = 1000; - var divisor = 0; - - // Act - var value = RunSafely.Execute(() => dividend / divisor); - - // Assert - value.Should().Be(default); - } - - [Fact] - public void Can_handle_simple_func_with_default_that_fails() - { - // Arrange - var dividend = 1000; - var divisor = 0; - var defaultResult = 500; - - // Act - var value = RunSafely.Execute(() => dividend / divisor, defaultResult); - - // Assert - value.Should().Be(defaultResult); - } - - [Fact] - public void Can_handle_simple_func_that_fails_and_extract_exception() - { - // Arrange - var dividend = 1000; - var divisor = 0; - var message = ""; - - // Act - var value = RunSafely.Execute(() => dividend / divisor, ex => message = ex.Message); - - // Assert - value.Should().Be(default); - message.Should().NotBeNullOrEmpty(); - message.Should().Contain("divide by zero"); - } - - [Fact] - public void Can_handle_simple_func_with_default_that_fails_and_extract_exception() - { - // Arrange - var dividend = 1000; - var divisor = 0; - var defaultResult = 500; - var message = ""; - - // Act - var value = RunSafely.Execute(() => dividend / divisor, defaultResult, ex => message = ex.Message); - - // Assert - value.Should().Be(defaultResult); - message.Should().NotBeNullOrEmpty(); - message.Should().Contain("divide by zero"); - } + // Arrange + var dividend = 1000; + var divisor = 0; + var defaultResult = 500; + + // Act + var value = RunSafely.Execute(() => dividend / divisor, defaultResult); + + // Assert + value.Should().Be(defaultResult); + } + + [Fact] + public void Can_handle_simple_func_that_fails_and_extract_exception() + { + // Arrange + var dividend = 1000; + var divisor = 0; + var message = ""; + + // Act + var value = RunSafely.Execute(() => dividend / divisor, ex => message = ex.Message); + + // Assert + value.Should().Be(default); + message.Should().NotBeNullOrEmpty(); + message.Should().Contain("divide by zero"); + } + + [Fact] + public void Can_handle_simple_func_with_default_that_fails_and_extract_exception() + { + // Arrange + var dividend = 1000; + var divisor = 0; + var defaultResult = 500; + var message = ""; + + // Act + var value = RunSafely.Execute(() => dividend / divisor, defaultResult, ex => message = ex.Message); + + // Assert + value.Should().Be(defaultResult); + message.Should().NotBeNullOrEmpty(); + message.Should().Contain("divide by zero"); + } + } + + public class ExecuteAsync_Tests + { + [Fact] + public async Task Can_run_simple_task_that_succeeds() + { + // Arrange + var dividend = 1000; + var divisor = 20; + var value = 0; + + var task = new Task(() => value = dividend / divisor); + task.Start(); + + // Act + await RunSafely.ExecuteAsync(task); + + // Assert + value.Should().Be(dividend / divisor); + } + + [Fact] + public async Task Can_handle_simple_action_that_fails() + { + // Arrange + var dividend = 1000; + var divisor = 0; + var value = 0; + + var task = new Task(() => value = dividend / divisor); + task.Start(); + + // Act + await RunSafely.ExecuteAsync(task); + + // Assert + value.Should().Be(0); + } + + [Fact] + public async Task Can_handle_simple_action_that_fails_and_extract_exception() + { + // Arrange + var dividend = 1000; + var divisor = 0; + var value = 0; + var message = ""; + + var task = new Task(() => value = dividend / divisor); + task.Start(); + + // Act + await RunSafely.ExecuteAsync(task, ex => message = ex.Message); + + // Assert + value.Should().Be(0); + message.Should().NotBeNullOrEmpty(); + message.Should().Contain("divide by zero"); + } + } + + public class ExecuteAsyncT_Tests + { + private static async Task DivideAsync(int dividend, int divisor) + { + var quotient = await Task.Run(() => dividend / divisor); + + return quotient; + } + + [Fact] + public async Task Can_run_simple_func_that_succeeds() + { + // Arrange + var dividend = 1000; + var divisor = 50; + + // Act + var value = await RunSafely.ExecuteAsync(DivideAsync(dividend, divisor)); + + // Assert + value.Should().Be(dividend / divisor); + } + + [Fact] + public async Task Can_run_simple_func_that_fails() + { + // Arrange + var dividend = 1000; + var divisor = 0; + var value = 0; + + // Act + value = await RunSafely.ExecuteAsync(DivideAsync(dividend, divisor)); + + // Assert + value.Should().Be(default); + } + + [Fact] + public async Task Can_run_simple_func_with_default_that_fails() + { + // Arrange + var dividend = 1000; + var divisor = 0; + var defaultResult = 500; + var value = 0; + + // Act + value = await RunSafely.ExecuteAsync(DivideAsync(dividend, divisor), defaultResult); + + // Assert + value.Should().Be(defaultResult); } - public class ExecuteAsync_Tests + [Fact] + public async Task Can_handle_simple_func_that_fails_and_extract_exception() { - [Fact] - public async Task Can_run_simple_task_that_succeeds() - { - // Arrange - var dividend = 1000; - var divisor = 20; - var value = 0; - - var task = new Task(() => value = dividend / divisor); - task.Start(); - - // Act - await RunSafely.ExecuteAsync(task); - - // Assert - value.Should().Be(dividend / divisor); - } - - [Fact] - public async Task Can_handle_simple_action_that_fails() - { - // Arrange - var dividend = 1000; - var divisor = 0; - var value = 0; - - var task = new Task(() => value = dividend / divisor); - task.Start(); - - // Act - await RunSafely.ExecuteAsync(task); - - // Assert - value.Should().Be(0); - } - - [Fact] - public async Task Can_handle_simple_action_that_fails_and_extract_exception() - { - // Arrange - var dividend = 1000; - var divisor = 0; - var value = 0; - var message = ""; - - var task = new Task(() => value = dividend / divisor); - task.Start(); - - // Act - await RunSafely.ExecuteAsync(task, ex => message = ex.Message); - - // Assert - value.Should().Be(0); - message.Should().NotBeNullOrEmpty(); - message.Should().Contain("divide by zero"); - } + // Arrange + var dividend = 1000; + var divisor = 0; + var value = 0; + var message = ""; + + // Act + value = await RunSafely.ExecuteAsync(DivideAsync(dividend, divisor), ex => message = ex.Message); + + // Assert + value.Should().Be(default); + message.Should().NotBeNull(); + message.Should().NotBeNull("divide by zero"); } - public class ExecuteAsyncT_Tests + [Fact] + public async Task Can_handle_simple_func_with_default_that_fails_and_extract_exception() { - private static async Task DivideAsync(int dividend, int divisor) - { - var quotient = await Task.Run(() => dividend / divisor); - - return quotient; - } - - [Fact] - public async Task Can_run_simple_func_that_succeeds() - { - // Arrange - var dividend = 1000; - var divisor = 50; - - // Act - var value = await RunSafely.ExecuteAsync(DivideAsync(dividend, divisor)); - - // Assert - value.Should().Be(dividend / divisor); - } - - [Fact] - public async Task Can_run_simple_func_that_fails() - { - // Arrange - var dividend = 1000; - var divisor = 0; - var value = 0; - - // Act - value = await RunSafely.ExecuteAsync(DivideAsync(dividend, divisor)); - - // Assert - value.Should().Be(default); - } - - [Fact] - public async Task Can_run_simple_func_with_default_that_fails() - { - // Arrange - var dividend = 1000; - var divisor = 0; - var defaultResult = 500; - var value = 0; - - // Act - value = await RunSafely.ExecuteAsync(DivideAsync(dividend, divisor), defaultResult); - - // Assert - value.Should().Be(defaultResult); - } - - [Fact] - public async Task Can_handle_simple_func_that_fails_and_extract_exception() - { - // Arrange - var dividend = 1000; - var divisor = 0; - var value = 0; - var message = ""; - - // Act - value = await RunSafely.ExecuteAsync(DivideAsync(dividend, divisor), ex => message = ex.Message); - - // Assert - value.Should().Be(default); - message.Should().NotBeNull(); - message.Should().NotBeNull("divide by zero"); - } - - [Fact] - public async Task Can_handle_simple_func_with_default_that_fails_and_extract_exception() - { - // Arrange - var dividend = 1000; - var divisor = 0; - var defaultResult = 500; - var value = 0; - var message = ""; - - // Act - value = await RunSafely.ExecuteAsync(DivideAsync(dividend, divisor), defaultResult, - ex => message = ex.Message); - - // Assert - value.Should().Be(defaultResult); - message.Should().NotBeNull(); - message.Should().Contain("divide by zero"); - } + // Arrange + var dividend = 1000; + var divisor = 0; + var defaultResult = 500; + var value = 0; + var message = ""; + + // Act + value = await RunSafely.ExecuteAsync(DivideAsync(dividend, divisor), defaultResult, + ex => message = ex.Message); + + // Assert + value.Should().Be(defaultResult); + message.Should().NotBeNull(); + message.Should().Contain("divide by zero"); } } } diff --git a/tests/DNX.Extensions.Tests/IO/DirectoryInfoExtensionsTests.cs b/tests/DNX.Extensions.Tests/IO/DirectoryInfoExtensionsTests.cs index 9055c00..3ba5a58 100644 --- a/tests/DNX.Extensions.Tests/IO/DirectoryInfoExtensionsTests.cs +++ b/tests/DNX.Extensions.Tests/IO/DirectoryInfoExtensionsTests.cs @@ -6,265 +6,264 @@ // ReSharper disable StringLiteralTypo -namespace DNX.Extensions.Tests.IO +namespace DNX.Extensions.Tests.IO; + +public class DirectoryInfoExtensionsTests : IDisposable { - public class DirectoryInfoExtensionsTests : IDisposable + private readonly DirectoryInfo _directoryInfo; + private readonly ITestOutputHelper _outputHelper; + + public DirectoryInfoExtensionsTests(ITestOutputHelper outputHelper) { - private readonly DirectoryInfo _directoryInfo; - private readonly ITestOutputHelper _outputHelper; + _outputHelper = outputHelper; - public DirectoryInfoExtensionsTests(ITestOutputHelper outputHelper) - { - _outputHelper = outputHelper; + var directoryPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); + _directoryInfo = new DirectoryInfo(directoryPath); + _directoryInfo.Create(); - var directoryPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); - _directoryInfo = new DirectoryInfo(directoryPath); - _directoryInfo.Create(); + SetupStandardFileStructure(_directoryInfo); + } - SetupStandardFileStructure(_directoryInfo); - } + public void Dispose() + { + _directoryInfo.Delete(true); + GC.SuppressFinalize(this); + } - public void Dispose() - { - _directoryInfo.Delete(true); - GC.SuppressFinalize(this); - } + internal static void CreateFile(DirectoryInfo directoryInfo, string fileName) + { + var filePath = Path.Combine(directoryInfo.FullName, fileName); - internal static void CreateFile(DirectoryInfo directoryInfo, string fileName) - { - var filePath = Path.Combine(directoryInfo.FullName, fileName); + File.WriteAllText(filePath, string.Empty); + } - File.WriteAllText(filePath, string.Empty); - } + internal static void SetupStandardFileStructure(DirectoryInfo directoryInfo) + { + var dir1 = directoryInfo.CreateSubdirectory("dir1"); + var dir2 = directoryInfo.CreateSubdirectory("dir2"); + var dir3 = dir1.CreateSubdirectory("dur3"); + var dir4 = dir2.CreateSubdirectory("dur4"); + + CreateFile(directoryInfo, "file.txt"); + CreateFile(directoryInfo, "file.json"); + CreateFile(dir1, "file1.txt"); + CreateFile(dir2, "file2.txt"); + CreateFile(dir1, "file1.json"); + CreateFile(dir2, "file2.json"); + CreateFile(dir3, "file1.tf"); + CreateFile(dir4, "file2.tf"); + } - internal static void SetupStandardFileStructure(DirectoryInfo directoryInfo) - { - var dir1 = directoryInfo.CreateSubdirectory("dir1"); - var dir2 = directoryInfo.CreateSubdirectory("dir2"); - var dir3 = dir1.CreateSubdirectory("dur3"); - var dir4 = dir2.CreateSubdirectory("dur4"); - - CreateFile(directoryInfo, "file.txt"); - CreateFile(directoryInfo, "file.json"); - CreateFile(dir1, "file1.txt"); - CreateFile(dir2, "file2.txt"); - CreateFile(dir1, "file1.json"); - CreateFile(dir2, "file2.json"); - CreateFile(dir3, "file1.tf"); - CreateFile(dir4, "file2.tf"); - } + [Fact] + public void FindFiles_for_directory_that_does_not_exist_finds_expected_files() + { + // Arrange + const string pattern = "*.txt"; + var directoryInfo = new DirectoryInfo(Path.Join(Path.GetTempPath(), Guid.NewGuid().ToString())); - [Fact] - public void FindFiles_for_directory_that_does_not_exist_finds_expected_files() - { - // Arrange - const string pattern = "*.txt"; - var directoryInfo = new DirectoryInfo(Path.Join(Path.GetTempPath(), Guid.NewGuid().ToString())); + // Act + var result = directoryInfo.FindFiles(pattern, false); - // Act - var result = directoryInfo.FindFiles(pattern, false); + // Assert + result.Should().NotBeNull(); + result.Count().Should().Be(0); + } - // Assert - result.Should().NotBeNull(); - result.Count().Should().Be(0); - } + [Fact] + public void FindFiles_for_single_pattern_without_recursion_finds_expected_files() + { + // Arrange + const string pattern = "*.txt"; - [Fact] - public void FindFiles_for_single_pattern_without_recursion_finds_expected_files() - { - // Arrange - const string pattern = "*.txt"; + // Act + var result = _directoryInfo.FindFiles(pattern, false); - // Act - var result = _directoryInfo.FindFiles(pattern, false); + // Assert + result.Should().NotBeNull(); + result.Count().Should().Be(1); + result.Count(x => x.Name == "file.txt").Should().Be(1); + } - // Assert - result.Should().NotBeNull(); - result.Count().Should().Be(1); - result.Count(x => x.Name == "file.txt").Should().Be(1); - } + [Fact] + public void FindFiles_for_single_pattern_with_recursion_finds_expected_files() + { + // Arrange + const string pattern = "*.txt"; + + // Act + var result = _directoryInfo.FindFiles(pattern, true); + + // Assert + result.Should().NotBeNull(); + result.Count().Should().Be(3); + result.Count(x => x.Name == "file.txt").Should().Be(1); + result.Count(x => x.Name == "file1.txt").Should().Be(1); + result.Count(x => x.Name == "file2.txt").Should().Be(1); + } - [Fact] - public void FindFiles_for_single_pattern_with_recursion_finds_expected_files() - { - // Arrange - const string pattern = "*.txt"; - - // Act - var result = _directoryInfo.FindFiles(pattern, true); - - // Assert - result.Should().NotBeNull(); - result.Count().Should().Be(3); - result.Count(x => x.Name == "file.txt").Should().Be(1); - result.Count(x => x.Name == "file1.txt").Should().Be(1); - result.Count(x => x.Name == "file2.txt").Should().Be(1); - } + [Fact] + public void FindFiles_for_multiple_patterns_without_recursion_finds_expected_files() + { + // Arrange + var patterns = new[] { "*.txt", "*.json" }; + SetupStandardFileStructure(_directoryInfo); + + // Act + var result = _directoryInfo.FindFiles(patterns, false); + + // Assert + result.Should().NotBeNull(); + result.Count().Should().Be(2); + result.Count(x => x.Name == "file.txt").Should().Be(1); + result.Count(x => x.Name == "file.json").Should().Be(1); + } - [Fact] - public void FindFiles_for_multiple_patterns_without_recursion_finds_expected_files() - { - // Arrange - var patterns = new[] { "*.txt", "*.json" }; - SetupStandardFileStructure(_directoryInfo); - - // Act - var result = _directoryInfo.FindFiles(patterns, false); - - // Assert - result.Should().NotBeNull(); - result.Count().Should().Be(2); - result.Count(x => x.Name == "file.txt").Should().Be(1); - result.Count(x => x.Name == "file.json").Should().Be(1); - } + [Fact] + public void FindFiles_for_multiple_patterns_with_recursion_finds_expected_files() + { + // Arrange + var patterns = new[] { "*.txt", "*.json" }; + SetupStandardFileStructure(_directoryInfo); + + // Act + var result = _directoryInfo.FindFiles(patterns, true); + + // Assert + result.Should().NotBeNull(); + result.Count().Should().Be(6); + result.Count(x => x.Name == "file.txt").Should().Be(1); + result.Count(x => x.Name == "file1.txt").Should().Be(1); + result.Count(x => x.Name == "file2.txt").Should().Be(1); + result.Count(x => x.Name == "file.json").Should().Be(1); + result.Count(x => x.Name == "file1.json").Should().Be(1); + result.Count(x => x.Name == "file2.json").Should().Be(1); + } - [Fact] - public void FindFiles_for_multiple_patterns_with_recursion_finds_expected_files() - { - // Arrange - var patterns = new[] { "*.txt", "*.json" }; - SetupStandardFileStructure(_directoryInfo); - - // Act - var result = _directoryInfo.FindFiles(patterns, true); - - // Assert - result.Should().NotBeNull(); - result.Count().Should().Be(6); - result.Count(x => x.Name == "file.txt").Should().Be(1); - result.Count(x => x.Name == "file1.txt").Should().Be(1); - result.Count(x => x.Name == "file2.txt").Should().Be(1); - result.Count(x => x.Name == "file.json").Should().Be(1); - result.Count(x => x.Name == "file1.json").Should().Be(1); - result.Count(x => x.Name == "file2.json").Should().Be(1); - } + [Fact] + public void FindDirectories_for_directory_that_does_not_exist_finds_expected_files() + { + // Arrange + const string pattern = "dir*"; + var directoryInfo = new DirectoryInfo(Path.Join(Path.GetTempPath(), Guid.NewGuid().ToString())); - [Fact] - public void FindDirectories_for_directory_that_does_not_exist_finds_expected_files() - { - // Arrange - const string pattern = "dir*"; - var directoryInfo = new DirectoryInfo(Path.Join(Path.GetTempPath(), Guid.NewGuid().ToString())); + // Act + var result = directoryInfo.FindDirectories(pattern, false); - // Act - var result = directoryInfo.FindDirectories(pattern, false); + // Assert + result.Should().NotBeNull(); + result.Count().Should().Be(0); + } - // Assert - result.Should().NotBeNull(); - result.Count().Should().Be(0); - } + [Fact] + public void FindDirectories_for_single_pattern_without_recursion_finds_expected_files() + { + // Arrange + const string pattern = "dir*"; - [Fact] - public void FindDirectories_for_single_pattern_without_recursion_finds_expected_files() - { - // Arrange - const string pattern = "dir*"; + // Act + var result = _directoryInfo.FindDirectories(pattern, false); - // Act - var result = _directoryInfo.FindDirectories(pattern, false); + // Assert + result.Should().NotBeNull(); + result.Count().Should().Be(2); + result.Count(x => x.Name == "dir1").Should().Be(1); + result.Count(x => x.Name == "dir2").Should().Be(1); + } - // Assert - result.Should().NotBeNull(); - result.Count().Should().Be(2); - result.Count(x => x.Name == "dir1").Should().Be(1); - result.Count(x => x.Name == "dir2").Should().Be(1); - } + [Fact] + public void FindDirectories_for_single_pattern_with_recursion_finds_expected_files() + { + // Arrange + const string pattern = "d?r*"; + + // Act + var result = _directoryInfo.FindDirectories(pattern, true); + + // Assert + result.Should().NotBeNull(); + result.Count().Should().Be(4); + result.Count(x => x.Name == "dir1").Should().Be(1); + result.Count(x => x.Name == "dir2").Should().Be(1); + result.Count(x => x.Name == "dur3").Should().Be(1); + result.Count(x => x.Name == "dur4").Should().Be(1); + } - [Fact] - public void FindDirectories_for_single_pattern_with_recursion_finds_expected_files() - { - // Arrange - const string pattern = "d?r*"; - - // Act - var result = _directoryInfo.FindDirectories(pattern, true); - - // Assert - result.Should().NotBeNull(); - result.Count().Should().Be(4); - result.Count(x => x.Name == "dir1").Should().Be(1); - result.Count(x => x.Name == "dir2").Should().Be(1); - result.Count(x => x.Name == "dur3").Should().Be(1); - result.Count(x => x.Name == "dur4").Should().Be(1); - } + [Fact] + public void FindDirectories_for_multiple_patterns_without_recursion_finds_expected_files() + { + // Arrange + var patterns = new[] { "dir*", "dur*" }; - [Fact] - public void FindDirectories_for_multiple_patterns_without_recursion_finds_expected_files() - { - // Arrange - var patterns = new[] { "dir*", "dur*" }; + // Act + var result = _directoryInfo.FindDirectories(patterns, false); - // Act - var result = _directoryInfo.FindDirectories(patterns, false); + // Assert + result.Should().NotBeNull(); + result.Count().Should().Be(2); + result.Count(x => x.Name == "dir1").Should().Be(1); + result.Count(x => x.Name == "dir2").Should().Be(1); + } - // Assert - result.Should().NotBeNull(); - result.Count().Should().Be(2); - result.Count(x => x.Name == "dir1").Should().Be(1); - result.Count(x => x.Name == "dir2").Should().Be(1); - } + [Fact] + public void FindDirectories_for_multiple_patterns_with_recursion_finds_expected_files() + { + // Arrange + var patterns = new[] { "dir*", "dur*" }; + + // Act + var result = _directoryInfo.FindDirectories(patterns, true); + + // Assert + result.Should().NotBeNull(); + result.Count().Should().Be(4); + result.Count(x => x.Name == "dir1").Should().Be(1); + result.Count(x => x.Name == "dir2").Should().Be(1); + result.Count(x => x.Name == "dur3").Should().Be(1); + result.Count(x => x.Name == "dur4").Should().Be(1); + } - [Fact] - public void FindDirectories_for_multiple_patterns_with_recursion_finds_expected_files() - { - // Arrange - var patterns = new[] { "dir*", "dur*" }; - - // Act - var result = _directoryInfo.FindDirectories(patterns, true); - - // Assert - result.Should().NotBeNull(); - result.Count().Should().Be(4); - result.Count(x => x.Name == "dir1").Should().Be(1); - result.Count(x => x.Name == "dir2").Should().Be(1); - result.Count(x => x.Name == "dur3").Should().Be(1); - result.Count(x => x.Name == "dur4").Should().Be(1); - } + [Theory] + [MemberData(nameof(GetRelativePath_Data))] + public void GetRelativePath_can_extract_relative_path_correctly(string dirName, string relativeToDirName, string expected) + { + _outputHelper.WriteLine($"{Environment.OSVersion.Platform} - Checking DirName: {dirName} -> {relativeToDirName} = {expected}"); - [Theory] - [MemberData(nameof(GetRelativePath_Data))] - public void GetRelativePath_can_extract_relative_path_correctly(string dirName, string relativeToDirName, string expected) - { - _outputHelper.WriteLine($"{Environment.OSVersion.Platform} - Checking DirName: {dirName} -> {relativeToDirName} = {expected}"); + var dirInfo = dirName == null ? null : new DirectoryInfo(dirName); + var relativeToDirInfo = relativeToDirName == null ? null : new DirectoryInfo(relativeToDirName); - var dirInfo = dirName == null ? null : new DirectoryInfo(dirName); - var relativeToDirInfo = relativeToDirName == null ? null : new DirectoryInfo(relativeToDirName); + var result = dirInfo.GetRelativePath(relativeToDirInfo); - var result = dirInfo.GetRelativePath(relativeToDirInfo); + result.Should().Be(expected, $"{nameof(dirName)}: {dirName} - {nameof(relativeToDirInfo)}: {relativeToDirInfo}"); + } - result.Should().Be(expected, $"{nameof(dirName)}: {dirName} - {nameof(relativeToDirInfo)}: {relativeToDirInfo}"); - } + public static TheoryData GetRelativePath_Data() + { + var guid1 = Guid.NewGuid().ToString(); + var guid2 = Guid.NewGuid().ToString(); + var guid3 = Guid.NewGuid().ToString(); - public static TheoryData GetRelativePath_Data() + var data = new TheoryData() { - var guid1 = Guid.NewGuid().ToString(); - var guid2 = Guid.NewGuid().ToString(); - var guid3 = Guid.NewGuid().ToString(); - - var data = new TheoryData() - { - { Path.Combine(Path.GetTempPath(), guid1), null, null }, - { null, Path.Combine(Path.GetTempPath(), guid1), null }, - { Path.Combine(Path.GetTempPath(), guid1), Path.Combine(Path.GetTempPath(), guid1), "" }, - { Path.Combine(Path.GetTempPath(), guid1), Path.Combine(Path.GetTempPath(), "abcdefg"), Path.Join("..", guid1) }, - { Path.Combine(Path.GetTempPath(), guid2), Path.Combine(Path.GetTempPath(), guid3), Path.Join("..", guid2) }, - { Path.Combine(Path.GetTempPath(), "abcdefg"), Path.Combine(Path.GetTempPath(), "abcdefg"), "" }, - { Path.Combine(Path.GetTempPath(), "abcdefg", "dir3"), Path.Combine(Path.GetTempPath(), "abcdefg"), "dir3" }, - { Path.Combine(Path.GetTempPath(), "abcdefg", "dir3"), Path.Combine(Path.GetTempPath(), "abcdefg", "dir3"), "" }, - { Path.Combine(Path.GetTempPath(), "folder1"), Path.Combine(Path.GetTempPath(), "folder2"), Path.Combine("..", "folder1") }, - }; - - if (Configuration.EnvironmentConfig.IsWindowsStyleFileSystem) - { - data.Add(Path.Combine(Path.GetTempPath(), "folder1"), Path.Combine("D:", "folder2"), Path.Combine(Path.GetTempPath(), "folder1")); - } - if (Configuration.EnvironmentConfig.IsLinuxStyleFileSystem) - { - data.Add(Path.Combine(Path.GetTempPath(), "folder1"), Path.Combine("/etc", "folder2"), Path.Combine(Path.GetTempPath(), "folder1").EnsureStartsWith(Path.Combine("..", ".."))); - } - - return data; + { Path.Combine(Path.GetTempPath(), guid1), null, null }, + { null, Path.Combine(Path.GetTempPath(), guid1), null }, + { Path.Combine(Path.GetTempPath(), guid1), Path.Combine(Path.GetTempPath(), guid1), "" }, + { Path.Combine(Path.GetTempPath(), guid1), Path.Combine(Path.GetTempPath(), "abcdefg"), Path.Join("..", guid1) }, + { Path.Combine(Path.GetTempPath(), guid2), Path.Combine(Path.GetTempPath(), guid3), Path.Join("..", guid2) }, + { Path.Combine(Path.GetTempPath(), "abcdefg"), Path.Combine(Path.GetTempPath(), "abcdefg"), "" }, + { Path.Combine(Path.GetTempPath(), "abcdefg", "dir3"), Path.Combine(Path.GetTempPath(), "abcdefg"), "dir3" }, + { Path.Combine(Path.GetTempPath(), "abcdefg", "dir3"), Path.Combine(Path.GetTempPath(), "abcdefg", "dir3"), "" }, + { Path.Combine(Path.GetTempPath(), "folder1"), Path.Combine(Path.GetTempPath(), "folder2"), Path.Combine("..", "folder1") }, + }; + + if (Configuration.EnvironmentConfig.IsWindowsStyleFileSystem) + { + data.Add(Path.Combine(Path.GetTempPath(), "folder1"), Path.Combine("D:", "folder2"), Path.Combine(Path.GetTempPath(), "folder1")); + } + if (Configuration.EnvironmentConfig.IsLinuxStyleFileSystem) + { + data.Add(Path.Combine(Path.GetTempPath(), "folder1"), Path.Combine("/etc", "folder2"), Path.Combine(Path.GetTempPath(), "folder1").EnsureStartsWith(Path.Combine("..", ".."))); } + + return data; } -} +} \ No newline at end of file diff --git a/tests/DNX.Extensions.Tests/IO/FileInfoExtensionsTests.cs b/tests/DNX.Extensions.Tests/IO/FileInfoExtensionsTests.cs index c0a2aa0..140391f 100644 --- a/tests/DNX.Extensions.Tests/IO/FileInfoExtensionsTests.cs +++ b/tests/DNX.Extensions.Tests/IO/FileInfoExtensionsTests.cs @@ -4,154 +4,153 @@ using Xunit; using Xunit.Abstractions; -namespace DNX.Extensions.Tests.IO +namespace DNX.Extensions.Tests.IO; + +public class FileInfoExtensionsTests(ITestOutputHelper outputHelper) { - public class FileInfoExtensionsTests(ITestOutputHelper outputHelper) + private static string DriveRoot1 { - private static string DriveRoot1 + get { - get - { - return Configuration.EnvironmentConfig.IsLinuxStyleFileSystem - ? "/root1" - : "C:"; - } + return Configuration.EnvironmentConfig.IsLinuxStyleFileSystem + ? "/root1" + : "C:"; } + } - private static string DriveRoot2 + private static string DriveRoot2 + { + get { - get - { - return Configuration.EnvironmentConfig.IsLinuxStyleFileSystem - ? "/root2" - : "D:"; - } + return Configuration.EnvironmentConfig.IsLinuxStyleFileSystem + ? "/root2" + : "D:"; } + } - [Theory] - [MemberData(nameof(GetRelativeFileName_Data))] - public void GetRelativeFileName_can_extract_relative_filename_correctly(string fileName, string dirName, string expected) - { - outputHelper.WriteLine($"{Environment.OSVersion.Platform} - Checking FileName: {fileName} -> {dirName} = {expected}"); + [Theory] + [MemberData(nameof(GetRelativeFileName_Data))] + public void GetRelativeFileName_can_extract_relative_filename_correctly(string fileName, string dirName, string expected) + { + outputHelper.WriteLine($"{Environment.OSVersion.Platform} - Checking FileName: {fileName} -> {dirName} = {expected}"); - var fileInfo = new FileInfo(fileName); - var dirInfo = new DirectoryInfo(dirName); + var fileInfo = new FileInfo(fileName); + var dirInfo = new DirectoryInfo(dirName); - var result = fileInfo.GetRelativeFileName(dirInfo); + var result = fileInfo.GetRelativeFileName(dirInfo); - result.Should().Be(expected, $"{nameof(dirName)}: {dirName} - {nameof(fileName)}: {fileName}"); - } + result.Should().Be(expected, $"{nameof(dirName)}: {dirName} - {nameof(fileName)}: {fileName}"); + } - [Theory] - [MemberData(nameof(GetRelativeFilePath_Data))] - public void GetRelativeFilePath_can_extract_relative_path_correctly(string fileName, string dirName, string expected) - { - outputHelper.WriteLine($"{Environment.OSVersion.Platform} - Checking FileName: {fileName} -> {dirName} = {expected}"); + [Theory] + [MemberData(nameof(GetRelativeFilePath_Data))] + public void GetRelativeFilePath_can_extract_relative_path_correctly(string fileName, string dirName, string expected) + { + outputHelper.WriteLine($"{Environment.OSVersion.Platform} - Checking FileName: {fileName} -> {dirName} = {expected}"); - var fileInfo = new FileInfo(fileName); - var dirInfo = new DirectoryInfo(dirName); + var fileInfo = new FileInfo(fileName); + var dirInfo = new DirectoryInfo(dirName); - var result = fileInfo.GetRelativeFilePath(dirInfo); + var result = fileInfo.GetRelativeFilePath(dirInfo); - result.Should().Be(expected, $"{nameof(dirName)}: {dirName} - {nameof(fileName)}: {fileName}"); - } + result.Should().Be(expected, $"{nameof(dirName)}: {dirName} - {nameof(fileName)}: {fileName}"); + } - [Theory] - [MemberData(nameof(FileSizeData))] - public void GetFriendlyFileSize_given_a_fileSize_should_return_expected_text(long fileSize, string expected) - { - var result = FileInfoExtensions.GetFriendlyFileSize(fileSize); + [Theory] + [MemberData(nameof(FileSizeData))] + public void GetFriendlyFileSize_given_a_fileSize_should_return_expected_text(long fileSize, string expected) + { + var result = FileInfoExtensions.GetFriendlyFileSize(fileSize); - result.Should().Be(expected); - } + result.Should().Be(expected); + } - [Fact] - public void GetFriendlyFileSize_given_an_invalid_FileInfo_should_return_expected_text() - { - // Arrange - var fileInfo = (FileInfo)null; + [Fact] + public void GetFriendlyFileSize_given_an_invalid_FileInfo_should_return_expected_text() + { + // Arrange + var fileInfo = (FileInfo)null; - // Act - var result = FileInfoExtensions.GetFriendlyFileSize(fileInfo); + // Act + var result = FileInfoExtensions.GetFriendlyFileSize(fileInfo); - // Assert - result.Should().Be("0B"); - } + // Assert + result.Should().Be("0B"); + } - [Theory] - [MemberData(nameof(FileSizeData))] - public void GetFriendlyFileSize_given_a_valid_FileInfo_should_return_expected_text(long fileSize, string expected) - { - var fileName = Path.GetTempFileName(); - var fileInfo = new FileInfo(fileName); + [Theory] + [MemberData(nameof(FileSizeData))] + public void GetFriendlyFileSize_given_a_valid_FileInfo_should_return_expected_text(long fileSize, string expected) + { + var fileName = Path.GetTempFileName(); + var fileInfo = new FileInfo(fileName); - var data = new byte[fileSize]; + var data = new byte[fileSize]; - File.WriteAllBytes(fileInfo.FullName, data); + File.WriteAllBytes(fileInfo.FullName, data); - // Act - var result = FileInfoExtensions.GetFriendlyFileSize(fileInfo); + // Act + var result = FileInfoExtensions.GetFriendlyFileSize(fileInfo); - // Assert - result.Should().Be(expected); + // Assert + result.Should().Be(expected); - // Cleanup - fileInfo.Delete(); - } + // Cleanup + fileInfo.Delete(); + } - public static TheoryData FileSizeData() + public static TheoryData FileSizeData() + { + return new TheoryData { - return new TheoryData - { - { 0, "0B" }, - { 1000, "1000B" }, - { 1023, "1023B" }, - { 1024, "1KB" }, - { 1536, "1.5KB" }, - { 1792, "1.8KB" }, - { 2048, "2KB" }, - { 10240, "10KB" }, - { 102400, "100KB" }, - { 1024000, "1000KB" }, - { 1048500, "1023.9KB" }, - { 1048575, "1024KB" }, - { 1048576, "1MB" }, - { 2097152, "2MB" }, - { 10485760, "10MB" }, - }; - } + { 0, "0B" }, + { 1000, "1000B" }, + { 1023, "1023B" }, + { 1024, "1KB" }, + { 1536, "1.5KB" }, + { 1792, "1.8KB" }, + { 2048, "2KB" }, + { 10240, "10KB" }, + { 102400, "100KB" }, + { 1024000, "1000KB" }, + { 1048500, "1023.9KB" }, + { 1048575, "1024KB" }, + { 1048576, "1MB" }, + { 2097152, "2MB" }, + { 10485760, "10MB" }, + }; + } - public static TheoryData GetRelativeFileName_Data() + public static TheoryData GetRelativeFileName_Data() + { + return new TheoryData { - return new TheoryData - { - { Path.Combine(DriveRoot1, "Temp", "abcdefg", "file.txt"), Path.Combine(DriveRoot1, "Temp", "abcdefg"), "file.txt" }, - { Path.Combine(DriveRoot1, "Temp", "abcdefg", "dir3", "file1.tf"), Path.Combine(DriveRoot1, "Temp", "abcdefg"), Path.Combine("dir3", "file1.tf") }, - { Path.Combine(DriveRoot1, "Temp", "abcdefg", "dir3", "file1.tf"), Path.Combine(DriveRoot1, "Temp", "abcdefg", "dir3"), "file1.tf" }, - { Path.Combine(DriveRoot1, "Temp", "abcdefg", "dir3", "file1.tf"), Path.Combine(DriveRoot1, "Temp", "abcdefg", "dir3"), "file1.tf" }, - { Path.Combine(DriveRoot1, "Temp", "folder1", "file.txt"), Path.Combine(DriveRoot2, "folder2"), - Configuration.EnvironmentConfig.IsLinuxStyleFileSystem - ? Path.Combine(DriveRoot1, "Temp", "folder1", "file.txt").EnsureStartsWith(Path.Combine("..", "..")) - : Path.Combine(DriveRoot1, "Temp", "folder1", "file.txt") - }, - { Path.Combine(DriveRoot1, "Temp", "folder1", "file.txt"), Path.Combine(DriveRoot1, "Temp", "folder2"), Path.Combine("..", "folder1", "file.txt") }, - }; - } + { Path.Combine(DriveRoot1, "Temp", "abcdefg", "file.txt"), Path.Combine(DriveRoot1, "Temp", "abcdefg"), "file.txt" }, + { Path.Combine(DriveRoot1, "Temp", "abcdefg", "dir3", "file1.tf"), Path.Combine(DriveRoot1, "Temp", "abcdefg"), Path.Combine("dir3", "file1.tf") }, + { Path.Combine(DriveRoot1, "Temp", "abcdefg", "dir3", "file1.tf"), Path.Combine(DriveRoot1, "Temp", "abcdefg", "dir3"), "file1.tf" }, + { Path.Combine(DriveRoot1, "Temp", "abcdefg", "dir3", "file1.tf"), Path.Combine(DriveRoot1, "Temp", "abcdefg", "dir3"), "file1.tf" }, + { Path.Combine(DriveRoot1, "Temp", "folder1", "file.txt"), Path.Combine(DriveRoot2, "folder2"), + Configuration.EnvironmentConfig.IsLinuxStyleFileSystem + ? Path.Combine(DriveRoot1, "Temp", "folder1", "file.txt").EnsureStartsWith(Path.Combine("..", "..")) + : Path.Combine(DriveRoot1, "Temp", "folder1", "file.txt") + }, + { Path.Combine(DriveRoot1, "Temp", "folder1", "file.txt"), Path.Combine(DriveRoot1, "Temp", "folder2"), Path.Combine("..", "folder1", "file.txt") }, + }; + } - public static TheoryData GetRelativeFilePath_Data() + public static TheoryData GetRelativeFilePath_Data() + { + return new TheoryData { - return new TheoryData - { - { Path.Combine(DriveRoot1, "Temp", "abcdefg", "file.txt"), Path.Combine(DriveRoot1, "Temp", "abcdefg"), "" }, - { Path.Combine(DriveRoot1, "Temp", "abcdefg", "dir3", "file1.tf"), Path.Combine(DriveRoot1, "Temp", "abcdefg"), "dir3" }, - { Path.Combine(DriveRoot1, "Temp", "abcdefg", "dir3", "file1.tf"), Path.Combine(DriveRoot1, "Temp", "abcdefg", "dir3"), "" }, - { Path.Combine(DriveRoot1, "Temp", "folder1", "file.txt"), Path.Combine(DriveRoot1, "Temp", "folder2"), Path.Combine("..", "folder1") }, - { Path.Combine(DriveRoot1, "Temp", "folder1", "file.txt"), Path.Combine(DriveRoot2, "folder2"), - Configuration.EnvironmentConfig.IsLinuxStyleFileSystem - ? Path.Combine(DriveRoot1, "Temp", "folder1").EnsureStartsWith(Path.Combine("..", "..")) - : Path.Combine(DriveRoot1, "Temp", "folder1") - }, - }; - } + { Path.Combine(DriveRoot1, "Temp", "abcdefg", "file.txt"), Path.Combine(DriveRoot1, "Temp", "abcdefg"), "" }, + { Path.Combine(DriveRoot1, "Temp", "abcdefg", "dir3", "file1.tf"), Path.Combine(DriveRoot1, "Temp", "abcdefg"), "dir3" }, + { Path.Combine(DriveRoot1, "Temp", "abcdefg", "dir3", "file1.tf"), Path.Combine(DriveRoot1, "Temp", "abcdefg", "dir3"), "" }, + { Path.Combine(DriveRoot1, "Temp", "folder1", "file.txt"), Path.Combine(DriveRoot1, "Temp", "folder2"), Path.Combine("..", "folder1") }, + { Path.Combine(DriveRoot1, "Temp", "folder1", "file.txt"), Path.Combine(DriveRoot2, "folder2"), + Configuration.EnvironmentConfig.IsLinuxStyleFileSystem + ? Path.Combine(DriveRoot1, "Temp", "folder1").EnsureStartsWith(Path.Combine("..", "..")) + : Path.Combine(DriveRoot1, "Temp", "folder1") + }, + }; } -} +} \ No newline at end of file diff --git a/tests/DNX.Extensions.Tests/Linq/EnumerableExtensionsTests.cs b/tests/DNX.Extensions.Tests/Linq/EnumerableExtensionsTests.cs index 2a172df..8fea594 100644 --- a/tests/DNX.Extensions.Tests/Linq/EnumerableExtensionsTests.cs +++ b/tests/DNX.Extensions.Tests/Linq/EnumerableExtensionsTests.cs @@ -7,431 +7,430 @@ // ReSharper disable InconsistentNaming -namespace DNX.Extensions.Tests.Linq +namespace DNX.Extensions.Tests.Linq; + +internal enum OneToFive { - internal enum OneToFive - { - One, - Two, - Three, - Four, - Five - } + One, + Two, + Three, + Four, + Five +} - public class EnumerableExtensionsTests +public class EnumerableExtensionsTests +{ + public class HasAny { - public class HasAny + [Theory] + [InlineData("1,2,3,4,5")] + [InlineData("Dave,Bob,Steve")] + public void Populated_Enumerable_returns_successfully_as_True(string itemList) { - [Theory] - [InlineData("1,2,3,4,5")] - [InlineData("Dave,Bob,Steve")] - public void Populated_Enumerable_returns_successfully_as_True(string itemList) - { - // Arrange - var enumerable = itemList.Split(","); + // Arrange + var enumerable = itemList.Split(","); - // Act - var result = enumerable.HasAny(); + // Act + var result = enumerable.HasAny(); - // Assert - result.Should().BeTrue(); - } + // Assert + result.Should().BeTrue(); + } - [Fact] - public void Empty_Enumerable_returns_successfully_as_False() - { - // Arrange - var enumerable = new List(); + [Fact] + public void Empty_Enumerable_returns_successfully_as_False() + { + // Arrange + var enumerable = new List(); - // Act - var result = enumerable.HasAny(); + // Act + var result = enumerable.HasAny(); - // Assert - result.Should().BeFalse(); - } + // Assert + result.Should().BeFalse(); + } - [Fact] - public void Null_Enumerable_returns_successfully_as_False() - { - // Arrange - List enumerable = null; + [Fact] + public void Null_Enumerable_returns_successfully_as_False() + { + // Arrange + List enumerable = null; - // Act - var result = enumerable.HasAny(); + // Act + var result = enumerable.HasAny(); - // Assert - result.Should().BeFalse(); - } + // Assert + result.Should().BeFalse(); } + } - public class HasAny_Predicate + public class HasAny_Predicate + { + [Theory] + [InlineData("", "1", false)] + [InlineData(null, "1", false)] + [InlineData("a1,b2,c1,d2,e1,f2,g1,h2,i1,j2", "1", true)] + [InlineData("a1,b2,c1,d2,e1,f2,g1,h2,i1,j2", "2", true)] + [InlineData("a1,b2,c1,d2,e1,f2,g1,h2,i1,j2", "0", false)] + public void Test_HasAny_predicate(string commaDelimitedArray, string suffix, bool expectedResult) { - [Theory] - [InlineData("", "1", false)] - [InlineData(null, "1", false)] - [InlineData("a1,b2,c1,d2,e1,f2,g1,h2,i1,j2", "1", true)] - [InlineData("a1,b2,c1,d2,e1,f2,g1,h2,i1,j2", "2", true)] - [InlineData("a1,b2,c1,d2,e1,f2,g1,h2,i1,j2", "0", false)] - public void Test_HasAny_predicate(string commaDelimitedArray, string suffix, bool expectedResult) - { - var enumerable = commaDelimitedArray? + var enumerable = commaDelimitedArray? .Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries) - ; + ; - var result = enumerable.HasAny(s => s.EndsWith(suffix)); + var result = enumerable.HasAny(s => s.EndsWith(suffix)); - result.Should().Be(expectedResult); - } + result.Should().Be(expectedResult); } + } - public class IsOneOf_Tests + public class IsOneOf_Tests + { + [Theory] + [MemberData(nameof(IsOneOf_string_Data))] + public void IsOneOf_with_string_data_can_operate_as_expected(string[] list, string candidate, bool expectedResult) { - [Theory] - [MemberData(nameof(IsOneOf_string_Data))] - public void IsOneOf_with_string_data_can_operate_as_expected(string[] list, string candidate, bool expectedResult) - { - var result = candidate.IsOneOf(list); + var result = candidate.IsOneOf(list); - result.Should().Be(expectedResult); - } + result.Should().Be(expectedResult); + } - [Theory] - [MemberData(nameof(IsOneOf_string_comparer_Data))] - public void IsOneOf_with_string_data_and_comparer_can_operate_as_expected(string[] list, string candidate, IEqualityComparer comparer, bool expectedResult) - { - var result = candidate.IsOneOf(list, comparer); + [Theory] + [MemberData(nameof(IsOneOf_string_comparer_Data))] + public void IsOneOf_with_string_data_and_comparer_can_operate_as_expected(string[] list, string candidate, IEqualityComparer comparer, bool expectedResult) + { + var result = candidate.IsOneOf(list, comparer); - result.Should().Be(expectedResult); - } + result.Should().Be(expectedResult); + } - [Fact] - public void IsOneOf_with_params_can_operate_as_expected() - { - ((string)null).IsOneOf().Should().Be(false); - ((string)null).IsOneOf((string[])null).Should().Be(false); - "Hello".IsOneOf((string[])null).Should().BeFalse(); - "Hello".IsOneOf().Should().Be(false); - "3".IsOneOf("1", "2", "3", "4", "5").Should().Be(true); - "6".IsOneOf("1", "2", "3", "4", "5").Should().Be(false); - "One".IsOneOf("One", "Two", "Three", "Four", "Five").Should().Be(true); - "Two".IsOneOf("One", "Two", "Three", "Four", "Five").Should().Be(true); - "Three".IsOneOf("One", "Two", "Three", "Four", "Five").Should().Be(true); - "Four".IsOneOf("One", "Two", "Three", "Four", "Five").Should().Be(true); - "Five".IsOneOf("One", "Two", "Three", "Four", "Five").Should().Be(true); - "five".IsOneOf("One", "Two", "Three", "Four", "Five").Should().Be(false); - "FIVE".IsOneOf("One", "Two", "Three", "Four", "Five").Should().Be(false); - "Six".IsOneOf("One", "Two", "Three", "Four", "Five").Should().Be(false); - 1.IsOneOf(1, 2, 3, 4, 5).Should().Be(true); - 3.IsOneOf(1, 2, 3, 4, 5).Should().Be(true); - 5.IsOneOf(1, 2, 3, 4, 5).Should().Be(true); - 6.IsOneOf(1, 2, 3, 4, 5).Should().Be(false); - OneToFive.One.IsOneOf(OneToFive.One, OneToFive.Three, OneToFive.Five).Should().Be(true); - OneToFive.Three.IsOneOf(OneToFive.One, OneToFive.Three, OneToFive.Five).Should().Be(true); - OneToFive.Five.IsOneOf(OneToFive.One, OneToFive.Three, OneToFive.Five).Should().Be(true); - OneToFive.Two.IsOneOf(OneToFive.One, OneToFive.Three, OneToFive.Five).Should().Be(false); - ((OneToFive)100).IsOneOf(OneToFive.One, OneToFive.Three, OneToFive.Five).Should().Be(false); - } + [Fact] + public void IsOneOf_with_params_can_operate_as_expected() + { + ((string)null).IsOneOf().Should().Be(false); + ((string)null).IsOneOf((string[])null).Should().Be(false); + "Hello".IsOneOf((string[])null).Should().BeFalse(); + "Hello".IsOneOf().Should().Be(false); + "3".IsOneOf("1", "2", "3", "4", "5").Should().Be(true); + "6".IsOneOf("1", "2", "3", "4", "5").Should().Be(false); + "One".IsOneOf("One", "Two", "Three", "Four", "Five").Should().Be(true); + "Two".IsOneOf("One", "Two", "Three", "Four", "Five").Should().Be(true); + "Three".IsOneOf("One", "Two", "Three", "Four", "Five").Should().Be(true); + "Four".IsOneOf("One", "Two", "Three", "Four", "Five").Should().Be(true); + "Five".IsOneOf("One", "Two", "Three", "Four", "Five").Should().Be(true); + "five".IsOneOf("One", "Two", "Three", "Four", "Five").Should().Be(false); + "FIVE".IsOneOf("One", "Two", "Three", "Four", "Five").Should().Be(false); + "Six".IsOneOf("One", "Two", "Three", "Four", "Five").Should().Be(false); + 1.IsOneOf(1, 2, 3, 4, 5).Should().Be(true); + 3.IsOneOf(1, 2, 3, 4, 5).Should().Be(true); + 5.IsOneOf(1, 2, 3, 4, 5).Should().Be(true); + 6.IsOneOf(1, 2, 3, 4, 5).Should().Be(false); + OneToFive.One.IsOneOf(OneToFive.One, OneToFive.Three, OneToFive.Five).Should().Be(true); + OneToFive.Three.IsOneOf(OneToFive.One, OneToFive.Three, OneToFive.Five).Should().Be(true); + OneToFive.Five.IsOneOf(OneToFive.One, OneToFive.Three, OneToFive.Five).Should().Be(true); + OneToFive.Two.IsOneOf(OneToFive.One, OneToFive.Three, OneToFive.Five).Should().Be(false); + ((OneToFive)100).IsOneOf(OneToFive.One, OneToFive.Three, OneToFive.Five).Should().Be(false); + } - [Fact] - public void IsOneOf_with_params_and_Comparer_can_operate_as_expected() - { - var comparer = StringComparer.FromComparison(StringComparison.CurrentCulture); - ((string)null).IsOneOf(comparer).Should().Be(false); - ((string)null).IsOneOf(comparer, null).Should().Be(false); - "Hello".IsOneOf((string[])null).Should().BeFalse(); - "Hello".IsOneOf().Should().Be(false); - "Hello".IsOneOf(comparer).Should().Be(false); - "3".IsOneOf(comparer, "1", "2", "3", "4", "5").Should().Be(true); - "6".IsOneOf(comparer, "1", "2", "3", "4", "5").Should().Be(false); - "One".IsOneOf(comparer, "One", "Two", "Three", "Four", "Five").Should().Be(true); - "Two".IsOneOf(comparer, "One", "Two", "Three", "Four", "Five").Should().Be(true); - "Three".IsOneOf(comparer, "One", "Two", "Three", "Four", "Five").Should().Be(true); - "Four".IsOneOf(comparer, "One", "Two", "Three", "Four", "Five").Should().Be(true); - "Five".IsOneOf(comparer, "One", "Two", "Three", "Four", "Five").Should().Be(true); - "five".IsOneOf(comparer, "One", "Two", "Three", "Four", "Five").Should().Be(false); - "FIVE".IsOneOf(comparer, "One", "Two", "Three", "Four", "Five").Should().Be(false); - "Six".IsOneOf(comparer, "One", "Two", "Three", "Four", "Five").Should().Be(false); - - comparer = StringComparer.FromComparison(StringComparison.CurrentCultureIgnoreCase); - "Hello".IsOneOf(comparer).Should().Be(false); - "3".IsOneOf(comparer, "1", "2", "3", "4", "5").Should().Be(true); - "6".IsOneOf(comparer, "1", "2", "3", "4", "5").Should().Be(false); - "One".IsOneOf(comparer, "One", "Two", "Three", "Four", "Five").Should().Be(true); - "Two".IsOneOf(comparer, "One", "Two", "Three", "Four", "Five").Should().Be(true); - "Three".IsOneOf(comparer, "One", "Two", "Three", "Four", "Five").Should().Be(true); - "Four".IsOneOf(comparer, "One", "Two", "Three", "Four", "Five").Should().Be(true); - "Five".IsOneOf(comparer, "One", "Two", "Three", "Four", "Five").Should().Be(true); - "five".IsOneOf(comparer, "One", "Two", "Three", "Four", "Five").Should().Be(true); - "FIVE".IsOneOf(comparer, "One", "Two", "Three", "Four", "Five").Should().Be(true); - "Six".IsOneOf(comparer, "One", "Two", "Three", "Four", "Five").Should().Be(false); - } + [Fact] + public void IsOneOf_with_params_and_Comparer_can_operate_as_expected() + { + var comparer = StringComparer.FromComparison(StringComparison.CurrentCulture); + ((string)null).IsOneOf(comparer).Should().Be(false); + ((string)null).IsOneOf(comparer, null).Should().Be(false); + "Hello".IsOneOf((string[])null).Should().BeFalse(); + "Hello".IsOneOf().Should().Be(false); + "Hello".IsOneOf(comparer).Should().Be(false); + "3".IsOneOf(comparer, "1", "2", "3", "4", "5").Should().Be(true); + "6".IsOneOf(comparer, "1", "2", "3", "4", "5").Should().Be(false); + "One".IsOneOf(comparer, "One", "Two", "Three", "Four", "Five").Should().Be(true); + "Two".IsOneOf(comparer, "One", "Two", "Three", "Four", "Five").Should().Be(true); + "Three".IsOneOf(comparer, "One", "Two", "Three", "Four", "Five").Should().Be(true); + "Four".IsOneOf(comparer, "One", "Two", "Three", "Four", "Five").Should().Be(true); + "Five".IsOneOf(comparer, "One", "Two", "Three", "Four", "Five").Should().Be(true); + "five".IsOneOf(comparer, "One", "Two", "Three", "Four", "Five").Should().Be(false); + "FIVE".IsOneOf(comparer, "One", "Two", "Three", "Four", "Five").Should().Be(false); + "Six".IsOneOf(comparer, "One", "Two", "Three", "Four", "Five").Should().Be(false); + + comparer = StringComparer.FromComparison(StringComparison.CurrentCultureIgnoreCase); + "Hello".IsOneOf(comparer).Should().Be(false); + "3".IsOneOf(comparer, "1", "2", "3", "4", "5").Should().Be(true); + "6".IsOneOf(comparer, "1", "2", "3", "4", "5").Should().Be(false); + "One".IsOneOf(comparer, "One", "Two", "Three", "Four", "Five").Should().Be(true); + "Two".IsOneOf(comparer, "One", "Two", "Three", "Four", "Five").Should().Be(true); + "Three".IsOneOf(comparer, "One", "Two", "Three", "Four", "Five").Should().Be(true); + "Four".IsOneOf(comparer, "One", "Two", "Three", "Four", "Five").Should().Be(true); + "Five".IsOneOf(comparer, "One", "Two", "Three", "Four", "Five").Should().Be(true); + "five".IsOneOf(comparer, "One", "Two", "Three", "Four", "Five").Should().Be(true); + "FIVE".IsOneOf(comparer, "One", "Two", "Three", "Four", "Five").Should().Be(true); + "Six".IsOneOf(comparer, "One", "Two", "Three", "Four", "Five").Should().Be(false); + } - #region Test Data + #region Test Data - public static TheoryData IsOneOf_string_Data() + public static TheoryData IsOneOf_string_Data() + { + return new TheoryData { - return new TheoryData - { - { null, "3", false }, - { Enumerable.Empty().ToArray(), "3", false }, - { "1,2,3,4,5".Split(','), "3", true }, - { "1,2,3,4,5".Split(','), "6", false }, - { "One,Two,Three,Four,Five".Split(','), "One", true }, - { "One,Two,Three,Four,Five".Split(','), "Two", true }, - { "One,Two,Three,Four,Five".Split(','), "Three", true }, - { "One,Two,Three,Four,Five".Split(','), "Four", true }, - { "One,Two,Three,Four,Five".Split(','), "Five", true }, - { "One,Two,Three,Four,Five".Split(','), "five", false }, - { "One,Two,Three,Four,Five".Split(','), "Six", false }, - }; - } + { null, "3", false }, + { Enumerable.Empty().ToArray(), "3", false }, + { "1,2,3,4,5".Split(','), "3", true }, + { "1,2,3,4,5".Split(','), "6", false }, + { "One,Two,Three,Four,Five".Split(','), "One", true }, + { "One,Two,Three,Four,Five".Split(','), "Two", true }, + { "One,Two,Three,Four,Five".Split(','), "Three", true }, + { "One,Two,Three,Four,Five".Split(','), "Four", true }, + { "One,Two,Three,Four,Five".Split(','), "Five", true }, + { "One,Two,Three,Four,Five".Split(','), "five", false }, + { "One,Two,Three,Four,Five".Split(','), "Six", false }, + }; + } - public static TheoryData, bool> IsOneOf_string_comparer_Data() + public static TheoryData, bool> IsOneOf_string_comparer_Data() + { + return new TheoryData, bool> { - return new TheoryData, bool> - { - { "1,2,3,4,5".Split(','), "3", StringComparer.FromComparison(StringComparison.CurrentCultureIgnoreCase), true }, - { "1,2,3,4,5".Split(','), "6", StringComparer.FromComparison(StringComparison.CurrentCulture), false }, - { "1,2,3,4,5".Split(','), "3", StringComparer.FromComparison(StringComparison.CurrentCultureIgnoreCase), true }, - { "1,2,3,4,5".Split(','), "6", StringComparer.FromComparison(StringComparison.CurrentCulture), false }, - - { "One,Two,Three,Four,Five".Split(','), "One", StringComparer.FromComparison(StringComparison.CurrentCultureIgnoreCase), true }, - { "One,Two,Three,Four,Five".Split(','), "Two", StringComparer.FromComparison(StringComparison.CurrentCultureIgnoreCase), true }, - { "One,Two,Three,Four,Five".Split(','), "Three", StringComparer.FromComparison(StringComparison.CurrentCultureIgnoreCase), true }, - { "One,Two,Three,Four,Five".Split(','), "Four", StringComparer.FromComparison(StringComparison.CurrentCultureIgnoreCase), true }, - { "One,Two,Three,Four,Five".Split(','), "Five", StringComparer.FromComparison(StringComparison.CurrentCultureIgnoreCase), true }, - { "One,Two,Three,Four,Five".Split(','), "five", StringComparer.FromComparison(StringComparison.CurrentCultureIgnoreCase), true }, - { "One,Two,Three,Four,Five".Split(','), "Six", StringComparer.FromComparison(StringComparison.CurrentCultureIgnoreCase), false }, - { "One,Two,Three,Four,Five".Split(','), "One", StringComparer.FromComparison(StringComparison.CurrentCulture), true }, - { "One,Two,Three,Four,Five".Split(','), "Two", StringComparer.FromComparison(StringComparison.CurrentCulture), true }, - { "One,Two,Three,Four,Five".Split(','), "Three", StringComparer.FromComparison(StringComparison.CurrentCulture), true }, - { "One,Two,Three,Four,Five".Split(','), "Four", StringComparer.FromComparison(StringComparison.CurrentCulture), true }, - { "One,Two,Three,Four,Five".Split(','), "Five", StringComparer.FromComparison(StringComparison.CurrentCulture), true }, - { "One,Two,Three,Four,Five".Split(','), "five", StringComparer.FromComparison(StringComparison.CurrentCulture), false }, - { "One,Two,Three,Four,Five".Split(','), "Six", StringComparer.FromComparison(StringComparison.CurrentCulture), false }, - - { "One,Two,Three,Four,Five".Split(','), "One", StringComparer.FromComparison(StringComparison.OrdinalIgnoreCase), true }, - { "One,Two,Three,Four,Five".Split(','), "Two", StringComparer.FromComparison(StringComparison.OrdinalIgnoreCase), true }, - { "One,Two,Three,Four,Five".Split(','), "Three", StringComparer.FromComparison(StringComparison.OrdinalIgnoreCase), true }, - { "One,Two,Three,Four,Five".Split(','), "Four", StringComparer.FromComparison(StringComparison.OrdinalIgnoreCase), true }, - { "One,Two,Three,Four,Five".Split(','), "Five", StringComparer.FromComparison(StringComparison.OrdinalIgnoreCase), true }, - { "One,Two,Three,Four,Five".Split(','), "five", StringComparer.FromComparison(StringComparison.OrdinalIgnoreCase), true }, - { "One,Two,Three,Four,Five".Split(','), "Six", StringComparer.FromComparison(StringComparison.OrdinalIgnoreCase), false }, - { "One,Two,Three,Four,Five".Split(','), "One", StringComparer.FromComparison(StringComparison.Ordinal), true }, - { "One,Two,Three,Four,Five".Split(','), "Two", StringComparer.FromComparison(StringComparison.Ordinal), true }, - { "One,Two,Three,Four,Five".Split(','), "Three", StringComparer.FromComparison(StringComparison.Ordinal), true }, - { "One,Two,Three,Four,Five".Split(','), "Four", StringComparer.FromComparison(StringComparison.Ordinal), true }, - { "One,Two,Three,Four,Five".Split(','), "Five", StringComparer.FromComparison(StringComparison.Ordinal), true }, - { "One,Two,Three,Four,Five".Split(','), "five", StringComparer.FromComparison(StringComparison.Ordinal), false }, - { "One,Two,Three,Four,Five".Split(','), "Six", StringComparer.FromComparison(StringComparison.Ordinal), false }, - - { "One,Two,Three,Four,Five".Split(','), "One", StringComparer.FromComparison(StringComparison.InvariantCultureIgnoreCase), true }, - { "One,Two,Three,Four,Five".Split(','), "Two", StringComparer.FromComparison(StringComparison.InvariantCultureIgnoreCase), true }, - { "One,Two,Three,Four,Five".Split(','), "Three", StringComparer.FromComparison(StringComparison.InvariantCultureIgnoreCase), true }, - { "One,Two,Three,Four,Five".Split(','), "Four", StringComparer.FromComparison(StringComparison.InvariantCultureIgnoreCase), true }, - { "One,Two,Three,Four,Five".Split(','), "Five", StringComparer.FromComparison(StringComparison.InvariantCultureIgnoreCase), true }, - { "One,Two,Three,Four,Five".Split(','), "five", StringComparer.FromComparison(StringComparison.InvariantCultureIgnoreCase), true }, - { "One,Two,Three,Four,Five".Split(','), "Six", StringComparer.FromComparison(StringComparison.InvariantCultureIgnoreCase), false }, - { "One,Two,Three,Four,Five".Split(','), "One", StringComparer.FromComparison(StringComparison.InvariantCulture), true }, - { "One,Two,Three,Four,Five".Split(','), "Two", StringComparer.FromComparison(StringComparison.InvariantCulture), true }, - { "One,Two,Three,Four,Five".Split(','), "Three", StringComparer.FromComparison(StringComparison.InvariantCulture), true }, - { "One,Two,Three,Four,Five".Split(','), "Four", StringComparer.FromComparison(StringComparison.InvariantCulture), true }, - { "One,Two,Three,Four,Five".Split(','), "Five", StringComparer.FromComparison(StringComparison.InvariantCulture), true }, - { "One,Two,Three,Four,Five".Split(','), "five", StringComparer.FromComparison(StringComparison.InvariantCulture), false }, - { "One,Two,Three,Four,Five".Split(','), "Six", StringComparer.FromComparison(StringComparison.InvariantCulture), false }, - }; - } - - #endregion + { "1,2,3,4,5".Split(','), "3", StringComparer.FromComparison(StringComparison.CurrentCultureIgnoreCase), true }, + { "1,2,3,4,5".Split(','), "6", StringComparer.FromComparison(StringComparison.CurrentCulture), false }, + { "1,2,3,4,5".Split(','), "3", StringComparer.FromComparison(StringComparison.CurrentCultureIgnoreCase), true }, + { "1,2,3,4,5".Split(','), "6", StringComparer.FromComparison(StringComparison.CurrentCulture), false }, + + { "One,Two,Three,Four,Five".Split(','), "One", StringComparer.FromComparison(StringComparison.CurrentCultureIgnoreCase), true }, + { "One,Two,Three,Four,Five".Split(','), "Two", StringComparer.FromComparison(StringComparison.CurrentCultureIgnoreCase), true }, + { "One,Two,Three,Four,Five".Split(','), "Three", StringComparer.FromComparison(StringComparison.CurrentCultureIgnoreCase), true }, + { "One,Two,Three,Four,Five".Split(','), "Four", StringComparer.FromComparison(StringComparison.CurrentCultureIgnoreCase), true }, + { "One,Two,Three,Four,Five".Split(','), "Five", StringComparer.FromComparison(StringComparison.CurrentCultureIgnoreCase), true }, + { "One,Two,Three,Four,Five".Split(','), "five", StringComparer.FromComparison(StringComparison.CurrentCultureIgnoreCase), true }, + { "One,Two,Three,Four,Five".Split(','), "Six", StringComparer.FromComparison(StringComparison.CurrentCultureIgnoreCase), false }, + { "One,Two,Three,Four,Five".Split(','), "One", StringComparer.FromComparison(StringComparison.CurrentCulture), true }, + { "One,Two,Three,Four,Five".Split(','), "Two", StringComparer.FromComparison(StringComparison.CurrentCulture), true }, + { "One,Two,Three,Four,Five".Split(','), "Three", StringComparer.FromComparison(StringComparison.CurrentCulture), true }, + { "One,Two,Three,Four,Five".Split(','), "Four", StringComparer.FromComparison(StringComparison.CurrentCulture), true }, + { "One,Two,Three,Four,Five".Split(','), "Five", StringComparer.FromComparison(StringComparison.CurrentCulture), true }, + { "One,Two,Three,Four,Five".Split(','), "five", StringComparer.FromComparison(StringComparison.CurrentCulture), false }, + { "One,Two,Three,Four,Five".Split(','), "Six", StringComparer.FromComparison(StringComparison.CurrentCulture), false }, + + { "One,Two,Three,Four,Five".Split(','), "One", StringComparer.FromComparison(StringComparison.OrdinalIgnoreCase), true }, + { "One,Two,Three,Four,Five".Split(','), "Two", StringComparer.FromComparison(StringComparison.OrdinalIgnoreCase), true }, + { "One,Two,Three,Four,Five".Split(','), "Three", StringComparer.FromComparison(StringComparison.OrdinalIgnoreCase), true }, + { "One,Two,Three,Four,Five".Split(','), "Four", StringComparer.FromComparison(StringComparison.OrdinalIgnoreCase), true }, + { "One,Two,Three,Four,Five".Split(','), "Five", StringComparer.FromComparison(StringComparison.OrdinalIgnoreCase), true }, + { "One,Two,Three,Four,Five".Split(','), "five", StringComparer.FromComparison(StringComparison.OrdinalIgnoreCase), true }, + { "One,Two,Three,Four,Five".Split(','), "Six", StringComparer.FromComparison(StringComparison.OrdinalIgnoreCase), false }, + { "One,Two,Three,Four,Five".Split(','), "One", StringComparer.FromComparison(StringComparison.Ordinal), true }, + { "One,Two,Three,Four,Five".Split(','), "Two", StringComparer.FromComparison(StringComparison.Ordinal), true }, + { "One,Two,Three,Four,Five".Split(','), "Three", StringComparer.FromComparison(StringComparison.Ordinal), true }, + { "One,Two,Three,Four,Five".Split(','), "Four", StringComparer.FromComparison(StringComparison.Ordinal), true }, + { "One,Two,Three,Four,Five".Split(','), "Five", StringComparer.FromComparison(StringComparison.Ordinal), true }, + { "One,Two,Three,Four,Five".Split(','), "five", StringComparer.FromComparison(StringComparison.Ordinal), false }, + { "One,Two,Three,Four,Five".Split(','), "Six", StringComparer.FromComparison(StringComparison.Ordinal), false }, + + { "One,Two,Three,Four,Five".Split(','), "One", StringComparer.FromComparison(StringComparison.InvariantCultureIgnoreCase), true }, + { "One,Two,Three,Four,Five".Split(','), "Two", StringComparer.FromComparison(StringComparison.InvariantCultureIgnoreCase), true }, + { "One,Two,Three,Four,Five".Split(','), "Three", StringComparer.FromComparison(StringComparison.InvariantCultureIgnoreCase), true }, + { "One,Two,Three,Four,Five".Split(','), "Four", StringComparer.FromComparison(StringComparison.InvariantCultureIgnoreCase), true }, + { "One,Two,Three,Four,Five".Split(','), "Five", StringComparer.FromComparison(StringComparison.InvariantCultureIgnoreCase), true }, + { "One,Two,Three,Four,Five".Split(','), "five", StringComparer.FromComparison(StringComparison.InvariantCultureIgnoreCase), true }, + { "One,Two,Three,Four,Five".Split(','), "Six", StringComparer.FromComparison(StringComparison.InvariantCultureIgnoreCase), false }, + { "One,Two,Three,Four,Five".Split(','), "One", StringComparer.FromComparison(StringComparison.InvariantCulture), true }, + { "One,Two,Three,Four,Five".Split(','), "Two", StringComparer.FromComparison(StringComparison.InvariantCulture), true }, + { "One,Two,Three,Four,Five".Split(','), "Three", StringComparer.FromComparison(StringComparison.InvariantCulture), true }, + { "One,Two,Three,Four,Five".Split(','), "Four", StringComparer.FromComparison(StringComparison.InvariantCulture), true }, + { "One,Two,Three,Four,Five".Split(','), "Five", StringComparer.FromComparison(StringComparison.InvariantCulture), true }, + { "One,Two,Three,Four,Five".Split(','), "five", StringComparer.FromComparison(StringComparison.InvariantCulture), false }, + { "One,Two,Three,Four,Five".Split(','), "Six", StringComparer.FromComparison(StringComparison.InvariantCulture), false }, + }; } - public class GetRandomItem(ITestOutputHelper testOutputHelper) + #endregion + } + + public class GetRandomItem(ITestOutputHelper testOutputHelper) + { + [Theory] + [InlineData("1,2,3,4,5", 10000)] + [InlineData("A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z", 10000)] + [InlineData("1,2", 100)] + [InlineData("1", 100)] + public void Populated_Enumerable_repeatedly_returns_an_item_successfully(string itemList, int count) { - [Theory] - [InlineData("1,2,3,4,5", 10000)] - [InlineData("A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z", 10000)] - [InlineData("1,2", 100)] - [InlineData("1", 100)] - public void Populated_Enumerable_repeatedly_returns_an_item_successfully(string itemList, int count) - { - // Arrange - var enumerable = itemList.Split(",", StringSplitOptions.RemoveEmptyEntries); + // Arrange + var enumerable = itemList.Split(",", StringSplitOptions.RemoveEmptyEntries); - var hitCounts = enumerable - .ToDictionary(x => x, x => 0); + var hitCounts = enumerable + .ToDictionary(x => x, x => 0); - Enumerable.Range(1, count) - .ToList() - .ForEach(x => - { - // Act - var result = enumerable.GetRandomItem(); + Enumerable.Range(1, count) + .ToList() + .ForEach(x => + { + // Act + var result = enumerable.GetRandomItem(); - hitCounts[result]++; + hitCounts[result]++; - // Assert - result.Should().NotBeNullOrEmpty(); - }); + // Assert + result.Should().NotBeNullOrEmpty(); + }); - // Assert - foreach (var kvp in hitCounts) - { - testOutputHelper.WriteLine("HitCount [{0}]: {1}", kvp.Key, kvp.Value); - kvp.Value.Should().BeGreaterThan(0); - } + // Assert + foreach (var kvp in hitCounts) + { + testOutputHelper.WriteLine("HitCount [{0}]: {1}", kvp.Key, kvp.Value); + kvp.Value.Should().BeGreaterThan(0); } + } - [Fact] - public void Empty_string_Enumerable_returns_default() - { - // Arrange - var enumerable = Enumerable.Empty(); + [Fact] + public void Empty_string_Enumerable_returns_default() + { + // Arrange + var enumerable = Enumerable.Empty(); - // Act - var result = enumerable.GetRandomItem(); + // Act + var result = enumerable.GetRandomItem(); - // Assert - result.Should().BeNull(); - } + // Assert + result.Should().BeNull(); + } - [Fact] - public void Null_string_Enumerable_returns_default() - { - // Arrange - IEnumerable enumerable = null; + [Fact] + public void Null_string_Enumerable_returns_default() + { + // Arrange + IEnumerable enumerable = null; - // Act - var result = enumerable.GetRandomItem(); + // Act + var result = enumerable.GetRandomItem(); - // Assert - result.Should().BeNull(); - } + // Assert + result.Should().BeNull(); + } - [Fact] - public void Empty_int_Enumerable_returns_default() - { - // Arrange - var enumerable = Enumerable.Empty(); + [Fact] + public void Empty_int_Enumerable_returns_default() + { + // Arrange + var enumerable = Enumerable.Empty(); - // Act - var result = enumerable.GetRandomItem(); + // Act + var result = enumerable.GetRandomItem(); - // Assert - result.Should().Be(default); - } + // Assert + result.Should().Be(default); + } - [Fact] - public void Null_int_Enumerable_returns_default() - { - // Arrange - IEnumerable enumerable = null; + [Fact] + public void Null_int_Enumerable_returns_default() + { + // Arrange + IEnumerable enumerable = null; - // Act - var result = enumerable.GetRandomItem(); + // Act + var result = enumerable.GetRandomItem(); - // Assert - result.Should().Be(default); - } + // Assert + result.Should().Be(default); } + } - public class GetAt + public class GetAt + { + [Theory] + [InlineData("1,2,3,4,5", 0, "1")] + [InlineData("1,2,3,4,5", 2, "3")] + [InlineData("1,2,3,4,5", 4, "5")] + [InlineData("1,2,3,4,5", -1, null)] + [InlineData("1,2,3,4,5", 5, null)] + [InlineData("1,2,3,4,5", 6, null)] + [InlineData("1,2,3,4,5", 100, null)] + [InlineData("1,2,3,4,5", -100, null)] + public void Populated_List_repeatedly_returns_an_item_successfully(string itemList, int index, string expected) { - [Theory] - [InlineData("1,2,3,4,5", 0, "1")] - [InlineData("1,2,3,4,5", 2, "3")] - [InlineData("1,2,3,4,5", 4, "5")] - [InlineData("1,2,3,4,5", -1, null)] - [InlineData("1,2,3,4,5", 5, null)] - [InlineData("1,2,3,4,5", 6, null)] - [InlineData("1,2,3,4,5", 100, null)] - [InlineData("1,2,3,4,5", -100, null)] - public void Populated_List_repeatedly_returns_an_item_successfully(string itemList, int index, string expected) - { - // Arrange - var items = itemList.Split(",", StringSplitOptions.RemoveEmptyEntries) - .ToList(); + // Arrange + var items = itemList.Split(",", StringSplitOptions.RemoveEmptyEntries) + .ToList(); - // Act - var result = items.GetAt(index); + // Act + var result = items.GetAt(index); - // Assert - result.Should().Be(expected); - } + // Assert + result.Should().Be(expected); + } - [Theory] - [InlineData("1,2,3,4,5", 0, "1")] - [InlineData("1,2,3,4,5", 2, "3")] - [InlineData("1,2,3,4,5", 4, "5")] - [InlineData("1,2,3,4,5", -1, null)] - [InlineData("1,2,3,4,5", 5, null)] - [InlineData("1,2,3,4,5", 6, null)] - [InlineData("1,2,3,4,5", 100, null)] - [InlineData("1,2,3,4,5", -100, null)] - public void Populated_Array_repeatedly_returns_an_item_successfully(string itemList, int index, string expected) - { - // Arrange - var items = itemList.Split(",", StringSplitOptions.RemoveEmptyEntries) - .ToArray(); + [Theory] + [InlineData("1,2,3,4,5", 0, "1")] + [InlineData("1,2,3,4,5", 2, "3")] + [InlineData("1,2,3,4,5", 4, "5")] + [InlineData("1,2,3,4,5", -1, null)] + [InlineData("1,2,3,4,5", 5, null)] + [InlineData("1,2,3,4,5", 6, null)] + [InlineData("1,2,3,4,5", 100, null)] + [InlineData("1,2,3,4,5", -100, null)] + public void Populated_Array_repeatedly_returns_an_item_successfully(string itemList, int index, string expected) + { + // Arrange + var items = itemList.Split(",", StringSplitOptions.RemoveEmptyEntries) + .ToArray(); - // Act - var result = items.GetAt(index); + // Act + var result = items.GetAt(index); - // Assert - result.Should().Be(expected); - } + // Assert + result.Should().Be(expected); + } - [Fact] - public void Empty_IList_returns_default() - { - // Arrange - var items = Enumerable.Empty() - .ToList(); + [Fact] + public void Empty_IList_returns_default() + { + // Arrange + var items = Enumerable.Empty() + .ToList(); - // Act - var result = items.GetAt(0); + // Act + var result = items.GetAt(0); - // Assert - result.Should().BeNull(); - } + // Assert + result.Should().BeNull(); + } - [Fact] - public void Empty_Array_returns_default() - { - // Arrange - var items = Array.Empty(); + [Fact] + public void Empty_Array_returns_default() + { + // Arrange + var items = Array.Empty(); - // Act - var result = items.GetAt(0); + // Act + var result = items.GetAt(0); - // Assert - result.Should().BeNull(); - } + // Assert + result.Should().BeNull(); + } - [Fact] - public void Null_IList_returns_default() - { - // Arrange - List items = null; + [Fact] + public void Null_IList_returns_default() + { + // Arrange + List items = null; - // Act - var result = items.GetAt(0); + // Act + var result = items.GetAt(0); - // Assert - result.Should().BeNull(); - } + // Assert + result.Should().BeNull(); + } - [Fact] - public void Null_Array_returns_default() - { - // Arrange - string[] items = null; + [Fact] + public void Null_Array_returns_default() + { + // Arrange + string[] items = null; - // Act - var result = items.GetAt(0); + // Act + var result = items.GetAt(0); - // Assert - result.Should().BeNull(); - } + // Assert + result.Should().BeNull(); } } -} +} \ No newline at end of file