From cdba96f3ddb586bb3e08a5cb699ce7960e3bb873 Mon Sep 17 00:00:00 2001 From: Matt Cantillon Date: Mon, 20 May 2013 12:33:41 +0100 Subject: [PATCH 1/3] Added missing logic gate methods to Boolean Extension --- .../BooleanExtensionTests.cs | 76 +++++++++++++------ src/NBasicExtensionMethod.sln | 6 ++ .../BooleanExtensions.cs | 26 +++++++ 3 files changed, 85 insertions(+), 23 deletions(-) diff --git a/src/NBasicExtensionMethod.Test.Fast/BooleanExtensionTests.cs b/src/NBasicExtensionMethod.Test.Fast/BooleanExtensionTests.cs index ea76c82..3064739 100644 --- a/src/NBasicExtensionMethod.Test.Fast/BooleanExtensionTests.cs +++ b/src/NBasicExtensionMethod.Test.Fast/BooleanExtensionTests.cs @@ -7,49 +7,79 @@ namespace NBasicExtensionMethod.Test.Fast [TestFixture] public class BooleanExtensionTests { + [Test] - public void And_Should_Return_True_When_Both_Values_Are_True() + [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 - bool bValueA = true; - bool bValueB = true; - + // 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) + [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() + [TestCase(true, false)] + [TestCase(false, true)] + public void Not_ReturnsOppositeOfPassedValue(bool bValueA, bool bExpectedResult) { - // Arrange - bool bValueA = false; - bool bValueB = false; + 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) + [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) { - // Arrange + 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.sln b/src/NBasicExtensionMethod.sln index 16917ac..1a60c81 100644 --- a/src/NBasicExtensionMethod.sln +++ b/src/NBasicExtensionMethod.sln @@ -5,6 +5,12 @@ 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", "{880B1B61-B1BC-41E2-AE34-AF685AAA816C}" + ProjectSection(SolutionItems) = preProject + .nuget\NuGet.exe = .nuget\NuGet.exe + .nuget\NuGet.targets = .nuget\NuGet.targets + EndProjectSection +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU diff --git a/src/NBasicExtensionMethod/BooleanExtensions.cs b/src/NBasicExtensionMethod/BooleanExtensions.cs index 08ed295..ed28ed3 100644 --- a/src/NBasicExtensionMethod/BooleanExtensions.cs +++ b/src/NBasicExtensionMethod/BooleanExtensions.cs @@ -11,5 +11,31 @@ 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 From 1bdaa731df487889a148587ebffc17b6cad5eeac Mon Sep 17 00:00:00 2001 From: Matt Cantillon Date: Mon, 20 May 2013 12:35:39 +0100 Subject: [PATCH 2/3] Fixed bug with WithCapitalizedFirstLetter method where is was throwing an exception even if the passed value was not null or white space. Added some tests around the StringExtensions --- .../NBasicExtensionMethod.Test.Fast.csproj | 1 + .../StringExtensionsTests.cs | 207 ++++++++++++++++++ src/NBasicExtensionMethod/StringExtensions.cs | 8 +- 3 files changed, 214 insertions(+), 2 deletions(-) create mode 100644 src/NBasicExtensionMethod.Test.Fast/StringExtensionsTests.cs diff --git a/src/NBasicExtensionMethod.Test.Fast/NBasicExtensionMethod.Test.Fast.csproj b/src/NBasicExtensionMethod.Test.Fast/NBasicExtensionMethod.Test.Fast.csproj index 27613e7..862dfd5 100644 --- a/src/NBasicExtensionMethod.Test.Fast/NBasicExtensionMethod.Test.Fast.csproj +++ b/src/NBasicExtensionMethod.Test.Fast/NBasicExtensionMethod.Test.Fast.csproj @@ -75,6 +75,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/StringExtensions.cs b/src/NBasicExtensionMethod/StringExtensions.cs index 1f33aec..b92403c 100644 --- a/src/NBasicExtensionMethod/StringExtensions.cs +++ b/src/NBasicExtensionMethod/StringExtensions.cs @@ -86,7 +86,11 @@ public static T TryParseOrDefault(this string s, T defaultValue) public static bool IsWebUrl(this string target) { - return !string.IsNullOrEmpty(target) && WebUriExpression.IsMatch(target); + bool bReturnValue = false; + + bReturnValue = !string.IsNullOrEmpty(target) && WebUriExpression.IsMatch(target); + + return bReturnValue; } public static bool IsEmail(this string target) @@ -282,7 +286,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."); } From cc1a2c4dc1545f49baaba67b646e9dea1828dc86 Mon Sep 17 00:00:00 2001 From: Matt Cantillon Date: Thu, 5 Jun 2014 09:19:45 +1000 Subject: [PATCH 3/3] Fixed build issues --- .../BooleanExtensionTests.cs | 8 ++++++-- src/NBasicExtensionMethod/StringExtensions.cs | 4 ---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/NBasicExtensionMethod.Test.Fast/BooleanExtensionTests.cs b/src/NBasicExtensionMethod.Test.Fast/BooleanExtensionTests.cs index 3e5ca9f..0895532 100644 --- a/src/NBasicExtensionMethod.Test.Fast/BooleanExtensionTests.cs +++ b/src/NBasicExtensionMethod.Test.Fast/BooleanExtensionTests.cs @@ -30,7 +30,8 @@ public class BooleanExtensionTests [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 + { + // Arrange // Act & Assert bValueA.And(bValueB).Should().Be(bExpectedResult); @@ -42,7 +43,8 @@ public void And_Should_Return_False_When_One_Value_Is_False(bool bValueA, bool b [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 + { + // Arrange // Act & Assert bValueA.Or(bValueB).Should().Be(bExpectedResult); @@ -52,6 +54,7 @@ public void Or_Should_Return_True_When_One_Value_Is_True(bool bValueA, bool bVal [TestCase(true, false)] [TestCase(false, true)] public void Not_ReturnsOppositeOfPassedValue(bool bValueA, bool bExpectedResult) + { bValueA.Not().Should().Be(bExpectedResult); } @@ -71,6 +74,7 @@ public void Nand_ReturnsExpectedValueForPassedParameters(bool bValueA, bool bVal [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); } diff --git a/src/NBasicExtensionMethod/StringExtensions.cs b/src/NBasicExtensionMethod/StringExtensions.cs index eb49b75..b38e638 100644 --- a/src/NBasicExtensionMethod/StringExtensions.cs +++ b/src/NBasicExtensionMethod/StringExtensions.cs @@ -86,10 +86,6 @@ public static T TryParseOrDefault(this string s, T defaultValue) public static bool IsWebUrl(this string target) { return !string.IsNullOrEmpty(target) && WebUriExpression.IsMatch(target); - - bReturnValue = !string.IsNullOrEmpty(target) && WebUriExpression.IsMatch(target); - - return bReturnValue; } public static bool IsEmail(this string target) {