using System.Collections; using System.ComponentModel.DataAnnotations; using System.Globalization; using System.IO; using System.Reflection; using System.Resources; using YukkuriMovieMaker.Commons; using YukkuriMovieMaker.UndoRedo; namespace UndoTree.Tests; public sealed class UndoTreeNamesTests { public static readonly TheoryData AnimationNames = new() { { "ja-JP", "アニメーション", "長さ" }, { "en-US", "Animation", "Length" }, { "zh-CN", "动画", "长度" }, { "zh-TW", "動畫", "長度" }, { "ko-KR", "애니메이션", "길이" }, { "es-ES", "Animación", "Longitud" }, { "ar-SA", "الرسوم المتحركة", "الطول" }, { "id-ID", "Animasi", "Panjang" }, }; [Theory] [MemberData(nameof(AnimationNames))] public void ANameIsTranslatedIntoTheGivenLanguage(string culture, string type, string member) { var cultureInfo = CultureInfo.GetCultureInfo(culture); Assert.Equal(type, UndoTreeNames.OfType(typeof(Animation), cultureInfo)); Assert.Equal(member, UndoTreeNames.OfMember(typeof(Animation), nameof(Animation.Length), cultureInfo)); } [Fact] public void TheLanguageOfTheTextsIsUsedByDefault() { Assert.Equal("Effects above", UndoTreeNames.OfMember(typeof(Animation), "AboveEffects")); Assert.Equal("上のエフェクト", UndoTreeNames.OfMember(typeof(Animation), "AboveEffects", CultureInfo.GetCultureInfo("ja-JP"))); } [Fact] public void ANameWithoutATranslationIsShownAsItIs() { Assert.Equal(nameof(Document), UndoTreeNames.OfType(typeof(Document))); Assert.Equal("List`1", UndoTreeNames.OfType(typeof(List))); Assert.Equal("Unlisted", UndoTreeNames.OfMember(typeof(Animation), "Unlisted")); } [Fact] public void ATypeOutsideTheHostNamespaceIsNotTranslatedEvenWithTheSameName() { var japanese = CultureInfo.GetCultureInfo("ja-JP"); Assert.Equal("アニメーション", UndoTreeNames.OfType(typeof(Animation), japanese)); Assert.Equal("Animation", UndoTreeNames.OfType(typeof(Foreign.Animation), japanese)); Assert.Equal("長さ", UndoTreeNames.OfMember(typeof(Animation), "Length", japanese)); Assert.Equal("Length", UndoTreeNames.OfMember(typeof(Foreign.Animation), "Length", japanese)); Assert.Equal("Length", UndoTreeNames.OfMember(null, "Length", japanese)); } [Fact] public void ANamespaceThatOnlyStartsWithTheHostNameIsNotTheHost() { var japanese = CultureInfo.GetCultureInfo("ja-JP"); Assert.Equal("Animation", UndoTreeNames.OfType(typeof(YukkuriMovieMakerExtras.Animation), japanese)); } [Theory] [MemberData(nameof(TextsTests.Cultures), MemberType = typeof(TextsTests))] public void AnAbbreviationIsTheSameInEveryLanguage(string culture) { var cultureInfo = CultureInfo.GetCultureInfo(culture); Assert.All(["X", "Y", "Z", "BPM", "FPS", "Hz"], member => Assert.Equal(member, UndoTreeNames.OfMember(typeof(Animation), member, cultureInfo))); } [Fact] public void EveryNameThatTheHostCanShowIsTranslated() { Assert.SkipUnless(HostAssemblyResolver.IsConfigured, "The host directory is not configured."); Assert.SkipWhen(Environment.GetEnvironmentVariable("GITHUB_ACTIONS") == "true", "The latest host release may add names before this plugin does."); var keys = Texts.ResourceManager.GetResourceSet(CultureInfo.InvariantCulture, true, false)! .Cast() .Select(entry => (string)entry.Key) .ToHashSet(StringComparer.OrdinalIgnoreCase); var missing = new SortedSet(StringComparer.Ordinal); var scanned = new List(); foreach (var file in Directory.GetFiles(HostAssemblyResolver.Directory, "YukkuriMovieMaker*.dll")) { foreach (var type in TypesOf(file).Where(type => !type.IsInterface && typeof(IUndoRedoable).IsAssignableFrom(type))) { scanned.Add(type); if (!type.IsAbstract && type.GetProperty("Label", BindingFlags.Instance | BindingFlags.Public) is null && !keys.Contains("Type_" + type.Name)) missing.Add("Type_" + type.Name); foreach (var property in type.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly)) { if (property.GetIndexParameters().Length > 0 || !property.CanWrite || property.GetCustomAttribute() is not null) continue; if (!HasDisplayName(property) && !keys.Contains("Member_" + property.Name)) missing.Add("Member_" + property.Name); } } } Assert.Contains(typeof(Animation), scanned); Assert.Empty(missing); } static IEnumerable TypesOf(string file) { Assembly assembly; try { assembly = Assembly.LoadFrom(file); } catch (Exception exception) when (exception is BadImageFormatException or FileLoadException or FileNotFoundException) { return []; } try { return assembly.GetTypes(); } catch (ReflectionTypeLoadException exception) { return exception.Types.OfType(); } } static bool HasDisplayName(PropertyInfo property) { try { return !string.IsNullOrEmpty(property.GetCustomAttribute()?.GetName()); } catch (Exception exception) when (exception is InvalidOperationException or MissingManifestResourceException or TargetInvocationException) { return false; } } }