IniFileTests.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. using System.IO;
  2. using System.Threading.Tasks;
  3. using Masuit.Tools.Files;
  4. using Xunit;
  5. namespace Masuit.Tools.Abstractions.Test.Files;
  6. public class IniFileTests
  7. {
  8. private readonly string _testFilePath = Path.Combine(Path.GetTempPath(), "test.ini");
  9. public IniFileTests()
  10. {
  11. if (File.Exists(_testFilePath))
  12. {
  13. File.Delete(_testFilePath);
  14. }
  15. }
  16. [Fact]
  17. public void Constructor_ShouldLoadExistingFile()
  18. {
  19. File.WriteAllText(_testFilePath, "[Section1]\nKey1=Value1\nKey2=Value2");
  20. var iniFile = new IniFile(_testFilePath);
  21. var section = iniFile.GetSection("Section1");
  22. Assert.Equal("Value1", section["Key1"]);
  23. Assert.Equal("Value2", section["Key2"]);
  24. }
  25. [Fact]
  26. public void Reload_ShouldReloadFile()
  27. {
  28. File.WriteAllText(_testFilePath, "[Section1]\nKey1=Value1\nKey2=Value2");
  29. var iniFile = new IniFile(_testFilePath);
  30. File.WriteAllText(_testFilePath, "[Section1]\nKey1=NewValue1\nKey2=NewValue2");
  31. iniFile.Reload();
  32. var section = iniFile.GetSection("Section1");
  33. Assert.Equal("NewValue1", section["Key1"]);
  34. Assert.Equal("NewValue2", section["Key2"]);
  35. }
  36. [Fact]
  37. public void Save_ShouldSaveToFile()
  38. {
  39. var iniFile = new IniFile(_testFilePath);
  40. iniFile.SetValue("Section1", "Key1", "Value1");
  41. iniFile.SetValue("Section1", "Key2", "Value2");
  42. iniFile.Save();
  43. var content = File.ReadAllText(_testFilePath);
  44. Assert.Contains("[Section1]", content);
  45. Assert.Contains("Key1=Value1", content);
  46. Assert.Contains("Key2=Value2", content);
  47. }
  48. [Fact]
  49. public async Task SaveAsync_ShouldSaveToFile()
  50. {
  51. var iniFile = new IniFile(_testFilePath);
  52. iniFile.SetValue("Section1", "Key1", "Value1");
  53. iniFile.SetValue("Section1", "Key2", "Value2");
  54. await iniFile.SaveAsync();
  55. var content = await File.ReadAllTextAsync(_testFilePath);
  56. Assert.Contains("[Section1]", content);
  57. Assert.Contains("Key1=Value1", content);
  58. Assert.Contains("Key2=Value2", content);
  59. }
  60. [Fact]
  61. public void GetValue_ShouldReturnDefaultValueIfKeyNotFound()
  62. {
  63. var iniFile = new IniFile(_testFilePath);
  64. var value = iniFile.GetValue("Section1", "Key1", "DefaultValue");
  65. Assert.Equal("DefaultValue", value);
  66. }
  67. [Fact]
  68. public void GetValue_ShouldReturnValueIfKeyFound()
  69. {
  70. var iniFile = new IniFile(_testFilePath);
  71. iniFile.SetValue("Section1", "Key1", "Value1");
  72. var value = iniFile.GetValue("Section1", "Key1");
  73. Assert.Equal("Value1", value);
  74. }
  75. [Fact]
  76. public void GetValue_Generic_ShouldReturnConvertedValue()
  77. {
  78. var iniFile = new IniFile(_testFilePath);
  79. iniFile.SetValue("Section1", "Key1", "123");
  80. var value = iniFile.GetValue<int>("Section1", "Key1");
  81. Assert.Equal(123, value);
  82. }
  83. [Fact]
  84. public void GetSections_ShouldReturnAllSections()
  85. {
  86. var iniFile = new IniFile(_testFilePath);
  87. iniFile.SetValue("Section1", "Key1", "Value1");
  88. iniFile.SetValue("Section2", "Key2", "Value2");
  89. var sections = iniFile.GetSections();
  90. Assert.Contains(sections, s => s.Name == "Section1");
  91. Assert.Contains(sections, s => s.Name == "Section2");
  92. }
  93. [Fact]
  94. public void GetSection_ShouldReturnAllKeyValuesInSection()
  95. {
  96. var iniFile = new IniFile(_testFilePath);
  97. iniFile.SetValue("Section1", "Key1", "Value1");
  98. iniFile.SetValue("Section1", "Key2", "Value2");
  99. var section = iniFile.GetSection("Section1");
  100. Assert.Equal("Value1", section["Key1"]);
  101. Assert.Equal("Value2", section["Key2"]);
  102. }
  103. [Fact]
  104. public void GetSection_Generic_ShouldReturnBoundObject()
  105. {
  106. var iniFile = new IniFile(_testFilePath);
  107. iniFile.SetValue("Section1", "Key1", "Value1");
  108. iniFile.SetValue("Section1", "Key2", "Value2");
  109. var section = iniFile.GetSection<TestSection>("Section1");
  110. Assert.Equal("Value1", section.Key1);
  111. Assert.Equal("Value2", section.Key2);
  112. }
  113. [Fact]
  114. public void SetValue_ShouldSetKeyValue()
  115. {
  116. var iniFile = new IniFile(_testFilePath);
  117. iniFile.SetValue("Section1", "Key1", "Value1");
  118. var value = iniFile.GetValue("Section1", "Key1");
  119. Assert.Equal("Value1", value);
  120. }
  121. [Fact]
  122. public void SetValue_Bool_ShouldSetKeyValue()
  123. {
  124. var iniFile = new IniFile(_testFilePath);
  125. iniFile.SetValue("Section1", "Key1", true);
  126. var value = iniFile.GetValue("Section1", "Key1");
  127. Assert.Equal("true", value);
  128. }
  129. [Fact]
  130. public void SetValue_Generic_ShouldSetKeyValue()
  131. {
  132. var iniFile = new IniFile(_testFilePath);
  133. iniFile.SetValue("Section1", "Key1", 123);
  134. var value = iniFile.GetValue("Section1", "Key1");
  135. Assert.Equal("123", value);
  136. }
  137. [Fact]
  138. public void ClearSection_ShouldClearAllKeysInSection()
  139. {
  140. var iniFile = new IniFile(_testFilePath);
  141. iniFile.SetValue("Section1", "Key1", "Value1");
  142. iniFile.ClearSection("Section1");
  143. var section = iniFile.GetSection("Section1");
  144. Assert.Empty(section);
  145. }
  146. [Fact]
  147. public void ClearAllSection_ShouldClearAllSections()
  148. {
  149. var iniFile = new IniFile(_testFilePath);
  150. iniFile.SetValue("Section1", "Key1", "Value1");
  151. iniFile.SetValue("Section2", "Key2", "Value2");
  152. iniFile.ClearAllSection();
  153. var sections = iniFile.GetSections();
  154. Assert.Empty(sections);
  155. }
  156. private class TestSection
  157. {
  158. public string Key1 { get; set; }
  159. public string Key2 { get; set; }
  160. }
  161. }