From 10276422bd7fd95eec59f99863546e334b7a1691 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20Gr=C3=BCnwald?= Date: Fri, 11 Aug 2023 23:55:30 +0200 Subject: [PATCH] [#316] Port Language Definitions --- .idea/.idea.PetroglyphTools/.idea/.gitignore | 13 +++ .../.idea/indexLayout.xml | 8 ++ .idea/.idea.PetroglyphTools/.idea/vcs.xml | 6 ++ .../Attributes/AttributeExtensions.cs | 1 - .../Binary/DatFileConverterTest.cs | 3 +- .../PG.StarWarsGame.Files.DAT.csproj | 2 +- .../AlamoLanguageDefinitionHelperTest.cs | 62 ++++++++++++ .../AlamoLanguageDefinitionIntegrityTest.cs | 96 ++++++++++--------- .../LocalisationTestConstants.cs | 42 ++++---- .../PG.StarWarsGame.Localisation.Test.csproj | 24 +++-- .../Util/LocalisationUtilityTest.cs | 57 ----------- .../Attributes/DefaultLanguageAttribute.cs | 15 +++ .../OfficiallySupportedLanguageAttribute.cs | 12 +++ .../Helper/AlamoLanguageDefinitionHelper.cs | 80 ++++++++++++++++ .../Helper/IAlamoLanguageDefinitionHelper.cs | 33 +++++++ .../Languages/AlamoLanguageDefinitionBase.cs | 55 +++++------ .../Builtin/ChineseAlamoLanguageDefinition.cs | 3 + .../Builtin/EnglishAlamoLanguageDefinition.cs | 3 + .../Builtin/FrenchAlamoLanguageDefinition.cs | 3 + .../Builtin/GermanAlamoLanguageDefinition.cs | 3 + .../Builtin/ItalianAlamoLanguageDefinition.cs | 3 + .../JapaneseAlamoLanguageDefinition.cs | 3 + .../Builtin/KoreanAlamoLanguageDefinition.cs | 3 + .../Builtin/PolishAlamoLanguageDefinition.cs | 3 + .../Builtin/RussianAlamoLanguageDefinition.cs | 3 + .../Builtin/SpanishAlamoLanguageDefinition.cs | 3 + .../Builtin/ThaiAlamoLanguageDefinition.cs | 3 + .../LocalisationDomain.cs | 26 +++++ .../PG.StarWarsGame.Localisation.csproj | 8 +- PG.Testing/ServiceTestBase.cs | 23 ++--- PG.Testing/TestConstants.cs | 12 ++- 31 files changed, 426 insertions(+), 185 deletions(-) create mode 100644 .idea/.idea.PetroglyphTools/.idea/.gitignore create mode 100644 .idea/.idea.PetroglyphTools/.idea/indexLayout.xml create mode 100644 .idea/.idea.PetroglyphTools/.idea/vcs.xml create mode 100644 PG.StarWarsGame.Localisation/PG.StarWarsGame.Localisation.Test/Commons/Helper/AlamoLanguageDefinitionHelperTest.cs delete mode 100644 PG.StarWarsGame.Localisation/PG.StarWarsGame.Localisation.Test/Util/LocalisationUtilityTest.cs create mode 100644 PG.StarWarsGame.Localisation/PG.StarWarsGame.Localisation/Commons/Helper/AlamoLanguageDefinitionHelper.cs create mode 100644 PG.StarWarsGame.Localisation/PG.StarWarsGame.Localisation/Commons/Helper/IAlamoLanguageDefinitionHelper.cs create mode 100644 PG.StarWarsGame.Localisation/PG.StarWarsGame.Localisation/LocalisationDomain.cs diff --git a/.idea/.idea.PetroglyphTools/.idea/.gitignore b/.idea/.idea.PetroglyphTools/.idea/.gitignore new file mode 100644 index 000000000..0c1005306 --- /dev/null +++ b/.idea/.idea.PetroglyphTools/.idea/.gitignore @@ -0,0 +1,13 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Rider ignored files +/.idea.PetroglyphTools.iml +/modules.xml +/contentModel.xml +/projectSettingsUpdater.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/.idea.PetroglyphTools/.idea/indexLayout.xml b/.idea/.idea.PetroglyphTools/.idea/indexLayout.xml new file mode 100644 index 000000000..7b08163ce --- /dev/null +++ b/.idea/.idea.PetroglyphTools/.idea/indexLayout.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/.idea.PetroglyphTools/.idea/vcs.xml b/.idea/.idea.PetroglyphTools/.idea/vcs.xml new file mode 100644 index 000000000..35eb1ddfb --- /dev/null +++ b/.idea/.idea.PetroglyphTools/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/PG.Commons/PG.Commons/Attributes/AttributeExtensions.cs b/PG.Commons/PG.Commons/Attributes/AttributeExtensions.cs index 179fac949..accda5707 100644 --- a/PG.Commons/PG.Commons/Attributes/AttributeExtensions.cs +++ b/PG.Commons/PG.Commons/Attributes/AttributeExtensions.cs @@ -20,7 +20,6 @@ public static class AttributeExtensions /// The function to retrieve the value. /// The type of the attribute. /// The return type. - /// The public static TValue? GetAttributeValueOrDefault(this Type type, Func valueSelector) where TAttribute : Attribute { diff --git a/PG.StarWarsGame.Files.DAT/PG.StarWarsGame.Files.DAT.Test/Binary/DatFileConverterTest.cs b/PG.StarWarsGame.Files.DAT/PG.StarWarsGame.Files.DAT.Test/Binary/DatFileConverterTest.cs index a20ac2e4a..7f1069807 100644 --- a/PG.StarWarsGame.Files.DAT/PG.StarWarsGame.Files.DAT.Test/Binary/DatFileConverterTest.cs +++ b/PG.StarWarsGame.Files.DAT/PG.StarWarsGame.Files.DAT.Test/Binary/DatFileConverterTest.cs @@ -38,8 +38,7 @@ protected override Type GetServiceClass() private DatFileConverter GetService() { - var svc = GetServiceInstance(); - return (DatFileConverter)svc; + return GetServiceInstance(); } [TestMethod] diff --git a/PG.StarWarsGame.Files.DAT/PG.StarWarsGame.Files.DAT/PG.StarWarsGame.Files.DAT.csproj b/PG.StarWarsGame.Files.DAT/PG.StarWarsGame.Files.DAT/PG.StarWarsGame.Files.DAT.csproj index 45807cf58..be88ff057 100644 --- a/PG.StarWarsGame.Files.DAT/PG.StarWarsGame.Files.DAT/PG.StarWarsGame.Files.DAT.csproj +++ b/PG.StarWarsGame.Files.DAT/PG.StarWarsGame.Files.DAT/PG.StarWarsGame.Files.DAT.csproj @@ -1,6 +1,6 @@  - netstandard2.0;netstandard2.1 + netstandard2.0;netstandard2.1,net7.0 PG.StarWarsGame.Files.DAT PG.StarWarsGame.Files.DAT AlamoEngineTools.PG.StarWarsGame.Files.DAT diff --git a/PG.StarWarsGame.Localisation/PG.StarWarsGame.Localisation.Test/Commons/Helper/AlamoLanguageDefinitionHelperTest.cs b/PG.StarWarsGame.Localisation/PG.StarWarsGame.Localisation.Test/Commons/Helper/AlamoLanguageDefinitionHelperTest.cs new file mode 100644 index 000000000..19b7ed7d4 --- /dev/null +++ b/PG.StarWarsGame.Localisation/PG.StarWarsGame.Localisation.Test/Commons/Helper/AlamoLanguageDefinitionHelperTest.cs @@ -0,0 +1,62 @@ +// Copyright (c) Alamo Engine Tools and contributors. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for details. + +using System; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using PG.Commons.Attributes; +using PG.StarWarsGame.Localisation.Attributes; +using PG.StarWarsGame.Localisation.Commons.Helper; +using PG.StarWarsGame.Localisation.Languages; +using PG.Testing; + +namespace PG.StarWarsGame.Localisation.Test.Commons.Helper; + +[TestClass] +public class AlamoLanguageDefinitionHelperTest : ServiceTestBase +{ + [TestMethod] + public void Test_GetAllAlamoLanguageDefinitions__ReturnsAtLeastDefaults() + { + var definitions = GetServiceInstance() + .GetAllRegisteredAlamoLanguageDefinitions(); + Assert.IsTrue(definitions.Count >= LocalisationTestConstants.RegisteredLanguageDefinitions.Count, + "This function should always at least return the default language definitions."); + var actualNumberOfLanguageDefinitionsMarkedAsOfficial = 0; + var actualDefaultLanguageCount = 0; + foreach (var alamoLanguageDefinition in definitions) + { + if (alamoLanguageDefinition.GetType() + .GetAttributeValueOrDefault((OfficiallySupportedLanguageAttribute o) => o.IsOfficiallySupported)) + { + actualNumberOfLanguageDefinitionsMarkedAsOfficial++; + } + + if (alamoLanguageDefinition.GetType() + .GetAttributeValueOrDefault((DefaultLanguageAttribute d) => d.IsDefaultLanguage)) + { + actualDefaultLanguageCount++; + } + } + + Assert.AreEqual(LocalisationTestConstants.RegisteredLanguageDefinitions.Count, + actualNumberOfLanguageDefinitionsMarkedAsOfficial, + "Someone added a language definition marked as official. This should not happen. Please remove the OfficiallySupportedLanguageAttribute from the offending language."); + Assert.AreEqual(1, actualDefaultLanguageCount, + "Someone added a language definition marked as default. This should not happen. Please remove the DefaultAttribute from the offending language."); + } + + [TestMethod] + public void Test_GetDefaultAlamoLanguageDefinition__ReturnsCorrectLanguage() + { + var l = GetServiceInstance().GetDefaultAlamoLanguageDefinition(); + Assert.AreEqual(LocalisationTestConstants.DefaultLanguage, l.GetType()); + var actual = + Activator.CreateInstance(LocalisationTestConstants.DefaultLanguage) as IAlamoLanguageDefinition; + Assert.AreEqual(actual, l); + } + + protected override Type GetServiceClass() + { + return typeof(AlamoLanguageDefinitionHelper); + } +} \ No newline at end of file diff --git a/PG.StarWarsGame.Localisation/PG.StarWarsGame.Localisation.Test/Languages/AlamoLanguageDefinitionIntegrityTest.cs b/PG.StarWarsGame.Localisation/PG.StarWarsGame.Localisation.Test/Languages/AlamoLanguageDefinitionIntegrityTest.cs index 013e4ed0d..c478fc3e3 100644 --- a/PG.StarWarsGame.Localisation/PG.StarWarsGame.Localisation.Test/Languages/AlamoLanguageDefinitionIntegrityTest.cs +++ b/PG.StarWarsGame.Localisation/PG.StarWarsGame.Localisation.Test/Languages/AlamoLanguageDefinitionIntegrityTest.cs @@ -2,59 +2,67 @@ // Licensed under the MIT license. See LICENSE file in the project root for details. using Microsoft.VisualStudio.TestTools.UnitTesting; -using PG.Core.Attributes; -using PG.Core.Localisation; -using PG.Core.Localisation.Attributes; -using PG.Core.Test; -using PG.StarWarsGame.Localisation.Util; -using System.Collections.Generic; - -namespace PG.StarWarsGame.Localisation.Test.Languages +using PG.Commons.Attributes; +using PG.StarWarsGame.Localisation.Attributes; +using PG.StarWarsGame.Localisation.Commons.Helper; +using PG.Testing; + +namespace PG.StarWarsGame.Localisation.Test.Languages; + +[TestClass] +[TestCategory(TestConstants.TestCategories.HOLY)] +public class AlamoLanguageDefinitionIntegrityTest { - [TestClass] - [TestCategory(TestConstants.TEST_TYPE_HOLY)] - public class AlamoLanguageDefinitionIntegrityTest + private IAlamoLanguageDefinitionHelper GetHelper() { - [TestMethod] - public void Test_CorrectNumberOfLanguages() - { - IList l = LocalisationUtility.GetAllAlamoLanguageDefinitions(); - Assert.AreEqual(LocalisationTestConstants.REGISTERED_LANGUAGE_DEFINITIONS.Count, l.Count, "An official language definition has been added or removed. This should never happen - if there is a good reason for this, please update LocalisationTestConstants.REGISTERED_LANGUAGE_DEFINITIONS accordingly."); - } + return new AlamoLanguageDefinitionHelper(TestConstants.Services); + } - [TestMethod] - public void Test_CorrectLanguagesRegistered() + [TestMethod] + public void Test_CorrectNumberOfLanguages() + { + var l = GetHelper().GetAllRegisteredAlamoLanguageDefinitions(); + Assert.AreEqual(LocalisationTestConstants.RegisteredLanguageDefinitions.Count, l.Count, + "An official language definition has been added or removed. This should never happen - if there is a good reason for this, please update LocalisationTestConstants.REGISTERED_LANGUAGE_DEFINITIONS accordingly."); + } + + [TestMethod] + public void Test_CorrectLanguagesRegistered() + { + var l = GetHelper().GetAllRegisteredAlamoLanguageDefinitions(); + foreach (var alamoLanguageDefinition in l) { - IList l = LocalisationUtility.GetAllAlamoLanguageDefinitions(); - foreach (IAlamoLanguageDefinition alamoLanguageDefinition in l) - { - Assert.IsTrue( - LocalisationTestConstants.REGISTERED_LANGUAGE_DEFINITIONS.Contains( - alamoLanguageDefinition.GetType())); - Assert.IsTrue(alamoLanguageDefinition.GetType().GetAttributeValueOrDefault((OfficiallySupportedLanguageAttribute o) => o.IsOfficiallySupported)); - } + Assert.IsTrue( + LocalisationTestConstants.RegisteredLanguageDefinitions.Contains( + alamoLanguageDefinition.GetType())); + Assert.IsTrue(alamoLanguageDefinition.GetType() + .GetAttributeValueOrDefault((OfficiallySupportedLanguageAttribute o) => o.IsOfficiallySupported)); } + } - [TestMethod] - public void Test_DefaultLanguageIsDefined() + [TestMethod] + public void Test_DefaultLanguageIsDefined() + { + var l = GetHelper().GetAllRegisteredAlamoLanguageDefinitions(); + var isDefaultDefined = false; + foreach (var alamoLanguageDefinition in l) { - IList l = LocalisationUtility.GetAllAlamoLanguageDefinitions(); - bool isDefaultDefined = false; - foreach (IAlamoLanguageDefinition alamoLanguageDefinition in l) + if (alamoLanguageDefinition.GetType() + .GetAttributeValueOrDefault((DefaultLanguageAttribute d) => d.IsDefaultLanguage)) { - if (alamoLanguageDefinition.GetType().GetAttributeValueOrDefault((DefaultAttribute d) => d.IsDefault)) - { - isDefaultDefined = true; - } + isDefaultDefined = true; } - Assert.IsTrue(isDefaultDefined, "No default language is defined. This should not happen. EnglishAlamoLanguageDefinition should have the Default attribute set."); } - [TestMethod] - public void Test_DefaultLanguageIsCorrect() - { - IAlamoLanguageDefinition l = LocalisationUtility.GetDefaultAlamoLanguageDefinition(); - Assert.AreEqual(LocalisationTestConstants.DEFAULT_LANGUAGE, l.GetType(), "The default language is not EnglishAlamoLanguageDefinition. This should never happen. Please ensure EnglishAlamoLanguageDefinition has the Default attribute set."); - } + Assert.IsTrue(isDefaultDefined, + "No default language is defined. This should not happen. EnglishAlamoLanguageDefinition should have the DefaultLanguage attribute set."); + } + + [TestMethod] + public void Test_DefaultLanguageIsCorrect() + { + var l = GetHelper().GetDefaultAlamoLanguageDefinition(); + Assert.AreEqual(LocalisationTestConstants.DefaultLanguage, l.GetType(), + "The default language is not EnglishAlamoLanguageDefinition. This should never happen. Please ensure EnglishAlamoLanguageDefinition has the Default attribute set."); } -} +} \ No newline at end of file diff --git a/PG.StarWarsGame.Localisation/PG.StarWarsGame.Localisation.Test/LocalisationTestConstants.cs b/PG.StarWarsGame.Localisation/PG.StarWarsGame.Localisation.Test/LocalisationTestConstants.cs index a4eb8d04c..0c9a78d2c 100644 --- a/PG.StarWarsGame.Localisation/PG.StarWarsGame.Localisation.Test/LocalisationTestConstants.cs +++ b/PG.StarWarsGame.Localisation/PG.StarWarsGame.Localisation.Test/LocalisationTestConstants.cs @@ -1,31 +1,29 @@ // Copyright (c) Alamo Engine Tools and contributors. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for details. -using PG.Core.Test; -using PG.StarWarsGame.Localisation.Languages; using System; using System.Collections.Generic; +using PG.StarWarsGame.Localisation.Languages.Builtin; +using PG.Testing; -namespace PG.StarWarsGame.Localisation.Test +namespace PG.StarWarsGame.Localisation.Test; + +public sealed class LocalisationTestConstants : TestConstants { - public sealed class LocalisationTestConstants : TestConstants + public static readonly IList RegisteredLanguageDefinitions = new List { - public static readonly IList REGISTERED_LANGUAGE_DEFINITIONS = new List - { - //[gruenwaldlu, 2021-04-18-12:02:51+2]: All officially supported languages are listed below. - typeof(ChineseAlamoLanguageDefinition) - , typeof(EnglishAlamoLanguageDefinition) - , typeof(FrenchAlamoLanguageDefinition) - , typeof(GermanAlamoLanguageDefinition) - , typeof(ItalianAlamoLanguageDefinition) - , typeof(JapaneseAlamoLanguageDefinition) - , typeof(KoreanAlamoLanguageDefinition) - , typeof(PolishAlamoLanguageDefinition) - , typeof(RussianAlamoLanguageDefinition) - , typeof(SpanishAlamoLanguageDefinition) - , typeof(ThaiAlamoLanguageDefinition) - }; + typeof(ChineseAlamoLanguageDefinition), + typeof(EnglishAlamoLanguageDefinition), + typeof(FrenchAlamoLanguageDefinition), + typeof(GermanAlamoLanguageDefinition), + typeof(ItalianAlamoLanguageDefinition), + typeof(JapaneseAlamoLanguageDefinition), + typeof(KoreanAlamoLanguageDefinition), + typeof(PolishAlamoLanguageDefinition), + typeof(RussianAlamoLanguageDefinition), + typeof(SpanishAlamoLanguageDefinition), + typeof(ThaiAlamoLanguageDefinition) + }; - public static readonly Type DEFAULT_LANGUAGE = typeof(EnglishAlamoLanguageDefinition); - } -} + public static readonly Type DefaultLanguage = typeof(EnglishAlamoLanguageDefinition); +} \ No newline at end of file diff --git a/PG.StarWarsGame.Localisation/PG.StarWarsGame.Localisation.Test/PG.StarWarsGame.Localisation.Test.csproj b/PG.StarWarsGame.Localisation/PG.StarWarsGame.Localisation.Test/PG.StarWarsGame.Localisation.Test.csproj index dcab51f05..3c8521928 100644 --- a/PG.StarWarsGame.Localisation/PG.StarWarsGame.Localisation.Test/PG.StarWarsGame.Localisation.Test.csproj +++ b/PG.StarWarsGame.Localisation/PG.StarWarsGame.Localisation.Test/PG.StarWarsGame.Localisation.Test.csproj @@ -1,15 +1,18 @@  - - false + + false net8.0 $(TargetFrameworks);net48 - - + AlamoEngineTools.PG.StarWarsGame.Localisation.Test + 0.1.0-alpha.1 + 0.1.0-alpha.1 + 0.1.0-alpha.1 + + all runtime; build; native; contentfiles; analyzers; buildtransitive - @@ -20,9 +23,10 @@ - - - - - + + + + + + \ No newline at end of file diff --git a/PG.StarWarsGame.Localisation/PG.StarWarsGame.Localisation.Test/Util/LocalisationUtilityTest.cs b/PG.StarWarsGame.Localisation/PG.StarWarsGame.Localisation.Test/Util/LocalisationUtilityTest.cs deleted file mode 100644 index 412352319..000000000 --- a/PG.StarWarsGame.Localisation/PG.StarWarsGame.Localisation.Test/Util/LocalisationUtilityTest.cs +++ /dev/null @@ -1,57 +0,0 @@ -// Copyright (c) Alamo Engine Tools and contributors. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for details. - -using Microsoft.VisualStudio.TestTools.UnitTesting; -using PG.Core.Attributes; -using PG.Core.Localisation; -using PG.Core.Localisation.Attributes; -using PG.Core.Test; -using PG.StarWarsGame.Localisation.Util; -using System; -using System.Collections.Generic; - -namespace PG.StarWarsGame.Localisation.Test.Util -{ - [TestClass] - [TestCategory(TestConstants.TEST_TYPE_UTILITY)] - public class LocalisationUtilityTest - { - [TestMethod] - public void Test_GetAllAlamoLanguageDefinitions__ReturnsAtLeastDefaults() - { - IList l = LocalisationUtility.GetAllAlamoLanguageDefinitions(); - Assert.IsTrue(l.Count >= LocalisationTestConstants.REGISTERED_LANGUAGE_DEFINITIONS.Count, - "This function should always at least return the default language definitions."); - int actualNumberOfLanguageDefinitionsMarkedAsOfficial = 0; - int actualDefaultLanguageCount = 0; - foreach (IAlamoLanguageDefinition alamoLanguageDefinition in l) - { - if (alamoLanguageDefinition.GetType() - .GetAttributeValueOrDefault((OfficiallySupportedLanguageAttribute o) => o.IsOfficiallySupported)) - { - actualNumberOfLanguageDefinitionsMarkedAsOfficial++; - } - - if (alamoLanguageDefinition.GetType() - .GetAttributeValueOrDefault((DefaultAttribute d) => d.IsDefault)) - { - actualDefaultLanguageCount++; - } - } - Assert.AreEqual(LocalisationTestConstants.REGISTERED_LANGUAGE_DEFINITIONS.Count, actualNumberOfLanguageDefinitionsMarkedAsOfficial, - "Someone added a language definition marked as official. This should not happen. Please remove the OfficiallySupportedLanguageAttribute from the offending language."); - Assert.AreEqual(1, actualDefaultLanguageCount, - "Someone added a language definition marked as default. This should not happen. Please remove the DefaultAttribute from the offending language."); - - } - - [TestMethod] - public void Test_GetDefaultAlamoLanguageDefinition__ReturnsCorrectLanguage() - { - IAlamoLanguageDefinition l = LocalisationUtility.GetDefaultAlamoLanguageDefinition(); - Assert.AreEqual(LocalisationTestConstants.DEFAULT_LANGUAGE, l.GetType()); - IAlamoLanguageDefinition actual = Activator.CreateInstance(LocalisationTestConstants.DEFAULT_LANGUAGE) as IAlamoLanguageDefinition; - Assert.AreEqual(actual, l); - } - } -} diff --git a/PG.StarWarsGame.Localisation/PG.StarWarsGame.Localisation/Attributes/DefaultLanguageAttribute.cs b/PG.StarWarsGame.Localisation/PG.StarWarsGame.Localisation/Attributes/DefaultLanguageAttribute.cs index 7d552254d..0f7b6bd56 100644 --- a/PG.StarWarsGame.Localisation/PG.StarWarsGame.Localisation/Attributes/DefaultLanguageAttribute.cs +++ b/PG.StarWarsGame.Localisation/PG.StarWarsGame.Localisation/Attributes/DefaultLanguageAttribute.cs @@ -2,18 +2,33 @@ // Licensed under the MIT license. See LICENSE file in the project root for details. using System; +using PG.StarWarsGame.Localisation.Languages; namespace PG.StarWarsGame.Localisation.Attributes; +/// +/// that can be used to mark a as the default +/// language. +/// public class DefaultLanguageAttribute : Attribute { + /// + /// Is set to true, if the language is a default. + /// public bool IsDefaultLanguage { get; } + /// + /// .ctor: Marks the given as default. + /// public DefaultLanguageAttribute() { IsDefaultLanguage = true; } + /// + /// .ctor + /// + /// Boolean parameter. Can be used to explicitly mark a language as not default. public DefaultLanguageAttribute(bool isDefaultLanguage) { IsDefaultLanguage = isDefaultLanguage; diff --git a/PG.StarWarsGame.Localisation/PG.StarWarsGame.Localisation/Attributes/OfficiallySupportedLanguageAttribute.cs b/PG.StarWarsGame.Localisation/PG.StarWarsGame.Localisation/Attributes/OfficiallySupportedLanguageAttribute.cs index f15f2a7b0..e69d8c49a 100644 --- a/PG.StarWarsGame.Localisation/PG.StarWarsGame.Localisation/Attributes/OfficiallySupportedLanguageAttribute.cs +++ b/PG.StarWarsGame.Localisation/PG.StarWarsGame.Localisation/Attributes/OfficiallySupportedLanguageAttribute.cs @@ -6,17 +6,29 @@ namespace PG.StarWarsGame.Localisation.Attributes; +/// +/// An that marks a language as officially supported. +/// [AttributeUsage(AttributeTargets.Class, Inherited = false)] [ExcludeFromCodeCoverage] public sealed class OfficiallySupportedLanguageAttribute : Attribute { + /// + /// Attribute value. + /// public bool IsOfficiallySupported { get; } + /// + /// .ctor + /// public OfficiallySupportedLanguageAttribute() { IsOfficiallySupported = true; } + /// + /// .ctor + /// public OfficiallySupportedLanguageAttribute(bool isOfficiallySupported) { IsOfficiallySupported = isOfficiallySupported; diff --git a/PG.StarWarsGame.Localisation/PG.StarWarsGame.Localisation/Commons/Helper/AlamoLanguageDefinitionHelper.cs b/PG.StarWarsGame.Localisation/PG.StarWarsGame.Localisation/Commons/Helper/AlamoLanguageDefinitionHelper.cs new file mode 100644 index 000000000..a1ee9016e --- /dev/null +++ b/PG.StarWarsGame.Localisation/PG.StarWarsGame.Localisation/Commons/Helper/AlamoLanguageDefinitionHelper.cs @@ -0,0 +1,80 @@ +// Copyright (c) Alamo Engine Tools and contributors. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for details. + +using System; +using System.Collections.Generic; +using System.Collections.Immutable; +using System.Linq; +using PG.Commons.Attributes; +using PG.Commons.Services; +using PG.StarWarsGame.Localisation.Attributes; +using PG.StarWarsGame.Localisation.Languages; + +namespace PG.StarWarsGame.Localisation.Commons.Helper; + +/// +internal class AlamoLanguageDefinitionHelper : ServiceBase, IAlamoLanguageDefinitionHelper +{ + private IImmutableDictionary LanguageDefinitionCache { get; } + + /// + public AlamoLanguageDefinitionHelper(IServiceProvider services) : base(services) + { + var d = new Dictionary(); + InitCache(d); + LanguageDefinitionCache = d.ToImmutableDictionary(); + } + + private void InitCache(IDictionary definitions) + { + var registeredLanguages = GetRegisteredTypes(); + foreach (var language in registeredLanguages) + { + if (Activator.CreateInstance(language) is IAlamoLanguageDefinition instance) + { + definitions.Add(language, instance); + } + } + } + + private static List GetRegisteredTypes() + { + return AppDomain.CurrentDomain.GetAssemblies().SelectMany(s => s.GetTypes()) + .Where(p => + typeof(IAlamoLanguageDefinition).IsAssignableFrom(p) && + typeof(AlamoLanguageDefinitionBase).IsAssignableFrom(p) && + p.IsClass && + !p.IsAbstract && + !p.IsInterface + ).ToList(); + } + + /// + public IReadOnlyCollection GetAllRegisteredAlamoLanguageDefinitions() + { + return LanguageDefinitionCache.Values.ToImmutableList(); + } + + /// + public IAlamoLanguageDefinition GetDefaultAlamoLanguageDefinition() + { + foreach (var definition in GetAllRegisteredAlamoLanguageDefinitions()) + { + if (definition.GetType() + .GetAttributeValueOrDefault(a => a.IsDefaultLanguage)) + { + return definition; + } + } + + throw new InvalidOperationException( + $"No default {nameof(IAlamoLanguageDefinition)} is defined. This should not happen."); + } + + /// + public bool IsOfficiallySupported(IAlamoLanguageDefinition languageDefinition) + { + return languageDefinition.GetType() + .GetAttributeValueOrDefault(a => a.IsOfficiallySupported); + } +} \ No newline at end of file diff --git a/PG.StarWarsGame.Localisation/PG.StarWarsGame.Localisation/Commons/Helper/IAlamoLanguageDefinitionHelper.cs b/PG.StarWarsGame.Localisation/PG.StarWarsGame.Localisation/Commons/Helper/IAlamoLanguageDefinitionHelper.cs new file mode 100644 index 000000000..10853ef67 --- /dev/null +++ b/PG.StarWarsGame.Localisation/PG.StarWarsGame.Localisation/Commons/Helper/IAlamoLanguageDefinitionHelper.cs @@ -0,0 +1,33 @@ +// Copyright (c) Alamo Engine Tools and contributors. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for details. + +using System.Collections.Generic; +using PG.Commons.Services; +using PG.StarWarsGame.Localisation.Languages; + +namespace PG.StarWarsGame.Localisation.Commons.Helper; + +/// +/// Helper service for dealing with s. +/// +public interface IAlamoLanguageDefinitionHelper : IService +{ + /// + /// Returns all currently registered implementations. + /// + /// + IReadOnlyCollection GetAllRegisteredAlamoLanguageDefinitions(); + + /// + /// Returns the default . + /// + /// + IAlamoLanguageDefinition GetDefaultAlamoLanguageDefinition(); + + /// + /// Checks if the game officially supports a given . + /// + /// The definition to check. + /// + bool IsOfficiallySupported(IAlamoLanguageDefinition languageDefinition); +} \ No newline at end of file diff --git a/PG.StarWarsGame.Localisation/PG.StarWarsGame.Localisation/Languages/AlamoLanguageDefinitionBase.cs b/PG.StarWarsGame.Localisation/PG.StarWarsGame.Localisation/Languages/AlamoLanguageDefinitionBase.cs index abb0869db..025227626 100644 --- a/PG.StarWarsGame.Localisation/PG.StarWarsGame.Localisation/Languages/AlamoLanguageDefinitionBase.cs +++ b/PG.StarWarsGame.Localisation/PG.StarWarsGame.Localisation/Languages/AlamoLanguageDefinitionBase.cs @@ -1,7 +1,6 @@ // Copyright (c) Alamo Engine Tools and contributors. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for details. -using System; using System.Globalization; using FluentValidation; using FluentValidation.Results; @@ -9,7 +8,10 @@ namespace PG.StarWarsGame.Localisation.Languages; -public abstract class AlamoLanguageDefinitionBase : IAlamoLanguageDefinition, IComparable, IValidatable +/// +/// Base definition for an implementation. +/// +public abstract class AlamoLanguageDefinitionBase : IAlamoLanguageDefinition, IValidatable { /// /// A string that is being used to identify the language of the *.DAT file, e.g. a language identifier @@ -23,25 +25,11 @@ public abstract class AlamoLanguageDefinitionBase : IAlamoLanguageDefinition, IC /// protected abstract CultureInfo ConfiguredCulture { get; } + /// public string LanguageIdentifier => ConfiguredLanguageIdentifier; - public CultureInfo Culture => ConfiguredCulture; - /// - /// Compares the current instance with another object of the same type and returns an integer that indicates - /// whether the current instance precedes, follows, or occurs in the same position in the sort order as the - /// other object. - /// - /// - /// The specific override differs in its sort order in such a way, - /// that all elements are ordered by their t. - /// - /// - /// - protected virtual int CompareToInternal(IAlamoLanguageDefinition other) - { - return string.Compare(Culture.TwoLetterISOLanguageName, other.Culture.TwoLetterISOLanguageName, - StringComparison.Ordinal); - } + /// + public CultureInfo Culture => ConfiguredCulture; /// /// Determines whether the specified object is equal to the current object. @@ -50,7 +38,7 @@ protected virtual int CompareToInternal(IAlamoLanguageDefinition other) /// protected virtual bool EqualsInternal(IAlamoLanguageDefinition other) { - return CompareTo(other) == 0; + return GetHashCodeInternal().CompareTo(other.GetHashCode()) == 0; } /// @@ -69,6 +57,7 @@ protected virtual int GetHashCodeInternal() } } + /// public override bool Equals(object? obj) { if (obj is null) @@ -84,46 +73,50 @@ public override bool Equals(object? obj) return obj is IAlamoLanguageDefinition alamoLanguageDefinition && EqualsInternal(alamoLanguageDefinition); } - public int CompareTo(object obj) - { - if (obj is not IAlamoLanguageDefinition other) - { - throw new ArgumentException( - $"The type of {obj.GetType()} is not assignable to {typeof(IAlamoLanguageDefinition)}. The two objects cannot be compared."); - } - - return CompareToInternal(other); - } - + /// public override int GetHashCode() { return GetHashCodeInternal(); } + /// public ValidationResult Validate() { return ValidateInternal(); } + /// + /// Base implementation. + /// protected virtual ValidationResult ValidateInternal() { AlamoLanguageDefinitionValidatorBase v = new(); return v.Validate(this); } + /// public void ValidateAndThrow() { ValidateAndThrowInternal(); } + /// + /// Base implementation. + /// protected virtual void ValidateAndThrowInternal() { AlamoLanguageDefinitionValidatorBase v = new(); v.ValidateAndThrow(this); } + /// + /// Base class for validations. + /// protected class AlamoLanguageDefinitionValidatorBase : AbstractValidator { + /// + /// .ctor + /// public AlamoLanguageDefinitionValidatorBase() { RuleFor(languageDefinition => languageDefinition.ConfiguredCulture).NotNull(); diff --git a/PG.StarWarsGame.Localisation/PG.StarWarsGame.Localisation/Languages/Builtin/ChineseAlamoLanguageDefinition.cs b/PG.StarWarsGame.Localisation/PG.StarWarsGame.Localisation/Languages/Builtin/ChineseAlamoLanguageDefinition.cs index 2d3ca5ad4..bdae86024 100644 --- a/PG.StarWarsGame.Localisation/PG.StarWarsGame.Localisation/Languages/Builtin/ChineseAlamoLanguageDefinition.cs +++ b/PG.StarWarsGame.Localisation/PG.StarWarsGame.Localisation/Languages/Builtin/ChineseAlamoLanguageDefinition.cs @@ -17,6 +17,9 @@ namespace PG.StarWarsGame.Localisation.Languages.Builtin; [OfficiallySupportedLanguage] public sealed class ChineseAlamoLanguageDefinition : AlamoLanguageDefinitionBase { + /// protected override string ConfiguredLanguageIdentifier => "CHINESE"; + + /// protected override CultureInfo ConfiguredCulture => CultureInfo.GetCultureInfo("zh-CN"); } \ No newline at end of file diff --git a/PG.StarWarsGame.Localisation/PG.StarWarsGame.Localisation/Languages/Builtin/EnglishAlamoLanguageDefinition.cs b/PG.StarWarsGame.Localisation/PG.StarWarsGame.Localisation/Languages/Builtin/EnglishAlamoLanguageDefinition.cs index c0a68b53a..67d7d6e27 100644 --- a/PG.StarWarsGame.Localisation/PG.StarWarsGame.Localisation/Languages/Builtin/EnglishAlamoLanguageDefinition.cs +++ b/PG.StarWarsGame.Localisation/PG.StarWarsGame.Localisation/Languages/Builtin/EnglishAlamoLanguageDefinition.cs @@ -19,6 +19,9 @@ namespace PG.StarWarsGame.Localisation.Languages.Builtin; [OfficiallySupportedLanguage] public sealed class EnglishAlamoLanguageDefinition : AlamoLanguageDefinitionBase { + /// protected override string ConfiguredLanguageIdentifier => "ENGLISH"; + + /// protected override CultureInfo ConfiguredCulture => CultureInfo.GetCultureInfo("en-US"); } \ No newline at end of file diff --git a/PG.StarWarsGame.Localisation/PG.StarWarsGame.Localisation/Languages/Builtin/FrenchAlamoLanguageDefinition.cs b/PG.StarWarsGame.Localisation/PG.StarWarsGame.Localisation/Languages/Builtin/FrenchAlamoLanguageDefinition.cs index 87c50d7e3..bc383f63a 100644 --- a/PG.StarWarsGame.Localisation/PG.StarWarsGame.Localisation/Languages/Builtin/FrenchAlamoLanguageDefinition.cs +++ b/PG.StarWarsGame.Localisation/PG.StarWarsGame.Localisation/Languages/Builtin/FrenchAlamoLanguageDefinition.cs @@ -17,6 +17,9 @@ namespace PG.StarWarsGame.Localisation.Languages.Builtin; [OfficiallySupportedLanguage] public sealed class FrenchAlamoLanguageDefinition : AlamoLanguageDefinitionBase { + /// protected override string ConfiguredLanguageIdentifier => "FRENCH"; + + /// protected override CultureInfo ConfiguredCulture => CultureInfo.GetCultureInfo("fr-FR"); } \ No newline at end of file diff --git a/PG.StarWarsGame.Localisation/PG.StarWarsGame.Localisation/Languages/Builtin/GermanAlamoLanguageDefinition.cs b/PG.StarWarsGame.Localisation/PG.StarWarsGame.Localisation/Languages/Builtin/GermanAlamoLanguageDefinition.cs index a6c066092..cc6e34299 100644 --- a/PG.StarWarsGame.Localisation/PG.StarWarsGame.Localisation/Languages/Builtin/GermanAlamoLanguageDefinition.cs +++ b/PG.StarWarsGame.Localisation/PG.StarWarsGame.Localisation/Languages/Builtin/GermanAlamoLanguageDefinition.cs @@ -17,6 +17,9 @@ namespace PG.StarWarsGame.Localisation.Languages.Builtin; [OfficiallySupportedLanguage] public sealed class GermanAlamoLanguageDefinition : AlamoLanguageDefinitionBase { + /// protected override string ConfiguredLanguageIdentifier => "GERMAN"; + + /// protected override CultureInfo ConfiguredCulture => CultureInfo.GetCultureInfo("de-DE"); } \ No newline at end of file diff --git a/PG.StarWarsGame.Localisation/PG.StarWarsGame.Localisation/Languages/Builtin/ItalianAlamoLanguageDefinition.cs b/PG.StarWarsGame.Localisation/PG.StarWarsGame.Localisation/Languages/Builtin/ItalianAlamoLanguageDefinition.cs index cfaf2bf6b..4adeb3efb 100644 --- a/PG.StarWarsGame.Localisation/PG.StarWarsGame.Localisation/Languages/Builtin/ItalianAlamoLanguageDefinition.cs +++ b/PG.StarWarsGame.Localisation/PG.StarWarsGame.Localisation/Languages/Builtin/ItalianAlamoLanguageDefinition.cs @@ -17,6 +17,9 @@ namespace PG.StarWarsGame.Localisation.Languages.Builtin; [OfficiallySupportedLanguage] public sealed class ItalianAlamoLanguageDefinition : AlamoLanguageDefinitionBase { + /// protected override string ConfiguredLanguageIdentifier => "ITALIAN"; + + /// protected override CultureInfo ConfiguredCulture => CultureInfo.GetCultureInfo("it-IT"); } \ No newline at end of file diff --git a/PG.StarWarsGame.Localisation/PG.StarWarsGame.Localisation/Languages/Builtin/JapaneseAlamoLanguageDefinition.cs b/PG.StarWarsGame.Localisation/PG.StarWarsGame.Localisation/Languages/Builtin/JapaneseAlamoLanguageDefinition.cs index 9428fe78e..a3e08326b 100644 --- a/PG.StarWarsGame.Localisation/PG.StarWarsGame.Localisation/Languages/Builtin/JapaneseAlamoLanguageDefinition.cs +++ b/PG.StarWarsGame.Localisation/PG.StarWarsGame.Localisation/Languages/Builtin/JapaneseAlamoLanguageDefinition.cs @@ -17,6 +17,9 @@ namespace PG.StarWarsGame.Localisation.Languages.Builtin; [OfficiallySupportedLanguage] public sealed class JapaneseAlamoLanguageDefinition : AlamoLanguageDefinitionBase { + /// protected override string ConfiguredLanguageIdentifier => "JAPANESE"; + + /// protected override CultureInfo ConfiguredCulture => CultureInfo.GetCultureInfo("ja"); } \ No newline at end of file diff --git a/PG.StarWarsGame.Localisation/PG.StarWarsGame.Localisation/Languages/Builtin/KoreanAlamoLanguageDefinition.cs b/PG.StarWarsGame.Localisation/PG.StarWarsGame.Localisation/Languages/Builtin/KoreanAlamoLanguageDefinition.cs index 794670bd8..35679f690 100644 --- a/PG.StarWarsGame.Localisation/PG.StarWarsGame.Localisation/Languages/Builtin/KoreanAlamoLanguageDefinition.cs +++ b/PG.StarWarsGame.Localisation/PG.StarWarsGame.Localisation/Languages/Builtin/KoreanAlamoLanguageDefinition.cs @@ -17,6 +17,9 @@ namespace PG.StarWarsGame.Localisation.Languages.Builtin; [OfficiallySupportedLanguage] public sealed class KoreanAlamoLanguageDefinition : AlamoLanguageDefinitionBase { + /// protected override string ConfiguredLanguageIdentifier => "KOREAN"; + + /// protected override CultureInfo ConfiguredCulture => CultureInfo.GetCultureInfo("ko"); } \ No newline at end of file diff --git a/PG.StarWarsGame.Localisation/PG.StarWarsGame.Localisation/Languages/Builtin/PolishAlamoLanguageDefinition.cs b/PG.StarWarsGame.Localisation/PG.StarWarsGame.Localisation/Languages/Builtin/PolishAlamoLanguageDefinition.cs index 6f6bcb25c..aab7fe14b 100644 --- a/PG.StarWarsGame.Localisation/PG.StarWarsGame.Localisation/Languages/Builtin/PolishAlamoLanguageDefinition.cs +++ b/PG.StarWarsGame.Localisation/PG.StarWarsGame.Localisation/Languages/Builtin/PolishAlamoLanguageDefinition.cs @@ -17,6 +17,9 @@ namespace PG.StarWarsGame.Localisation.Languages.Builtin; [OfficiallySupportedLanguage] public sealed class PolishAlamoLanguageDefinition : AlamoLanguageDefinitionBase { + /// protected override string ConfiguredLanguageIdentifier => "POLISH"; + + /// protected override CultureInfo ConfiguredCulture => CultureInfo.GetCultureInfo("pl"); } \ No newline at end of file diff --git a/PG.StarWarsGame.Localisation/PG.StarWarsGame.Localisation/Languages/Builtin/RussianAlamoLanguageDefinition.cs b/PG.StarWarsGame.Localisation/PG.StarWarsGame.Localisation/Languages/Builtin/RussianAlamoLanguageDefinition.cs index 46ee96ba2..d66b471fc 100644 --- a/PG.StarWarsGame.Localisation/PG.StarWarsGame.Localisation/Languages/Builtin/RussianAlamoLanguageDefinition.cs +++ b/PG.StarWarsGame.Localisation/PG.StarWarsGame.Localisation/Languages/Builtin/RussianAlamoLanguageDefinition.cs @@ -17,6 +17,9 @@ namespace PG.StarWarsGame.Localisation.Languages.Builtin; [OfficiallySupportedLanguage] public sealed class RussianAlamoLanguageDefinition : AlamoLanguageDefinitionBase { + /// protected override string ConfiguredLanguageIdentifier => "RUSSIAN"; + + /// protected override CultureInfo ConfiguredCulture => CultureInfo.GetCultureInfo("ru"); } \ No newline at end of file diff --git a/PG.StarWarsGame.Localisation/PG.StarWarsGame.Localisation/Languages/Builtin/SpanishAlamoLanguageDefinition.cs b/PG.StarWarsGame.Localisation/PG.StarWarsGame.Localisation/Languages/Builtin/SpanishAlamoLanguageDefinition.cs index 20f4586b3..26903b35c 100644 --- a/PG.StarWarsGame.Localisation/PG.StarWarsGame.Localisation/Languages/Builtin/SpanishAlamoLanguageDefinition.cs +++ b/PG.StarWarsGame.Localisation/PG.StarWarsGame.Localisation/Languages/Builtin/SpanishAlamoLanguageDefinition.cs @@ -17,6 +17,9 @@ namespace PG.StarWarsGame.Localisation.Languages.Builtin; [OfficiallySupportedLanguage] public sealed class SpanishAlamoLanguageDefinition : AlamoLanguageDefinitionBase { + /// protected override string ConfiguredLanguageIdentifier => "SPANISH"; + + /// protected override CultureInfo ConfiguredCulture => CultureInfo.GetCultureInfo("es-ES"); } \ No newline at end of file diff --git a/PG.StarWarsGame.Localisation/PG.StarWarsGame.Localisation/Languages/Builtin/ThaiAlamoLanguageDefinition.cs b/PG.StarWarsGame.Localisation/PG.StarWarsGame.Localisation/Languages/Builtin/ThaiAlamoLanguageDefinition.cs index 6cc100980..4e4422e32 100644 --- a/PG.StarWarsGame.Localisation/PG.StarWarsGame.Localisation/Languages/Builtin/ThaiAlamoLanguageDefinition.cs +++ b/PG.StarWarsGame.Localisation/PG.StarWarsGame.Localisation/Languages/Builtin/ThaiAlamoLanguageDefinition.cs @@ -17,6 +17,9 @@ namespace PG.StarWarsGame.Localisation.Languages.Builtin; [OfficiallySupportedLanguage] public sealed class ThaiAlamoLanguageDefinition : AlamoLanguageDefinitionBase { + /// protected override string ConfiguredLanguageIdentifier => "THAI"; + + /// protected override CultureInfo ConfiguredCulture => CultureInfo.GetCultureInfo("th"); } \ No newline at end of file diff --git a/PG.StarWarsGame.Localisation/PG.StarWarsGame.Localisation/LocalisationDomain.cs b/PG.StarWarsGame.Localisation/PG.StarWarsGame.Localisation/LocalisationDomain.cs new file mode 100644 index 000000000..e8276d935 --- /dev/null +++ b/PG.StarWarsGame.Localisation/PG.StarWarsGame.Localisation/LocalisationDomain.cs @@ -0,0 +1,26 @@ +// Copyright (c) Alamo Engine Tools and contributors. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for details. + +using Microsoft.Extensions.DependencyInjection; +using PG.StarWarsGame.Files.DAT; +using PG.StarWarsGame.Localisation.Commons.Helper; + +namespace PG.StarWarsGame.Localisation; + +/// +/// Provides methods to initialize this library. +/// +public static class LocalisationDomain +{ + /// + /// Adds the requires services for this library to the service collection. + /// + /// The service collection to populate. + public static void RegisterServices(IServiceCollection serviceCollection) + { + DatDomain.RegisterServices(serviceCollection); + serviceCollection + .AddSingleton(sp => new AlamoLanguageDefinitionHelper(sp)) + ; + } +} \ No newline at end of file diff --git a/PG.StarWarsGame.Localisation/PG.StarWarsGame.Localisation/PG.StarWarsGame.Localisation.csproj b/PG.StarWarsGame.Localisation/PG.StarWarsGame.Localisation/PG.StarWarsGame.Localisation.csproj index 4dc5b9e6d..611edd871 100644 --- a/PG.StarWarsGame.Localisation/PG.StarWarsGame.Localisation/PG.StarWarsGame.Localisation.csproj +++ b/PG.StarWarsGame.Localisation/PG.StarWarsGame.Localisation/PG.StarWarsGame.Localisation.csproj @@ -7,9 +7,14 @@ 0.1.0-alpha.1 0.1.0-alpha.1 + + true + true + true + - + @@ -62,5 +67,6 @@ + diff --git a/PG.Testing/ServiceTestBase.cs b/PG.Testing/ServiceTestBase.cs index 157f719ca..230d593d7 100644 --- a/PG.Testing/ServiceTestBase.cs +++ b/PG.Testing/ServiceTestBase.cs @@ -12,25 +12,26 @@ namespace PG.Testing; [TestCategory(TestConstants.TestCategories.SERVICE)] public abstract class ServiceTestBase { - private IFileSystem FileSystem { get; set; } + private IFileSystem FileSystem { get; set; } = null!; protected abstract Type GetServiceClass(); - protected virtual IService GetServiceInstance() + + protected virtual T GetServiceInstance() where T : ServiceBase, IService { - return GetServiceInstance(GetServiceProviderInternal()); + return GetServiceInstance(GetServiceProviderInternal()); } - protected virtual IService GetServiceInstance(IServiceProvider services) + protected virtual T GetServiceInstance(IServiceProvider services) where T : ServiceBase, IService { - FileSystem = (IFileSystem) services.GetService(typeof(IFileSystem)); - return (IService)Activator.CreateInstance(GetServiceClass(), services); + FileSystem = (IFileSystem)(services.GetService(typeof(IFileSystem)) ?? throw new InvalidOperationException()); + return Activator.CreateInstance(GetServiceClass(), services) as T ?? throw new InvalidOperationException(); } protected internal virtual IServiceProvider GetServiceProviderInternal() { return TestConstants.Services; } - + [TestMethod] public void Test_ServiceBaseSetup__IsValid() @@ -40,11 +41,11 @@ public void Test_ServiceBaseSetup__IsValid() private void TestBaseSetup() { - using var svc = GetServiceInstance(); + var svc = GetServiceInstance(); Assert.IsTrue(svc.GetType().IsSubclassOf(typeof(ServiceBase))); - var svc0 = (ServiceBase)svc; - Assert.IsNotNull(svc0.Logger); - Assert.IsNotNull(svc0.FileSystem); + Assert.IsNotNull(svc); + Assert.IsNotNull(svc.Logger); + Assert.IsNotNull(svc.FileSystem); TestBaseSetupInternal(svc); } diff --git a/PG.Testing/TestConstants.cs b/PG.Testing/TestConstants.cs index 9bd2d6845..74536f989 100644 --- a/PG.Testing/TestConstants.cs +++ b/PG.Testing/TestConstants.cs @@ -11,16 +11,17 @@ namespace PG.Testing; /// -/// Constants that can be re-used in all test projects. +/// Constants that can be re-used in all test projects. /// public class TestConstants { /// - /// The Linux platform name + /// The Linux platform name /// public const string PLATFORM_LINUX = "LINUX"; + /// - /// The Windows platform name + /// The Windows platform name /// public const string PLATFORM_WINDOWS = "WINDOWS"; @@ -43,14 +44,15 @@ private static IServiceProvider GetServiceProvider() } var collection = new ServiceCollection(); + RegisterServicesInternal(collection); - + s_serviceProvider = collection .BuildServiceProvider(); return s_serviceProvider; } - private static void RegisterServicesInternal(ServiceCollection collection) + protected static void RegisterServicesInternal(ServiceCollection collection) { collection .AddSingleton()