0.2.0-dev Download ZIP
158 lines ·7.1 KB Raw
| 1 | using System.Globalization; |
| 2 | using EvilEngine.Conversion; |
| 3 | using EvilEngine.Formats.Text; |
| 4 | using EvilEngine.Mounts; |
| 5 | |
| 6 | namespace EvilEngine.Host.Tests; |
| 7 | |
| 8 | public sealed class TextRegistryTests |
| 9 | { |
| 10 | private static readonly LanguageTag English = LanguageTag.English; |
| 11 | private static readonly LanguageTag German = LanguageTag.Parse("de"); |
| 12 | private static readonly LanguageTag Austrian = LanguageTag.Parse("de-AT"); |
| 13 | |
| 14 | private static TextSource Table(string @namespace, string language, string origin, params TextEntry[] entries) |
| 15 | { |
| 16 | return new TextSource(@namespace, LanguageTag.Parse(language), origin, () => new StringTable(entries)); |
| 17 | } |
| 18 | |
| 19 | private static TextRegistry Gmod(params TextSource[] more) |
| 20 | { |
| 21 | return new TextRegistry([ |
| 22 | Table("gmod", "en", "gmod/pc/1", |
| 23 | new TextEntry("npc_kleiner", "Dr. Isaac Kleiner") |
| 24 | { From = "resource/localization/en/entities.properties" }, |
| 25 | new TextEntry("spawnmenu.category.other", "Other"), |
| 26 | new TextEntry("npc_alyx", "Alyx Vance")), |
| 27 | Table("gmod", "de", "gmod/pc/1", |
| 28 | new TextEntry("spawnmenu.category.other", "Sonstige") |
| 29 | { From = "resource/localization/de/spawnmenu.properties" }, |
| 30 | new TextEntry("npc_alyx", "Alyx Vance (de)")), |
| 31 | .. more |
| 32 | ]); |
| 33 | } |
| 34 | |
| 35 | [Fact] |
| 36 | public void ResolvesATokenInItsHoldersNamespaceThroughTheFallbackChain() |
| 37 | { |
| 38 | var text = Gmod(); |
| 39 | |
| 40 | Assert.Equal("Sonstige", text.Resolve("gmod", "#spawnmenu.category.other", Austrian)); |
| 41 | Assert.Equal("Dr. Isaac Kleiner", text.Resolve("gmod", "#npc_kleiner", Austrian)); |
| 42 | Assert.Equal("#npc_nobody", text.Resolve("gmod", "#npc_nobody", Austrian)); |
| 43 | Assert.Equal("Noah", text.Resolve("gmod", "Noah", Austrian)); |
| 44 | Assert.Equal("#", text.Resolve("gmod", "#", Austrian)); |
| 45 | var kleiner = text.Find("gmod", "npc_kleiner", Austrian); |
| 46 | Assert.Equal(English, kleiner.From); |
| 47 | Assert.Equal(2, kleiner.Step); |
| 48 | Assert.Equal("resource/localization/en/entities.properties", kleiner.File); |
| 49 | Assert.Equal(["de-AT", "de", "en"], text.Chain("gmod", Austrian).Select(tag => tag.Value)); |
| 50 | } |
| 51 | |
| 52 | [Fact] |
| 53 | public void ReadsANamespaceNamedInTheTokenAndKeepsUnknownOnesAsKeys() |
| 54 | { |
| 55 | var text = Gmod(); |
| 56 | |
| 57 | Assert.Equal("Sonstige", text.Resolve("pals", "#gmod:spawnmenu.category.other", German)); |
| 58 | Assert.Equal("#nowhere:npc_kleiner", text.Resolve("gmod", "#nowhere:npc_kleiner", German)); |
| 59 | Assert.Equal("#npc_kleiner", text.Resolve("pals", "#npc_kleiner", German)); |
| 60 | } |
| 61 | |
| 62 | [Fact] |
| 63 | public void FallsBackToTheNamespacesOwnLanguageWhenItHasNoEnglish() |
| 64 | { |
| 65 | var text = new TextRegistry([ |
| 66 | Table("skate3", "ja", "skate3/ps3/1", new TextEntry("title", "スケート3")), |
| 67 | Table("skate3", "fr", "skate3/ps3/1", new TextEntry("other", "Autre")) |
| 68 | ]); |
| 69 | |
| 70 | Assert.Equal(["de", "en", "ja"], text.Chain("skate3", German).Select(tag => tag.Value)); |
| 71 | Assert.Equal("スケート3", text.Resolve("skate3", "#title", German)); |
| 72 | Assert.Equal("#other", text.Resolve("skate3", "#other", German)); |
| 73 | } |
| 74 | |
| 75 | [Fact] |
| 76 | public void MergesPluginTablesOverTheMountsKeyByKeyInLoadOrder() |
| 77 | { |
| 78 | var text = Gmod( |
| 79 | Table("gmod", "de", "Plugin first", new TextEntry("npc_alyx", "Alyx (first)"), |
| 80 | new TextEntry("npc_kleiner", "Dr. Kleiner (first)")), |
| 81 | Table("gmod", "de", "Plugin second", new TextEntry("npc_alyx", "Alyx (second)")) with |
| 82 | { |
| 83 | File = "Content/text/gmod/de.toml" |
| 84 | }); |
| 85 | |
| 86 | Assert.Equal("Alyx (second)", text.Resolve("gmod", "#npc_alyx", German)); |
| 87 | Assert.Equal("Dr. Kleiner (first)", text.Resolve("gmod", "#npc_kleiner", German)); |
| 88 | Assert.Equal("Sonstige", text.Resolve("gmod", "#spawnmenu.category.other", German)); |
| 89 | Assert.Equal("Dr. Isaac Kleiner", text.Resolve("gmod", "#npc_kleiner", English)); |
| 90 | } |
| 91 | |
| 92 | [Fact] |
| 93 | public void SaysWhereAStringCameFromAndWhatItOverrode() |
| 94 | { |
| 95 | var text = Gmod(Table("gmod", "de", "Plugin pals", new TextEntry("npc_alyx", "Alyx!") { Line = 3 }) |
| 96 | with |
| 97 | { |
| 98 | File = "Content/text/gmod/de.toml" |
| 99 | }); |
| 100 | |
| 101 | Assert.Equal( |
| 102 | [ |
| 103 | "gmod npc_alyx in de-AT", |
| 104 | " gmod/de-AT: no table", |
| 105 | " gmod/de: gives \"Alyx!\" from Plugin pals, Content/text/gmod/de.toml:3", |
| 106 | " gmod/de: overridden \"Alyx Vance (de)\" from gmod/pc/1", |
| 107 | " gmod/en: behind it \"Alyx Vance\" from gmod/pc/1" |
| 108 | ], |
| 109 | text.Why("gmod", "npc_alyx", Austrian)); |
| 110 | Assert.Equal(["gmod npc_gone in en", " gmod/en: not in it", " nothing gives it, so it shows as #npc_gone"], |
| 111 | text.Why("gmod", "npc_gone", English)); |
| 112 | } |
| 113 | |
| 114 | [Fact] |
| 115 | public void ReportsABrokenTableAndKeepsTheRest() |
| 116 | { |
| 117 | var text = Gmod(new TextSource("gmod", German, "Plugin broken", |
| 118 | () => throw new TextFormatException("Content/text/gmod/de.toml", "'npc_alyx' must be a string."))); |
| 119 | |
| 120 | Assert.Equal("Alyx Vance (de)", text.Resolve("gmod", "#npc_alyx", German)); |
| 121 | Assert.Contains(text.Problems, problem => problem.ToString().Contains("'npc_alyx' must be a string.")); |
| 122 | } |
| 123 | |
| 124 | [Fact] |
| 125 | public void ReadsAPluginsContentTextTablesThroughThePipeline() |
| 126 | { |
| 127 | var conversion = new ConversionPipeline(new MemoryConversionCache()); |
| 128 | conversion.AddNamespace(new ContentNamespace("pals", new MemorySource("pals", CaseRule.Sensitive) |
| 129 | .Add("text/gmod/pt-BR.toml", "npc_kleiner = \"Dr. Kleiner (pt-BR)\"\n") |
| 130 | .Add("text/pals/en.toml", "category = \"Friends\"\ngreeting = \"Hello, {1}!\"\n") |
| 131 | .Add("text/pals/notes.txt", "not a table") |
| 132 | .Add("text/broken/de.toml", "a = 1\n"))); |
| 133 | conversion.Bind("pals", new TextTableConverter()); |
| 134 | |
| 135 | var text = TextRegistry.Build([], conversion, ["pals", "missing"]); |
| 136 | |
| 137 | Assert.Equal("Dr. Kleiner (pt-BR)", text.Resolve("gmod", "#npc_kleiner", LanguageTag.Parse("pt-br"))); |
| 138 | Assert.Equal("Friends", text.Resolve("pals", "#category", German)); |
| 139 | var found = text.Find("pals", "greeting", English); |
| 140 | Assert.Equal("Content/text/pals/en.toml", found.File); |
| 141 | Assert.Equal(2, found.Entry!.Line); |
| 142 | Assert.Equal("#a", text.Resolve("broken", "#a", German)); |
| 143 | Assert.Contains(text.Problems, problem => problem.ToString().Contains("'a' must be a string.")); |
| 144 | } |
| 145 | |
| 146 | [Fact] |
| 147 | public void ChoosesTheSettingThenSteamsThenTheSystemsThenEnglish() |
| 148 | { |
| 149 | Assert.Equal(English, PlayerLanguage.Choose()); |
| 150 | Assert.Equal(German, PlayerLanguage.Choose(system: German)); |
| 151 | Assert.Equal(LanguageTag.Parse("pt-BR"), |
| 152 | PlayerLanguage.Choose(steam: PlayerLanguage.Steam("brazilian"), system: German)); |
| 153 | Assert.Equal(Austrian, PlayerLanguage.Choose(Austrian, LanguageTag.Parse("pt-BR"), German)); |
| 154 | Assert.Equal(LanguageTag.Parse("en-GB"), PlayerLanguage.System(CultureInfo.GetCultureInfo("en-GB"))); |
| 155 | Assert.Null(PlayerLanguage.System(CultureInfo.InvariantCulture)); |
| 156 | Assert.Null(PlayerLanguage.Steam("klingon")); |
| 157 | } |
| 158 | } |