123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236 |
- using System;
- using System.Collections.Concurrent;
- using Microsoft.VisualStudio.TestTools.UnitTesting;
- using System.IO;
- using System.Linq;
- using System.Threading;
- using UnityEngine;
- namespace BepInEx.Configuration.Tests
- {
- [TestClass]
- public class ConfigFileTests
- {
- private static ConcurrentBag<ConfigFile> _toRemove;
- [ClassInitialize]
- public static void Init(TestContext context)
- {
- _toRemove = new ConcurrentBag<ConfigFile>();
- }
- [ClassCleanup]
- public static void Cleanup()
- {
- foreach (var configFile in _toRemove)
- File.Delete(configFile.ConfigFilePath);
- }
- private static ConfigFile MakeConfig()
- {
- string 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.Wrap("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();
- c.Wrap("Cat", "Key", 0, new ConfigDescription("Test"));
- var eventFired = new AutoResetEvent(false);
- c.ConfigReloaded += (sender, args) => eventFired.Set();
- c.Save();
- 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.Wrap("Cat", "Key", 0, new ConfigDescription("Test"));
- Assert.AreEqual(w.Value, 1);
- var w2 = c.Wrap("Cat", "Key2", 0, new ConfigDescription("Test"));
- Assert.AreEqual(w2.Value, 0);
- }
- [TestMethod]
- public void ReadTest2()
- {
- var c = MakeConfig();
- var w = c.Wrap("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 eventFired = new AutoResetEvent(false);
- c.ConfigReloaded += (sender, args) => eventFired.Set();
- c.Save();
- Assert.IsFalse(eventFired.WaitOne(200));
- }
- [TestMethod]
- public void EventTestWrapper()
- {
- var c = MakeConfig();
- var w = c.Wrap("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 EventTestReload()
- {
- var c = MakeConfig();
- var eventFired = false;
- var w = c.Wrap("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 w = c.Wrap("Cat", "Key", 0, new ConfigDescription("Test", new AcceptableValueRange<int>(0, 2)));
- Assert.AreEqual(0, w.Value);
- w.Value = 2;
- Assert.AreEqual(2, w.Value);
- w.Value = -2;
- Assert.AreEqual(0, w.Value);
- w.Value = 4;
- Assert.AreEqual(2, w.Value);
- }
- [TestMethod]
- [ExpectedException(typeof(ArgumentException))]
- public void ValueRangeBadTypeTest()
- {
- var c = MakeConfig();
- c.Wrap("Cat", "Key", 0, new ConfigDescription("Test", new AcceptableValueRange<float>(1, 2)));
- Assert.Fail();
- }
- [TestMethod]
- public void ValueRangeDefaultTest()
- {
- var c = MakeConfig();
- var w = c.Wrap("Cat", "Key", 0, new ConfigDescription("Test", new AcceptableValueRange<int>(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.Wrap("Cat", "Key", 0, new ConfigDescription("Test", new AcceptableValueRange<int>(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 w = c.Wrap<string>("Cat", "Key", "kek", new ConfigDescription("Test", new AcceptableValueList<string>("lel", "kek", "wew", "why")));
- Assert.AreEqual("kek", w.Value);
- w.Value = "wew";
- Assert.AreEqual("wew", w.Value);
- w.Value = "no";
- Assert.AreEqual("lel", w.Value);
- w.Value = null;
- Assert.AreEqual("lel", w.Value);
- }
- [TestMethod]
- public void KeyShortcutTest()
- {
- var shortcut = new KeyboardShortcut(KeyCode.H, KeyCode.O, KeyCode.R, KeyCode.S, KeyCode.E, KeyCode.Y);
- var s = shortcut.Serialize();
- var d = KeyboardShortcut.Deserialize(s);
- Assert.AreEqual(shortcut, d);
- var c = MakeConfig();
- var w = c.Wrap("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);
- }
- }
- }
|