1
0
mirror of https://github.com/oliverbooth/X10D synced 2024-11-25 22:58:46 +00:00

feat: add value-passthru overloads for ConcatIf

This commit is contained in:
Oliver Booth 2023-04-14 14:17:33 +01:00
parent 6f3a667e37
commit 23dee3d2b8
No known key found for this signature in database
GPG Key ID: 20BEB9DC87961025
2 changed files with 154 additions and 5 deletions

View File

@ -112,8 +112,10 @@ public class StringTests
{ {
Assert.That("Hello".ConcatIf(true, " World"), Is.EqualTo("Hello World")); Assert.That("Hello".ConcatIf(true, " World"), Is.EqualTo("Hello World"));
Assert.That("Hello".ConcatIf(true, () => " World"), Is.EqualTo("Hello World")); Assert.That("Hello".ConcatIf(true, () => " World"), Is.EqualTo("Hello World"));
Assert.That("Hello".ConcatIf(true, _ => " World"), Is.EqualTo("Hello World"));
Assert.That("Hello".ConcatIf(() => true, " World"), Is.EqualTo("Hello World")); Assert.That("Hello".ConcatIf(() => true, " World"), Is.EqualTo("Hello World"));
Assert.That("Hello".ConcatIf(() => true, () => " World"), Is.EqualTo("Hello World")); Assert.That("Hello".ConcatIf(() => true, () => " World"), Is.EqualTo("Hello World"));
Assert.That("Hello".ConcatIf(() => true, _ => " World"), Is.EqualTo("Hello World"));
}); });
} }
@ -124,23 +126,36 @@ public class StringTests
{ {
Assert.That("Hello".ConcatIf(false, " World"), Is.EqualTo("Hello")); Assert.That("Hello".ConcatIf(false, " World"), Is.EqualTo("Hello"));
Assert.That("Hello".ConcatIf(false, () => " World"), Is.EqualTo("Hello")); Assert.That("Hello".ConcatIf(false, () => " World"), Is.EqualTo("Hello"));
Assert.That("Hello".ConcatIf(false, _ => " World"), Is.EqualTo("Hello"));
Assert.That("Hello".ConcatIf(() => false, " World"), Is.EqualTo("Hello")); Assert.That("Hello".ConcatIf(() => false, " World"), Is.EqualTo("Hello"));
Assert.That("Hello".ConcatIf(() => false, () => " World"), Is.EqualTo("Hello")); Assert.That("Hello".ConcatIf(() => false, () => " World"), Is.EqualTo("Hello"));
Assert.That("Hello".ConcatIf(() => false, _ => " World"), Is.EqualTo("Hello"));
Assert.That("Hello".ConcatIf(_ => false, " World"), Is.EqualTo("Hello"));
Assert.That("Hello".ConcatIf(_ => false, () => " World"), Is.EqualTo("Hello"));
Assert.That("Hello".ConcatIf(_ => false, _ => " World"), Is.EqualTo("Hello"));
}); });
} }
[Test] [Test]
public void ConcatIf_ShouldThrowArgumentNullException_GivenNullConditionFactory() public void ConcatIf_ShouldThrowArgumentNullException_GivenNullConditionFactory()
{ {
Assert.Throws<ArgumentNullException>(() => _ = "".ConcatIf(null!, "Hello World")); Assert.Throws<ArgumentNullException>(() => _ = "".ConcatIf((Func<bool>)null!, "Hello World"));
Assert.Throws<ArgumentNullException>(() => _ = "".ConcatIf(null!, () => "Hello World")); Assert.Throws<ArgumentNullException>(() => _ = "".ConcatIf((Func<bool>)null!, () => "Hello World"));
Assert.Throws<ArgumentNullException>(() => _ = "".ConcatIf((Func<bool>)null!, _ => "Hello World"));
Assert.Throws<ArgumentNullException>(() => _ = "".ConcatIf((Func<string?, bool>)null!, "Hello World"));
Assert.Throws<ArgumentNullException>(() => _ = "".ConcatIf((Func<string?, bool>)null!, () => "Hello World"));
Assert.Throws<ArgumentNullException>(() => _ = "".ConcatIf((Func<string?, bool>)null!, _ => "Hello World"));
} }
[Test] [Test]
public void ConcatIf_ShouldThrowArgumentNullException_GivenNullValueFactory() public void ConcatIf_ShouldThrowArgumentNullException_GivenNullValueFactory()
{ {
Assert.Throws<ArgumentNullException>(() => _ = "".ConcatIf(true, (Func<string>?)null!)); Assert.Throws<ArgumentNullException>(() => _ = "".ConcatIf(true, (Func<string?>)null!));
Assert.Throws<ArgumentNullException>(() => _ = "".ConcatIf(() => true, (Func<string>?)null!)); Assert.Throws<ArgumentNullException>(() => _ = "".ConcatIf(() => true, (Func<string?>)null!));
Assert.Throws<ArgumentNullException>(() => _ = "".ConcatIf(_ => true, (Func<string?>)null!));
Assert.Throws<ArgumentNullException>(() => _ = "".ConcatIf(true, (Func<string?, string?>)null!));
Assert.Throws<ArgumentNullException>(() => _ = "".ConcatIf(() => true, (Func<string?, string?>)null!));
Assert.Throws<ArgumentNullException>(() => _ = "".ConcatIf(_ => true, (Func<string?, string?>)null!));
} }
[Test] [Test]

