Skip to content

Commit e1708e7

Browse files
Copilotmarcominerva
andcommitted
Add GetValueOrDefault extension methods for Guid
Co-authored-by: marcominerva <3522534+marcominerva@users.noreply.github.com>
1 parent 150481b commit e1708e7

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed

src/TinyHelpers/Extensions/GuidExtensions.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,15 @@ public static bool HasValue([NotNullWhen(true)] this Guid? input)
7070
public static Guid GetValueOrCreateNew(this Guid input)
7171
=> input.IsEmpty() ? Guid.NewGuid() : input;
7272

73+
/// <summary>
74+
/// Gets the actual value of this <see cref="Guid"/> instance, if it is different from <c>Guid.Empty</c>; otherwise, returns the specified default value.
75+
/// </summary>
76+
/// <param name="input">The <see cref="Guid"/> to test.</param>
77+
/// <param name="defaultValue">The default <see cref="Guid"/> to return if the input is <c>Guid.Empty</c>.</param>
78+
/// <returns>The actual value of this <see cref="Guid"/> instance, if it is different from <c>Guid.Empty</c>; otherwise, the specified default value.</returns>
79+
public static Guid GetValueOrDefault(this Guid input, Guid defaultValue)
80+
=> input.IsEmpty() ? defaultValue : input;
81+
7382
/// <summary>
7483
/// Gets the actual value of this <see cref="Guid"/> instance, if it is different from <see langword="null"/> and <c>Guid.Empty</c>; otherwise, creates a new <see cref="Guid"/> using <see cref="Guid.NewGuid()"/>.
7584
/// </summary>
@@ -78,6 +87,15 @@ public static Guid GetValueOrCreateNew(this Guid input)
7887
public static Guid GetValueOrCreateNew(this Guid? input)
7988
=> input.IsEmpty() ? Guid.NewGuid() : input!.Value;
8089

90+
/// <summary>
91+
/// Gets the actual value of this <see cref="Guid"/> instance, if it is different from <see langword="null"/> and <c>Guid.Empty</c>; otherwise, returns the specified default value.
92+
/// </summary>
93+
/// <param name="input">The <see cref="Guid"/> to test.</param>
94+
/// <param name="defaultValue">The default <see cref="Guid"/> to return if the input is <see langword="null"/> or <c>Guid.Empty</c>.</param>
95+
/// <returns>The actual value of this <see cref="Guid"/> instance, if it is different from <see langword="null"/> and <c>Guid.Empty</c>; otherwise, the specified default value.</returns>
96+
public static Guid GetValueOrDefault(this Guid? input, Guid defaultValue)
97+
=> input.IsEmpty() ? defaultValue : input!.Value;
98+
8199
#if NET9_0_OR_GREATER
82100
/// <summary>
83101
/// Gets the actual value of this <see cref="Guid"/> instance, if it is different from <c>Guid.Empty</c>; otherwise, creates a new <see cref="Guid"/> using <see cref="Guid.NewGuid()"/>.

tests/TinyHelpers.Tests/Extensions/GuidExtensionsTests.cs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,4 +212,74 @@ public void NullableGuidIsNotEmpty_GetValueOrCreateNew_Should_Return_TheSameGuid
212212
// Assert
213213
Assert.Equal(input, value);
214214
}
215+
216+
[Fact]
217+
public void GuidIsEmpty_GetValueOrDefault_Should_Return_DefaultValue()
218+
{
219+
// Arrange
220+
var input = Guid.Empty;
221+
var defaultValue = Guid.NewGuid();
222+
223+
// Act
224+
var value = input.GetValueOrDefault(defaultValue);
225+
226+
// Assert
227+
Assert.Equal(defaultValue, value);
228+
}
229+
230+
[Fact]
231+
public void GuidIsNotEmpty_GetValueOrDefault_Should_Return_TheSameGuid()
232+
{
233+
// Arrange
234+
var input = Guid.NewGuid();
235+
var defaultValue = Guid.NewGuid();
236+
237+
// Act
238+
var value = input.GetValueOrDefault(defaultValue);
239+
240+
// Assert
241+
Assert.Equal(input, value);
242+
}
243+
244+
[Fact]
245+
public void NullableGuidIsEmpty_GetValueOrDefault_Should_Return_DefaultValue()
246+
{
247+
// Arrange
248+
Guid? input = Guid.Empty;
249+
var defaultValue = Guid.NewGuid();
250+
251+
// Act
252+
var value = input.GetValueOrDefault(defaultValue);
253+
254+
// Assert
255+
Assert.Equal(defaultValue, value);
256+
}
257+
258+
[Fact]
259+
public void NullableGuidIsNull_GetValueOrDefault_Should_Return_DefaultValue()
260+
{
261+
// Arrange
262+
Guid? input = null;
263+
var defaultValue = Guid.NewGuid();
264+
265+
// Act
266+
var value = input.GetValueOrDefault(defaultValue);
267+
268+
// Assert
269+
Assert.Equal(defaultValue, value);
270+
}
271+
272+
[Fact]
273+
public void NullableGuidIsNotEmpty_GetValueOrDefault_Should_Return_TheSameGuid()
274+
{
275+
// Arrange
276+
Guid? input = Guid.NewGuid();
277+
var defaultValue = Guid.NewGuid();
278+
279+
// Act
280+
var value = input.GetValueOrDefault(defaultValue);
281+
282+
// Assert
283+
Assert.Equal(input, value);
284+
}
215285
}

0 commit comments

Comments
 (0)