diff --git a/src/NBasicExtensionMethod.Test.Fast/BooleanExtensionTests.cs b/src/NBasicExtensionMethod.Test.Fast/BooleanExtensionTests.cs index 6f646b1..0895532 100644 --- a/src/NBasicExtensionMethod.Test.Fast/BooleanExtensionTests.cs +++ b/src/NBasicExtensionMethod.Test.Fast/BooleanExtensionTests.cs @@ -23,45 +23,79 @@ namespace NBasicExtensionMethod.Test.Fast [TestFixture] public class BooleanExtensionTests { - [Test] - public void And_Should_Return_True_When_Both_Values_Are_True() { - // Arrange - bool bValueA = true; - bool bValueB = true; + [Test] + [TestCase(true, true, true)] + [TestCase(true, false, false)] + [TestCase(false, true, false)] + [TestCase(false, false, false)] + public void And_Should_Return_False_When_One_Value_Is_False(bool bValueA, bool bValueB, bool bExpectedResult) + { + // Arrange + // Act & Assert - bValueA.And(bValueB).Should().Be(true); + bValueA.And(bValueB).Should().Be(bExpectedResult); } [Test] - [TestCase(true, false)] - [TestCase(false, true)] - public void And_Should_Return_False_When_One_Value_Is_False(bool bValueA, bool bValueB) { - // Arrange + [TestCase(false, true, true)] + [TestCase(true, false, true)] + [TestCase(true, true, true)] + [TestCase(false, false, false)] + public void Or_Should_Return_True_When_One_Value_Is_True(bool bValueA, bool bValueB, bool bExpectedResult) + { + // Arrange // Act & Assert - bValueA.And(bValueB).Should().Be(false); + bValueA.Or(bValueB).Should().Be(bExpectedResult); } [Test] - public void Or_Should_Return_False_When_Both_Values_Are_False() { - // Arrange - bool bValueA = false; - bool bValueB = false; + [TestCase(true, false)] + [TestCase(false, true)] + public void Not_ReturnsOppositeOfPassedValue(bool bValueA, bool bExpectedResult) + { + bValueA.Not().Should().Be(bExpectedResult); + } - // Act & Assert - bValueA.Or(bValueB).Should().Be(false); + [Test] + [TestCase(true,true,false)] + [TestCase(false,false,true)] + [TestCase(true,false,true)] + [TestCase(false,true,true)] + public void Nand_ReturnsExpectedValueForPassedParameters(bool bValueA, bool bValueB, bool bExpectedResult) + { + bValueA.NAnd(bValueB).Should().Be(bExpectedResult); } [Test] - [TestCase(false, true)] - [TestCase(true, false)] - [TestCase(true, true)] - public void Or_Should_Return_True_When_One_Value_Is_True(bool bValueA, bool bValueB) { - // Arrange + [TestCase(true, true, true)] + [TestCase(false, false, true)] + [TestCase(true, false, false)] + [TestCase(false, true, false)] + public void XNOr_WhenValuesAreTheSame_ReturnsTrue(bool bValueA, bool bValueB, bool bExpectedResult) + { + bValueA.XNOr(bValueB).Should().Be(bExpectedResult); + } - // Act & Assert - bValueA.Or(bValueB).Should().Be(true); + [Test] + [TestCase(true, true, false)] + [TestCase(false, false, true)] + [TestCase(true, false, false)] + [TestCase(false, true, false)] + public void NOr_WhenBothValuesAreFalse_ReturnsTrue(bool bValueA, bool bValueB, bool bExpectedResult) + { + bValueA.NOr(bValueB).Should().Be(bExpectedResult); + } + + [Test] + [TestCase(true, true, false)] + [TestCase(false, false, false)] + [TestCase(true, false, true)] + [TestCase(false, true, true)] + public void XOr_WhenValuesAreDifferent_ReturnsTrue(bool bValueA, bool bValueB, bool bExpectedResult) + { + bValueA.XOr(bValueB).Should().Be(bExpectedResult); } } } \ No newline at end of file diff --git a/src/NBasicExtensionMethod.Test.Fast/NBasicExtensionMethod.Test.Fast.csproj b/src/NBasicExtensionMethod.Test.Fast/NBasicExtensionMethod.Test.Fast.csproj index 6ed8cb3..7017ded 100644 --- a/src/NBasicExtensionMethod.Test.Fast/NBasicExtensionMethod.Test.Fast.csproj +++ b/src/NBasicExtensionMethod.Test.Fast/NBasicExtensionMethod.Test.Fast.csproj @@ -67,6 +67,7 @@ + diff --git a/src/NBasicExtensionMethod.Test.Fast/StringExtensionsTests.cs b/src/NBasicExtensionMethod.Test.Fast/StringExtensionsTests.cs new file mode 100644 index 0000000..f57777b --- /dev/null +++ b/src/NBasicExtensionMethod.Test.Fast/StringExtensionsTests.cs @@ -0,0 +1,207 @@ +using System; +using FluentAssertions; +using NUnit.Framework; + +namespace NBasicExtensionMethod.Test.Fast +{ + [TestFixture] + public class StringExtensionsTests + { + [Test] + public void IsWebUrl_Should_Return_True_When_Passed_Valid_FQ_Web_Url() + { + // Arrange + string sDummyValidWebUrl = "http://www.bbc.co.uk"; + // Act + + // Assert + sDummyValidWebUrl.IsWebUrl().Should().BeTrue(); + } + + [Test] + public void IsWebUrl_Should_Return_True_When_Passed_Valid_Web_Url_Containing_Only_Top_Level_Domain() + { + // Arrange + string sDummyValidWebUrl = "http://bbc.co.uk"; + // Act + + // Assert + sDummyValidWebUrl.IsWebUrl().Should().BeTrue(); + } + + [Test] + [Ignore] + public void IsWebUrl_Should_Return_True_When_Passed_Valid_Web_Url_Missing_Protocal() + { + // Arrange + string sDummyValidWebUrl = "bbc.co.uk"; + // Act + + // Assert + sDummyValidWebUrl.IsWebUrl().Should().BeTrue(); + } + + [Test] + public void IsWebUrl_Should_Return_False_When_Passed_Invalid_Web_Url() + { + // Arrange + string sDummyInvalidWebUrl = "http:///www.bbc.co.uk"; + // Act + + // Assert + sDummyInvalidWebUrl.IsWebUrl().Should().BeFalse(); + } + + [Test] + public void IsWebUrl_Should_Return_True_When_Passed_Valid_FQ_Domain_And_Valid_Page_Path() + { + // Arrange + string sDummyValidFQWebUrlWithPagePath = "http://www.bbc.co.uk/news/london"; + // Act + + // Assert + sDummyValidFQWebUrlWithPagePath.IsWebUrl().Should().BeTrue(); + } + + [Test] + public void IsWebUrl_Should_Return_False_When_Passed_An_Empty_Value() + { + // Arrange + string sEmptyValue = string.Empty; + // Act + + // Assert + sEmptyValue.IsWebUrl().Should().BeFalse(); + } + + [Test] + public void IsWebUrl_Should_Return_False_When_Passed_A_Null_Value() + { + // Arrange + string sNullValue = null; + // Act + + // Assert + sNullValue.IsWebUrl().Should().BeFalse(); + } + + [Test] + public void IsWebUrl_Should_Return_False_When_Passed_A_Value_With_Invalid_Characters() + { + // Arrange + string sDummyInvalidUrlWithInvalidCharacters = "http://www.bbc.co.uk/^«"; + // Act + + // Assert + sDummyInvalidUrlWithInvalidCharacters.IsWebUrl().Should().BeFalse(); + } + + [Test] + public void IsEmail_Should_Return_False_When_Passed_An_Empty_Value() + { + // Arrange + string sEmptyValue = string.Empty; + // Act + + // Assert + sEmptyValue.IsEmail().Should().BeFalse(); + } + + [Test] + public void IsEmail_Should_Return_False_When_Passed_A_Null_Value() + { + // Arrange + string sNullValue = null; + // Act + + // Assert + sNullValue.IsEmail().Should().BeFalse(); + } + + [Test] + public void IsEmail_Should_Return_True_When_Passed_Valid_Email_Address() + { + // Arrange + string sDummyValidEmailAddress = "firstname.lastname@mydomain.com"; + // Act + + // Assert + sDummyValidEmailAddress.IsEmail().Should().BeTrue(); + } + + [Test] + public void IsEmail_Should_Return_True_When_Passed_Valid_Email_Address_With_Multi_TLD() + { + // Arrange + string sDummyValidEmailAddress = "firstname.lastname@mydomain.org.uk"; + // Act + + // Assert + sDummyValidEmailAddress.IsEmail().Should().BeTrue(); + } + + [Test] + public void StripWhitespace_Should_Return_Passed_Value_With_Spaces_Removed() + { + // Arrange + string sDummyValueWithSpaces = " Hello World, How Are You? "; + // Act + + // Assert + sDummyValueWithSpaces.StripWhitespace().Should().Be("HelloWorld,HowAreYou?"); + } + + [Test] + public void StripWhitespace_Should_Return_Empty_Value_When_Passed_A_Value_Of_Just_A_Space() + { + // Arrange + string sDummyValueOfJustOneSpace = " "; + // Act + + // Assert + sDummyValueOfJustOneSpace.StripWhitespace().Should().BeEmpty(); + } + + [Test] + public void StripWhitespace_Should_Return_Null_Value_When_Passed_A_Null_Value() + { + // Arrange + string sDummyNullValue = null; + // Act + + // Assert + sDummyNullValue.Should().BeNull(); + } + + [Test] + public void NormalizeWhitespaceToSingleSpaces_Should_Return_Passed_Value_With_Normalized_Spaces() + { + // Arrange + string sDummyValueWithUnnormalizedSpaces = " Hello World, How Are You? "; + // Act + + // Assert + sDummyValueWithUnnormalizedSpaces.NormalizeWhitespaceToSingleSpaces().Should().Be( + " Hello World, How Are You? "); + } + + [Test] + [ExpectedException(typeof(ArgumentNullException))] + public void NormalizeWhitespaceToSingleSpaces_Should_Throw_ArgumentNullException_Exception_When_Passed_Null() + { + // Arrange + string sDummyNullValue = null; + // Act + + // Assert + sDummyNullValue.NormalizeWhitespaceToSingleSpaces().Should().BeNull(); + } + + [Test] + public void WithCapitalizedFirstLetter_WithLowercaseFirstLetter_ReturnsValueWithCapitalizedFirstLetter() + { + string sValue = "test"; + sValue.WithCapitalizedFirstLetter().Should().StartWith("T"); + } + } +} \ No newline at end of file diff --git a/src/NBasicExtensionMethod.sln b/src/NBasicExtensionMethod.sln index 03aac29..1a60c81 100644 --- a/src/NBasicExtensionMethod.sln +++ b/src/NBasicExtensionMethod.sln @@ -5,9 +5,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NBasicExtensionMethod", "NB EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NBasicExtensionMethod.Test.Fast", "NBasicExtensionMethod.Test.Fast\NBasicExtensionMethod.Test.Fast.csproj", "{2475C604-1321-4966-9CB7-5DCAC1349E87}" EndProject -Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".nuget", ".nuget", "{46B5681F-02A5-4EFD-A196-729E66B99B01}" +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".nuget", ".nuget", "{880B1B61-B1BC-41E2-AE34-AF685AAA816C}" ProjectSection(SolutionItems) = preProject - .nuget\packages.config = .nuget\packages.config + .nuget\NuGet.exe = .nuget\NuGet.exe + .nuget\NuGet.targets = .nuget\NuGet.targets EndProjectSection EndProject Global diff --git a/src/NBasicExtensionMethod/BooleanExtensions.cs b/src/NBasicExtensionMethod/BooleanExtensions.cs index 6b58295..4720211 100644 --- a/src/NBasicExtensionMethod/BooleanExtensions.cs +++ b/src/NBasicExtensionMethod/BooleanExtensions.cs @@ -26,5 +26,31 @@ public static bool And(this bool b1, bool b2) { public static bool Or(this bool b1, bool b2) { return b1 || b2; } + + public static bool Not(this bool b1) + { + return !b1; + } + + public static bool NAnd(this bool b1, bool b2) + { + return !(b1 && b2); + } + + public static bool XNOr(this bool b1, bool b2) + { + return !(b1 != b2); + } + + public static bool NOr(this bool b1, bool b2) + { + return !(b1 || b2); + } + + public static bool XOr(this bool b1, bool b2) + { + return (b1 != b2); + } + } } \ No newline at end of file diff --git a/src/NBasicExtensionMethod/StringExtensions.cs b/src/NBasicExtensionMethod/StringExtensions.cs index 293ffee..b38e638 100644 --- a/src/NBasicExtensionMethod/StringExtensions.cs +++ b/src/NBasicExtensionMethod/StringExtensions.cs @@ -249,6 +249,7 @@ public static string GetAssemblyQualifiedName(this string typeName) { public static string WithCapitalizedFirstLetter(this string s) { if (s.IsNotNullOrWhiteSpace()) { + if (s.IsNullOrWhiteSpace()) throw new Exception("String to capitalize is empty."); }