// This file (IniFileReaderTest.cs) is a part of the IBBoard.Ini.Tests library and is copyright 2009 IBBoard. // // The file and the library/program it is in are licensed under the GNU LGPL license, either version 3 of the License or (at your option) any later version. Please see COPYING.LGPL for more information and the full license. using System; using System.IO; using NUnit.Framework; namespace IBBoard.Ini { [TestFixture()] public class IniFileReaderTest { [Test()] public void TestIniFileReaderReturnsEmptyObjectForEmptyIniText() { IniFile file = IniFileReader.ReadFile(""); Assert.AreEqual(0, file.Sections.Length); } [Test()] public void TestIniFileReaderContainsExpectedSectionForSingleSectionIniText() { IniFile file = IniFileReader.ReadFile("[ASection]"); Assert.AreEqual(1, file.Sections.Length); Assert.IsTrue(file.HasSection("ASection")); Assert.IsNotNull(file["ASection"]); Assert.AreEqual(0, file["ASection"].Keys.Length); Assert.IsNotInstanceOfType(typeof(NonExistantIniSection), file["ASection"]); } [Test()] public void TestIniFileReaderContainsExpectedSectionForSingleSectionIniTextWithContent() { IniFile file = IniFileReader.ReadFile("[ASection]\nsomething=fibble"); Assert.AreEqual(1, file.Sections.Length); Assert.IsTrue(file.HasSection("ASection")); Assert.IsNotNull(file["ASection"]); Assert.AreEqual(1, file["ASection"].Keys.Length); Assert.IsNotInstanceOfType(typeof(NonExistantIniSection), file["ASection"]); } [Test()] public void TestIniFileReaderContainsExpectedSectionForSingleSectionIniTextWithMultiLineContent() { IniFile file = IniFileReader.ReadFile("[ASection]\nsomething=fibble\nfobble_key=foo\nabcde=fghij\nfoo9=bar2"); Assert.AreEqual(1, file.Sections.Length); Assert.IsTrue(file.HasSection("ASection")); IniSection section = file["ASection"]; Assert.IsNotNull(section); Assert.AreEqual(4, section.Keys.Length); Assert.IsNotInstanceOfType(typeof(NonExistantIniSection), file["ASection"]); string[] keys = new string[]{"something", "fobble_key", "abcde", "foo9"}; foreach (string key in keys) { bool found = false; foreach (string sectionKey in section.Keys) { if (key == sectionKey) { found = true; break; } } Assert.IsTrue(found, "Did not find "+key); } } [Test()] public void TestIniFileReaderContainsExpectedSectionsForTwoSectionIniTextWithContent() { IniFile file = IniFileReader.ReadFile("[ASection]\nsomething=fibble\n[BSection]\nsomethingElse=fibble"); Assert.AreEqual(2, file.Sections.Length); Assert.IsTrue(file.HasSection("ASection")); Assert.IsNotNull(file["ASection"]); Assert.AreEqual(1, file["ASection"].Keys.Length); Assert.IsNotInstanceOfType(typeof(NonExistantIniSection), file["ASection"]); Assert.IsTrue(file.HasSection("BSection")); Assert.IsNotNull(file["BSection"]); Assert.AreEqual(1, file["BSection"].Keys.Length); Assert.IsNotInstanceOfType(typeof(NonExistantIniSection), file["BSection"]); } [Test()] public void TestIniFileReaderContainsExpectedSectionsForTwoConsecutiveSectionSectionsInText() { IniFile file = IniFileReader.ReadFile("[SectionA]\n[SectionB]"); Assert.AreEqual(2, file.Sections.Length); Assert.IsTrue(file.HasSection("SectionA")); Assert.IsNotNull(file["SectionA"]); Assert.AreEqual(0, file["SectionA"].Keys.Length); Assert.IsNotInstanceOfType(typeof(NonExistantIniSection), file["SectionA"]); Assert.IsTrue(file.HasSection("SectionB")); Assert.IsNotNull(file["SectionB"]); Assert.AreEqual(0, file["SectionB"].Keys.Length); Assert.IsNotInstanceOfType(typeof(NonExistantIniSection), file["SectionB"]); } [Test()] public void TestIniFileReaderContainsExpectedSectionWhenTextPaddedAtStart() { IniFile file = IniFileReader.ReadFile("\nnon-blank\n[ASection]\nsomething=fibble"); Assert.AreEqual(1, file.Sections.Length); Assert.IsTrue(file.HasSection("ASection")); Assert.IsNotNull(file["ASection"]); Assert.AreEqual(1, file["ASection"].Keys.Length); Assert.IsNotInstanceOfType(typeof(NonExistantIniSection), file["ASection"]); } [Test()] public void TestIniFileReaderReturnsEmptyObjectForEmptyIniFile() { IniFile file = IniFileReader.ReadFile(new FileInfo("EmptyFile.ini")); Assert.AreEqual(0, file.Sections.Length); } [Test()] public void TestIniFileReaderContainsExpectedSectionForSingleSectionIniFile() { IniFile file = IniFileReader.ReadFile(new FileInfo("SingleSectionSingleLine.ini")); Assert.AreEqual(1, file.Sections.Length); Assert.IsTrue(file.HasSection("ASection")); Assert.IsNotNull(file["ASection"]); Assert.AreEqual(0, file["ASection"].Keys.Length); Assert.IsNotInstanceOfType(typeof(NonExistantIniSection), file["ASection"]); } [Test()] public void TestIniFileReaderContainsExpectedSectionForSingleSectionIniFileWithContent() { IniFile file = IniFileReader.ReadFile(new FileInfo("SingleSectionWithContent.ini")); Assert.AreEqual(1, file.Sections.Length); Assert.IsTrue(file.HasSection("ASection")); Assert.IsNotNull(file["ASection"]); Assert.AreEqual(1, file["ASection"].Keys.Length); Assert.IsNotInstanceOfType(typeof(NonExistantIniSection), file["ASection"]); } [Test()] public void TestIniFileReaderContainsExpectedSectionsForTwoSectionIniFileWithContent() { IniFile file = IniFileReader.ReadFile(new FileInfo("DoubleSectionWithContent.ini")); Assert.AreEqual(2, file.Sections.Length); Assert.IsTrue(file.HasSection("ASection")); Assert.IsNotNull(file["ASection"]); Assert.AreEqual(1, file["ASection"].Keys.Length); Assert.IsNotInstanceOfType(typeof(NonExistantIniSection), file["ASection"]); Assert.IsTrue(file.HasSection("BSection")); Assert.IsNotNull(file["BSection"]); Assert.AreEqual(1, file["BSection"].Keys.Length); Assert.IsNotInstanceOfType(typeof(NonExistantIniSection), file["BSection"]); } [Test()] public void TestIniFileReaderContainsExpectedSectionForSingleSectionIniFileWithMultiLineContent() { IniFile file = IniFileReader.ReadFile(new FileInfo("SingleSectionWithMultiLineContent.ini")); Assert.AreEqual(1, file.Sections.Length); Assert.IsTrue(file.HasSection("ASection")); IniSection section = file["ASection"]; Assert.IsNotNull(section); Assert.AreEqual(4, section.Keys.Length); Assert.IsNotInstanceOfType(typeof(NonExistantIniSection), file["ASection"]); string[] keys = new string[]{"something", "fobble_key", "abcde", "foo9"}; foreach (string key in keys) { bool found = false; foreach (string sectionKey in section.Keys) { if (key == sectionKey) { found = true; break; } } Assert.IsTrue(found, "Did not find "+key); } } [Test()] public void TestIniFileReaderContainsExpectedSectionWhenFilePaddedAtStart() { IniFile file = IniFileReader.ReadFile(new FileInfo("StartPadded.ini")); Assert.AreEqual(1, file.Sections.Length); Assert.IsTrue(file.HasSection("ASection")); Assert.IsNotNull(file["ASection"]); Assert.AreEqual(1, file["ASection"].Keys.Length); Assert.IsNotInstanceOfType(typeof(NonExistantIniSection), file["ASection"]); } [Test()] public void TestIniFileReaderContainsExpectedSectionsForTwoConsecutiveSectionSectionsInFile() { IniFile file = IniFileReader.ReadFile(new FileInfo("ConsecutiveSection.ini")); Assert.AreEqual(2, file.Sections.Length); Assert.IsTrue(file.HasSection("SectionA")); Assert.IsNotNull(file["SectionA"]); Assert.AreEqual(0, file["SectionA"].Keys.Length); Assert.IsNotInstanceOfType(typeof(NonExistantIniSection), file["SectionA"]); Assert.IsTrue(file.HasSection("SectionB")); Assert.IsNotNull(file["SectionB"]); Assert.AreEqual(0, file["SectionB"].Keys.Length); Assert.IsNotInstanceOfType(typeof(NonExistantIniSection), file["SectionB"]); } } }