public void Construct_GivenFileNameWhichDoesntExist_ShouldNotThrow() { //---------------Set up test pack------------------- using (var tempFile = new AutoDeletingTempFile(".ini")) { File.Delete(tempFile.Path); //---------------Assert Precondition---------------- Assert.IsFalse(File.Exists(tempFile.Path)); //---------------Execute Test ---------------------- INIFile_EXPOSES_Sections iniFile = null; Assert.DoesNotThrow(() => iniFile = Create(tempFile.Path)); //---------------Test Result ----------------------- Assert.IsNotNull(iniFile.Data); Assert.IsFalse(iniFile.Data.Keys.Any(k => k != "")); } }
public void Parse_WhenValueIsQuoted_ShouldSetDataValueUnQuoted() { //---------------Set up test pack------------------- using (var tempFile = new AutoDeletingTempFile(".ini")) { var section = RandString(); var key = RandString(); var value = RandString(); var iniDataLines = new[] { "[" + section + "]", key + "=\"" + value + "\"" }; tempFile.Write(iniDataLines); //---------------Assert Precondition---------------- Assert.IsTrue(File.Exists(tempFile.Path)); //---------------Execute Test ---------------------- var iniFile = new INIFile_EXPOSES_Sections(tempFile.Path); //---------------Test Result ----------------------- Assert.IsNotNull(iniFile.Data); Assert.IsTrue(iniFile.Data.Keys.Contains(section)); Assert.AreEqual(value, iniFile.Data[section][key]); } }
public void Construct_GivenFileName_ShouldLoadFile() { //---------------Set up test pack------------------- using (var tempFile = new AutoDeletingTempFile(".ini")) { var section = RandString(); var key = RandString(); var value = RandString(); var iniDataLines = new[] { "[" + section + "]", key + "=" + value }; tempFile.Write(iniDataLines); //---------------Assert Precondition---------------- Assert.IsTrue(File.Exists(tempFile.Path)); //---------------Execute Test ---------------------- var iniFile = new INIFile_EXPOSES_Sections(tempFile.Path); //---------------Test Result ----------------------- Assert.IsNotNull(iniFile.Data); Assert.IsTrue(iniFile.Data.Keys.Contains(section)); Assert.AreEqual(iniFile.Data[section][key], value); } }
public void Construct_WhenGivenPathButFileDoesntExist_ShouldCreateFile() { //---------------Set up test pack------------------- using (var tempFile = new AutoDeletingTempFile(".ini")) { File.Delete(tempFile.Path); //---------------Assert Precondition---------------- Assert.IsFalse(File.Exists(tempFile.Path)); //---------------Execute Test ---------------------- var iniFile = Create(tempFile.Path); //---------------Test Result ----------------------- Assert.IsTrue(File.Exists(tempFile.Path)); } }
public void ShouldLoadMultipleSectionsAndKeys_NotCaringAboutKeyOrSectionCase() { using (var tempFile = new AutoDeletingTempFile(".ini")) { //---------------Set up test pack------------------- var lines = new[] { "[Section1]", "path=\"C:\\tmp\\something.txt\"", "color=Red", "", "[Section2]", "number=12", "Season = Green" }; tempFile.Write(lines); //---------------Assert Precondition---------------- //---------------Execute Test ---------------------- var iniFile = Create(tempFile.Path); //---------------Test Result ----------------------- Assert.AreEqual("C:\\tmp\\something.txt", iniFile.Data["Section1"]["path"]); Assert.AreEqual("Red", iniFile.Data["Section1"]["color"]); Assert.AreEqual("12", iniFile.Data["section2"]["number"]); Assert.AreEqual("Green", iniFile.Data["Section2"]["season"]); } }
public void Persist_GivenNoFileName_ShouldWriteINIDataToOriginalFile() { //---------------Set up test pack------------------- using (var tempFile = new AutoDeletingTempFile(".ini")) { //---------------Assert Precondition---------------- var writer = Create(tempFile.Path); var section = RandString(); var key = RandString(); var value = RandString(); writer.AddSection(section); writer.Data[section][key] = value; //---------------Execute Test ---------------------- writer.Persist(); var reader = Create(tempFile.Path); //---------------Test Result ----------------------- Assert.AreEqual(value, reader.Data[section][key]); } }
public void Parse_WhenValueIsQuoted_ShouldSetDataValueUnQuoted() { //---------------Set up test pack------------------- using (var tempFile = new AutoDeletingTempFile(".ini")) { var section = RandString(); var key = RandString(); var value = RandString(); var iniDataLines = new[] { "[" + section + "]", key + "=\"" + value + "\"" }; tempFile.Write(iniDataLines); //---------------Assert Precondition---------------- Assert.IsTrue(File.Exists(tempFile.Path)); //---------------Execute Test ---------------------- var iniFile = new INIFile_EXPOSES_Sections(tempFile.Path); //---------------Test Result ----------------------- Assert.IsNotNull(iniFile.Data); Assert.IsTrue(iniFile.Data.Keys.Contains(section)); Assert.AreEqual(value, iniFile.Data[section][key]); } }
public void IndexedGetter_WhenSectionDoesntExist_ShouldCreateSection() { //---------------Set up test pack------------------- var section = RandomValueGen.GetRandomString(); var key = RandomValueGen.GetRandomString(); var value = RandomValueGen.GetRandomString(); //---------------Assert Precondition---------------- using (var tempFile = new AutoDeletingTempFile(".ini")) { var ini = new INIFile(tempFile.Path); ini[section][key] = value; ini.Persist(); //---------------Execute Test ---------------------- var ini2 = new INIFile(tempFile.Path); var result = ini2[section][key]; //---------------Test Result ----------------------- Assert.AreEqual(value, result); } }
public void Construct_WhenSourceContainsComments_ShouldIgnoreCommentedParts() { using (var tempFile = new AutoDeletingTempFile(".ini")) { //---------------Set up test pack------------------- var src = new[] { "; created by some generator", "[general] ; general settings go here", ";this line should be ignored", "key=value ; this part of the line should be ignored" }; File.WriteAllBytes(tempFile.Path, Encoding.UTF8.GetBytes(String.Join(Environment.NewLine, src))); //---------------Assert Precondition---------------- //---------------Execute Test ---------------------- var ini = Create(tempFile.Path); //---------------Test Result ----------------------- Assert.AreEqual(1, ini.Data.Count); Assert.AreEqual(1, ini["general"].Keys.Count); Assert.AreEqual(1, ini["General"].Keys.Count); Assert.AreEqual("value", ini["general"]["key"]); } }