Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More code coverage #1871

Merged
merged 8 commits into from Apr 5, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 3 additions & 12 deletions Src/FluentAssertions/Common/Guard.cs
Expand Up @@ -8,10 +8,7 @@ internal static class Guard
{
public static void ThrowIfArgumentIsNull<T>([ValidatedNotNull] T obj, string paramName)
{
if (obj is null)
{
throw new ArgumentNullException(paramName);
}
ThrowIfArgumentIsNull(obj, paramName, string.Empty);
ITaluone marked this conversation as resolved.
Show resolved Hide resolved
}

public static void ThrowIfArgumentIsNull<T>([ValidatedNotNull] T obj, string paramName, string message)
Expand All @@ -24,10 +21,7 @@ public static void ThrowIfArgumentIsNull<T>([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)
Expand All @@ -49,10 +43,7 @@ public static void ThrowIfArgumentIsOutOfRange<T>(T value, string paramName)

public static void ThrowIfArgumentContainsNull<T>(IEnumerable<T> 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<T>(IEnumerable<T> values, string paramName, string message)
Expand Down
89 changes: 89 additions & 0 deletions 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<ArgumentNullException>();
}

[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<ArgumentNullException>();
}

[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<ArgumentNullException>();
}

[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();
}
}
}