View File

@ -157,6 +157,27 @@ public static class StringExtensions
return conditionFactory() ? value + appendValue : value; return conditionFactory() ? value + appendValue : value;
} }
/// <summary>
/// Appends a string to the current string if the specified condition evaluates to <see langword="true" />.
/// </summary>
/// <param name="value">The current string.</param>
/// <param name="conditionFactory">
/// The function that returns the condition to evaluate, with <paramref name="value" /> given as an argument.
/// </param>
/// <param name="appendValue">The string to append if the condition is true.</param>
/// <returns>The concatenated string.</returns>
/// <exception cref="ArgumentNullException"><paramref name="conditionFactory" /> is <see langword="null" />.</exception>
[Pure]
public static string? ConcatIf(this string? value, Func<string?, bool> conditionFactory, string? appendValue)
{
if (conditionFactory is null)
{
throw new ArgumentNullException(nameof(conditionFactory));
}
return conditionFactory(value) ? value + appendValue : value;
}
/// <summary> /// <summary>
/// Appends a string to the current string if the specified condition evaluates to <see langword="true" />. /// Appends a string to the current string if the specified condition evaluates to <see langword="true" />.
/// </summary> /// </summary>
@ -164,6 +185,7 @@ public static class StringExtensions
/// <param name="condition">The condition to evaluate.</param> /// <param name="condition">The condition to evaluate.</param>
/// <param name="valueFactory">The function that returns the string to append if the condition is true.</param> /// <param name="valueFactory">The function that returns the string to append if the condition is true.</param>
/// <returns>The concatenated string.</returns> /// <returns>The concatenated string.</returns>
/// <exception cref="ArgumentNullException"><paramref name="valueFactory" /> is <see langword="null" />.</exception>
[Pure] [Pure]
public static string? ConcatIf(this string? value, bool condition, Func<string?> valueFactory) public static string? ConcatIf(this string? value, bool condition, Func<string?> valueFactory)
{ {
@ -175,6 +197,28 @@ public static class StringExtensions
return condition ? value + valueFactory() : value; return condition ? value + valueFactory() : value;
} }
/// <summary>
/// Appends a string to the current string if the specified condition evaluates to <see langword="true" />.
/// </summary>
/// <param name="value">The current string.</param>
/// <param name="condition">The condition to evaluate.</param>
/// <param name="valueFactory">
/// The function that returns the string to append if the condition is true, with <paramref name="value" /> given as an
/// argument.
/// </param>
/// <returns>The concatenated string.</returns>
/// <exception cref="ArgumentNullException"><paramref name="valueFactory" /> is <see langword="null" />.</exception>
[Pure]
public static string? ConcatIf(this string? value, bool condition, Func<string?, string?> valueFactory)
{
if (valueFactory is null)
{
throw new ArgumentNullException(nameof(valueFactory));
}
return condition ? value + valueFactory(value) : value;
}
/// <summary> /// <summary>
/// Appends a string to the current string if the specified condition evaluates to <see langword="true" />. /// Appends a string to the current string if the specified condition evaluates to <see langword="true" />.
/// </summary> /// </summary>
@ -182,7 +226,9 @@ public static class StringExtensions
/// <param name="conditionFactory">The function that returns the condition to evaluate.</param> /// <param name="conditionFactory">The function that returns the condition to evaluate.</param>
/// <param name="valueFactory">The function that returns the string to append if the condition is true.</param> /// <param name="valueFactory">The function that returns the string to append if the condition is true.</param>
/// <returns>The concatenated string.</returns> /// <returns>The concatenated string.</returns>
/// <exception cref="ArgumentNullException"><paramref name="conditionFactory" /> is <see langword="null" />.</exception> /// <exception cref="ArgumentNullException">
/// <paramref name="conditionFactory" /> or <paramref name="valueFactory" /> is <see langword="null" />.
/// </exception>
[Pure] [Pure]
public static string? ConcatIf(this string? value, Func<bool> conditionFactory, Func<string?> valueFactory) public static string? ConcatIf(this string? value, Func<bool> conditionFactory, Func<string?> valueFactory)
{ {
@ -199,6 +245,94 @@ public static class StringExtensions
return conditionFactory() ? value + valueFactory() : value; return conditionFactory() ? value + valueFactory() : value;
} }
/// <summary>
/// Appends a string to the current string if the specified condition evaluates to <see langword="true" />.
/// </summary>
/// <param name="value">The current string.</param>
/// <param name="conditionFactory">The function that returns the condition to evaluate.</param>
/// <param name="valueFactory">
/// The function that returns the string to append if the condition is true, with <paramref name="value" /> given as an
/// argument.
/// </param>
/// <returns>The concatenated string.</returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="conditionFactory" /> or <paramref name="valueFactory" /> is <see langword="null" />.
/// </exception>
[Pure]
public static string? ConcatIf(this string? value, Func<bool> conditionFactory, Func<string?, string?> valueFactory)
{
if (conditionFactory is null)
{
throw new ArgumentNullException(nameof(conditionFactory));
}
if (valueFactory is null)
{
throw new ArgumentNullException(nameof(valueFactory));
}
return conditionFactory() ? value + valueFactory(value) : value;
}
/// <summary>
/// Appends a string to the current string if the specified condition evaluates to <see langword="true" />.
/// </summary>
/// <param name="value">The current string.</param>
/// <param name="conditionFactory">
/// The function that returns the condition to evaluate, with <paramref name="value" /> given as an argument.
/// </param>
/// <param name="valueFactory">The function that returns the string to append if the condition is true.</param>
/// <returns>The concatenated string.</returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="conditionFactory" /> or <paramref name="valueFactory" /> is <see langword="null" />.
/// </exception>
[Pure]
public static string? ConcatIf(this string? value, Func<string?, bool> conditionFactory, Func<string?> valueFactory)
{
if (conditionFactory is null)
{
throw new ArgumentNullException(nameof(conditionFactory));
}
if (valueFactory is null)
{
throw new ArgumentNullException(nameof(valueFactory));
}
return conditionFactory(value) ? value + valueFactory() : value;
}
/// <summary>
/// Appends a string to the current string if the specified condition evaluates to <see langword="true" />.
/// </summary>
/// <param name="value">The current string.</param>
/// <param name="conditionFactory">
/// The function that returns the condition to evaluate, with <paramref name="value" /> given as an argument.
/// </param>
/// <param name="valueFactory">
/// The function that returns the string to append if the condition is true, with <paramref name="value" /> given as an
/// argument.
/// </param>
/// <returns>The concatenated string.</returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="conditionFactory" /> or <paramref name="valueFactory" /> is <see langword="null" />.
/// </exception>
[Pure]
public static string? ConcatIf(this string? value, Func<string?, bool> conditionFactory, Func<string?, string?> valueFactory)
{
if (conditionFactory is null)
{
throw new ArgumentNullException(nameof(conditionFactory));
}
if (valueFactory is null)
{
throw new ArgumentNullException(nameof(valueFactory));
}
return conditionFactory(value) ? value + valueFactory(value) : value;
}
/// <summary> /// <summary>
/// Counts the occurrences of a character within the current character span. /// Counts the occurrences of a character within the current character span.
/// </summary> /// </summary>