diff --git a/Shared/Extensions/EnumerableExtensions.cs b/Shared/Extensions/EnumerableExtensions.cs index 53e84a4..ba0a2ba 100644 --- a/Shared/Extensions/EnumerableExtensions.cs +++ b/Shared/Extensions/EnumerableExtensions.cs @@ -1,6 +1,4 @@ -using System.Collections.Concurrent; - -namespace MeloongCore.Extensions; +namespace MeloongCore.Extensions; public static class EnumerableExtensions { #region 去重 diff --git a/Shared/General/Config.cs b/Shared/General/Config.cs index ed055bd..adef049 100644 --- a/Shared/General/Config.cs +++ b/Shared/General/Config.cs @@ -1,4 +1,3 @@ -using System.Collections.Concurrent; using System.Security.Cryptography; namespace MeloongCore; diff --git a/Shared/General/Logger.cs b/Shared/General/Logger.cs index 2303b76..ce7370f 100644 --- a/Shared/General/Logger.cs +++ b/Shared/General/Logger.cs @@ -1,5 +1,4 @@ -using System.Collections.Concurrent; -using System.Diagnostics; +using System.Diagnostics; namespace MeloongCore; diff --git a/Shared/MeloongCore.csproj b/Shared/MeloongCore.csproj index ae55104..06f9eb2 100644 --- a/Shared/MeloongCore.csproj +++ b/Shared/MeloongCore.csproj @@ -25,6 +25,7 @@ + diff --git a/Shared/Utils/EnumUtils.cs b/Shared/Utils/EnumUtils.cs index 72169fb..4fa5cf4 100644 --- a/Shared/Utils/EnumUtils.cs +++ b/Shared/Utils/EnumUtils.cs @@ -1,5 +1,4 @@ -using System.Collections.Concurrent; -using System.Globalization; +using System.Globalization; namespace MeloongCore; public static class EnumUtils { diff --git a/Tests/General/ConfigTest.cs b/Tests/General/ConfigTest.cs new file mode 100644 index 0000000..d426437 --- /dev/null +++ b/Tests/General/ConfigTest.cs @@ -0,0 +1,77 @@ +namespace MeloongCore.Tests; +public class ConfigTest : TestWithFolder { + + #region JsonConfigProvider + + [Test] + public async Task JsonConfigProvider_字符串列表() { + string configFile = Path.Combine(tempFolder, "config.json"); + var entry = new ConfigEntry>("list", [], new JsonConfigProvider(configFile)); + + entry.Set(["Alpha", "中文", "123"]); + + var reloadedEntry = new ConfigEntry>("list", [], new JsonConfigProvider(configFile)); + var reloadedValue = reloadedEntry.Get(); + await Assert.That(reloadedValue is not null).IsTrue(); + await Assert.That(string.Join("|", reloadedValue!)).IsEqualTo("Alpha|中文|123"); + await Assert.That(FileUtils.ReadAsString(configFile)).Contains("\"list\": ["); + } + + [Test] + public async Task JsonConfigProvider_自定义类列表() { + string configFile = Path.Combine(tempFolder, "config.json"); + var entry = new ConfigEntry>("items", [], new JsonConfigProvider(configFile)); + + entry.Set([ + new() { Name = "Fabric", Count = 2, Enabled = true }, + new() { Name = "Forge 中文", Count = 5, Enabled = false } + ]); + + var reloadedEntry = new ConfigEntry>("items", [], new JsonConfigProvider(configFile)); + var reloadedValue = reloadedEntry.Get(); + await Assert.That(reloadedValue is not null).IsTrue(); + await Assert.That(string.Join("|", reloadedValue!.Select(i => $"{i.Name}:{i.Count}:{i.Enabled}"))).IsEqualTo("Fabric:2:True|Forge 中文:5:False"); + + string json = FileUtils.ReadAsString(configFile); + await Assert.That(json).Contains("\"items\": ["); + await Assert.That(json).Contains("\"Name\": \"Fabric\""); + await Assert.That(json).Contains("\"Count\": 2"); + await Assert.That(json).Contains("\"Enabled\": true"); + await Assert.That(json).Contains("\"Name\": \"Forge 中文\""); + await Assert.That(json).Contains("\"Enabled\": false"); + } + + [Test] + public async Task ConfigEntry_SetNull_RoundTripsThroughJson() { + string configFile = Path.Combine(tempFolder, "config.json"); + var entry = new ConfigEntry("nullable", "fallback", new JsonConfigProvider(configFile)); + + entry.Set(null); + + var reloadedEntry = new ConfigEntry("nullable", "fallback", new JsonConfigProvider(configFile)); + await Assert.That(reloadedEntry.HasValue()).IsTrue(); + await Assert.That(reloadedEntry.Get() is null).IsTrue(); + await Assert.That(FileUtils.ReadAsString(configFile)).Contains("\"nullable\": null"); + } + + [Test] + public async Task ConfigEntry_SetString_RoundTripsThroughJson() { + string configFile = Path.Combine(tempFolder, "config.json"); + var entry = new ConfigEntry("text", "fallback", new JsonConfigProvider(configFile)); + + entry.Set("plain text 中文"); + + var reloadedEntry = new ConfigEntry("text", "fallback", new JsonConfigProvider(configFile)); + await Assert.That(reloadedEntry.Get()).IsEqualTo("plain text 中文"); + await Assert.That(FileUtils.ReadAsString(configFile)).Contains("\"text\": \"plain text 中文\""); + } + + public class TestConfigItem { + public string Name { get; set; } = ""; + public int Count { get; set; } + public bool Enabled { get; set; } + } + + #endregion + +} diff --git a/Tests/MeloongCore.Tests.csproj b/Tests/MeloongCore.Tests.csproj index 0fb81f8..60c3fc8 100644 --- a/Tests/MeloongCore.Tests.csproj +++ b/Tests/MeloongCore.Tests.csproj @@ -59,6 +59,7 @@ + diff --git a/Wpf/MeloongCore.Wpf.csproj b/Wpf/MeloongCore.Wpf.csproj index 981e6fb..abbd303 100644 --- a/Wpf/MeloongCore.Wpf.csproj +++ b/Wpf/MeloongCore.Wpf.csproj @@ -26,6 +26,7 @@ +