|
| 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 | +} |
0 commit comments