Skip to content

Commit

Permalink
[#316] Port Language Definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
gruenwaldlk committed Oct 12, 2023
1 parent dd9f9c4 commit 1027642
Show file tree
Hide file tree
Showing 31 changed files with 426 additions and 185 deletions.
13 changes: 13 additions & 0 deletions .idea/.idea.PetroglyphTools/.idea/.gitignore

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

8 changes: 8 additions & 0 deletions .idea/.idea.PetroglyphTools/.idea/indexLayout.xml

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

6 changes: 6 additions & 0 deletions .idea/.idea.PetroglyphTools/.idea/vcs.xml

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

1 change: 0 additions & 1 deletion PG.Commons/PG.Commons/Attributes/AttributeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ public static class AttributeExtensions
/// <param name="valueSelector">The function to retrieve the value.</param>
/// <typeparam name="TAttribute">The type of the attribute.</typeparam>
/// <typeparam name="TValue">The return type.</typeparam>
/// <returns>The <see cref="TValue" /></returns>
public static TValue? GetAttributeValueOrDefault<TAttribute, TValue>(this Type type,
Func<TAttribute, TValue> valueSelector) where TAttribute : Attribute
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ protected override Type GetServiceClass()

private DatFileConverter GetService()
{
var svc = GetServiceInstance();
return (DatFileConverter)svc;
return GetServiceInstance<DatFileConverter>();
}

[TestMethod]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard2.0;netstandard2.1</TargetFrameworks>
<TargetFrameworks>netstandard2.0;netstandard2.1,net7.0</TargetFrameworks>
<Title>PG.StarWarsGame.Files.DAT</Title>
<Product>PG.StarWarsGame.Files.DAT</Product>
<PackageId>AlamoEngineTools.PG.StarWarsGame.Files.DAT</PackageId>
Expand Down
Original file line number Diff line number Diff line change
@@ -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<AlamoLanguageDefinitionHelper>()
.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<AlamoLanguageDefinitionHelper>().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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<IAlamoLanguageDefinition> 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<IAlamoLanguageDefinition> 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<IAlamoLanguageDefinition> 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.");
}
}
}
Original file line number Diff line number Diff line change
@@ -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<Type> RegisteredLanguageDefinitions = new List<Type>
{
public static readonly IList<Type> REGISTERED_LANGUAGE_DEFINITIONS = new List<Type>
{
//[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);
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<IsPackable>false</IsPackable>
<PropertyGroup>
<IsPackable>false</IsPackable>
<TargetFrameworks>net8.0</TargetFrameworks>
<TargetFrameworks Condition="!$([MSBuild]::IsOsUnixLike())">$(TargetFrameworks);net48</TargetFrameworks>
</PropertyGroup>
<ItemGroup>
<PackageId>AlamoEngineTools.PG.StarWarsGame.Localisation.Test</PackageId>
<Version>0.1.0-alpha.1</Version>
<AssemblyVersion>0.1.0-alpha.1</AssemblyVersion>
<FileVersion>0.1.0-alpha.1</FileVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="coverlet.msbuild" Version="6.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0-preview.6.23329.7"/>
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.0-preview.6.23329.7"/>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0-preview-23371-04"/>
<PackageReference Include="MSTest.TestAdapter" Version="3.1.1"/>
Expand All @@ -20,9 +23,10 @@
</PackageReference>
<PackageReference Include="System.IO.Abstractions" Version="19.2.51"/>
<PackageReference Include="System.IO.Abstractions.TestingHelpers" Version="19.2.51"/>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\PG.Testing\PG.Testing.csproj" />
<ProjectReference Include="..\PG.StarWarsGame.Localisation\PG.StarWarsGame.Localisation.csproj" />
</ItemGroup>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\PG.Commons\PG.Commons.Test\PG.Commons.Test.csproj"/>
<ProjectReference Include="..\..\PG.Testing\PG.Testing.csproj"/>
<ProjectReference Include="..\PG.StarWarsGame.Localisation\PG.StarWarsGame.Localisation.csproj"/>
</ItemGroup>
</Project>

This file was deleted.

Loading

0 comments on commit 1027642

Please sign in to comment.