diff --git a/StevenVolckaert.EnterpriseLibrary.sln b/StevenVolckaert.EnterpriseLibrary.sln index 8d9694f..1857e07 100644 --- a/StevenVolckaert.EnterpriseLibrary.sln +++ b/StevenVolckaert.EnterpriseLibrary.sln @@ -1,7 +1,7 @@  Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 15 -VisualStudioVersion = 15.0.26228.9 +VisualStudioVersion = 15.0.27004.2009 MinimumVisualStudioVersion = 10.0.40219.1 Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution items", "Solution items", "{00269925-9ABB-475F-A63B-B57C55E26CD3}" ProjectSection(SolutionItems) = preProject @@ -18,7 +18,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{54EC9438-05E EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StevenVolckaert.Core", "src\StevenVolckaert.Core\StevenVolckaert.Core.csproj", "{9493EB60-BD91-426A-BDF1-4390456046E1}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StevenVolckaert.Core.Tests", "test\StevenVolckaert.Core.Tests\StevenVolckaert.Core.Tests.csproj", "{B2222404-4375-458C-9F04-C5F1D279CBA6}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StevenVolckaert.Core.Tests", "test\StevenVolckaert.Core.Tests\StevenVolckaert.Core.Tests.csproj", "{F6EAF516-F648-48C8-93EE-EC532AD996CA}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -30,16 +30,19 @@ Global {9493EB60-BD91-426A-BDF1-4390456046E1}.Debug|Any CPU.Build.0 = Debug|Any CPU {9493EB60-BD91-426A-BDF1-4390456046E1}.Release|Any CPU.ActiveCfg = Release|Any CPU {9493EB60-BD91-426A-BDF1-4390456046E1}.Release|Any CPU.Build.0 = Release|Any CPU - {B2222404-4375-458C-9F04-C5F1D279CBA6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {B2222404-4375-458C-9F04-C5F1D279CBA6}.Debug|Any CPU.Build.0 = Debug|Any CPU - {B2222404-4375-458C-9F04-C5F1D279CBA6}.Release|Any CPU.ActiveCfg = Release|Any CPU - {B2222404-4375-458C-9F04-C5F1D279CBA6}.Release|Any CPU.Build.0 = Release|Any CPU + {F6EAF516-F648-48C8-93EE-EC532AD996CA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {F6EAF516-F648-48C8-93EE-EC532AD996CA}.Debug|Any CPU.Build.0 = Debug|Any CPU + {F6EAF516-F648-48C8-93EE-EC532AD996CA}.Release|Any CPU.ActiveCfg = Release|Any CPU + {F6EAF516-F648-48C8-93EE-EC532AD996CA}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection GlobalSection(NestedProjects) = preSolution {9493EB60-BD91-426A-BDF1-4390456046E1} = {54EC9438-05ED-4ACD-AEF6-B31B353069EA} - {B2222404-4375-458C-9F04-C5F1D279CBA6} = {887100A5-003F-4148-85D8-9D9C9E7E5125} + {F6EAF516-F648-48C8-93EE-EC532AD996CA} = {887100A5-003F-4148-85D8-9D9C9E7E5125} + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {C23D0273-EEBA-4B5A-8DBE-8C742A67738E} EndGlobalSection EndGlobal diff --git a/src/StevenVolckaert.Core/IEnumerableExtensions.cs b/src/StevenVolckaert.Core/IEnumerableExtensions.cs index 4b83f37..467c2a1 100644 --- a/src/StevenVolckaert.Core/IEnumerableExtensions.cs +++ b/src/StevenVolckaert.Core/IEnumerableExtensions.cs @@ -69,7 +69,7 @@ public static IEnumerable Prepend(this IEnumerable source, T element) /// Determines whether a sequence contains no elements. /// /// The type of the elements of . - /// The instance to check for emptiness. + /// The instance to check for emptiness. /// /// true if the source sequence contains no elements; otherwise, false. /// @@ -82,6 +82,22 @@ public static bool IsEmpty(this IEnumerable source) return !source.Any(); } + /// + /// Determines whether a sequence is null or contains no elements. + /// + /// The type of the elements of . + /// + /// The instance to check for null and emptiness. + /// + /// + /// true if the source sequence is null or contains no elements; + /// otherwise, false. + /// + public static bool IsNullOrEmpty(this IEnumerable source) + { + return source.IsNull() || source.IsEmpty(); + } + /// /// Determines whether none of the elements of a sequence satisfies a condition. /// diff --git a/src/StevenVolckaert.Core/ObjectExtensions.cs b/src/StevenVolckaert.Core/ObjectExtensions.cs index 00e43be..657eacb 100644 --- a/src/StevenVolckaert.Core/ObjectExtensions.cs +++ b/src/StevenVolckaert.Core/ObjectExtensions.cs @@ -13,9 +13,12 @@ public static class ObjectExtensions /// /// Casts the object as a of the specified type. /// - /// The type to cast the elements of to. + /// + /// The type to cast the elements of to. + /// /// - /// The that needs to be casted to a of the specified type. + /// The that needs to be casted to a of the specified + /// type. /// /// /// A that contains each element of the @@ -40,5 +43,33 @@ public static List AsList(this object source) ? ((List)source).Cast().ToList() : result; } + + /// + /// Returns a value that indicates whether the object is null. + /// + /// + /// The instance this extension method affects. + /// + /// + /// true if is null; otherwise, false. + /// + public static bool IsNull(this object value) + { + return null == value; + } + + /// + /// Returns a value that indicates whether the object is not null. + /// + /// + /// The instance this extension method affects. + /// + /// + /// true if is not null; otherwise, false. + /// + public static bool IsNotNull(this object value) + { + return null != value; + } } } diff --git a/src/StevenVolckaert.Core/StevenVolckaert.Core.csproj b/src/StevenVolckaert.Core/StevenVolckaert.Core.csproj index bac2198..7e258fb 100644 --- a/src/StevenVolckaert.Core/StevenVolckaert.Core.csproj +++ b/src/StevenVolckaert.Core/StevenVolckaert.Core.csproj @@ -21,9 +21,7 @@ See https://github.com/stevenvolckaert/enterprise-library for more information.< false false StevenVolckaert - 1.1.1.0 - 1.1.1.0 - 1.1.1 + 1.2.0 diff --git a/src/StevenVolckaert.Core/StringExtensions.cs b/src/StevenVolckaert.Core/StringExtensions.cs index 597a5af..0a4c11d 100644 --- a/src/StevenVolckaert.Core/StringExtensions.cs +++ b/src/StevenVolckaert.Core/StringExtensions.cs @@ -375,6 +375,31 @@ public static TEnum ParseAs(this string value, bool ignoreCase) return (TEnum)Enum.Parse(typeof(TEnum), value, ignoreCase); } + /// + /// Converts the string to a specified enumeration, or returns the enumeration's default value + /// if the conversion fails. + /// + /// The type of to parse as. + /// The value. + public static TEnum TryParseAs(this string value) + where TEnum : struct + { + return value.TryParseAs(ignoreCase: false); + } + + /// + /// Converts the string to a specified enumeration, or returns the enumeration's default value + /// if the conversion fails. A value specifies whether the operation is case-insensitive. + /// + /// The type of to parse as. + /// The value. + /// true to ignore case; false to regard case. + public static TEnum TryParseAs(this string value, bool ignoreCase) + where TEnum : struct + { + return value.TryParseAs(defaultResult: default(TEnum), ignoreCase: ignoreCase); + } + /// /// Converts the string to a specified enumeration, or returns a specified default result if /// the conversion fails. @@ -390,7 +415,7 @@ public static TEnum TryParseAs(this string value, TEnum defaultResult) /// /// Converts the string to a specified enumeration, or returns a specified default result if - /// the conversion fails. A value specified whether the operation is case-insensitive. + /// the conversion fails. A value specifies whether the operation is case-insensitive. /// /// The type of to parse as. /// The value. diff --git a/test/StevenVolckaert.Core.Tests/Globalization/CultureManagerTests.cs b/test/StevenVolckaert.Core.Tests/Globalization/CultureManagerTests.cs index 05c77f7..ced40a3 100644 --- a/test/StevenVolckaert.Core.Tests/Globalization/CultureManagerTests.cs +++ b/test/StevenVolckaert.Core.Tests/Globalization/CultureManagerTests.cs @@ -1,9 +1,8 @@ namespace StevenVolckaert.Globalization.Tests { using Globalization; - using Microsoft.VisualStudio.TestTools.UnitTesting; + using Xunit; - [TestClass] public class CultureManagerTests { /* TODO Implement the unit tests. For instance: Calling any of the SetCulture methods should return @@ -11,55 +10,47 @@ public class CultureManagerTests * culture manager (after execution). Steven Volckaert. December 13, 2016. */ - //[TestMethod] - public void CultureManagerTest() - { - Assert.Fail(); - } + //[Fact] + //public void CultureManagerTest() + //{ + //} - //[TestMethod] - public void CultureManagerTest1() - { - Assert.Fail(); - } + //[Fact] + //public void CultureManagerTest1() + //{ + //} - //[TestMethod] - public void GetCultureInfoTest() - { - Assert.Fail(); - } + //[Fact] + //public void GetCultureInfoTest() + //{ + //} - //[TestMethod] - public void GetNeutralCultureNameTest() - { - Assert.Fail(); - } + //[Fact] + //public void GetNeutralCultureNameTest() + //{ + //} - //[TestMethod] - public void IsCultureSupportedTest() - { - Assert.Fail(); - } + //[Fact] + //public void IsCultureSupportedTest() + //{ + //} - //[TestMethod] - public void IsSpecificCultureSupportedTest() - { - Assert.Fail(); - } + //[Fact] + //public void IsSpecificCultureSupportedTest() + //{ + //} - //[TestMethod] - public void IsCultureSelectedTest() - { - Assert.Fail(); - } + //[Fact] + //public void IsCultureSelectedTest() + //{ + //} - //[TestMethod] - public void SetCultureTest1() - { - Assert.Fail(); - } + //[Fact] + //public void SetCultureTest1() + //{ + //} - [TestMethod] + [Fact] public void SetCultureTest2() { var cultureManager = @@ -68,23 +59,21 @@ public void SetCultureTest2() supportedCultureNames: new string[] { "en-US", "nl-BE" } ); - Assert.AreEqual("en-US", cultureManager.SetCulture("pt", "zh-Hans")); - Assert.AreEqual("nl-BE", cultureManager.SetCulture("pt", "zh-Hans", "nl-BE")); + Assert.Equal("en-US", cultureManager.SetCulture("pt", "zh-Hans")); + Assert.Equal("nl-BE", cultureManager.SetCulture("pt", "zh-Hans", "nl-BE")); // TODO Add additional asserts. // Steven Volckaert. December 13, 2016. } - //[TestMethod] - public void SetDefaultCultureTest() - { - Assert.Fail(); - } + //[Fact] + //public void SetDefaultCultureTest() + //{ + //} - //[TestMethod] - public void SetSpecificCultureTest() - { - Assert.Fail(); - } + //[Fact] + //public void SetSpecificCultureTest() + //{ + //} } } diff --git a/test/StevenVolckaert.Core.Tests/ICollectionExtensionsTests.cs b/test/StevenVolckaert.Core.Tests/ICollectionExtensionsTests.cs index c0053ac..e441078 100644 --- a/test/StevenVolckaert.Core.Tests/ICollectionExtensionsTests.cs +++ b/test/StevenVolckaert.Core.Tests/ICollectionExtensionsTests.cs @@ -1,45 +1,44 @@ namespace StevenVolckaert.Tests { using System.Collections.Generic; - using Microsoft.VisualStudio.TestTools.UnitTesting; + using Xunit; - [TestClass] public class ICollectionExtensionsTests { - [TestMethod] + [Fact] public void AddRangeTest() { - var source = new List { "foo", "bar", "baz" }; - var other = new List { "bar", "foo", "qux" }; - var expected = new List { "foo", "bar", "baz", "bar", "foo", "qux" }; + var subject = new List { "foo", "bar", "baz" }; + var other = new string[] { "bar", "foo", "qux" }; + var expected = new string[] { "foo", "bar", "baz", "bar", "foo", "qux" }; - source.AddRange(other); + subject.AddRange(other); - CollectionAssert.AreEqual(expected, source); + Assert.Equal(expected, subject); } - [TestMethod] + [Fact] public void AddRangeIndistinctTest() { - var source = new List { "foo", "bar", "baz" }; - var other = new List { "bar", "foo", "qux" }; - var expected = new List { "foo", "bar", "baz", "bar", "foo", "qux" }; + var subject = new List { "foo", "bar", "baz" }; + var other = new string[] { "bar", "foo", "qux" }; + var expected = new string[] { "foo", "bar", "baz", "bar", "foo", "qux" }; - source.AddRange(other, isDistinct: false); + subject.AddRange(other, isDistinct: false); - CollectionAssert.AreEqual(expected, source); + Assert.Equal(expected, subject); } - [TestMethod] + [Fact] public void AddRangeDistinctTest() { - var source = new List { "foo", "bar", "baz" }; - var other = new List { "bar", "foo", "qux" }; - var expected = new List { "foo", "bar", "baz", "qux" }; + var subject = new List { "foo", "bar", "baz" }; + var other = new string[] { "bar", "foo", "qux" }; + var expected = new string[] { "foo", "bar", "baz", "qux" }; - source.AddRange(other, isDistinct: true); + subject.AddRange(other, isDistinct: true); - CollectionAssert.AreEqual(expected, source); + Assert.Equal(expected, subject); } } } diff --git a/test/StevenVolckaert.Core.Tests/IEnumerableExtensionsTests.cs b/test/StevenVolckaert.Core.Tests/IEnumerableExtensionsTests.cs index 671e4b0..3323cf4 100644 --- a/test/StevenVolckaert.Core.Tests/IEnumerableExtensionsTests.cs +++ b/test/StevenVolckaert.Core.Tests/IEnumerableExtensionsTests.cs @@ -1,181 +1,223 @@ namespace StevenVolckaert.Tests { - using Microsoft.VisualStudio.TestTools.UnitTesting; using System; + using System.Collections.Generic; using System.Linq; + using Xunit; - [TestClass] public class IEnumerableExtensionsTests { - [TestMethod] + [Fact] public void AppendTest() { - var source = new string[] { "foo", "bar" }; + var subject = new string[] { "foo", "bar" }; var expected = new string[] { "foo", "bar", "baz" }; - var actual = source.Append("baz").ToArray(); + var actual = subject.Append("baz").ToArray(); - CollectionAssert.AreEqual(expected, actual); + Assert.Equal(expected, actual); } - [TestMethod] + [Fact] public void PrependTest() { - var source = new string[] { "bar", "baz" }; + var subject = new string[] { "bar", "baz" }; var expected = new string[] { "foo", "bar", "baz" }; - var actual = source.Prepend("foo").ToArray(); + var actual = subject.Prepend("foo").ToArray(); - CollectionAssert.AreEqual(expected, actual); + Assert.Equal(expected, actual); } - [TestMethod] + [Fact] public void ExceptTest() { - var source = new string[] { "foo", "bar", "baz" }; + var subject = new string[] { "foo", "bar", "baz" }; - var actual = source.Except("xxx"); - Assert.IsTrue(actual.Count() == 3); - Assert.IsFalse(actual.Contains("xxx")); + var actual = subject.Except("xxx"); + Assert.True(actual.Count() == 3); + Assert.DoesNotContain("xxx", collection: actual); - foreach (var item in source) + foreach (var item in subject) { - actual = source.Except(item); - Assert.IsTrue(actual.Count() == 2); - Assert.IsFalse(actual.Contains(item)); + actual = subject.Except(item); + Assert.True(actual.Count() == 2); + Assert.DoesNotContain(item, collection: actual); } } - [TestMethod] + [Fact] public void IsSubsetOf_ReturnsTrue() { - var source = new string[] { "foo", "bar", "baz" }; + var subject = new string[] { "foo", "bar", "baz" }; var other = new string[] { "x", "baz", "1", "bar", "foo", "y", "something", "z", "else" }; - Assert.IsTrue(source.IsSubsetOf(other)); + Assert.True(subject.IsSubsetOf(other)); } - [TestMethod] + [Fact] public void IsSubsetOf_ReturnsFalse() { - var source = new string[] { "foo", "bar", "baz" }; + var subject = new string[] { "foo", "bar", "baz" }; var other = new string[] { "x", "bax", "1", "bar", "foo", "y", "something", "z", "else" }; - Assert.IsFalse(source.IsSubsetOf(other)); + Assert.False(subject.IsSubsetOf(other)); } - [TestMethod, ExpectedException(typeof(ArgumentNullException))] + [Fact] + public void IsEmpty_ReturnsTrue() + { + var subject = new string[] { }; + Assert.True(subject.IsEmpty()); + } + + [Fact] + public void IsEmpty_ReturnsFalse() + { + var subject = new string[] { "foo" }; + Assert.False(subject.IsEmpty()); + } + + [Fact] + public void IsEmpty_ThrowsArgumentNullException() + { + IEnumerable subject = null; + Assert.Throws(() => subject.IsEmpty()); + } + + [Fact] + public void IsNullOrEmpty_ReturnsTrue() + { + IEnumerable subject = null; + Assert.True(subject.IsNullOrEmpty()); + + subject = Enumerable.Empty(); + Assert.True(subject.IsNullOrEmpty()); + } + + [Fact] + public void IsNullOrEmpty_ReturnsFalse() + { + IEnumerable subject = new string[] { "foo" }; + Assert.False(subject.IsNullOrEmpty()); + } + + [Fact] public void OrderByOrdinal_ThrowsImmediatelyWhenArgumentIsNull() { - string[] source = null; - source.OrderByOrdinal(x => x); + IEnumerable subject = null; + Assert.Throws(() => subject.OrderByOrdinal(x => x)); } - [TestMethod, ExpectedException(typeof(ArgumentNullException))] + [Fact] public void OrderByOrdinal_ThrowsImmediatelyWhenKeySelectorIsNull() { - var source = new string[] { "foo 11", "baz 11", "foo", "bar", "baz", "foo 1", "bar 20", "foo 10", "bar 1", "baz 2" }; - source.OrderByOrdinal(null); + var subject = new string[] { "foo 11", "baz 11", "foo", "bar", "baz", "foo 1", "bar 20", "foo 10", "bar 1", "baz 2" }; + + Assert.Throws(() => subject.OrderByOrdinal(null)); } - [TestMethod] + [Fact] public void OrderByOrdinalTest() { - var source = new string[] { "foo 11", "baz 11", "foo", "bar", "baz", "foo 1", "bar 20", "foo 10", "bar 1", "baz 2" }; + var subject = new string[] { "foo 11", "baz 11", "foo", "bar", "baz", "foo 1", "bar 20", "foo 10", "bar 1", "baz 2" }; var expected = new string[] { "bar", "bar 1", "bar 20", "baz", "baz 2", "baz 11", "foo", "foo 1", "foo 10", "foo 11" }; - var actual = source.OrderByOrdinal(x => x).ToArray(); + var actual = subject.OrderByOrdinal(x => x).ToArray(); - CollectionAssert.AreEqual(expected, actual); + Assert.Equal(expected, actual); - source = new string[] { "1", "10", "100", "101", "102", "11", "12", "13", "14", "15", "16", "17", "18", "19", "2", "20", "3", "4", "5", "6", "7", "8", "9" }; + subject = new string[] { "1", "10", "100", "101", "102", "11", "12", "13", "14", "15", "16", "17", "18", "19", "2", "20", "3", "4", "5", "6", "7", "8", "9" }; expected = new string[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "100", "101", "102" }; - actual = source.OrderByOrdinal(x => x).ToArray(); + actual = subject.OrderByOrdinal(x => x).ToArray(); - CollectionAssert.AreEqual(expected, actual); + Assert.Equal(expected, actual); - source = new string[] { "File 1.txt", "File 10.txt", "File 11.csv", "File 2.jpg", "File 20.xls", "File 21.ppt", "File 3.doc" }; + subject = new string[] { "File 1.txt", "File 10.txt", "File 11.csv", "File 2.jpg", "File 20.xls", "File 21.ppt", "File 3.doc" }; expected = new string[] { "File 1.txt", "File 2.jpg", "File 3.doc", "File 10.txt", "File 11.csv", "File 20.xls", "File 21.ppt" }; - actual = source.OrderByOrdinal(x => x).ToArray(); + actual = subject.OrderByOrdinal(x => x).ToArray(); - CollectionAssert.AreEqual(expected, actual); + Assert.Equal(expected, actual); // Verify case-sensitive ordering. - source = new string[] { "File 1.txt", "file 10.txt", "File 11.csv", "file 2.jpg", "File 20.xls", "File 21.ppt", "File 3.doc" }; + subject = new string[] { "File 1.txt", "file 10.txt", "File 11.csv", "file 2.jpg", "File 20.xls", "File 21.ppt", "File 3.doc" }; expected = new string[] { "file 2.jpg", "file 10.txt", "File 1.txt", "File 3.doc", "File 11.csv", "File 20.xls", "File 21.ppt" }; - actual = source.OrderByOrdinal(x => x).ToArray(); + actual = subject.OrderByOrdinal(x => x).ToArray(); - CollectionAssert.AreEqual(expected, actual); + Assert.Equal(expected, actual); } - [TestMethod] + [Fact] public void OrderByOrdinalCaseInsensitiveTest() { - var source = new string[] { "File 1.txt", "file 10.txt", "File 11.csv", "file 2.jpg", "file 20.xls", "File 21.ppt", "File 3.doc" }; + var subject = new string[] { "File 1.txt", "file 10.txt", "File 11.csv", "file 2.jpg", "file 20.xls", "File 21.ppt", "File 3.doc" }; var expected = new string[] { "File 1.txt", "file 2.jpg", "File 3.doc", "file 10.txt", "File 11.csv", "file 20.xls", "File 21.ppt" }; - var actual = source.OrderByOrdinal(x => x, ignoreCase: true).ToArray(); + var actual = subject.OrderByOrdinal(x => x, ignoreCase: true).ToArray(); - CollectionAssert.AreEqual(expected, actual); + Assert.Equal(expected, actual); } - [TestMethod] + [Fact] public void OrderByOrdinalDescendingTest() { - var source = new string[] { "foo 11", "baz 11", "foo", "bar", "baz", "foo 1", "bar 20", "foo 10", "bar 1", "baz 2" }; + var subject = new string[] { "foo 11", "baz 11", "foo", "bar", "baz", "foo 1", "bar 20", "foo 10", "bar 1", "baz 2" }; var expected = new string[] { "foo 11", "foo 10", "foo 1", "foo", "baz 11", "baz 2", "baz", "bar 20", "bar 1", "bar" }; - var actual = source.OrderByOrdinalDescending(x => x).ToArray(); + var actual = subject.OrderByOrdinalDescending(x => x).ToArray(); - CollectionAssert.AreEqual(expected, actual); + Assert.Equal(expected, actual); - source = new string[] { "1", "10", "100", "101", "102", "11", "12", "13", "14", "15", "16", "17", "18", "19", "2", "20", "3", "4", "5", "6", "7", "8", "9" }; + subject = new string[] { "1", "10", "100", "101", "102", "11", "12", "13", "14", "15", "16", "17", "18", "19", "2", "20", "3", "4", "5", "6", "7", "8", "9" }; expected = new string[] { "102", "101", "100", "20", "19", "18", "17", "16", "15", "14", "13", "12", "11", "10", "9", "8", "7", "6", "5", "4", "3", "2", "1" }; - actual = source.OrderByOrdinalDescending(x => x).ToArray(); + actual = subject.OrderByOrdinalDescending(x => x).ToArray(); - CollectionAssert.AreEqual(expected, actual); + Assert.Equal(expected, actual); - source = new string[] { "File 1.txt", "File 10.txt", "File 11.csv", "File 2.jpg", "File 20.xls", "File 21.ppt", "File 3.doc" }; + subject = new string[] { "File 1.txt", "File 10.txt", "File 11.csv", "File 2.jpg", "File 20.xls", "File 21.ppt", "File 3.doc" }; expected = new string[] { "File 21.ppt", "File 20.xls", "File 11.csv", "File 10.txt", "File 3.doc", "File 2.jpg", "File 1.txt" }; - actual = source.OrderByOrdinalDescending(x => x).ToArray(); + actual = subject.OrderByOrdinalDescending(x => x).ToArray(); - CollectionAssert.AreEqual(expected, actual); + Assert.Equal(expected, actual); // Verify case-sensitive ordering. - source = new string[] { "File 1.txt", "file 10.txt", "File 11.csv", "file 2.jpg", "File 20.xls", "File 21.ppt", "File 3.doc" }; + subject = new string[] { "File 1.txt", "file 10.txt", "File 11.csv", "file 2.jpg", "File 20.xls", "File 21.ppt", "File 3.doc" }; expected = new string[] { "File 21.ppt", "File 20.xls", "File 11.csv", "File 3.doc", "File 1.txt", "file 10.txt", "file 2.jpg" }; - actual = source.OrderByOrdinalDescending(x => x).ToArray(); + actual = subject.OrderByOrdinalDescending(x => x).ToArray(); - CollectionAssert.AreEqual(expected, actual); + Assert.Equal(expected, actual); } - [TestMethod] + [Fact] public void OrderByOrdinalDescendingCaseInsensitiveTest() { - var source = new string[] { "File 1.txt", "file 10.txt", "File 11.csv", "file 2.jpg", "file 20.xls", "File 21.ppt", "File 3.doc" }; + var subject = new string[] { "File 1.txt", "file 10.txt", "File 11.csv", "file 2.jpg", "file 20.xls", "File 21.ppt", "File 3.doc" }; var expected = new string[] { "File 21.ppt", "file 20.xls", "File 11.csv", "file 10.txt", "File 3.doc", "file 2.jpg", "File 1.txt" }; - var actual = source.OrderByOrdinalDescending(x => x, ignoreCase: true).ToArray(); + var actual = subject.OrderByOrdinalDescending(x => x, ignoreCase: true).ToArray(); - CollectionAssert.AreEqual(expected, actual); + Assert.Equal(expected, actual); } - [TestMethod, ExpectedException(typeof(ArgumentNullException))] + [Fact] public void ToObservableCollectionTest_ThrowsImmediatelyWhenArgumentIsNull() { - string[] source = null; - var collection = source.ToObservableCollection(); + string[] subject = null; + + Assert.Throws( + () => subject.ToObservableCollection() + ); } - [TestMethod] + [Fact] public void ToStringTest() { - var source = new string[] { "File 1.txt", "file 10.txt", "File 11.csv" }; + var subject = new string[] { "File 1.txt", "file 10.txt", "File 11.csv" }; var expected = "File 1.txt; file 10.txt; File 11.csv"; - var actual = source.ToString(separator: "; "); + var actual = subject.ToString(separator: "; "); - Assert.AreEqual(expected, actual); + Assert.Equal(expected, actual); } - [TestMethod] + [Fact] public void UnionTest() { - var source = new string[] { "foo", "bar" }; - var actual = source.Union("foo").Union("baz"); - Assert.IsTrue(actual.Count() == 3); - Assert.IsTrue(actual.Contains("baz")); + var subject = new string[] { "foo", "bar" }; + var actual = subject.Union("foo").Union("baz"); + Assert.True(actual.Count() == 3); + Assert.Contains("baz", actual); } } } diff --git a/test/StevenVolckaert.Core.Tests/ResourcesTests.cs b/test/StevenVolckaert.Core.Tests/ResourcesTests.cs index 1633edd..7b6263a 100644 --- a/test/StevenVolckaert.Core.Tests/ResourcesTests.cs +++ b/test/StevenVolckaert.Core.Tests/ResourcesTests.cs @@ -1,17 +1,16 @@ namespace StevenVolckaert.Tests { - using Microsoft.VisualStudio.TestTools.UnitTesting; + using Xunit; - [TestClass] public class ResourcesTests { - [TestMethod] + [Fact] public void CanAccessResources() { var resourceManager = Resources.ResourceManager; - Assert.IsNotNull(resourceManager); - Assert.IsNotNull(Resources.ValueNullEmptyOrWhiteSpace); + Assert.NotNull(resourceManager); + Assert.NotNull(Resources.ValueNullEmptyOrWhiteSpace); } } } diff --git a/test/StevenVolckaert.Core.Tests/StevenVolckaert.Core.Tests.csproj b/test/StevenVolckaert.Core.Tests/StevenVolckaert.Core.Tests.csproj index 86056b3..25e6f79 100644 --- a/test/StevenVolckaert.Core.Tests/StevenVolckaert.Core.Tests.csproj +++ b/test/StevenVolckaert.Core.Tests/StevenVolckaert.Core.Tests.csproj @@ -1,22 +1,19 @@ - + - netcoreapp1.1 + netcoreapp2.0 StevenVolckaert.Tests - 1.1.1 + false - - - + + + - - - diff --git a/test/StevenVolckaert.Core.Tests/StringExtensionsTests.cs b/test/StevenVolckaert.Core.Tests/StringExtensionsTests.cs index e6ccec1..299b904 100644 --- a/test/StevenVolckaert.Core.Tests/StringExtensionsTests.cs +++ b/test/StevenVolckaert.Core.Tests/StringExtensionsTests.cs @@ -1,119 +1,163 @@ namespace StevenVolckaert.Tests { using System; - using System.Collections.Generic; - using Microsoft.VisualStudio.TestTools.UnitTesting; + using Xunit; - [TestClass] public class StringExtensionsTests { - [TestMethod] - public void IsGuidTest() - { - var validGuidStrings = new List - { - // Lower case, no dashes, no braces. - "dfdc04eb167941cd8e23ef66d6cc756e", - // Upper case, no dashes, no braces. - "DFDC04EB167941CD8E23EF66D6CC756E", - // lower case, with dashes, no braces. - "dfdc04eb-1679-41cd-8e23-ef66d6cc756e", - // Upper case, with dashes, no braces. - "DFDC04EB-1679-41CD-8E23-EF66D6CC756E", - // Lower case, with dashes, with braces. - "{dfdc04eb-1679-41cd-8e23-ef66d6cc756e}", - // Upper case, with dashes, with braces. - "{DFDC04EB-1679-41CD-8E23-EF66D6CC756E}", - }; - - validGuidStrings.ForEach( - x => Assert.IsTrue(x.IsGuid(), $"Expected string value '{x}' to be a valid GUID.") - ); + [Theory] + // Lower case, no dashes, no braces. + [InlineData("dfdc04eb167941cd8e23ef66d6cc756e")] + // Upper case, no dashes, no braces. + [InlineData("DFDC04EB167941CD8E23EF66D6CC756E")] + // lower case, with dashes, no braces. + [InlineData("dfdc04eb-1679-41cd-8e23-ef66d6cc756e")] + // Upper case, with dashes, no braces. + [InlineData("DFDC04EB-1679-41CD-8E23-EF66D6CC756E")] + // Lower case, with dashes, with braces. + [InlineData("{dfdc04eb-1679-41cd-8e23-ef66d6cc756e}")] + // Upper case, with dashes, with braces. + [InlineData("{DFDC04EB-1679-41CD-8E23-EF66D6CC756E}")] + public void IsGuid_ReturnsTrue(string subject) + { + Assert.True(subject.IsGuid(), $"Expected string value '{subject}' to be a valid GUID."); + } - var invalidGuidStrings = new List - { - // Contains invalid character ('g'). - "gfdc04eb167941cd8e23ef66d6cc756e", - // Not enough characters. - "FDC04EB167941CD8E23EF66D6CC756E", - // Not enough characters. - "{dfdc04eb167941cd8e23ef66d6cc756}", - // Dash in the wrong place. - "DFDC04EB-16794-1CD-8E23EF66D6CC756E", - // A random string. - "Hello world!", - }; - - invalidGuidStrings.ForEach( - x => Assert.IsFalse(x.IsGuid(), $"Expected string value '{x}' to be an invalid GUID.") - ); + [Theory] + // Contains invalid character ('g'). + [InlineData("gfdc04eb167941cd8e23ef66d6cc756e")] + // Not enough characters. + [InlineData("FDC04EB167941CD8E23EF66D6CC756E")] + // Not enough characters. + [InlineData("{dfdc04eb167941cd8e23ef66d6cc756}")] + // Dash in the wrong place. + [InlineData("DFDC04EB-16794-1CD-8E23EF66D6CC756E")] + // A random string. + [InlineData("Hello world!")] + public void IsGuid_ReturnsFalse(string subject) + { + Assert.False(subject.IsGuid(), $"Expected string value '{subject}' to be an invalid GUID."); } public enum MockedEnumeration { + Qux = 0, Foo, - Bar + Bar, + } + + public enum MockedEnumerationWithoutDefaultValue + { + Foo = 1, + Bar = 2, + } + + [Theory] + [InlineData(MockedEnumeration.Foo, "Foo")] + [InlineData(MockedEnumeration.Bar, "Bar")] + public void ParseAs_Succeeds(MockedEnumeration expected, string subject) + { + Assert.Equal(expected, subject.TryParseAs()); } - [TestMethod] - public void ParseAs_Succeeds() + [Theory] + [InlineData(MockedEnumeration.Foo, "foo")] + [InlineData(MockedEnumeration.Bar, "baR")] + public void ParseAs_Fails(MockedEnumeration notExpected, string subject) { - Assert.AreEqual(MockedEnumeration.Foo, "Foo".ParseAs()); - Assert.AreEqual(MockedEnumeration.Bar, "Bar".ParseAs()); + Assert.NotEqual(notExpected, subject.TryParseAs()); } - [TestMethod, ExpectedException(typeof(ArgumentException))] + [Fact] public void ParseAs_ThrowsArgumentException() { - // "Baz" is not en enumeration value of MockedEnumeration. - "Baz".ParseAs(); + // "Baz" is not a valid enumeration value of MockedEnumeration, so it should throw an exception. + Assert.Throws(() => "Baz".ParseAs()); } - [TestMethod, ExpectedException(typeof(ArgumentNullException))] + [Fact] public void ParseAs_ThrowsArgumentNullException() { - string str = null; - str.ParseAs(); + string subject = null; + Assert.Throws(() => subject.ParseAs()); + } + + [Theory] + [InlineData(MockedEnumeration.Foo, "foo")] + [InlineData(MockedEnumeration.Bar, "baR")] + public void ParseAs_WhileIgnoringCase_Succeeds(MockedEnumeration expected, string subject) + { + Assert.Equal(expected, subject.TryParseAs(ignoreCase: true)); + } + + [Fact] + public void ParseAs_WhileIgnoringCase_ThrowsArgumentException() + { + // "baz" is not a valid enumeration value of MockedEnumeration, so it should throw an exception. + Assert.Throws(() => "baz".ParseAs(ignoreCase: true)); } - [TestMethod] - public void ParseAsWhileIgnoringCase_Succeeds() + [Theory] + [InlineData(MockedEnumeration.Foo, "Foo")] + [InlineData(MockedEnumeration.Bar, "Bar")] + [InlineData(MockedEnumeration.Qux, "Qux")] + public void TryParseAs_Succeeds(MockedEnumeration expected, string subject) { - Assert.AreEqual(MockedEnumeration.Foo, "foo".ParseAs(ignoreCase: true)); - Assert.AreEqual(MockedEnumeration.Bar, "bar".ParseAs(ignoreCase: true)); + Assert.Equal(expected, subject.TryParseAs()); } - [TestMethod, ExpectedException(typeof(ArgumentException))] - public void ParseAsWhileIgnoringCase_ThrowsArgumentException() + [Theory] + [InlineData(MockedEnumerationWithoutDefaultValue.Foo, "Foo")] + [InlineData(MockedEnumerationWithoutDefaultValue.Bar, "Bar")] + [InlineData(default(MockedEnumerationWithoutDefaultValue), "Baz")] + public void TryParseAs_EnumerationWithoutDefaultValue_Succeeds(MockedEnumerationWithoutDefaultValue expected, string subject) { - "baz".ParseAs(ignoreCase: true); + Assert.Equal(expected, subject.TryParseAs()); } - [TestMethod] - public void TryParseAs_Succeeds() + [Theory] + [InlineData(MockedEnumeration.Foo, "Foo", MockedEnumeration.Foo)] + [InlineData(MockedEnumeration.Bar, "Bar", MockedEnumeration.Foo)] + [InlineData(MockedEnumeration.Foo, "Baz", MockedEnumeration.Foo)] + [InlineData(MockedEnumeration.Bar, null, MockedEnumeration.Bar)] + public void TryParseAs_WithDefaultResult_Succeeds( + MockedEnumeration expected, + string subject, + MockedEnumeration defaultResult + ) { - Assert.AreEqual(MockedEnumeration.Foo, "Foo".TryParseAs(MockedEnumeration.Foo)); - Assert.AreEqual(MockedEnumeration.Bar, "Bar".TryParseAs(MockedEnumeration.Foo)); - Assert.AreEqual(MockedEnumeration.Foo, "Baz".TryParseAs(MockedEnumeration.Foo)); + Assert.Equal(expected, subject.TryParseAs(defaultResult)); + } - string str = null; - Assert.AreEqual(MockedEnumeration.Bar, str.TryParseAs(MockedEnumeration.Bar)); + [Theory] + [InlineData(MockedEnumeration.Foo, "foo")] + [InlineData(MockedEnumeration.Bar, "bAR")] + [InlineData(MockedEnumeration.Foo, "baz")] + public void TryParseAs_WithDefaultResult_WhileIgnoringCase_Succeeds( + MockedEnumeration expected, + string subject + ) + { + Assert.Equal( + expected: expected, + actual: subject.TryParseAs(defaultResult: MockedEnumeration.Foo, ignoreCase: true) + ); } - [TestMethod] - public void TryParseAsWhileIgnoringCase_Succeeds() + [Theory] + [InlineData("Foo", " Foo ")] + [InlineData("Bar", "Bar")] + [InlineData("Baz", " Baz")] + public void TryTrim(string expected, string subject) { - Assert.AreEqual(MockedEnumeration.Foo, "foo".TryParseAs(MockedEnumeration.Foo, ignoreCase: true)); - Assert.AreEqual(MockedEnumeration.Bar, "bar".TryParseAs(MockedEnumeration.Foo, ignoreCase: true)); - Assert.AreEqual(MockedEnumeration.Foo, "baz".TryParseAs(MockedEnumeration.Foo, ignoreCase: true)); + Assert.Equal(expected, subject.TryTrim()); } - [TestMethod] - public void TryTrimTest() + [Fact] + public void TryTrim_ReturnsNull() { - Assert.AreEqual("Foo", " Foo ".TryTrim()); - Assert.AreEqual("Bar", "Bar".TryTrim()); - Assert.AreEqual(null, ((String)null).TryTrim()); + string subject = null; + Assert.Null(subject.TryTrim()); } } } diff --git a/test/StevenVolckaert.Core.Tests/UnitConvertTests.cs b/test/StevenVolckaert.Core.Tests/UnitConvertTests.cs index 8fbb1b7..5363262 100644 --- a/test/StevenVolckaert.Core.Tests/UnitConvertTests.cs +++ b/test/StevenVolckaert.Core.Tests/UnitConvertTests.cs @@ -1,72 +1,73 @@ namespace StevenVolckaert.Tests { - using System; using System.Collections.Generic; - using Microsoft.VisualStudio.TestTools.UnitTesting; + using Xunit; - [TestClass] public class UnitConvertTests { /// - /// A list of tuples that links degrees Celsius to degrees Fahrenheit. + /// Returns a sequence of data that links degrees Celsius to degrees Fahrenheit. /// Source http://www.rapidtables.com/convert/temperature/celsius-to-fahrenheit.htm /// - private static readonly List> _temperatureTuples = - new List> - { - Tuple.Create(-50, -58), - Tuple.Create(-40, -40), - Tuple.Create(-30, -22), - Tuple.Create(-20, -4), - Tuple.Create(-10, 14), - Tuple.Create(-9, 16), - Tuple.Create(-8, 18), - Tuple.Create(-7, 19), - Tuple.Create(-6, 21), - Tuple.Create(-5, 23), - Tuple.Create(-4, 25), - Tuple.Create(-3, 27), - Tuple.Create(-2, 28), - Tuple.Create(-1, 30), - Tuple.Create(0, 32), - Tuple.Create(1, 34), - Tuple.Create(2, 36), - Tuple.Create(3, 37), - Tuple.Create(4, 39), - Tuple.Create(5, 41), - Tuple.Create(6, 43), - Tuple.Create(7, 45), - Tuple.Create(8, 46), - Tuple.Create(9, 48), - Tuple.Create(10, 50), - Tuple.Create(20, 68), - Tuple.Create(21, 70), - Tuple.Create(30, 86), - Tuple.Create(37, 99), - Tuple.Create(40, 104), - Tuple.Create(50, 122), - Tuple.Create(60, 140), - Tuple.Create(70, 158), - Tuple.Create(80, 176), - Tuple.Create(90, 194), - Tuple.Create(100, 212), - Tuple.Create(200, 392), - Tuple.Create(300, 572), - }; + public static IEnumerable CelsiusToFahrenheitData() + { + yield return new object[] { -50, -58 }; + yield return new object[] { -40, -40 }; + yield return new object[] { -30, -22 }; + yield return new object[] { -20, -4 }; + yield return new object[] { -10, 14 }; + yield return new object[] { -9, 16 }; + yield return new object[] { -8, 18 }; + yield return new object[] { -7, 19 }; + yield return new object[] { -6, 21 }; + yield return new object[] { -5, 23 }; + yield return new object[] { -4, 25 }; + yield return new object[] { -3, 27 }; + yield return new object[] { -2, 28 }; + yield return new object[] { -1, 30 }; + yield return new object[] { 0, 32 }; + yield return new object[] { 1, 34 }; + yield return new object[] { 2, 36 }; + yield return new object[] { 3, 37 }; + yield return new object[] { 4, 39 }; + yield return new object[] { 5, 41 }; + yield return new object[] { 6, 43 }; + yield return new object[] { 7, 45 }; + yield return new object[] { 8, 46 }; + yield return new object[] { 9, 48 }; + yield return new object[] { 10, 50 }; + yield return new object[] { 20, 68 }; + yield return new object[] { 21, 70 }; + yield return new object[] { 30, 86 }; + yield return new object[] { 37, 99 }; + yield return new object[] { 40, 104 }; + yield return new object[] { 50, 122 }; + yield return new object[] { 60, 140 }; + yield return new object[] { 70, 158 }; + yield return new object[] { 80, 176 }; + yield return new object[] { 90, 194 }; + yield return new object[] { 100, 212 }; + yield return new object[] { 200, 392 }; + yield return new object[] { 300, 572 }; + } - [TestMethod] - public void FromCelsiusToFahrenheitTest() + [Theory] + [MemberData(nameof(CelsiusToFahrenheitData))] + public void FromCelsiustoFahrenheit(int degreesInCelsius, int degreesInFahrenheit) { - _temperatureTuples.ForEach( - x => Assert.AreEqual(x.Item2, UnitConvert.FromCelsiusToFahrenheit(x.Item1)) + Assert.Equal( + expected: degreesInFahrenheit, + actual: UnitConvert.FromCelsiusToFahrenheit(degreesInCelsius) ); } - [TestMethod] - public void FromFahrenheitToCelsiusTest() + [Theory] + [MemberData(nameof(CelsiusToFahrenheitData))] + public void FromFahrenheitToCelsiusTest(int degreesInCelsius, int degreesInFahrenheit) { - _temperatureTuples.ForEach( - x => Assert.AreEqual(x.Item1, UnitConvert.FromFahrenheitToCelsius(x.Item2)) + Assert.Equal( + expected: degreesInCelsius, + actual: UnitConvert.FromFahrenheitToCelsius(degreesInFahrenheit) ); } }