diff --git a/Readme.md b/Readme.md index c18f72a..0e3792c 100644 --- a/Readme.md +++ b/Readme.md @@ -1,20 +1,34 @@ -# AStar.Dev.Utilities +# AStar Utilities -## GitHub build +A collection of useful utilities. At the moment, the collection is small but will grow as time and need permits. -[![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) +## Utilities -## SonarCloud Analysis Results +### String Utilities + +* 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 -[![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) +* ToJson - as you might expect, converts the object to the appropriate JSON representation. + +## GitHub build +[![Build and test solution](https://github.com/astar-development/astar-dev-utilities/actions/workflows/dotnet.yml/badge.svg)](https://github.com/astar-development/astar-dev-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=astar-development_astar-dev-utilities&metric=alert_status)](https://sonarcloud.io/summary/new_code?id=astar-development_astar-dev-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=astar-development_astar-dev-utilities&metric=bugs)](https://sonarcloud.io/summary/new_code?id=astar-development_astar-dev-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=astar-development_astar-dev-utilities&metric=security_rating)](https://sonarcloud.io/summary/new_code?id=astar-development_astar-dev-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=astar-development_astar-dev-utilities&metric=vulnerabilities)](https://sonarcloud.io/summary/new_code?id=astar-development_astar-dev-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=astar-development_astar-dev-utilities&metric=coverage)](https://sonarcloud.io/summary/new_code?id=astar-development_astar-dev-utilities) +[![Code Smells](https://sonarcloud.io/api/project_badges/measure?project=astar-development_astar-dev-utilities&metric=code_smells)](https://sonarcloud.io/summary/new_code?id=astar-development_astar-dev-utilities) \ No newline at end of file diff --git a/src/AStar.Dev.Utilities/AStar.Dev.Utilities.csproj b/src/AStar.Dev.Utilities/AStar.Dev.Utilities.csproj index 6317fe3..dfb4b13 100644 --- a/src/AStar.Dev.Utilities/AStar.Dev.Utilities.csproj +++ b/src/AStar.Dev.Utilities/AStar.Dev.Utilities.csproj @@ -29,12 +29,12 @@ git https://github.com/jbarden/astar-dev-utilities A collection of useful utilities. - 1.4.0 + 1.5.0 AStar Development, Jason Barden $(AssemblyName).xml AStar.png True - version 1.4.0, no changes - version increased as part of the migration to the new AStar NuGet / GitHub organisations. + version 1.5.0 contains the initial code port from the jbarden NuGet / GitHub organisations. diff --git a/src/AStar.Dev.Utilities/AStar.Dev.Utilities.xml b/src/AStar.Dev.Utilities/AStar.Dev.Utilities.xml new file mode 100644 index 0000000..0814568 --- /dev/null +++ b/src/AStar.Dev.Utilities/AStar.Dev.Utilities.xml @@ -0,0 +1,95 @@ + + + + AStar.Dev.Utilities + + + + + The see> class contains static / constant properties to simplify and centralise various settings. + + + + + Returns an instance of configured with the Web defaults. + + + + + + + + + + + + + + + + + + The class contains some useful methods to enable various tasks + to be performed in a more fluid, English sentence, style. + + + + + The ToJson method, as you might expect, converts the supplied object to its JSON equivalent. + + The object to convert to JSON. + The JSON string of the object supplied. + + + + The class contains some useful methods to enable checks to be + performed in a more fluid, English sentence, style. + + + + + The IsNull method, as you might expect, checks whether the string is, in fact, null. + + The string to check for being null. + True if the string is null, False otherwise. + + + + The IsNotNull method, as you might expect, checks whether the string is not null. + + The string to check for being not null. + True if the string is not null, False otherwise. + + + + The IsNullOrWhiteSpace method, as you might expect, checks whether the string is, in fact, null, empty or whitespace. + + The string to check for being null, empty or whitespace. + True if the string is null, empty or whitespace, False otherwise. + + + + The IsNotNullOrWhiteSpace method, as you might expect, checks whether the string is not null, empty or whitespace. + + The string to check for being not null, empty or whitespace. + True if the string is not null, empty or whitespace, False otherwise. + + + + The FromJson method, as you might expect, converts the supplied JSON to the specified object. + + The required type of the object to deserialise to. + The JSON representation of the object. + A deserialised object based on the original JSON. + + + + The FromJson method, as you might expect, converts the supplied JSON to the specified object. + + The required type of the object to deserialise to. + The JSON representation of the object. + Allows the specific options to be set to control deserialisation. + A deserialised object based on the original JSON. + + + diff --git a/src/AStar.Dev.Utilities/Constants.cs b/src/AStar.Dev.Utilities/Constants.cs new file mode 100644 index 0000000..c2165c2 --- /dev/null +++ b/src/AStar.Dev.Utilities/Constants.cs @@ -0,0 +1,14 @@ +using System.Text.Json; + +namespace AStar.Utilities; + +/// +/// The see> class contains static / constant properties to simplify and centralise various settings. +/// +public static class Constants +{ + /// + /// Returns an instance of configured with the Web defaults. + /// + public static JsonSerializerOptions WebDeserialisationSettings => new(JsonSerializerDefaults.Web); +} diff --git a/src/AStar.Dev.Utilities/EnumExtensions.cs b/src/AStar.Dev.Utilities/EnumExtensions.cs new file mode 100644 index 0000000..1b46cef --- /dev/null +++ b/src/AStar.Dev.Utilities/EnumExtensions.cs @@ -0,0 +1,15 @@ +namespace AStar.Utilities; + +/// +/// +/// +public static class EnumExtensions +{ + /// + /// + /// + /// + /// + /// + public static T ParseEnum(this string value) => (T)Enum.Parse(typeof(T), value, true); +} diff --git a/src/AStar.Dev.Utilities/ObjectExtensions.cs b/src/AStar.Dev.Utilities/ObjectExtensions.cs new file mode 100644 index 0000000..f9dc415 --- /dev/null +++ b/src/AStar.Dev.Utilities/ObjectExtensions.cs @@ -0,0 +1,17 @@ +using System.Text.Json; + +namespace AStar.Utilities; + +/// +/// The class contains some useful methods to enable various tasks +/// to be performed in a more fluid, English sentence, style. +/// +public static class ObjectExtensions +{ + /// + /// The ToJson method, as you might expect, converts the supplied object to its JSON equivalent. + /// + /// The object to convert to JSON. + /// The JSON string of the object supplied. + public static string ToJson(this T @object) => JsonSerializer.Serialize(@object); +} diff --git a/src/AStar.Dev.Utilities/StringExtensions.cs b/src/AStar.Dev.Utilities/StringExtensions.cs new file mode 100644 index 0000000..63aa841 --- /dev/null +++ b/src/AStar.Dev.Utilities/StringExtensions.cs @@ -0,0 +1,55 @@ +using System.Text.Json; + +namespace AStar.Utilities; + +/// +/// The class contains some useful methods to enable checks to be +/// performed in a more fluid, English sentence, style. +/// +public static class StringExtensions +{ + /// + /// The IsNull method, as you might expect, checks whether the string is, in fact, null. + /// + /// The string to check for being null. + /// True if the string is null, False otherwise. + public static bool IsNull(this string? value) => value is null; + + /// + /// The IsNotNull method, as you might expect, checks whether the string is not null. + /// + /// The string to check for being not null. + /// True if the string is not null, False otherwise. + public static bool IsNotNull(this string? value) => !value.IsNull(); + + /// + /// The IsNullOrWhiteSpace method, as you might expect, checks whether the string is, in fact, null, empty or whitespace. + /// + /// The string to check for being null, empty or whitespace. + /// True if the string is null, empty or whitespace, False otherwise. + public static bool IsNullOrWhiteSpace(this string? value) => string.IsNullOrWhiteSpace(value); + + /// + /// The IsNotNullOrWhiteSpace method, as you might expect, checks whether the string is not null, empty or whitespace. + /// + /// The string to check for being not null, empty or whitespace. + /// True if the string is not null, empty or whitespace, False otherwise. + public static bool IsNotNullOrWhiteSpace(this string? value) => !value.IsNullOrWhiteSpace(); + + /// + /// The FromJson method, as you might expect, converts the supplied JSON to the specified object. + /// + /// The required type of the object to deserialise to. + /// The JSON representation of the object. + /// A deserialised object based on the original JSON. + public static T FromJson(this string json) => JsonSerializer.Deserialize(json)!; + + /// + /// The FromJson method, as you might expect, converts the supplied JSON to the specified object. + /// + /// The required type of the object to deserialise to. + /// The JSON representation of the object. + /// Allows the specific options to be set to control deserialisation. + /// A deserialised object based on the original JSON. + public static T FromJson(this string json, JsonSerializerOptions options) => JsonSerializer.Deserialize(json, options)!; +} diff --git a/tests/unit/AStar.Dev.Utilities.Unit.Tests/AStar.Dev.Utilities.Unit.Tests.csproj b/tests/unit/AStar.Dev.Utilities.Unit.Tests/AStar.Dev.Utilities.Unit.Tests.csproj index 5994372..8bca3ab 100644 --- a/tests/unit/AStar.Dev.Utilities.Unit.Tests/AStar.Dev.Utilities.Unit.Tests.csproj +++ b/tests/unit/AStar.Dev.Utilities.Unit.Tests/AStar.Dev.Utilities.Unit.Tests.csproj @@ -22,6 +22,10 @@ + + + + diff --git a/tests/unit/AStar.Utilities.Unit.Tests/AStar.Utilities.Unit.Tests.csproj b/tests/unit/AStar.Utilities.Unit.Tests/AStar.Utilities.Unit.Tests.csproj new file mode 100644 index 0000000..dd959ba --- /dev/null +++ b/tests/unit/AStar.Utilities.Unit.Tests/AStar.Utilities.Unit.Tests.csproj @@ -0,0 +1,38 @@ + + + + net8.0 + enable + enable + + false + true + + + + 1701;1702;IDE0058; + + + + 1701;1702;IDE0058; + + + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + + + + + diff --git a/tests/unit/AStar.Utilities.Unit.Tests/GlobalUsings.cs b/tests/unit/AStar.Utilities.Unit.Tests/GlobalUsings.cs new file mode 100644 index 0000000..7b25ddd --- /dev/null +++ b/tests/unit/AStar.Utilities.Unit.Tests/GlobalUsings.cs @@ -0,0 +1,2 @@ +global using FluentAssertions; +global using Xunit; diff --git a/tests/unit/AStar.Utilities.Unit.Tests/Helpers/AnyClass.cs b/tests/unit/AStar.Utilities.Unit.Tests/Helpers/AnyClass.cs new file mode 100644 index 0000000..99d8dd0 --- /dev/null +++ b/tests/unit/AStar.Utilities.Unit.Tests/Helpers/AnyClass.cs @@ -0,0 +1,6 @@ +namespace AStar.Utilities.Unit.Tests.Helpers; + +internal class AnyClass +{ + public int Id { get; set; } +} diff --git a/tests/unit/AStar.Utilities.Unit.Tests/ObjectExtensionsShould.cs b/tests/unit/AStar.Utilities.Unit.Tests/ObjectExtensionsShould.cs new file mode 100644 index 0000000..611c025 --- /dev/null +++ b/tests/unit/AStar.Utilities.Unit.Tests/ObjectExtensionsShould.cs @@ -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}"); + } +} diff --git a/tests/unit/AStar.Utilities.Unit.Tests/StringExtensionsShould.cs b/tests/unit/AStar.Utilities.Unit.Tests/StringExtensionsShould.cs new file mode 100644 index 0000000..56d08a3 --- /dev/null +++ b/tests/unit/AStar.Utilities.Unit.Tests/StringExtensionsShould.cs @@ -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(); + + objectFromJson.Id.Should().Be(1); + } +}