ConfigFileTests.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. using System;
  2. using System.Collections.Concurrent;
  3. using Microsoft.VisualStudio.TestTools.UnitTesting;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Threading;
  7. using UnityEngine;
  8. namespace BepInEx.Configuration.Tests
  9. {
  10. [TestClass]
  11. public class ConfigFileTests
  12. {
  13. private static ConcurrentBag<ConfigFile> _toRemove;
  14. [ClassInitialize]
  15. public static void Init(TestContext context)
  16. {
  17. _toRemove = new ConcurrentBag<ConfigFile>();
  18. }
  19. [ClassCleanup]
  20. public static void Cleanup()
  21. {
  22. foreach (var configFile in _toRemove)
  23. {
  24. configFile.StopWatching();
  25. File.Delete(configFile.ConfigFilePath);
  26. }
  27. }
  28. private static ConfigFile MakeConfig()
  29. {
  30. string configPath = Path.GetTempFileName();
  31. if (configPath == null) throw new InvalidOperationException("Wtf");
  32. var config = new ConfigFile(configPath, true);
  33. _toRemove.Add(config);
  34. return config;
  35. }
  36. [TestMethod]
  37. public void SaveTest()
  38. {
  39. MakeConfig().Save();
  40. }
  41. [TestMethod]
  42. public void SaveTestValueChange()
  43. {
  44. var c = MakeConfig();
  45. var w = c.Wrap("Cat", "Key", 0, new ConfigDescription("Test"));
  46. var lines = File.ReadAllLines(c.ConfigFilePath);
  47. Assert.AreEqual(0, lines.Count(x => x.Equals("[Cat]")));
  48. Assert.AreEqual(0, lines.Count(x => x.Equals("# Test")));
  49. Assert.AreEqual(0, lines.Count(x => x.Equals("Key = 0")));
  50. c.Save();
  51. lines = File.ReadAllLines(c.ConfigFilePath);
  52. Assert.AreEqual(1, lines.Count(x => x.Equals("[Cat]")));
  53. Assert.AreEqual(1, lines.Count(x => x.Equals("# Test")));
  54. Assert.AreEqual(1, lines.Count(x => x.Equals("Key = 0")));
  55. w.Value = 69;
  56. lines = File.ReadAllLines(c.ConfigFilePath);
  57. Assert.AreEqual(1, lines.Count(x => x.Equals("[Cat]")));
  58. Assert.AreEqual(1, lines.Count(x => x.Equals("# Test")));
  59. Assert.AreEqual(1, lines.Count(x => x.Equals("Key = 69")));
  60. }
  61. [TestMethod]
  62. public void AutoSaveTest()
  63. {
  64. var c = MakeConfig();
  65. c.Wrap("Cat", "Key", 0, new ConfigDescription("Test"));
  66. var eventFired = new AutoResetEvent(false);
  67. c.ConfigReloaded += (sender, args) => eventFired.Set();
  68. c.Save();
  69. Assert.IsFalse(eventFired.WaitOne(200));
  70. }
  71. [TestMethod]
  72. public void ReadTest()
  73. {
  74. var c = MakeConfig();
  75. File.WriteAllText(c.ConfigFilePath, "[Cat]\n# Test\nKey=1\n");
  76. c.Reload();
  77. var w = c.Wrap("Cat", "Key", 0, new ConfigDescription("Test"));
  78. Assert.AreEqual(w.Value, 1);
  79. var w2 = c.Wrap("Cat", "Key2", 0, new ConfigDescription("Test"));
  80. Assert.AreEqual(w2.Value, 0);
  81. }
  82. [TestMethod]
  83. public void ReadTest2()
  84. {
  85. var c = MakeConfig();
  86. var w = c.Wrap("Cat", "Key", 0, new ConfigDescription("Test"));
  87. Assert.AreEqual(w.Value, 0);
  88. File.WriteAllText(c.ConfigFilePath, "[Cat]\n# Test\nKey = 1 \n");
  89. c.Reload();
  90. Assert.AreEqual(w.Value, 1);
  91. }
  92. [TestMethod]
  93. public void FileWatchTest()
  94. {
  95. var c = MakeConfig();
  96. var w = c.Wrap("Cat", "Key", 0, new ConfigDescription("Test"));
  97. Assert.AreEqual(w.Value, 0);
  98. var eventFired = new AutoResetEvent(false);
  99. w.SettingChanged += (sender, args) => eventFired.Set();
  100. File.WriteAllText(c.ConfigFilePath, "[Cat]\n# Test\nKey = 1 \n");
  101. Assert.IsTrue(eventFired.WaitOne(500));
  102. Assert.AreEqual(w.Value, 1);
  103. }
  104. [TestMethod]
  105. public void FileWatchTestNoSelfReload()
  106. {
  107. var c = MakeConfig();
  108. var eventFired = new AutoResetEvent(false);
  109. c.ConfigReloaded += (sender, args) => eventFired.Set();
  110. c.Save();
  111. Assert.IsFalse(eventFired.WaitOne(200));
  112. }
  113. [TestMethod]
  114. public void EventTestWrapper()
  115. {
  116. var c = MakeConfig();
  117. var w = c.Wrap("Cat", "Key", 0, new ConfigDescription("Test"));
  118. File.WriteAllText(c.ConfigFilePath, "[Cat]\n# Test\nKey=1\n");
  119. var eventFired = false;
  120. w.SettingChanged += (sender, args) => eventFired = true;
  121. c.Reload();
  122. Assert.IsTrue(eventFired);
  123. }
  124. [TestMethod]
  125. public void EventTestReload()
  126. {
  127. var c = MakeConfig();
  128. var eventFired = false;
  129. var w = c.Wrap("Cat", "Key", 0, new ConfigDescription("Test"));
  130. w.SettingChanged += (sender, args) => eventFired = true;
  131. Assert.IsFalse(eventFired);
  132. File.WriteAllText(c.ConfigFilePath, "[Cat]\n# Test\nKey=1\n");
  133. c.Reload();
  134. Assert.IsTrue(eventFired);
  135. }
  136. [TestMethod]
  137. public void ValueRangeTest()
  138. {
  139. var c = MakeConfig();
  140. var w = c.Wrap("Cat", "Key", 0, new ConfigDescription("Test", new AcceptableValueRange<int>(0, 2)));
  141. Assert.AreEqual(0, w.Value);
  142. w.Value = 2;
  143. Assert.AreEqual(2, w.Value);
  144. w.Value = -2;
  145. Assert.AreEqual(0, w.Value);
  146. w.Value = 4;
  147. Assert.AreEqual(2, w.Value);
  148. }
  149. [TestMethod]
  150. [ExpectedException(typeof(ArgumentException))]
  151. public void ValueRangeBadTypeTest()
  152. {
  153. var c = MakeConfig();
  154. c.Wrap("Cat", "Key", 0, new ConfigDescription("Test", new AcceptableValueRange<float>(1, 2)));
  155. Assert.Fail();
  156. }
  157. [TestMethod]
  158. public void ValueRangeDefaultTest()
  159. {
  160. var c = MakeConfig();
  161. var w = c.Wrap("Cat", "Key", 0, new ConfigDescription("Test", new AcceptableValueRange<int>(1, 2)));
  162. Assert.AreEqual(w.Value, 1);
  163. }
  164. [TestMethod]
  165. public void ValueRangeLoadTest()
  166. {
  167. var c = MakeConfig();
  168. c.StopWatching();
  169. File.WriteAllText(c.ConfigFilePath, "[Cat]\nKey = 1\n");
  170. c.Reload();
  171. var w = c.Wrap("Cat", "Key", 0, new ConfigDescription("Test", new AcceptableValueRange<int>(0, 2)));
  172. Assert.AreEqual(w.Value, 1);
  173. File.WriteAllText(c.ConfigFilePath, "[Cat]\nKey = 5\n");
  174. c.Reload();
  175. Assert.AreEqual(w.Value, 2);
  176. }
  177. [TestMethod]
  178. public void ValueListTest()
  179. {
  180. var c = MakeConfig();
  181. var w = c.Wrap<string>("Cat", "Key", "kek", new ConfigDescription("Test", new AcceptableValueList<string>("lel", "kek", "wew", "why")));
  182. Assert.AreEqual("kek", w.Value);
  183. w.Value = "wew";
  184. Assert.AreEqual("wew", w.Value);
  185. w.Value = "no";
  186. Assert.AreEqual("lel", w.Value);
  187. w.Value = null;
  188. Assert.AreEqual("lel", w.Value);
  189. }
  190. [TestMethod]
  191. public void KeyShortcutTest()
  192. {
  193. var shortcut = new KeyboardShortcut(KeyCode.H, KeyCode.O, KeyCode.R, KeyCode.S, KeyCode.E, KeyCode.Y);
  194. var s = shortcut.Serialize();
  195. var d = KeyboardShortcut.Deserialize(s);
  196. Assert.AreEqual(shortcut, d);
  197. var c = MakeConfig();
  198. var w = c.Wrap("Cat", "Key", new KeyboardShortcut(KeyCode.A, KeyCode.LeftShift));
  199. Assert.AreEqual(new KeyboardShortcut(KeyCode.A, KeyCode.LeftShift), w.Value);
  200. w.Value = shortcut;
  201. c.Reload();
  202. Assert.AreEqual(shortcut, w.Value);
  203. }
  204. }
  205. }