Skip to content

Commit 3de9c1f

Browse files
committed
LanguageSupportService
1 parent cec8cb1 commit 3de9c1f

File tree

3 files changed

+92
-3
lines changed

3 files changed

+92
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
// Copyright (c) Alamo Engine Tools and contributors. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for details.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.IO.Abstractions;
7+
using System.Linq;
8+
using System.Reflection;
9+
using Microsoft.Extensions.DependencyInjection;
10+
using PG.Commons.Extensibility;
11+
using PG.StarWarsGame.Components.Localisation.Attributes;
12+
using PG.StarWarsGame.Components.Localisation.Languages;
13+
using PG.StarWarsGame.Components.Localisation.Languages.BuiltIn;
14+
using PG.StarWarsGame.Components.Localisation.Services;
15+
using Testably.Abstractions.Testing;
16+
using Xunit;
17+
18+
namespace PG.StarWarsGame.Components.Localisation.Test.Services;
19+
20+
public class AlamoLanguageSupportServiceTest
21+
{
22+
private readonly MockFileSystem _fileSystem = new();
23+
private readonly IAlamoLanguageSupportService _service;
24+
25+
private readonly HashSet<Type> builtin_types = new()
26+
{
27+
typeof(ChineseAlamoLanguageDefinition),
28+
typeof(EnglishAlamoLanguageDefinition),
29+
typeof(FrenchAlamoLanguageDefinition),
30+
typeof(GermanAlamoLanguageDefinition),
31+
typeof(ItalianAlamoLanguageDefinition),
32+
typeof(JapaneseAlamoLanguageDefinition),
33+
typeof(KoreanAlamoLanguageDefinition),
34+
typeof(PolishAlamoLanguageDefinition),
35+
typeof(RussianAlamoLanguageDefinition),
36+
typeof(SpanishAlamoLanguageDefinition),
37+
typeof(ThaiAlamoLanguageDefinition)
38+
};
39+
40+
public AlamoLanguageSupportServiceTest()
41+
{
42+
var sc = new ServiceCollection();
43+
sc.AddSingleton<IFileSystem>(_fileSystem);
44+
sc.CollectPgServiceContributions();
45+
_service = sc.BuildServiceProvider().GetRequiredService<IAlamoLanguageSupportService>();
46+
}
47+
48+
[Fact]
49+
public void Test_DefaultLanguageDefinitionDefinedExactlyOnce()
50+
{
51+
var languageDefinitions = AppDomain.CurrentDomain.GetAssemblies()
52+
.SelectMany(assemblyTypes => assemblyTypes.GetTypes())
53+
.Where(assemblyType => typeof(IAlamoLanguageDefinition).IsAssignableFrom(assemblyType) &&
54+
assemblyType is { IsClass: true, IsAbstract: false })
55+
.Where(t => t.GetCustomAttribute(typeof(DefaultLanguageAttribute)) != null)
56+
.ToList();
57+
Assert.Single(languageDefinitions);
58+
Assert.NotNull(languageDefinitions[0]);
59+
}
60+
61+
[Fact]
62+
public void Test_GetDefaultLanguageDefinition_ReturnsEnglish()
63+
{
64+
var def = _service.GetDefaultLanguageDefinition();
65+
Assert.NotNull(def);
66+
Assert.Equal(def, new EnglishAlamoLanguageDefinition());
67+
}
68+
69+
[Theory]
70+
[InlineData(null, typeof(EnglishAlamoLanguageDefinition))]
71+
[InlineData(typeof(EnglishAlamoLanguageDefinition), typeof(EnglishAlamoLanguageDefinition))]
72+
[InlineData(typeof(ChineseAlamoLanguageDefinition), typeof(ChineseAlamoLanguageDefinition))]
73+
public void Test_GetFallbackLanguageDefinition_ReturnsDesired(Type? definition, Type expected)
74+
{
75+
var exp = (IAlamoLanguageDefinition)Activator.CreateInstance(expected)!;
76+
if (definition != null)
77+
_service.WithOverrideFallbackLanguage((IAlamoLanguageDefinition)Activator.CreateInstance(definition)!);
78+
else
79+
_service.WithOverrideFallbackLanguage(new EnglishAlamoLanguageDefinition());
80+
Assert.Equal(exp, _service.GetFallbackLanguageDefinition());
81+
}
82+
83+
[Fact]
84+
public void Test_IsBuiltInLanguageDefinition_ReturnsTrue()
85+
{
86+
foreach (var b in builtin_types)
87+
Assert.True(_service.IsBuiltInLanguageDefinition((IAlamoLanguageDefinition)Activator.CreateInstance(b)!));
88+
}
89+
}

PG.StarWarsGame.Components.Localisation/PG.StarWarsGame.Components.Localisation/Services/AlamoLanguageSupportService.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public bool IsBuiltInLanguageDefinition(IAlamoLanguageDefinition definition)
2929
}
3030

3131
/// <inheritdoc />
32-
public IAlamoLanguageDefinition GetFallBackLanguageDefinition()
32+
public IAlamoLanguageDefinition GetFallbackLanguageDefinition()
3333
{
3434
return _fallbackAlamoLanguageDefinition;
3535
}

PG.StarWarsGame.Components.Localisation/PG.StarWarsGame.Components.Localisation/Services/IAlamoLanguageSupportService.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public interface IAlamoLanguageSupportService
2323
/// overridden with <see cref="WithOverrideFallbackLanguage" />
2424
/// </summary>
2525
/// <returns></returns>
26-
IAlamoLanguageDefinition GetFallBackLanguageDefinition();
26+
IAlamoLanguageDefinition GetFallbackLanguageDefinition();
2727

2828
/// <summary>
2929
/// Allows for overriding the fallback language.
@@ -38,4 +38,4 @@ public interface IAlamoLanguageSupportService
3838
/// </summary>
3939
/// <returns></returns>
4040
IAlamoLanguageDefinition GetDefaultLanguageDefinition();
41-
}
41+
}

0 commit comments

Comments
 (0)