ConfigFileTests.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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.Wrap("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.Wrap("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.Wrap("Cat", "Key", 0, new ConfigDescription("Test"));
  75. Assert.AreEqual(w.Value, 1);
  76. var w2 = c.Wrap("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.Wrap("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.Wrap("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 EventTestReload()
  111. {
  112. var c = MakeConfig();
  113. var eventFired = false;
  114. var w = c.Wrap("Cat", "Key", 0, new ConfigDescription("Test"));
  115. w.SettingChanged += (sender, args) => eventFired = true;
  116. Assert.IsFalse(eventFired);
  117. File.WriteAllText(c.ConfigFilePath, "[Cat]\n# Test\nKey=1\n");
  118. c.Reload();
  119. Assert.IsTrue(eventFired);
  120. }
  121. [TestMethod]
  122. public void ValueRangeTest()
  123. {
  124. var c = MakeConfig();
  125. var w = c.Wrap("Cat", "Key", 0, new ConfigDescription("Test", new AcceptableValueRange<int>(0, 2)));
  126. Assert.AreEqual(0, w.Value);
  127. w.Value = 2;
  128. Assert.AreEqual(2, w.Value);
  129. w.Value = -2;
  130. Assert.AreEqual(0, w.Value);
  131. w.Value = 4;
  132. Assert.AreEqual(2, w.Value);
  133. }
  134. [TestMethod]
  135. [ExpectedException(typeof(ArgumentException))]
  136. public void ValueRangeBadTypeTest()
  137. {
  138. var c = MakeConfig();
  139. c.Wrap("Cat", "Key", 0, new ConfigDescription("Test", new AcceptableValueRange<float>(1, 2)));
  140. Assert.Fail();
  141. }
  142. [TestMethod]
  143. public void ValueRangeDefaultTest()
  144. {
  145. var c = MakeConfig();
  146. var w = c.Wrap("Cat", "Key", 0, new ConfigDescription("Test", new AcceptableValueRange<int>(1, 2)));
  147. Assert.AreEqual(w.Value, 1);
  148. }
  149. [TestMethod]
  150. public void ValueRangeLoadTest()
  151. {
  152. var c = MakeConfig();
  153. File.WriteAllText(c.ConfigFilePath, "[Cat]\nKey = 1\n");
  154. c.Reload();
  155. var w = c.Wrap("Cat", "Key", 0, new ConfigDescription("Test", new AcceptableValueRange<int>(0, 2)));
  156. Assert.AreEqual(w.Value, 1);
  157. File.WriteAllText(c.ConfigFilePath, "[Cat]\nKey = 5\n");
  158. c.Reload();
  159. Assert.AreEqual(w.Value, 2);
  160. }
  161. [TestMethod]
  162. public void ValueListTest()
  163. {
  164. var c = MakeConfig();
  165. var w = c.Wrap<string>("Cat", "Key", "kek", new ConfigDescription("Test", new AcceptableValueList<string>("lel", "kek", "wew", "why")));
  166. Assert.AreEqual("kek", w.Value);
  167. w.Value = "wew";
  168. Assert.AreEqual("wew", w.Value);
  169. w.Value = "no";
  170. Assert.AreEqual("lel", w.Value);
  171. w.Value = null;
  172. Assert.AreEqual("lel", w.Value);
  173. }
  174. [TestMethod]
  175. public void KeyShortcutTest()
  176. {
  177. var shortcut = new KeyboardShortcut(KeyCode.H, KeyCode.O, KeyCode.R, KeyCode.S, KeyCode.E, KeyCode.Y);
  178. var s = shortcut.Serialize();
  179. var d = KeyboardShortcut.Deserialize(s);
  180. Assert.AreEqual(shortcut, d);
  181. var c = MakeConfig();
  182. var w = c.Wrap("Cat", "Key", new KeyboardShortcut(KeyCode.A, KeyCode.LeftShift));
  183. Assert.AreEqual(new KeyboardShortcut(KeyCode.A, KeyCode.LeftShift), w.Value);
  184. w.Value = shortcut;
  185. c.Reload();
  186. Assert.AreEqual(shortcut, w.Value);
  187. }
  188. }
  189. }