From 07887d9d1f2e65d171d2d96aaa6b8227f61b4f50 Mon Sep 17 00:00:00 2001 From: Jason Barden Date: Fri, 18 Oct 2024 05:45:01 +0100 Subject: [PATCH 1/2] the initial code port from the jbarden NuGet / GitHub organisations --- AStar.Utilities.sln | 56 +++++++++++++++++++ Readme.md | 33 +++++++---- .../AStar.Dev.Utilities.csproj | 4 +- src/AStar.Dev.Utilities/Constants.cs | 14 +++++ src/AStar.Dev.Utilities/EnumExtensions.cs | 15 +++++ src/AStar.Dev.Utilities/ObjectExtensions.cs | 17 ++++++ src/AStar.Dev.Utilities/StringExtensions.cs | 55 ++++++++++++++++++ .../AStar.Utilities.Unit.Tests.csproj | 38 +++++++++++++ .../GlobalUsings.cs | 2 + .../Helpers/AnyClass.cs | 6 ++ .../ObjectExtensionsShould.cs | 14 +++++ .../StringExtensionsShould.cs | 48 ++++++++++++++++ 12 files changed, 290 insertions(+), 12 deletions(-) create mode 100644 AStar.Utilities.sln create mode 100644 src/AStar.Dev.Utilities/Constants.cs create mode 100644 src/AStar.Dev.Utilities/EnumExtensions.cs create mode 100644 src/AStar.Dev.Utilities/ObjectExtensions.cs create mode 100644 src/AStar.Dev.Utilities/StringExtensions.cs create mode 100644 tests/unit/AStar.Utilities.Unit.Tests/AStar.Utilities.Unit.Tests.csproj create mode 100644 tests/unit/AStar.Utilities.Unit.Tests/GlobalUsings.cs create mode 100644 tests/unit/AStar.Utilities.Unit.Tests/Helpers/AnyClass.cs create mode 100644 tests/unit/AStar.Utilities.Unit.Tests/ObjectExtensionsShould.cs create mode 100644 tests/unit/AStar.Utilities.Unit.Tests/StringExtensionsShould.cs diff --git a/AStar.Utilities.sln b/AStar.Utilities.sln new file mode 100644 index 0000000..b6975bc --- /dev/null +++ b/AStar.Utilities.sln @@ -0,0 +1,56 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.7.34024.191 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AStar.Utilities", "src\AStar.Utilities\AStar.Utilities.csproj", "{E4D86C2D-C9F7-4738-93AD-8E726B12AA97}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{F278FE7A-ED8B-4C98-98EA-2A99803A4A82}" + ProjectSection(SolutionItems) = preProject + LICENSE = LICENSE + README.md = README.md + EndProjectSection +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{8618654D-3A13-4B92-80FA-CE48DF281276}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AStar.Utilities.Unit.Tests", "tests\unit\AStar.Utilities.Unit.Tests\AStar.Utilities.Unit.Tests.csproj", "{6CD0E1F8-AD4A-46F3-9622-4DED14EFEF59}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{35AB19D2-EB84-456C-98BE-158393D488B4}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "unit", "unit", "{7D9D09F2-797B-4B79-B6A4-499EC673DAEC}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".github", ".github", "{D6963DCB-A452-4C85-88A0-C7F604CFE8CA}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "workflows", "workflows", "{27076F0E-3C06-45EE-B4F2-B584BADF5DAA}" + ProjectSection(SolutionItems) = preProject + .github\workflows\dotnet.yml = .github\workflows\dotnet.yml + EndProjectSection +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {E4D86C2D-C9F7-4738-93AD-8E726B12AA97}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {E4D86C2D-C9F7-4738-93AD-8E726B12AA97}.Debug|Any CPU.Build.0 = Debug|Any CPU + {E4D86C2D-C9F7-4738-93AD-8E726B12AA97}.Release|Any CPU.ActiveCfg = Release|Any CPU + {E4D86C2D-C9F7-4738-93AD-8E726B12AA97}.Release|Any CPU.Build.0 = Release|Any CPU + {6CD0E1F8-AD4A-46F3-9622-4DED14EFEF59}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {6CD0E1F8-AD4A-46F3-9622-4DED14EFEF59}.Debug|Any CPU.Build.0 = Debug|Any CPU + {6CD0E1F8-AD4A-46F3-9622-4DED14EFEF59}.Release|Any CPU.ActiveCfg = Release|Any CPU + {6CD0E1F8-AD4A-46F3-9622-4DED14EFEF59}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(NestedProjects) = preSolution + {E4D86C2D-C9F7-4738-93AD-8E726B12AA97} = {8618654D-3A13-4B92-80FA-CE48DF281276} + {6CD0E1F8-AD4A-46F3-9622-4DED14EFEF59} = {7D9D09F2-797B-4B79-B6A4-499EC673DAEC} + {7D9D09F2-797B-4B79-B6A4-499EC673DAEC} = {35AB19D2-EB84-456C-98BE-158393D488B4} + {27076F0E-3C06-45EE-B4F2-B584BADF5DAA} = {D6963DCB-A452-4C85-88A0-C7F604CFE8CA} + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {14B5B932-6D11-440E-A885-CC9B10F840DA} + EndGlobalSection +EndGlobal diff --git a/Readme.md b/Readme.md index c18f72a..93b8073 100644 --- a/Readme.md +++ b/Readme.md @@ -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) \ 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/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.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); + } +} From efc650e29dc2490198b984a93a80f9d5a0eaf92e Mon Sep 17 00:00:00 2001 From: Jason Barden Date: Fri, 18 Oct 2024 05:59:22 +0100 Subject: [PATCH 2/2] Update the ReadMe to correct the links --- Readme.md | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/Readme.md b/Readme.md index 93b8073..0e3792c 100644 --- a/Readme.md +++ b/Readme.md @@ -1,4 +1,5 @@ # AStar Utilities + A collection of useful utilities. At the moment, the collection is small but will grow as time and need permits. ## Utilities @@ -16,18 +17,18 @@ A collection of useful utilities. At the moment, the collection is small but wil * ToJson - as you might expect, converts the object to the appropriate JSON representation. ## 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) +[![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 -[![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) +[![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) -[![Bugs](https://sonarcloud.io/api/project_badges/measure?project=jbarden_astar-utilities&metric=bugs)](https://sonarcloud.io/summary/new_code?id=jbarden_astar-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) -[![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) +[![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) -[![Vulnerabilities](https://sonarcloud.io/api/project_badges/measure?project=jbarden_astar-utilities&metric=vulnerabilities)](https://sonarcloud.io/summary/new_code?id=jbarden_astar-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) -[![Coverage](https://sonarcloud.io/api/project_badges/measure?project=jbarden_astar-utilities&metric=coverage)](https://sonarcloud.io/summary/new_code?id=jbarden_astar-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=jbarden_astar-utilities&metric=code_smells)](https://sonarcloud.io/summary/new_code?id=jbarden_astar-utilities) \ No newline at end of file +[![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