|
| 1 | +using System.Xml.Linq; |
| 2 | + |
| 3 | +namespace PrompterOne.Web.Tests; |
| 4 | + |
| 5 | +public sealed class LocalizationBrandNameContractTests |
| 6 | +{ |
| 7 | + private const string BrandToken = "PrompterOne"; |
| 8 | + private const string DefaultResourceFileName = "SharedResource.resx"; |
| 9 | + private static readonly string RepoRoot = ResolveRepoRoot(); |
| 10 | + private static readonly string LocalizationDirectory = Path.Combine(RepoRoot, "src", "PrompterOne.Shared", "Localization"); |
| 11 | + private static readonly string[] ResourcePaths = Directory.EnumerateFiles(LocalizationDirectory, "SharedResource*.resx", SearchOption.TopDirectoryOnly) |
| 12 | + .OrderBy(path => path, StringComparer.Ordinal) |
| 13 | + .ToArray(); |
| 14 | + |
| 15 | + private static readonly string[] ForbiddenBrandVariants = |
| 16 | + [ |
| 17 | + "Prompter One", |
| 18 | + "SuflerOne", |
| 19 | + "СуфлерOne" |
| 20 | + ]; |
| 21 | + |
| 22 | + private static readonly IReadOnlyDictionary<string, string> DefaultResourceValues = LoadResourceValues( |
| 23 | + Path.Combine(LocalizationDirectory, DefaultResourceFileName)); |
| 24 | + |
| 25 | + private static readonly string[] BrandBearingKeys = DefaultResourceValues |
| 26 | + .Where(entry => entry.Value.Contains(BrandToken, StringComparison.Ordinal)) |
| 27 | + .Select(entry => entry.Key) |
| 28 | + .OrderBy(key => key, StringComparer.Ordinal) |
| 29 | + .ToArray(); |
| 30 | + |
| 31 | + public static IEnumerable<(string ResourcePath, string ForbiddenVariant)> ForbiddenVariantCases => |
| 32 | + ResourcePaths.SelectMany( |
| 33 | + resourcePath => ForbiddenBrandVariants.Select(forbiddenVariant => (resourcePath, forbiddenVariant))); |
| 34 | + |
| 35 | + public static IEnumerable<(string ResourcePath, string ResourceKey)> BrandBearingResourceCases => |
| 36 | + ResourcePaths.SelectMany( |
| 37 | + resourcePath => |
| 38 | + { |
| 39 | + var resourceValues = LoadResourceValues(resourcePath); |
| 40 | + return BrandBearingKeys |
| 41 | + .Where(resourceValues.ContainsKey) |
| 42 | + .Select(resourceKey => (resourcePath, resourceKey)); |
| 43 | + }); |
| 44 | + |
| 45 | + [Test] |
| 46 | + [MethodDataSource(nameof(ForbiddenVariantCases))] |
| 47 | + public void SharedResources_DoNotContainLocalizedBrandVariants(string resourcePath, string forbiddenVariant) |
| 48 | + { |
| 49 | + var resourceFileText = File.ReadAllText(resourcePath); |
| 50 | + |
| 51 | + Assert.DoesNotContain(forbiddenVariant, resourceFileText, StringComparison.Ordinal); |
| 52 | + } |
| 53 | + |
| 54 | + [Test] |
| 55 | + [MethodDataSource(nameof(BrandBearingResourceCases))] |
| 56 | + public void SharedResources_KeepExactBrandToken_ForBrandBearingValues(string resourcePath, string resourceKey) |
| 57 | + { |
| 58 | + var resourceValues = LoadResourceValues(resourcePath); |
| 59 | + var resourceValue = resourceValues[resourceKey]; |
| 60 | + |
| 61 | + Assert.Contains( |
| 62 | + BrandToken, |
| 63 | + resourceValue, |
| 64 | + StringComparison.Ordinal); |
| 65 | + } |
| 66 | + |
| 67 | + private static IReadOnlyDictionary<string, string> LoadResourceValues(string resourcePath) |
| 68 | + { |
| 69 | + var document = XDocument.Load(resourcePath); |
| 70 | + |
| 71 | + return document.Root? |
| 72 | + .Elements("data") |
| 73 | + .Select( |
| 74 | + element => new |
| 75 | + { |
| 76 | + Name = element.Attribute("name")?.Value, |
| 77 | + Value = element.Element("value")?.Value |
| 78 | + }) |
| 79 | + .Where(entry => !string.IsNullOrWhiteSpace(entry.Name) && entry.Value is not null) |
| 80 | + .ToDictionary(entry => entry.Name!, entry => entry.Value!, StringComparer.Ordinal) |
| 81 | + ?? new Dictionary<string, string>(StringComparer.Ordinal); |
| 82 | + } |
| 83 | + |
| 84 | + private static string ResolveRepoRoot() => |
| 85 | + Path.GetFullPath(Path.Combine(AppContext.BaseDirectory, "../../../../../")); |
| 86 | +} |
0 commit comments