-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add string and generic type extensions
- Loading branch information
1 parent
c8e185d
commit 8808dc1
Showing
7 changed files
with
171 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using System; | ||
using System.Text.Json; | ||
|
||
namespace Simple.Extensions | ||
{ | ||
public static class GenericTypeExtensions | ||
{ | ||
/// <summary> | ||
/// Converts the provided value into a <see cref="string"/> | ||
/// </summary> | ||
/// <typeparam name="T"></typeparam> | ||
/// <param name="instance">The value to convert</param> | ||
/// <param name="options">Options to control the conversion behavior</param> | ||
/// <returns>A <see cref="string"/> representation of the value</returns> | ||
public static string ToJson<T>(this T instance, JsonSerializerOptions options = null) | ||
=> instance == null | ||
? throw new ArgumentNullException(nameof(instance)) | ||
: JsonSerializer.Serialize(instance, options); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
Testing/Simple.Extensions.Testing/GenericTypeExtensionsTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using FluentAssertions; | ||
using Simple.Extensions.Testing.Helpers; | ||
|
||
namespace Simple.Extensions.Testing; | ||
public class GenericTypeExtensionsTests | ||
{ | ||
[Fact] | ||
public void ToJson_EmptyValue_ThrowException() | ||
{ | ||
SampleData.Person user = null!; | ||
|
||
var act = () => user.ToJson(); | ||
|
||
act.Should().Throw<ArgumentNullException>().WithParameterName("instance"); | ||
} | ||
|
||
[Fact] | ||
public void ToJson_ObjectValue_ConvertToJsonString() | ||
{ | ||
SampleData.Person user = new(1, "tarek"); | ||
|
||
var result = user.ToJson(); | ||
|
||
result.Should().Be(""""{"Id":1,"Name":"tarek"}""""); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters