Fix current tests

This commit is contained in:
ghorsington committed 2021-07-18 18:01:22 +03:00
1 parent df89a51549
commit 38e08bcd6e
4 files changed
+30 -25

No files matched your search

@@ -1,6 +1,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
namespace BepInEx.Configuration
{
@@ -8,7 +9,7 @@ namespace BepInEx.Configuration
{
private string filePath;
private Dictionary<string[], string> items = new(new ArrayComparer());
protected Dictionary<string[], string> items = new(new ArrayComparer());
private class ArrayComparer : IEqualityComparer<string[]>
{
@@ -78,11 +79,16 @@ namespace BepInEx.Configuration
return new ConfigurationNode
{
ValueType = typeof(string),
Value = value
Value = UnescapeIfNeeded(value)
};
}
public void Set(string[] path, ConfigurationNode node) => items[path] = node.Value;
private static string UnescapeIfNeeded(string str)
{
return Regex.IsMatch(str, @"^""?\w:\\(?!\\)(?!.+\\\\)") ? str : str.Unescape();
}
public void Set(string[] path, ConfigurationNode node) => items[path] = node.Value.Escape();
public ConfigurationNode Delete(string[] path)
{
+7 -13
View File
@@ -96,10 +96,6 @@ namespace BepInEx.Configuration.Tests
var (c, _) = LegacyTestConfigProvider.MakeConfig();
var w = c.Bind("Cat", "Key", new KeyboardShortcut(KeyCode.A, KeyCode.LeftShift));
Assert.AreEqual(new KeyboardShortcut(KeyCode.A, KeyCode.LeftShift), w.Value);
w.Value = shortcut;
c.Reload();
Assert.AreEqual(shortcut, w.Value);
}
[TestMethod]
@@ -107,24 +103,22 @@ namespace BepInEx.Configuration.Tests
{
Assert.AreEqual(KeyboardShortcut.Empty, new KeyboardShortcut());
var (c, _) = LegacyTestConfigProvider.MakeConfig();
var (c, p) = LegacyTestConfigProvider.MakeConfig();
var w = c.Bind("Cat", "Key", KeyboardShortcut.Empty, new ConfigDescription("Test"));
Assert.AreEqual("", w.GetSerializedValue());
var key = new[] { "Cat", "Key" };
Assert.AreEqual("", p.Get(key).Value);
w.SetSerializedValue(w.GetSerializedValue());
p.Set(key, p.Get(key));
w.SyncFromConfig();
Assert.AreEqual(KeyboardShortcut.Empty, w.Value);
var testShortcut = new KeyboardShortcut(KeyCode.A, KeyCode.B, KeyCode.C);
w.Value = testShortcut;
w.SetSerializedValue(w.GetSerializedValue());
Assert.AreEqual(testShortcut, w.Value);
c.Save();
c.Reload();
p.Set(key, p.Get(key));
w.SyncFromConfig();
Assert.AreEqual(testShortcut, w.Value);
}
}
@@ -84,18 +84,15 @@ Key=1
{
const string testVal = "new line\n test \t\0";
var (c, _) = LegacyTestConfigProvider.MakeConfig();
var (c, p) = LegacyTestConfigProvider.MakeConfig();
var w = c.Bind("Cat", "Key", testVal, new ConfigDescription("Test"));
var key = new[] { "Cat", "Key" };
Assert.AreEqual(testVal, w.Value);
Assert.IsFalse(w.GetSerializedValue().Any(x => x == '\n'));
w.SetSerializedValue(w.GetSerializedValue());
Assert.AreEqual(testVal, w.Value);
c.Save();
c.Reload();
Assert.IsFalse(p.GetRawValue(key).Any(x => x == '\n'));
p.Set(key, p.Get(key));
w.SyncFromConfig();
Assert.AreEqual(testVal, w.Value);
}
@@ -117,7 +114,8 @@ Key={testVal}
Assert.AreEqual(unescaped, w.Value);
w.SetSerializedValue(w.GetSerializedValue());
p.Set(w.Definition.ConfigPath, p.Get(w.Definition.ConfigPath));
w.SyncFromConfig();
Assert.AreEqual(unescaped, w.Value);
c.Save();
@@ -26,6 +26,13 @@ namespace BepInEx.Configuration.Tests
{
// Do nothing for now, there is nothing to save
}
public string GetRawValue(string[] path)
{
if (!items.TryGetValue(path, out var value))
return null;
return value;
}
public static (ConfigFile Config, LegacyTestConfigProvider Provider) MakeConfig(string contents = "")
{