Files
Examples/GerstITS.Examples.WebClient.Console/Tests/CommonFeaturesRunner.cs
T

122 lines
3.6 KiB
C#

using GerstITS.Common;
using GerstITS.Examples.WebClients.Examples.Api;
using GerstITS.IoC.DotNetCore;
using GerstITS.System.Environment;
using GerstITS.System.Json;
using GerstITS.System.Reflection;
namespace GerstITS.Examples.WebClient.Console.Tests;
internal sealed class CommonFeaturesRunner : IApplicationStart
{
#region Fields
private readonly ISystemClock _systemClock;
private readonly IJsonConvert _jsonConvert;
private readonly IActivator _activator;
#endregion
#region Constructors
public CommonFeaturesRunner(ISystemClock systemClock,
IJsonConvert jsonConvert,
IActivator activator)
{
_systemClock = systemClock;
_jsonConvert = jsonConvert;
_activator = activator;
}
#endregion
#region IApplicationStart
public void Run()
{
RunStringExtensions();
RunObjectExtensions();
RunEnumerableExtensions();
RunRanges();
RunFluentExceptionHandling();
RunCryptography();
RunSystemAbstractions();
}
#endregion
#region Methods
private static void RunStringExtensions()
{
Print($"---> ToCamelCase: {"HelloWorld".ToCamelCase()}");
Print($"---> ToValue<int>: {"42".ToValue<int>()}");
Print($"---> ToValue<TimeSpan> with default: {"invalid".ToValue(TimeSpan.FromMinutes(5))}");
Print($"---> IsNullOrWhiteSpace: {" ".IsNullOrWhiteSpace()}");
Print($"---> ReplaceFirstOccurrence: {"a-a-a".ReplaceFirstOccurrence("a", "b")}");
Print($"---> GetDeterministicHashCode: {"stable".GetDeterministicHashCode()}");
}
private static void RunObjectExtensions()
{
object nothing = null;
Print($"---> IsNull: {nothing.IsNull()}, IsNotNull: {"value".IsNotNull()}");
Print($"---> Enum Parse: {"descending".Parse<SortingDirections>()}");
}
private static void RunEnumerableExtensions()
{
var numbers = new[] { 1, 2, 2, 3 };
Print($"---> IsNotNullOrEmpty: {numbers.IsNotNullOrEmpty()}");
Print($"---> AsEnumerable: {1.AsEnumerable().Count()}");
numbers.Distinct()
.ForEach(i => Print($"---> ForEach: {i}"));
}
private static void RunRanges()
{
var january = new Range<DateTime>(new DateTime(2026, 1, 1), new DateTime(2026, 1, 31));
var midJanuaryToFebruary = new Range<DateTime>(new DateTime(2026, 1, 15), new DateTime(2026, 2, 15));
Print($"---> Range intersects: {january.Intersects(midJanuaryToFebruary)}");
}
private static void RunFluentExceptionHandling()
{
try
{
(-1).Is(x => x < 0)
.ThenThrows<ArgumentOutOfRangeException>()
.WithMessage(x => $"The value '{x}' must not be negative.");
}
catch (ArgumentOutOfRangeException exception)
{
Print($"---> Fluent exception handling: {exception.Message}");
}
}
private static void RunCryptography()
{
var encrypted = "A secret value".Encrypt();
Print($"---> Encrypt/Decrypt roundtrip: {encrypted.Decrypt()}");
}
private void RunSystemAbstractions()
{
Print($"---> ISystemClock.UtcNow: {_systemClock.UtcNow}");
Print($"---> IJsonConvert.Serialize: {_jsonConvert.Serialize(new { Name = "Example" })}");
Print($"---> IActivator.CreateInstance: {_activator.CreateInstance<Range<int>>(0, 10).End}");
}
private static void Print(string message)
{
global::System.Console.WriteLine(message);
}
#endregion
}