From 1449bcc353817894a813901d6d32b782169061d5 Mon Sep 17 00:00:00 2001 From: ghorsington Date: Sun, 18 Jul 2021 13:19:25 +0300 Subject: [PATCH] Port tests to new API --- .../Provider/LegacyConfigurationProvider.cs | 18 +- BepInExTests/Configuration/ConfigFileTests.cs | 203 +----------------- .../Configuration/LegacyConfigTests.cs | 154 +++++++++++++ .../Configuration/LegacyTestConfigProvider.cs | 30 +++ 4 files changed, 204 insertions(+), 201 deletions(-) create mode 100644 BepInExTests/Configuration/LegacyConfigTests.cs create mode 100644 BepInExTests/Configuration/LegacyTestConfigProvider.cs diff --git a/BepInEx.Core/Configuration/Provider/LegacyConfigurationProvider.cs b/BepInEx.Core/Configuration/Provider/LegacyConfigurationProvider.cs index 12e2e91..c32025c 100644 --- a/BepInEx.Core/Configuration/Provider/LegacyConfigurationProvider.cs +++ b/BepInEx.Core/Configuration/Provider/LegacyConfigurationProvider.cs @@ -28,18 +28,24 @@ namespace BepInEx.Configuration public LegacyConfigurationProvider(string filePath) { - if (!File.Exists(filePath)) - throw new FileNotFoundException(filePath); this.filePath = filePath; } - public void Load() + public virtual IEnumerable ReadAllLines() { + if (!File.Exists(filePath)) + throw new FileNotFoundException(filePath); + return File.ReadAllLines(filePath); + } + + public virtual void Load() + { + items.Clear(); var currentSection = new string[0]; - - foreach (var rawLine in File.ReadAllLines(filePath)) + + foreach (var rawLine in ReadAllLines()) { var line = rawLine.Trim(); @@ -64,7 +70,7 @@ namespace BepInEx.Configuration } // TODO: Write to TOML instead - public void Save() => throw new System.NotImplementedException(); + public virtual void Save() => throw new System.NotImplementedException(); public ConfigurationNode Get(string[] path) { diff --git a/BepInExTests/Configuration/ConfigFileTests.cs b/BepInExTests/Configuration/ConfigFileTests.cs index e74f13d..45e6484 100644 --- a/BepInExTests/Configuration/ConfigFileTests.cs +++ b/BepInExTests/Configuration/ConfigFileTests.cs @@ -1,8 +1,5 @@ extern alias UEC; using System; -using System.Collections.Concurrent; -using System.IO; -using System.Linq; using System.Threading; using Microsoft.VisualStudio.TestTools.UnitTesting; using UEC::UnityEngine; @@ -12,58 +9,10 @@ namespace BepInEx.Configuration.Tests [TestClass] public class ConfigFileTests { - private static ConcurrentBag _toRemove; - - [ClassInitialize] - public static void Init(TestContext context) => _toRemove = new ConcurrentBag(); - - [ClassCleanup] - public static void Cleanup() - { - foreach (var configFile in _toRemove) - File.Delete(configFile.ConfigFilePath); - } - - private static ConfigFile MakeConfig() - { - var configPath = Path.GetTempFileName(); - if (configPath == null) throw new InvalidOperationException("Wtf"); - var config = new ConfigFile(configPath, true); - _toRemove.Add(config); - return config; - } - - [TestMethod] - public void SaveTest() => MakeConfig().Save(); - - [TestMethod] - public void SaveTestValueChange() - { - var c = MakeConfig(); - - var w = c.Bind("Cat", "Key", 0, new ConfigDescription("Test")); - var lines = File.ReadAllLines(c.ConfigFilePath); - Assert.AreEqual(1, lines.Count(x => x.Equals("[Cat]"))); - Assert.AreEqual(1, lines.Count(x => x.Equals("## Test"))); - Assert.AreEqual(1, lines.Count(x => x.Equals("Key = 0"))); - - c.Save(); - lines = File.ReadAllLines(c.ConfigFilePath); - Assert.AreEqual(1, lines.Count(x => x.Equals("[Cat]"))); - Assert.AreEqual(1, lines.Count(x => x.Equals("## Test"))); - Assert.AreEqual(1, lines.Count(x => x.Equals("Key = 0"))); - - w.Value = 69; - lines = File.ReadAllLines(c.ConfigFilePath); - Assert.AreEqual(1, lines.Count(x => x.Equals("[Cat]"))); - Assert.AreEqual(1, lines.Count(x => x.Equals("## Test"))); - Assert.AreEqual(1, lines.Count(x => x.Equals("Key = 69"))); - } - [TestMethod] public void AutoSaveTest() { - var c = MakeConfig(); + var (c, _) = LegacyTestConfigProvider.MakeConfig(); c.Bind("Cat", "Key", 0, new ConfigDescription("Test")); var eventFired = new AutoResetEvent(false); @@ -74,35 +23,10 @@ namespace BepInEx.Configuration.Tests Assert.IsFalse(eventFired.WaitOne(200)); } - [TestMethod] - public void ReadTest() - { - var c = MakeConfig(); - File.WriteAllText(c.ConfigFilePath, "[Cat]\n# Test\nKey=1\n"); - c.Reload(); - var w = c.Bind("Cat", "Key", 0, "Test"); - Assert.AreEqual(w.Value, 1); - var w2 = c.Bind("Cat", "Key2", 0, new ConfigDescription("Test")); - Assert.AreEqual(w2.Value, 0); - } - - [TestMethod] - public void ReadTest2() - { - var c = MakeConfig(); - var w = c.Bind("Cat", "Key", 0, new ConfigDescription("Test")); - Assert.AreEqual(w.Value, 0); - - File.WriteAllText(c.ConfigFilePath, "[Cat]\n# Test\nKey = 1 \n"); - - c.Reload(); - Assert.AreEqual(w.Value, 1); - } - [TestMethod] public void FileWatchTestNoSelfReload() { - var c = MakeConfig(); + var (c, _) = LegacyTestConfigProvider.MakeConfig(); var eventFired = new AutoResetEvent(false); c.ConfigReloaded += (sender, args) => eventFired.Set(); @@ -112,59 +36,10 @@ namespace BepInEx.Configuration.Tests Assert.IsFalse(eventFired.WaitOne(200)); } - [TestMethod] - public void EventTestWrapper() - { - var c = MakeConfig(); - var w = c.Bind("Cat", "Key", 0, new ConfigDescription("Test")); - - File.WriteAllText(c.ConfigFilePath, "[Cat]\n# Test\nKey=1\n"); - - var eventFired = false; - w.SettingChanged += (sender, args) => eventFired = true; - - c.Reload(); - - Assert.IsTrue(eventFired); - } - - [TestMethod] - public void PersistHomeless() - { - var c = MakeConfig(); - - File.WriteAllText(c.ConfigFilePath, "[Cat]\n# Test\nKey=1\nHomeless=0"); - c.Reload(); - - var w = c.Bind("Cat", "Key", 0, new ConfigDescription("Test")); - - c.Save(); - - Assert.IsTrue(File.ReadAllLines(c.ConfigFilePath) - .Single(x => x.StartsWith("Homeless") && x.EndsWith("0")) != null); - } - - [TestMethod] - public void EventTestReload() - { - var c = MakeConfig(); - var eventFired = false; - - var w = c.Bind("Cat", "Key", 0, new ConfigDescription("Test")); - w.SettingChanged += (sender, args) => eventFired = true; - - Assert.IsFalse(eventFired); - - File.WriteAllText(c.ConfigFilePath, "[Cat]\n# Test\nKey=1\n"); - c.Reload(); - - Assert.IsTrue(eventFired); - } - [TestMethod] public void ValueRangeTest() { - var c = MakeConfig(); + var (c, _) = LegacyTestConfigProvider.MakeConfig(); var w = c.Bind("Cat", "Key", 0, new ConfigDescription("Test", new AcceptableValueRange(0, 2))); Assert.AreEqual(0, w.Value); @@ -180,7 +55,7 @@ namespace BepInEx.Configuration.Tests [ExpectedException(typeof(ArgumentException))] public void ValueRangeBadTypeTest() { - var c = MakeConfig(); + var (c, _) = LegacyTestConfigProvider.MakeConfig(); c.Bind("Cat", "Key", 0, new ConfigDescription("Test", new AcceptableValueRange(1, 2))); Assert.Fail(); } @@ -188,34 +63,16 @@ namespace BepInEx.Configuration.Tests [TestMethod] public void ValueRangeDefaultTest() { - var c = MakeConfig(); + var (c, _) = LegacyTestConfigProvider.MakeConfig(); var w = c.Bind("Cat", "Key", 0, new ConfigDescription("Test", new AcceptableValueRange(1, 2))); Assert.AreEqual(w.Value, 1); } - [TestMethod] - public void ValueRangeLoadTest() - { - var c = MakeConfig(); - - File.WriteAllText(c.ConfigFilePath, "[Cat]\nKey = 1\n"); - c.Reload(); - - var w = c.Bind("Cat", "Key", 0, new ConfigDescription("Test", new AcceptableValueRange(0, 2))); - - Assert.AreEqual(w.Value, 1); - - File.WriteAllText(c.ConfigFilePath, "[Cat]\nKey = 5\n"); - c.Reload(); - - Assert.AreEqual(w.Value, 2); - } - [TestMethod] public void ValueListTest() { - var c = MakeConfig(); + var (c, _) = LegacyTestConfigProvider.MakeConfig(); var w = c.Bind("Cat", "Key", "kek", new ConfigDescription("Test", new AcceptableValueList("lel", "kek", "wew", "why"))); @@ -236,7 +93,7 @@ namespace BepInEx.Configuration.Tests var d = KeyboardShortcut.Deserialize(s); Assert.AreEqual(shortcut, d); - var c = MakeConfig(); + 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); @@ -250,7 +107,7 @@ namespace BepInEx.Configuration.Tests { Assert.AreEqual(KeyboardShortcut.Empty, new KeyboardShortcut()); - var c = MakeConfig(); + var (c, _) = LegacyTestConfigProvider.MakeConfig(); var w = c.Bind("Cat", "Key", KeyboardShortcut.Empty, new ConfigDescription("Test")); @@ -270,49 +127,5 @@ namespace BepInEx.Configuration.Tests Assert.AreEqual(testShortcut, w.Value); } - - [TestMethod] - public void StringEscapeChars() - { - const string testVal = "new line\n test \t\0"; - - var c = MakeConfig(); - var w = c.Bind("Cat", "Key", testVal, new ConfigDescription("Test")); - - 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.AreEqual(testVal, w.Value); - } - - [TestMethod] - public void UnescapedPathString() - { - var c = MakeConfig(); - var w = c.Bind("Cat", "Key", "", new ConfigDescription("Test")); - - var unescaped = @"D:\test\p ath"; - foreach (var testVal in new[] { unescaped, @"D:\\test\\p ath" }) - { - File.WriteAllText(c.ConfigFilePath, $"[Cat]\n# Test\nKey={testVal}\n"); - c.Reload(); - - Assert.AreEqual(unescaped, w.Value); - - w.SetSerializedValue(w.GetSerializedValue()); - Assert.AreEqual(unescaped, w.Value); - - c.Save(); - c.Reload(); - - Assert.AreEqual(unescaped, w.Value); - } - } } } diff --git a/BepInExTests/Configuration/LegacyConfigTests.cs b/BepInExTests/Configuration/LegacyConfigTests.cs new file mode 100644 index 0000000..6ff85d5 --- /dev/null +++ b/BepInExTests/Configuration/LegacyConfigTests.cs @@ -0,0 +1,154 @@ +using System.Linq; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace BepInEx.Configuration.Tests +{ + [TestClass] + public class LegacyConfigTests + { + [TestMethod] + public void ReadTest() + { + var (c, _) = LegacyTestConfigProvider.MakeConfig(@" +[Cat] +# Test +Key=1 +"); + c.Reload(); + var w = c.Bind("Cat", "Key", 0, "Test"); + Assert.AreEqual(w.Value, 1); + var w2 = c.Bind("Cat", "Key2", 0, new ConfigDescription("Test")); + Assert.AreEqual(w2.Value, 0); + } + + [TestMethod] + public void ReadTest2() + { + var (c, provider) = LegacyTestConfigProvider.MakeConfig(); + var w = c.Bind("Cat", "Key", 0, new ConfigDescription("Test")); + Assert.AreEqual(w.Value, 0); + + provider.FileContents = @" +[Cat] +# Test +Key=1 +"; + + c.Reload(); + Assert.AreEqual(w.Value, 1); + } + + [TestMethod] + public void EventTestWrapper() + { + var (c, p) = LegacyTestConfigProvider.MakeConfig(); + var w = c.Bind("Cat", "Key", 0, new ConfigDescription("Test")); + + p.FileContents = @" +[Cat] +# Test +Key=1 +"; + + var eventFired = false; + w.SettingChanged += (sender, args) => eventFired = true; + + c.Reload(); + + Assert.IsTrue(eventFired); + } + + [TestMethod] + public void EventTestReload() + { + var (c, p) = LegacyTestConfigProvider.MakeConfig(); + var eventFired = false; + + var w = c.Bind("Cat", "Key", 0, new ConfigDescription("Test")); + w.SettingChanged += (sender, args) => eventFired = true; + + Assert.IsFalse(eventFired); + + p.FileContents = @" +[Cat] +# Test +Key=1 +"; + c.Reload(); + + Assert.IsTrue(eventFired); + } + + [TestMethod] + public void StringEscapeChars() + { + const string testVal = "new line\n test \t\0"; + + var (c, _) = LegacyTestConfigProvider.MakeConfig(); + var w = c.Bind("Cat", "Key", testVal, new ConfigDescription("Test")); + + 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.AreEqual(testVal, w.Value); + } + + [TestMethod] + public void UnescapedPathString() + { + var (c, p) = LegacyTestConfigProvider.MakeConfig(); + var w = c.Bind("Cat", "Key", "", new ConfigDescription("Test")); + + var unescaped = @"D:\test\p ath"; + foreach (var testVal in new[] { unescaped, @"D:\\test\\p ath" }) + { + p.FileContents = $@" +[Cat] +# Test +Key={testVal} +"; + c.Reload(); + + Assert.AreEqual(unescaped, w.Value); + + w.SetSerializedValue(w.GetSerializedValue()); + Assert.AreEqual(unescaped, w.Value); + + c.Save(); + c.Reload(); + + Assert.AreEqual(unescaped, w.Value); + } + } + + [TestMethod] + public void ValueRangeLoadTest() + { + var (c, p) = LegacyTestConfigProvider.MakeConfig(); + + p.FileContents = @" +[Cat] +Key = 1 +"; + c.Reload(); + + var w = c.Bind("Cat", "Key", 0, new ConfigDescription("Test", new AcceptableValueRange(0, 2))); + + Assert.AreEqual(w.Value, 1); + + p.FileContents = @" +[Cat] +Key = 5 +"; + c.Reload(); + + Assert.AreEqual(w.Value, 2); + } + } +} diff --git a/BepInExTests/Configuration/LegacyTestConfigProvider.cs b/BepInExTests/Configuration/LegacyTestConfigProvider.cs new file mode 100644 index 0000000..8a6f9a1 --- /dev/null +++ b/BepInExTests/Configuration/LegacyTestConfigProvider.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; + +namespace BepInEx.Configuration.Tests +{ + public class LegacyTestConfigProvider : LegacyConfigurationProvider + { + public string FileContents { get; set; } + + public LegacyTestConfigProvider() : base("none") + { + } + + public override IEnumerable ReadAllLines() + { + return FileContents.Split(Environment.NewLine); + } + + public override void Save() + { + // Do nothing for now, there is nothing to save + } + + public static (ConfigFile Config, LegacyTestConfigProvider Provider) MakeConfig(string contents = "") + { + var provider = new LegacyTestConfigProvider{ FileContents = contents }; + return (new ConfigFile(provider, false, null), provider); + } + } +}