ConfigFileTests.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. File.Delete(configFile.ConfigFilePath);
  24. }
  25. private static ConfigFile MakeConfig()
  26. {
  27. string configPath = Path.GetTempFileName();
  28. if (configPath == null) throw new InvalidOperationException("Wtf");
  29. var config = new ConfigFile(configPath, true);
  30. _toRemove.Add(config);
  31. return config;
  32. }
  33. [TestMethod]
  34. public void SaveTest()
  35. {
  36. MakeConfig().Save();
  37. }
  38. [TestMethod]
  39. public void SaveTestValueChange()
  40. {
  41. var c = MakeConfig();
  42. var w = c.AddSetting("Cat", "Key", 0, new ConfigDescription("Test"));
  43. var lines = File.ReadAllLines(c.ConfigFilePath);
  44. Assert.AreEqual(1, lines.Count(x => x.Equals("[Cat]")));
  45. Assert.AreEqual(1, lines.Count(x => x.Equals("## Test")));
  46. Assert.AreEqual(1, lines.Count(x => x.Equals("Key = 0")));
  47. c.Save();
  48. lines = File.ReadAllLines(c.ConfigFilePath);
  49. Assert.AreEqual(1, lines.Count(x => x.Equals("[Cat]")));
  50. Assert.AreEqual(1, lines.Count(x => x.Equals("## Test")));
  51. Assert.AreEqual(1, lines.Count(x => x.Equals("Key = 0")));
  52. w.Value = 69;
  53. lines = File.ReadAllLines(c.ConfigFilePath);
  54. Assert.AreEqual(1, lines.Count(x => x.Equals("[Cat]")));
  55. Assert.AreEqual(1, lines.Count(x => x.Equals("## Test")));
  56. Assert.AreEqual(1, lines.Count(x => x.Equals("Key = 69")));
  57. }
  58. [TestMethod]
  59. public void AutoSaveTest()
  60. {
  61. var c = MakeConfig();
  62. c.AddSetting("Cat", "Key", 0, new ConfigDescription("Test"));
  63. var eventFired = new AutoResetEvent(false);
  64. c.ConfigReloaded += (sender, args) => eventFired.Set();
  65. c.Save();
  66. Assert.IsFalse(eventFired.WaitOne(200));
  67. }
  68. [TestMethod]
  69. public void ReadTest()
  70. {
  71. var c = MakeConfig();
  72. File.WriteAllText(c.ConfigFilePath, "[Cat]\n# Test\nKey=1\n");
  73. c.Reload();
  74. var w = c.AddSetting("Cat", "Key", 0, "Test");
  75. Assert.AreEqual(w.Value, 1);
  76. var w2 = c.AddSetting("Cat", "Key2", 0, new ConfigDescription("Test"));
  77. Assert.AreEqual(w2.Value, 0);
  78. }
  79. [TestMethod]
  80. public void ReadTest2()
  81. {
  82. var c = MakeConfig();
  83. var w = c.AddSetting("Cat", "Key", 0, new ConfigDescription("Test"));
  84. Assert.AreEqual(w.Value, 0);
  85. File.WriteAllText(c.ConfigFilePath, "[Cat]\n# Test\nKey = 1 \n");
  86. c.Reload();
  87. Assert.AreEqual(w.Value, 1);
  88. }
  89. [TestMethod]
  90. public void FileWatchTestNoSelfReload()
  91. {
  92. var c = MakeConfig();
  93. var eventFired = new AutoResetEvent(false);
  94. c.ConfigReloaded += (sender, args) => eventFired.Set();
  95. c.Save();
  96. Assert.IsFalse(eventFired.WaitOne(200));
  97. }
  98. [TestMethod]
  99. public void EventTestWrapper()
  100. {
  101. var c = MakeConfig();
  102. var w = c.AddSetting("Cat", "Key", 0, new ConfigDescription("Test"));
  103. File.WriteAllText(c.ConfigFilePath, "[Cat]\n# Test\nKey=1\n");
  104. var eventFired = false;
  105. w.SettingChanged += (sender, args) => eventFired = true;
  106. c.Reload();
  107. Assert.IsTrue(eventFired);
  108. }
  109. [TestMethod]
  110. public void PersistHomeless()
  111. {
  112. var c = MakeConfig();
  113. File.WriteAllText(c.ConfigFilePath, "[Cat]\n# Test\nKey=1\nHomeless=0");
  114. c.Reload();
  115. var w = c.AddSetting("Cat", "Key", 0, new ConfigDescription("Test"));
  116. c.Save();
  117. Assert.IsTrue(File.ReadAllLines(c.ConfigFilePath).Single(x => x.StartsWith("Homeless") && x.EndsWith("0")) != null);
  118. }
  119. [TestMethod]
  120. public void EventTestReload()
  121. {
  122. var c = MakeConfig();
  123. var eventFired = false;
  124. var w = c.AddSetting("Cat", "Key", 0, new ConfigDescription("Test"));
  125. w.SettingChanged += (sender, args) => eventFired = true;
  126. Assert.IsFalse(eventFired);
  127. File.WriteAllText(c.ConfigFilePath, "[Cat]\n# Test\nKey=1\n");
  128. c.Reload();
  129. Assert.IsTrue(eventFired);
  130. }
  131. [TestMethod]
  132. public void ValueRangeTest()
  133. {
  134. var c = MakeConfig();
  135. var w = c.AddSetting("Cat", "Key", 0, new ConfigDescription("Test", new AcceptableValueRange<int>(0, 2)));
  136. Assert.AreEqual(0, w.Value);
  137. w.Value = 2;
  138. Assert.AreEqual(2, w.Value);
  139. w.Value = -2;
  140. Assert.AreEqual(0, w.Value);
  141. w.Value = 4;
  142. Assert.AreEqual(2, w.Value);
  143. }
  144. [TestMethod]
  145. [ExpectedException(typeof(ArgumentException))]
  146. public void ValueRangeBadTypeTest()
  147. {
  148. var c = MakeConfig();
  149. c.AddSetting("Cat", "Key", 0, new ConfigDescription("Test", new AcceptableValueRange<float>(1, 2)));
  150. Assert.Fail();
  151. }
  152. [TestMethod]
  153. public void ValueRangeDefaultTest()
  154. {
  155. var c = MakeConfig();
  156. var w = c.AddSetting("Cat", "Key", 0, new ConfigDescription("Test", new AcceptableValueRange<int>(1, 2)));
  157. Assert.AreEqual(w.Value, 1);
  158. }
  159. [TestMethod]
  160. public void ValueRangeLoadTest()
  161. {
  162. var c = MakeConfig();
  163. File.WriteAllText(c.ConfigFilePath, "[Cat]\nKey = 1\n");
  164. c.Reload();
  165. var w = c.AddSetting("Cat", "Key", 0, new ConfigDescription("Test", new AcceptableValueRange<int>(0, 2)));
  166. Assert.AreEqual(w.Value, 1);
  167. File.WriteAllText(c.ConfigFilePath, "[Cat]\nKey = 5\n");
  168. c.Reload();
  169. Assert.AreEqual(w.Value, 2);
  170. }
  171. [TestMethod]
  172. public void ValueListTest()
  173. {
  174. var c = MakeConfig();
  175. var w = c.AddSetting("Cat", "Key", "kek", new ConfigDescription("Test", new AcceptableValueList<string>("lel", "kek", "wew", "why")));
  176. Assert.AreEqual("kek", w.Value);
  177. w.Value = "wew";
  178. Assert.AreEqual("wew", w.Value);
  179. w.Value = "no";
  180. Assert.AreEqual("lel", w.Value);
  181. w.Value = null;
  182. Assert.AreEqual("lel", w.Value);
  183. }
  184. [TestMethod]
  185. public void KeyboardShortcutTest()
  186. {
  187. var shortcut = new KeyboardShortcut(KeyCode.H, KeyCode.O, KeyCode.R, KeyCode.S, KeyCode.E, KeyCode.Y);
  188. var s = shortcut.Serialize();
  189. var d = KeyboardShortcut.Deserialize(s);
  190. Assert.AreEqual(shortcut, d);
  191. var c = MakeConfig();
  192. var w = c.AddSetting("Cat", "Key", new KeyboardShortcut(KeyCode.A, KeyCode.LeftShift));
  193. Assert.AreEqual(new KeyboardShortcut(KeyCode.A, KeyCode.LeftShift), w.Value);
  194. w.Value = shortcut;
  195. c.Reload();
  196. Assert.AreEqual(shortcut, w.Value);
  197. }
  198. [TestMethod]
  199. public void KeyboardShortcutTest2()
  200. {
  201. Assert.AreEqual(KeyboardShortcut.Empty, new KeyboardShortcut());
  202. var c = MakeConfig();
  203. var w = c.AddSetting("Cat", "Key", KeyboardShortcut.Empty, new ConfigDescription("Test"));
  204. Assert.AreEqual("", w.GetSerializedValue());
  205. w.SetSerializedValue(w.GetSerializedValue());
  206. Assert.AreEqual(KeyboardShortcut.Empty, w.Value);
  207. var testShortcut = new KeyboardShortcut(KeyCode.A, KeyCode.B, KeyCode.C);
  208. w.Value = testShortcut;
  209. w.SetSerializedValue(w.GetSerializedValue());
  210. Assert.AreEqual(testShortcut, w.Value);
  211. c.Save();
  212. c.Reload();
  213. Assert.AreEqual(testShortcut, w.Value);
  214. }
  215. [TestMethod]
  216. public void StringEscapeChars()
  217. {
  218. const string testVal = "new line\n test \t\0";
  219. var c = MakeConfig();
  220. var w = c.AddSetting("Cat", "Key", testVal, new ConfigDescription("Test"));
  221. Assert.AreEqual(testVal, w.Value);
  222. Assert.IsFalse(w.GetSerializedValue().Any(x => x == '\n'));
  223. w.SetSerializedValue(w.GetSerializedValue());
  224. Assert.AreEqual(testVal, w.Value);
  225. c.Save();
  226. c.Reload();
  227. Assert.AreEqual(testVal, w.Value);
  228. }
  229. [TestMethod]
  230. public void UnescapedPathString()
  231. {
  232. var c = MakeConfig();
  233. var w = c.AddSetting("Cat", "Key", "", new ConfigDescription("Test"));
  234. var unescaped = @"D:\test\p ath";
  235. foreach (string testVal in new[] { unescaped, @"D:\\test\\p ath" })
  236. {
  237. File.WriteAllText(c.ConfigFilePath, $"[Cat]\n# Test\nKey={testVal}\n");
  238. c.Reload();
  239. Assert.AreEqual(unescaped, w.Value);
  240. w.SetSerializedValue(w.GetSerializedValue());
  241. Assert.AreEqual(unescaped, w.Value);
  242. c.Save();
  243. c.Reload();
  244. Assert.AreEqual(unescaped, w.Value);
  245. }
  246. }
  247. }
  248. }