Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 0c122e2

Browse files
authoredMar 2, 2021
Merge pull request #5 from pfpack/feature/Primitives
Primitives: Strings / General
2 parents 87d0d84 + 153c627 commit 0c122e2

File tree

9 files changed

+161
-9
lines changed

9 files changed

+161
-9
lines changed
 

‎src/primitives/Primitives.Tests/StringExtensions.Tests/OrEmpty.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public void OrEmpty_SourceIsNotNull_ExpectSourceValue(
2626
string source)
2727
{
2828
var actual = source.OrEmpty();
29-
Assert.AreEqual(source, actual);
29+
Assert.AreSame(source, actual);
3030
}
3131
}
3232
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#nullable enable
2+
3+
using NUnit.Framework;
4+
using System;
5+
using static PrimeFuncPack.UnitTest.TestData;
6+
7+
namespace PrimeFuncPack.Primitives.Tests
8+
{
9+
partial class StringExtensionsTests
10+
{
11+
[Test]
12+
public void OrNullIfEmpty_SourceIsEmpty_ExpectNull()
13+
{
14+
string? source = string.Empty;
15+
16+
var actual = source.OrNullIfEmpty();
17+
Assert.IsNull(actual);
18+
}
19+
20+
[Test]
21+
[TestCase(null)]
22+
[TestCase(WhiteSpaceString)]
23+
[TestCase(TabString)]
24+
[TestCase(SomeString)]
25+
public void OrNullIfEmpty_SourceIsNotEmpty_ExpectSourceValue(
26+
string? source)
27+
{
28+
var actual = source.OrNullIfEmpty();
29+
Assert.AreSame(source, actual);
30+
}
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#nullable enable
2+
3+
using NUnit.Framework;
4+
using System;
5+
using static PrimeFuncPack.UnitTest.TestData;
6+
7+
namespace PrimeFuncPack.Primitives.Tests
8+
{
9+
partial class StringExtensionsTests
10+
{
11+
[Test]
12+
[TestCase(EmptyString)]
13+
[TestCase(WhiteSpaceString)]
14+
[TestCase(ThreeWhiteSpacesString)]
15+
[TestCase(TabString)]
16+
public void OrNullIfWhiteSpace_SourceIsWhiteSpace_ExpectNull(
17+
string? source)
18+
{
19+
var actual = source.OrNullIfWhiteSpace();
20+
Assert.IsNull(actual);
21+
}
22+
23+
[Test]
24+
[TestCase(null)]
25+
[TestCase(SomeString)]
26+
public void OrNullIfWhiteSpace_SourceIsNotWhiteSpace_ExpectSourceValue(
27+
string? source)
28+
{
29+
var actual = source.OrNullIfWhiteSpace();
30+
Assert.AreSame(source, actual);
31+
}
32+
}
33+
}

‎src/primitives/Primitives.Tests/Strings.Tests/OrEmpty.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public void OrEmpty_SourceIsNotNull_ExpectSourceValue(
2626
string source)
2727
{
2828
var actual = Strings.OrEmpty(source);
29-
Assert.AreEqual(source, actual);
29+
Assert.AreSame(source, actual);
3030
}
3131
}
3232
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#nullable enable
2+
3+
using NUnit.Framework;
4+
using System;
5+
using static PrimeFuncPack.UnitTest.TestData;
6+
7+
namespace PrimeFuncPack.Primitives.Tests
8+
{
9+
partial class StringsTests
10+
{
11+
[Test]
12+
public void OrNullIfEmpty_SourceIsEmpty_ExpectNull()
13+
{
14+
string? source = string.Empty;
15+
16+
var actual = Strings.OrNullIfEmpty(source);
17+
Assert.IsNull(actual);
18+
}
19+
20+
[Test]
21+
[TestCase(null)]
22+
[TestCase(WhiteSpaceString)]
23+
[TestCase(TabString)]
24+
[TestCase(SomeString)]
25+
public void OrNullIfEmpty_SourceIsNotEmpty_ExpectSourceValue(
26+
string? source)
27+
{
28+
var actual = Strings.OrNullIfEmpty(source);
29+
Assert.AreSame(source, actual);
30+
}
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#nullable enable
2+
3+
using NUnit.Framework;
4+
using System;
5+
using static PrimeFuncPack.UnitTest.TestData;
6+
7+
namespace PrimeFuncPack.Primitives.Tests
8+
{
9+
partial class StringsTests
10+
{
11+
[Test]
12+
[TestCase(EmptyString)]
13+
[TestCase(WhiteSpaceString)]
14+
[TestCase(ThreeWhiteSpacesString)]
15+
[TestCase(TabString)]
16+
public void OrNullIfWhiteSpace_SourceIsWhiteSpace_ExpectNull(
17+
string? source)
18+
{
19+
var actual = Strings.OrNullIfWhiteSpace(source);
20+
Assert.IsNull(actual);
21+
}
22+
23+
[Test]
24+
[TestCase(null)]
25+
[TestCase(SomeString)]
26+
public void OrNullIfWhiteSpace_SourceIsNotWhiteSpace_ExpectSourceValue(
27+
string? source)
28+
{
29+
var actual = Strings.OrNullIfWhiteSpace(source);
30+
Assert.AreSame(source, actual);
31+
}
32+
}
33+
}

‎src/primitives/Primitives/Pipeline/PipelineExtensions.Pipe.PipeSelf.cs

+8-6
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ namespace System
55
partial class PipelineExtensions
66
{
77
public static TResult Pipe<T, TResult>(this T value, Func<T, TResult> pipe)
8-
=>
9-
(pipe ?? throw new ArgumentNullException(nameof(pipe)))
10-
.Invoke(value);
8+
{
9+
_ = pipe ?? throw new ArgumentNullException(nameof(pipe));
10+
return pipe.Invoke(value);
11+
}
1112
}
1213
}
1314

@@ -18,8 +19,9 @@ namespace PrimeFuncPack
1819
partial class PipelineExtensions
1920
{
2021
public static TResult PipeSelf<T, TResult>(this T value, Func<T, TResult> pipe)
21-
=>
22-
(pipe ?? throw new ArgumentNullException(nameof(pipe)))
23-
.Invoke(value);
22+
{
23+
_ = pipe ?? throw new ArgumentNullException(nameof(pipe));
24+
return pipe.Invoke(value);
25+
}
2426
}
2527
}

‎src/primitives/Primitives/Strings/StringExtensions.cs

+8
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@ public static string OrEmpty(this string? value)
88
=>
99
Strings.OrEmpty(value);
1010

11+
public static string? OrNullIfEmpty(this string? value)
12+
=>
13+
Strings.OrNullIfEmpty(value);
14+
15+
public static string? OrNullIfWhiteSpace(this string? value)
16+
=>
17+
Strings.OrNullIfWhiteSpace(value);
18+
1119
public static string ToStringOrEmpty<T>(this T value)
1220
=>
1321
Strings.ToStringOrEmpty(value);

‎src/primitives/Primitives/Strings/Strings.cs

+13-1
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,20 @@ public static string OrEmpty(string? value)
1212
=>
1313
value ?? Empty;
1414

15+
public static string? OrNullIfEmpty(string? value)
16+
=>
17+
string.IsNullOrEmpty(value)
18+
? null
19+
: value;
20+
21+
public static string? OrNullIfWhiteSpace(string? value)
22+
=>
23+
string.IsNullOrWhiteSpace(value)
24+
? null
25+
: value;
26+
1527
public static string ToStringOrEmpty<T>(T value)
1628
=>
17-
value switch { not null => value.ToString() ?? Empty, _ => Empty };
29+
value?.ToString() ?? Empty;
1830
}
1931
}

0 commit comments

Comments
 (0)
Please sign in to comment.