Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 23 additions & 10 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,33 @@
# AStar.Dev.Utilities
# AStar Utilities
A collection of useful utilities. At the moment, the collection is small but will grow as time and need permits.

## GitHub build
## Utilities

[![Build and test solution](https://github.com/jbarden/astar-dev-utilities/actions/workflows/dotnet.yml/badge.svg)](https://github.com/jbarden/astar-dev-utilities/actions/workflows/dotnet.yml)
### String Utilities

## SonarCloud Analysis Results
* IsNull - as you might expect, checks whether the string is, in fact, null.
* IsNotNull - as you might expect, checks whether the string is not null.
* IsNullOrWhiteSpace - as you might expect, checks whether the string is, in fact, null, empty or whitespace. *new*
* IsNotNullOrWhiteSpace - as you might expect, checks whether the string is not null, empty or whitespace. *new*
* FromJson - as you might expect, converts the JSON representation to the requested Type.

### String Utilities

* ToJson - as you might expect, converts the object to the appropriate JSON representation.

[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=jbarden_astar-dev-utilities&metric=alert_status)](https://sonarcloud.io/summary/new_code?id=jbarden_astar-dev-utilities)
## GitHub build
[![Build and test solution](https://github.com/jbarden/astar-utilities/actions/workflows/dotnet.yml/badge.svg)](https://github.com/jbarden/astar-utilities/actions/workflows/dotnet.yml)

## SonarCloud Analysis Results

[![Bugs](https://sonarcloud.io/api/project_badges/measure?project=jbarden_astar-dev-utilities&metric=bugs)](https://sonarcloud.io/summary/new_code?id=jbarden_astar-dev-utilities)
[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=jbarden_astar-utilities&metric=alert_status)](https://sonarcloud.io/summary/new_code?id=jbarden_astar-utilities)

[![Security Rating](https://sonarcloud.io/api/project_badges/measure?project=jbarden_astar-dev-utilities&metric=security_rating)](https://sonarcloud.io/summary/new_code?id=jbarden_astar-dev-utilities)
[![Bugs](https://sonarcloud.io/api/project_badges/measure?project=jbarden_astar-utilities&metric=bugs)](https://sonarcloud.io/summary/new_code?id=jbarden_astar-utilities)

[![Vulnerabilities](https://sonarcloud.io/api/project_badges/measure?project=jbarden_astar-dev-utilities&metric=vulnerabilities)](https://sonarcloud.io/summary/new_code?id=jbarden_astar-dev-utilities)
[![Security Rating](https://sonarcloud.io/api/project_badges/measure?project=jbarden_astar-utilities&metric=security_rating)](https://sonarcloud.io/summary/new_code?id=jbarden_astar-utilities)

[![Coverage](https://sonarcloud.io/api/project_badges/measure?project=jbarden_astar-dev-utilities&metric=coverage)](https://sonarcloud.io/summary/new_code?id=jbarden_astar-dev-utilities)
[![Vulnerabilities](https://sonarcloud.io/api/project_badges/measure?project=jbarden_astar-utilities&metric=vulnerabilities)](https://sonarcloud.io/summary/new_code?id=jbarden_astar-utilities)

[![Code Smells](https://sonarcloud.io/api/project_badges/measure?project=jbarden_astar-dev-utilities&metric=code_smells)](https://sonarcloud.io/summary/new_code?id=jbarden_astar-dev-utilities)
[![Coverage](https://sonarcloud.io/api/project_badges/measure?project=jbarden_astar-utilities&metric=coverage)](https://sonarcloud.io/summary/new_code?id=jbarden_astar-utilities)

[![Code Smells](https://sonarcloud.io/api/project_badges/measure?project=jbarden_astar-utilities&metric=code_smells)](https://sonarcloud.io/summary/new_code?id=jbarden_astar-utilities)
4 changes: 2 additions & 2 deletions src/AStar.Dev.Utilities/AStar.Dev.Utilities.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@
<RepositoryType>git</RepositoryType>
<PackageProjectUrl>https://github.com/jbarden/astar-dev-utilities</PackageProjectUrl>
<Description>A collection of useful utilities.</Description>
<Version>1.4.0</Version>
<Version>1.5.0</Version>
<Authors>AStar Development, Jason Barden</Authors>
<DocumentationFile>$(AssemblyName).xml</DocumentationFile>
<PackageIcon>AStar.png</PackageIcon>
<PackageRequireLicenseAcceptance>True</PackageRequireLicenseAcceptance>
<PackageReleaseNotes>version 1.4.0, no changes - version increased as part of the migration to the new AStar NuGet / GitHub organisations.</PackageReleaseNotes>
<PackageReleaseNotes>version 1.5.0 contains the initial code port from the jbarden NuGet / GitHub organisations.</PackageReleaseNotes>
</PropertyGroup>

<ItemGroup>
Expand Down
95 changes: 95 additions & 0 deletions src/AStar.Dev.Utilities/AStar.Dev.Utilities.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions src/AStar.Dev.Utilities/Constants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.Text.Json;

namespace AStar.Utilities;

/// <summary>
/// The <see href="Constants"></see>see> class contains static / constant properties to simplify and centralise various settings.
/// </summary>
public static class Constants
{
/// <summary>
/// Returns an instance of <see href="JsonSerializerOptions"></see> configured with the Web defaults.
/// </summary>
public static JsonSerializerOptions WebDeserialisationSettings => new(JsonSerializerDefaults.Web);
}
15 changes: 15 additions & 0 deletions src/AStar.Dev.Utilities/EnumExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace AStar.Utilities;

/// <summary>
///
/// </summary>
public static class EnumExtensions
{
/// <summary>
///
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="value"></param>
/// <returns></returns>
public static T ParseEnum<T>(this string value) => (T)Enum.Parse(typeof(T), value, true);
}
17 changes: 17 additions & 0 deletions src/AStar.Dev.Utilities/ObjectExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System.Text.Json;

namespace AStar.Utilities;

/// <summary>
/// The <see cref="ObjectExtensions" /> class contains some useful methods to enable various tasks
/// to be performed in a more fluid, English sentence, style.
/// </summary>
public static class ObjectExtensions
{
/// <summary>
/// The ToJson method, as you might expect, converts the supplied object to its JSON equivalent.
/// </summary>
/// <param name="object">The object to convert to JSON.</param>
/// <returns>The JSON string of the object supplied.</returns>
public static string ToJson<T>(this T @object) => JsonSerializer.Serialize(@object);
}
55 changes: 55 additions & 0 deletions src/AStar.Dev.Utilities/StringExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System.Text.Json;

namespace AStar.Utilities;

/// <summary>
/// The <see cref="StringExtensions" /> class contains some useful methods to enable checks to be
/// performed in a more fluid, English sentence, style.
/// </summary>
public static class StringExtensions
{
/// <summary>
/// The IsNull method, as you might expect, checks whether the string is, in fact, null.
/// </summary>
/// <param name="value">The string to check for being null.</param>
/// <returns>True if the string is null, False otherwise.</returns>
public static bool IsNull(this string? value) => value is null;

/// <summary>
/// The IsNotNull method, as you might expect, checks whether the string is not null.
/// </summary>
/// <param name="value">The string to check for being not null.</param>
/// <returns>True if the string is not null, False otherwise.</returns>
public static bool IsNotNull(this string? value) => !value.IsNull();

/// <summary>
/// The IsNullOrWhiteSpace method, as you might expect, checks whether the string is, in fact, null, empty or whitespace.
/// </summary>
/// <param name="value">The string to check for being null, empty or whitespace.</param>
/// <returns>True if the string is null, empty or whitespace, False otherwise.</returns>
public static bool IsNullOrWhiteSpace(this string? value) => string.IsNullOrWhiteSpace(value);

/// <summary>
/// The IsNotNullOrWhiteSpace method, as you might expect, checks whether the string is not null, empty or whitespace.
/// </summary>
/// <param name="value">The string to check for being not null, empty or whitespace.</param>
/// <returns>True if the string is not null, empty or whitespace, False otherwise.</returns>
public static bool IsNotNullOrWhiteSpace(this string? value) => !value.IsNullOrWhiteSpace();

/// <summary>
/// The FromJson method, as you might expect, converts the supplied JSON to the specified object.
/// </summary>
/// <typeparam name="T">The required type of the object to deserialise to.</typeparam>
/// <param name="json">The JSON representation of the object.</param>
/// <returns>A deserialised object based on the original JSON.</returns>
public static T FromJson<T>(this string json) => JsonSerializer.Deserialize<T>(json)!;

/// <summary>
/// The FromJson method, as you might expect, converts the supplied JSON to the specified object.
/// </summary>
/// <typeparam name="T">The required type of the object to deserialise to.</typeparam>
/// <param name="json">The JSON representation of the object.</param>
/// <param name="options">Allows the specific <see href="JsonSerializerOptions">options</see> to be set to control deserialisation.</param>
/// <returns>A deserialised object based on the original JSON.</returns>
public static T FromJson<T>(this string json, JsonSerializerOptions options) => JsonSerializer.Deserialize<T>(json, options)!;
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\src\AStar.Dev.Utilities\AStar.Dev.Utilities.csproj" />
</ItemGroup>

<ItemGroup>
<Using Include="Xunit" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<NoWarn>1701;1702;IDE0058;</NoWarn>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<NoWarn>1701;1702;IDE0058;</NoWarn>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.12.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="xunit" Version="2.9.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\src\AStar.Utilities\AStar.Utilities.csproj" />
</ItemGroup>

</Project>
2 changes: 2 additions & 0 deletions tests/unit/AStar.Utilities.Unit.Tests/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
global using FluentAssertions;
global using Xunit;
6 changes: 6 additions & 0 deletions tests/unit/AStar.Utilities.Unit.Tests/Helpers/AnyClass.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace AStar.Utilities.Unit.Tests.Helpers;

internal class AnyClass
{
public int Id { get; set; }
}
14 changes: 14 additions & 0 deletions tests/unit/AStar.Utilities.Unit.Tests/ObjectExtensionsShould.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using AStar.Utilities.Unit.Tests.Helpers;

namespace AStar.Utilities.Unit.Tests;

public class ObjectExtensionsShould
{
[Fact]
public void ReturnTheJsonRepresentationOfThePassedObject()
{
var anyClass = new AnyClass { Id = 1 };

anyClass.ToJson().Should().Be("{\"Id\":1}");
}
}
48 changes: 48 additions & 0 deletions tests/unit/AStar.Utilities.Unit.Tests/StringExtensionsShould.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using AStar.Utilities.Unit.Tests.Helpers;

namespace AStar.Utilities.Unit.Tests;

public class StringExtensionsShould
{
[Fact]
public void ReturnTrueForCallToIsNullWhenStringIsNull()
{
string nullString = null!;

nullString.IsNull().Should().BeTrue();
}

[Fact]
public void ReturnFalseForCallToIsNullWhenStringIsNotNull()
{
var nullString = string.Empty;

nullString.IsNull().Should().BeFalse();
}

[Fact]
public void ReturnFalseForCallToIsNotNullWhenStringIsNull()
{
string nullString = null!;

nullString.IsNotNull().Should().BeFalse();
}

[Fact]
public void ReturnTrueForCallToIsNotNullWhenStringIsNotNull()
{
var nullString = string.Empty;

nullString.IsNotNull().Should().BeTrue();
}

[Fact]
public void ReturnTheExpectedObjectFromTheFromJsonMethod()
{
const string testJson = "{\"Id\":1}";

var objectFromJson = testJson.FromJson<AnyClass>();

objectFromJson.Id.Should().Be(1);
}
}