From 05fb2a5524c5dbaf6bbb8585205ea38ccfd8fb5b Mon Sep 17 00:00:00 2001 From: Lukas Gasselsberger | alu-one Date: Thu, 31 Mar 2022 14:28:51 +0200 Subject: [PATCH 1/8] Add test for guard helper class --- Src/FluentAssertions/Common/Guard.cs | 15 +--- .../Common/GuardSpecs.cs | 89 +++++++++++++++++++ 2 files changed, 92 insertions(+), 12 deletions(-) create mode 100644 Tests/FluentAssertions.Specs/Common/GuardSpecs.cs diff --git a/Src/FluentAssertions/Common/Guard.cs b/Src/FluentAssertions/Common/Guard.cs index fdfb97520b..bcdf6fd421 100644 --- a/Src/FluentAssertions/Common/Guard.cs +++ b/Src/FluentAssertions/Common/Guard.cs @@ -8,10 +8,7 @@ internal static class Guard { public static void ThrowIfArgumentIsNull([ValidatedNotNull] T obj, string paramName) { - if (obj is null) - { - throw new ArgumentNullException(paramName); - } + ThrowIfArgumentIsNull(obj, paramName, string.Empty); } public static void ThrowIfArgumentIsNull([ValidatedNotNull] T obj, string paramName, string message) @@ -24,10 +21,7 @@ public static void ThrowIfArgumentIsNull([ValidatedNotNull] T obj, string par public static void ThrowIfArgumentIsNullOrEmpty([ValidatedNotNull] string str, string paramName) { - if (string.IsNullOrEmpty(str)) - { - throw new ArgumentNullException(paramName); - } + ThrowIfArgumentIsNullOrEmpty(str, paramName, string.Empty); } public static void ThrowIfArgumentIsNullOrEmpty([ValidatedNotNull] string str, string paramName, string message) @@ -49,10 +43,7 @@ public static void ThrowIfArgumentIsOutOfRange(T value, string paramName) public static void ThrowIfArgumentContainsNull(IEnumerable values, string paramName) { - if (values.Any(t => t is null)) - { - throw new ArgumentNullException(paramName, "Collection contains a null value"); - } + ThrowIfArgumentContainsNull(values, paramName, "Collection contains a null value"); } public static void ThrowIfArgumentContainsNull(IEnumerable values, string paramName, string message) diff --git a/Tests/FluentAssertions.Specs/Common/GuardSpecs.cs b/Tests/FluentAssertions.Specs/Common/GuardSpecs.cs new file mode 100644 index 0000000000..5057c97625 --- /dev/null +++ b/Tests/FluentAssertions.Specs/Common/GuardSpecs.cs @@ -0,0 +1,89 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using FluentAssertions.Common; +using Xunit; + +namespace FluentAssertions.Specs.Common +{ + public class GuardSpecs + { + [Fact] + public void When_element_is_null_it_throws() + { + // Arrange + object o = null; + + // Act + Action act = () => Guard.ThrowIfArgumentIsNull(o, nameof(o), ""); + + // Assert + act.Should().ThrowExactly(); + } + + [Fact] + public void When_element_is_not_null_it_do_not_throw() + { + // Arrange + object o = new object(); + + // Act + Action act = () => Guard.ThrowIfArgumentIsNull(o, nameof(o), ""); + + // Assert + act.Should().NotThrow(); + } + + [Theory] + [InlineData("")] + [InlineData(null)] + public void When_argument_is_null_or_empty_it_throws(string s) + { + // Act + Action act = () => Guard.ThrowIfArgumentIsNullOrEmpty(s, nameof(s)); + + // Assert + act.Should().ThrowExactly(); + } + + [Theory] + [InlineData("a")] + [InlineData("\n")] + public void When_argument_is_not_null_or_empty_it_does_not_throw(string s) + { + // Act + Action act = () => Guard.ThrowIfArgumentIsNullOrEmpty(s, nameof(s)); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_any_of_the_elements_is_null_it_throws() + { + // Arrange + object[] o = new object[] { new object(), null }; + + // Act + Action act = () => Guard.ThrowIfArgumentContainsNull(o, nameof(o)); + + // Assert + act.Should().ThrowExactly(); + } + + [Fact] + public void When_all_of_the_elements_are_not_null_it_does_not_throw() + { + // Arrange + object[] o = new object[] { new object(), new object() }; + + // Act + Action act = () => Guard.ThrowIfArgumentContainsNull(o, nameof(o)); + + // Assert + act.Should().NotThrow(); + } + } +} From 2f5360896fe3e4abdb38b4968b47218282fa91c0 Mon Sep 17 00:00:00 2001 From: Lukas Gasselsberger | alu-one Date: Fri, 1 Apr 2022 07:00:53 +0200 Subject: [PATCH 2/8] Partly revert commit 05fb2a5524c5dbaf6bbb8585205ea38ccfd8fb5b --- Src/FluentAssertions/Common/Guard.cs | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/Src/FluentAssertions/Common/Guard.cs b/Src/FluentAssertions/Common/Guard.cs index bcdf6fd421..c830bd84ac 100644 --- a/Src/FluentAssertions/Common/Guard.cs +++ b/Src/FluentAssertions/Common/Guard.cs @@ -8,7 +8,10 @@ internal static class Guard { public static void ThrowIfArgumentIsNull([ValidatedNotNull] T obj, string paramName) { - ThrowIfArgumentIsNull(obj, paramName, string.Empty); + if (obj is null) + { + throw new ArgumentNullException(paramName); + } } public static void ThrowIfArgumentIsNull([ValidatedNotNull] T obj, string paramName, string message) @@ -21,7 +24,10 @@ public static void ThrowIfArgumentIsNull([ValidatedNotNull] T obj, string par public static void ThrowIfArgumentIsNullOrEmpty([ValidatedNotNull] string str, string paramName) { - ThrowIfArgumentIsNullOrEmpty(str, paramName, string.Empty); + if (string.IsNullOrEmpty(str)) + { + throw new ArgumentNullException(paramName); + } } public static void ThrowIfArgumentIsNullOrEmpty([ValidatedNotNull] string str, string paramName, string message) @@ -43,7 +49,10 @@ public static void ThrowIfArgumentIsOutOfRange(T value, string paramName) public static void ThrowIfArgumentContainsNull(IEnumerable values, string paramName) { - ThrowIfArgumentContainsNull(values, paramName, "Collection contains a null value"); + if (values.Any(t => t is null)) + { + throw new ArgumentNullException(paramName, "Collection contains a null value"); + }; } public static void ThrowIfArgumentContainsNull(IEnumerable values, string paramName, string message) From f1b39726e69acff30f145506fa8ce752749a6f1a Mon Sep 17 00:00:00 2001 From: Lukas Gasselsberger | alu-one Date: Fri, 1 Apr 2022 07:03:46 +0200 Subject: [PATCH 3/8] Small typo --- Src/FluentAssertions/Common/Guard.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Src/FluentAssertions/Common/Guard.cs b/Src/FluentAssertions/Common/Guard.cs index c830bd84ac..fdfb97520b 100644 --- a/Src/FluentAssertions/Common/Guard.cs +++ b/Src/FluentAssertions/Common/Guard.cs @@ -52,7 +52,7 @@ public static void ThrowIfArgumentContainsNull(IEnumerable values, string if (values.Any(t => t is null)) { throw new ArgumentNullException(paramName, "Collection contains a null value"); - }; + } } public static void ThrowIfArgumentContainsNull(IEnumerable values, string paramName, string message) From b571510f99f6c0931d414b5c551b04dd20ce5782 Mon Sep 17 00:00:00 2001 From: Lukas Gasselsberger | alu-one Date: Fri, 1 Apr 2022 08:18:52 +0200 Subject: [PATCH 4/8] Add tests for missing `Guard` methods --- .../Common/GuardSpecs.cs | 89 +++++++++++++++++-- 1 file changed, 83 insertions(+), 6 deletions(-) diff --git a/Tests/FluentAssertions.Specs/Common/GuardSpecs.cs b/Tests/FluentAssertions.Specs/Common/GuardSpecs.cs index 5057c97625..7dfb189609 100644 --- a/Tests/FluentAssertions.Specs/Common/GuardSpecs.cs +++ b/Tests/FluentAssertions.Specs/Common/GuardSpecs.cs @@ -4,6 +4,7 @@ using System.Text; using System.Threading.Tasks; using FluentAssertions.Common; +using FluentAssertions.Execution; using Xunit; namespace FluentAssertions.Specs.Common @@ -11,7 +12,7 @@ namespace FluentAssertions.Specs.Common public class GuardSpecs { [Fact] - public void When_element_is_null_it_throws() + public void When_element_is_null_it_throws_with_empty_message() { // Arrange object o = null; @@ -24,7 +25,20 @@ public void When_element_is_null_it_throws() } [Fact] - public void When_element_is_not_null_it_do_not_throw() + public void When_element_is_null_it_throws_with_no_message() + { + // Arrange + object o = null; + + // Act + Action act = () => Guard.ThrowIfArgumentIsNull(o, nameof(o)); + + // Assert + act.Should().ThrowExactly(); + } + + [Fact] + public void When_element_is_not_null_it_do_not_throw_with_empty_message() { // Arrange object o = new object(); @@ -36,10 +50,35 @@ public void When_element_is_not_null_it_do_not_throw() act.Should().NotThrow(); } + [Fact] + public void When_element_is_not_null_it_do_not_throw_with_no_message() + { + // Arrange + object o = new object(); + + // Act + Action act = () => Guard.ThrowIfArgumentIsNull(o, nameof(o)); + + // Assert + act.Should().NotThrow(); + } + + [Theory] + [InlineData("")] + [InlineData(null)] + public void When_argument_is_null_or_empty_it_throws_with_empty_message(string s) + { + // Act + Action act = () => Guard.ThrowIfArgumentIsNullOrEmpty(s, nameof(s), ""); + + // Assert + act.Should().ThrowExactly(); + } + [Theory] [InlineData("")] [InlineData(null)] - public void When_argument_is_null_or_empty_it_throws(string s) + public void When_argument_is_null_or_empty_it_throws_with_no_message(string s) { // Act Action act = () => Guard.ThrowIfArgumentIsNullOrEmpty(s, nameof(s)); @@ -51,7 +90,19 @@ public void When_argument_is_null_or_empty_it_throws(string s) [Theory] [InlineData("a")] [InlineData("\n")] - public void When_argument_is_not_null_or_empty_it_does_not_throw(string s) + public void When_argument_is_not_null_or_empty_it_does_not_throw_with_empty_message(string s) + { + // Act + Action act = () => Guard.ThrowIfArgumentIsNullOrEmpty(s, nameof(s), ""); + + // Assert + act.Should().NotThrow(); + } + + [Theory] + [InlineData("a")] + [InlineData("\n")] + public void When_argument_is_not_null_or_empty_it_does_not_throw_with_no_message(string s) { // Act Action act = () => Guard.ThrowIfArgumentIsNullOrEmpty(s, nameof(s)); @@ -61,7 +112,20 @@ public void When_argument_is_not_null_or_empty_it_does_not_throw(string s) } [Fact] - public void When_any_of_the_elements_is_null_it_throws() + public void When_any_of_the_elements_is_null_it_throws_with_empty_message() + { + // Arrange + object[] o = new object[] { new object(), null }; + + // Act + Action act = () => Guard.ThrowIfArgumentContainsNull(o, nameof(o), ""); + + // Assert + act.Should().ThrowExactly(); + } + + [Fact] + public void When_any_of_the_elements_is_null_it_throws_with_no_message() { // Arrange object[] o = new object[] { new object(), null }; @@ -74,7 +138,20 @@ public void When_any_of_the_elements_is_null_it_throws() } [Fact] - public void When_all_of_the_elements_are_not_null_it_does_not_throw() + public void When_all_of_the_elements_are_not_null_it_does_not_throw_with_empty_message() + { + // Arrange + object[] o = new object[] { new object(), new object() }; + + // Act + Action act = () => Guard.ThrowIfArgumentContainsNull(o, nameof(o), ""); + + // Assert + act.Should().NotThrow(); + } + + [Fact] + public void When_all_of_the_elements_are_not_null_it_does_not_throw_with_no_message() { // Arrange object[] o = new object[] { new object(), new object() }; From 01003e022be961e1177cc94acfc739aa921a9dcc Mon Sep 17 00:00:00 2001 From: Lukas Gasselsberger | alu-one Date: Fri, 1 Apr 2022 12:06:37 +0200 Subject: [PATCH 5/8] Add missing test for `ThrowIfArgumentIsOutOfRange()` --- .../Common/GuardSpecs.cs | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/Tests/FluentAssertions.Specs/Common/GuardSpecs.cs b/Tests/FluentAssertions.Specs/Common/GuardSpecs.cs index 7dfb189609..a61f69495d 100644 --- a/Tests/FluentAssertions.Specs/Common/GuardSpecs.cs +++ b/Tests/FluentAssertions.Specs/Common/GuardSpecs.cs @@ -162,5 +162,31 @@ public void When_all_of_the_elements_are_not_null_it_does_not_throw_with_no_mess // Assert act.Should().NotThrow(); } + + [Fact] + public void When_enum_member_is_not_available_it_throws() + { + // Arrange + var actual = (CSharpAccessModifier)1000; + + // Act + Action act = () => Guard.ThrowIfArgumentIsOutOfRange(actual, nameof(actual)); + + // Assert + act.Should().Throw(); + } + + [Fact] + public void When_enum_member_is_available_it_does_not_throw() + { + // Arrange + var actual = (CSharpAccessModifier)1; + + // Act + Action act = () => Guard.ThrowIfArgumentIsOutOfRange(actual, nameof(actual)); + + // Assert + act.Should().NotThrow(); + } } } From ab943d6944dde272e8f3470103bc7bbdb0bbab2c Mon Sep 17 00:00:00 2001 From: Lukas Gasselsberger | alu-one Date: Mon, 4 Apr 2022 08:28:50 +0200 Subject: [PATCH 6/8] Remove unused code --- Src/FluentAssertions/Common/Guard.cs | 8 ------ .../Common/GuardSpecs.cs | 26 ------------------- 2 files changed, 34 deletions(-) diff --git a/Src/FluentAssertions/Common/Guard.cs b/Src/FluentAssertions/Common/Guard.cs index fdfb97520b..c6e2e4c15b 100644 --- a/Src/FluentAssertions/Common/Guard.cs +++ b/Src/FluentAssertions/Common/Guard.cs @@ -55,14 +55,6 @@ public static void ThrowIfArgumentContainsNull(IEnumerable values, string } } - public static void ThrowIfArgumentContainsNull(IEnumerable values, string paramName, string message) - { - if (values.Any(t => t is null)) - { - throw new ArgumentNullException(paramName, message); - } - } - public static void ThrowIfArgumentIsEmpty(IEnumerable values, string paramName, string message) { if (!values.Any()) diff --git a/Tests/FluentAssertions.Specs/Common/GuardSpecs.cs b/Tests/FluentAssertions.Specs/Common/GuardSpecs.cs index a61f69495d..d8e6eeead0 100644 --- a/Tests/FluentAssertions.Specs/Common/GuardSpecs.cs +++ b/Tests/FluentAssertions.Specs/Common/GuardSpecs.cs @@ -111,19 +111,6 @@ public void When_argument_is_not_null_or_empty_it_does_not_throw_with_no_message act.Should().NotThrow(); } - [Fact] - public void When_any_of_the_elements_is_null_it_throws_with_empty_message() - { - // Arrange - object[] o = new object[] { new object(), null }; - - // Act - Action act = () => Guard.ThrowIfArgumentContainsNull(o, nameof(o), ""); - - // Assert - act.Should().ThrowExactly(); - } - [Fact] public void When_any_of_the_elements_is_null_it_throws_with_no_message() { @@ -137,19 +124,6 @@ public void When_any_of_the_elements_is_null_it_throws_with_no_message() act.Should().ThrowExactly(); } - [Fact] - public void When_all_of_the_elements_are_not_null_it_does_not_throw_with_empty_message() - { - // Arrange - object[] o = new object[] { new object(), new object() }; - - // Act - Action act = () => Guard.ThrowIfArgumentContainsNull(o, nameof(o), ""); - - // Assert - act.Should().NotThrow(); - } - [Fact] public void When_all_of_the_elements_are_not_null_it_does_not_throw_with_no_message() { From 47563d281552e387dadfdca9f9af027bfe15d081 Mon Sep 17 00:00:00 2001 From: Lukas Gasselsberger | alu-one Date: Mon, 4 Apr 2022 08:43:49 +0200 Subject: [PATCH 7/8] Add last missing tests for `Guard` --- .../Common/GuardSpecs.cs | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/Tests/FluentAssertions.Specs/Common/GuardSpecs.cs b/Tests/FluentAssertions.Specs/Common/GuardSpecs.cs index d8e6eeead0..6504767559 100644 --- a/Tests/FluentAssertions.Specs/Common/GuardSpecs.cs +++ b/Tests/FluentAssertions.Specs/Common/GuardSpecs.cs @@ -162,5 +162,31 @@ public void When_enum_member_is_available_it_does_not_throw() // Assert act.Should().NotThrow(); } + + [Fact] + public void When_collection_has_no_elements_it_throws() + { + // Arrange + var actual = new object[0]; + + // Act + Action act = () => Guard.ThrowIfArgumentIsEmpty(actual, nameof(actual), ""); + + // Assert + act.Should().Throw(); + } + + [Fact] + public void When_collection_is_not_empty_it_does_not_throw() + { + // Arrange + var actual = new object[] { new object() }; + + // Act + Action act = () => Guard.ThrowIfArgumentIsEmpty(actual, nameof(actual), ""); + + // Assert + act.Should().NotThrow(); + } } } From 9eef107145e2c079dc623622e26b6909604ac195 Mon Sep 17 00:00:00 2001 From: Lukas Gasselsberger | alu-one Date: Tue, 5 Apr 2022 07:30:58 +0200 Subject: [PATCH 8/8] Remove all `Guard` tests --- .../Common/GuardSpecs.cs | 192 ------------------ 1 file changed, 192 deletions(-) delete mode 100644 Tests/FluentAssertions.Specs/Common/GuardSpecs.cs diff --git a/Tests/FluentAssertions.Specs/Common/GuardSpecs.cs b/Tests/FluentAssertions.Specs/Common/GuardSpecs.cs deleted file mode 100644 index 6504767559..0000000000 --- a/Tests/FluentAssertions.Specs/Common/GuardSpecs.cs +++ /dev/null @@ -1,192 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using FluentAssertions.Common; -using FluentAssertions.Execution; -using Xunit; - -namespace FluentAssertions.Specs.Common -{ - public class GuardSpecs - { - [Fact] - public void When_element_is_null_it_throws_with_empty_message() - { - // Arrange - object o = null; - - // Act - Action act = () => Guard.ThrowIfArgumentIsNull(o, nameof(o), ""); - - // Assert - act.Should().ThrowExactly(); - } - - [Fact] - public void When_element_is_null_it_throws_with_no_message() - { - // Arrange - object o = null; - - // Act - Action act = () => Guard.ThrowIfArgumentIsNull(o, nameof(o)); - - // Assert - act.Should().ThrowExactly(); - } - - [Fact] - public void When_element_is_not_null_it_do_not_throw_with_empty_message() - { - // Arrange - object o = new object(); - - // Act - Action act = () => Guard.ThrowIfArgumentIsNull(o, nameof(o), ""); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_element_is_not_null_it_do_not_throw_with_no_message() - { - // Arrange - object o = new object(); - - // Act - Action act = () => Guard.ThrowIfArgumentIsNull(o, nameof(o)); - - // Assert - act.Should().NotThrow(); - } - - [Theory] - [InlineData("")] - [InlineData(null)] - public void When_argument_is_null_or_empty_it_throws_with_empty_message(string s) - { - // Act - Action act = () => Guard.ThrowIfArgumentIsNullOrEmpty(s, nameof(s), ""); - - // Assert - act.Should().ThrowExactly(); - } - - [Theory] - [InlineData("")] - [InlineData(null)] - public void When_argument_is_null_or_empty_it_throws_with_no_message(string s) - { - // Act - Action act = () => Guard.ThrowIfArgumentIsNullOrEmpty(s, nameof(s)); - - // Assert - act.Should().ThrowExactly(); - } - - [Theory] - [InlineData("a")] - [InlineData("\n")] - public void When_argument_is_not_null_or_empty_it_does_not_throw_with_empty_message(string s) - { - // Act - Action act = () => Guard.ThrowIfArgumentIsNullOrEmpty(s, nameof(s), ""); - - // Assert - act.Should().NotThrow(); - } - - [Theory] - [InlineData("a")] - [InlineData("\n")] - public void When_argument_is_not_null_or_empty_it_does_not_throw_with_no_message(string s) - { - // Act - Action act = () => Guard.ThrowIfArgumentIsNullOrEmpty(s, nameof(s)); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_any_of_the_elements_is_null_it_throws_with_no_message() - { - // Arrange - object[] o = new object[] { new object(), null }; - - // Act - Action act = () => Guard.ThrowIfArgumentContainsNull(o, nameof(o)); - - // Assert - act.Should().ThrowExactly(); - } - - [Fact] - public void When_all_of_the_elements_are_not_null_it_does_not_throw_with_no_message() - { - // Arrange - object[] o = new object[] { new object(), new object() }; - - // Act - Action act = () => Guard.ThrowIfArgumentContainsNull(o, nameof(o)); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_enum_member_is_not_available_it_throws() - { - // Arrange - var actual = (CSharpAccessModifier)1000; - - // Act - Action act = () => Guard.ThrowIfArgumentIsOutOfRange(actual, nameof(actual)); - - // Assert - act.Should().Throw(); - } - - [Fact] - public void When_enum_member_is_available_it_does_not_throw() - { - // Arrange - var actual = (CSharpAccessModifier)1; - - // Act - Action act = () => Guard.ThrowIfArgumentIsOutOfRange(actual, nameof(actual)); - - // Assert - act.Should().NotThrow(); - } - - [Fact] - public void When_collection_has_no_elements_it_throws() - { - // Arrange - var actual = new object[0]; - - // Act - Action act = () => Guard.ThrowIfArgumentIsEmpty(actual, nameof(actual), ""); - - // Assert - act.Should().Throw(); - } - - [Fact] - public void When_collection_is_not_empty_it_does_not_throw() - { - // Arrange - var actual = new object[] { new object() }; - - // Act - Action act = () => Guard.ThrowIfArgumentIsEmpty(actual, nameof(actual), ""); - - // Assert - act.Should().NotThrow(); - } - } -}