龙腾猫跃 пре 4 месеци
родитељ
комит
ada0032079

+ 1 - 3
Shared/Extensions/EnumerableExtensions.cs

@@ -1,6 +1,4 @@
-using System.Collections.Concurrent;
-
-namespace MeloongCore.Extensions;
+namespace MeloongCore.Extensions;
 public static class EnumerableExtensions {
 
     #region 去重

+ 0 - 1
Shared/General/Config.cs

@@ -1,4 +1,3 @@
-using System.Collections.Concurrent;
 using System.Security.Cryptography;
 
 namespace MeloongCore;

+ 1 - 2
Shared/General/Logger.cs

@@ -1,5 +1,4 @@
-using System.Collections.Concurrent;
-using System.Diagnostics;
+using System.Diagnostics;
 
 namespace MeloongCore;
 

+ 1 - 0
Shared/MeloongCore.csproj

@@ -25,6 +25,7 @@
 		<Using Include="System.Diagnostics.CodeAnalysis" />
 		<Using Include="System.Collections" />
 		<Using Include="System.Collections.Generic" />
+		<Using Include="System.Collections.Concurrent" />
 		<Using Include="System.Text" />
 		<Using Include="System.Net" />
 		<Using Include="System.Runtime.CompilerServices" />

+ 1 - 2
Shared/Utils/EnumUtils.cs

@@ -1,5 +1,4 @@
-using System.Collections.Concurrent;
-using System.Globalization;
+using System.Globalization;
 
 namespace MeloongCore;
 public static class EnumUtils {

+ 77 - 0
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<string>>("list", [], new JsonConfigProvider(configFile));
+
+        entry.Set(["Alpha", "中文", "123"]);
+
+        var reloadedEntry = new ConfigEntry<List<string>>("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<List<TestConfigItem>>("items", [], new JsonConfigProvider(configFile));
+
+        entry.Set([
+            new() { Name = "Fabric", Count = 2, Enabled = true },
+            new() { Name = "Forge 中文", Count = 5, Enabled = false }
+        ]);
+
+        var reloadedEntry = new ConfigEntry<List<TestConfigItem>>("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<string>("nullable", "fallback", new JsonConfigProvider(configFile));
+
+        entry.Set(null);
+
+        var reloadedEntry = new ConfigEntry<string>("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<string>("text", "fallback", new JsonConfigProvider(configFile));
+
+        entry.Set("plain text 中文");
+
+        var reloadedEntry = new ConfigEntry<string>("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
+
+}

+ 1 - 0
Tests/MeloongCore.Tests.csproj

@@ -59,6 +59,7 @@
 		<Using Include="System.Diagnostics.CodeAnalysis" />
 		<Using Include="System.Collections" />
 		<Using Include="System.Collections.Generic" />
+		<Using Include="System.Collections.Concurrent" />
 		<Using Include="System.Text" />
 		<Using Include="System.Net" />
 		<Using Include="System.Runtime.CompilerServices" />

+ 1 - 0
Wpf/MeloongCore.Wpf.csproj

@@ -26,6 +26,7 @@
 		<Using Include="System.Diagnostics.CodeAnalysis" />
 		<Using Include="System.Collections" />
 		<Using Include="System.Collections.Generic" />
+		<Using Include="System.Collections.Concurrent" />
 		<Using Include="System.Text" />
 		<Using Include="System.Net" />
 		<Using Include="System.Runtime.CompilerServices" />