Esempio n. 1
0
        public void BindValueWithInternalDataTest()
        {
            string iniFileContent = "[Source Section]\n" +
                                    "Source Key 1 = Source Value 1\n" +
                                    "Source Key 2 = Source Value 2\n" +
                                    "Source Key 3 = Source Value 3\n" +

                                    "[Binding Section]\n" +
                                    "Source Key 4 = Source Value 4\n" +
                                    "Test Key 1 = @{Source Section|Source Key 1}\n" +
                                    "Test Key 2-3 = @{Source Section|Source Key 2} and @{Source Section|Source Key 3}\n" +
                                    "Test Key 1-4 = @{Source Section|Source Key 1} and @{Source Key 4}\n" +
                                    "Test Key X = @{Unknown}";

            IniFile file = IniUtilities.LoadIniFileContent(iniFileContent, new IniOptions());

            file.ValueBinding.Bind();

            var bindedSection = file.Sections["Binding Section"];

            Assert.AreEqual("Source Value 1", bindedSection.Keys["Test Key 1"].Value);
            Assert.AreEqual("Source Value 2 and Source Value 3", bindedSection.Keys["Test Key 2-3"].Value);
            Assert.AreEqual("Source Value 1 and Source Value 4", bindedSection.Keys["Test Key 1-4"].Value);
            Assert.AreEqual("@{Unknown}", bindedSection.Keys["Test Key X"].Value);
        }
Esempio n. 2
0
        public void BindValueWithExternalDataTest()
        {
            string iniFileContent = "[Binding Section]\n" +
                                    "Test Key 1 = @{First Tester}\n" +
                                    "Test Key 2-3 = @{Second Tester} and @{Third Tester}\n" +
                                    "Test Key 3-3-4 = {@{Third Tester}, @{Third Tester}, @{Fourth Tester}}\n" +
                                    "Test Key X = @{Unknown}\n" +
                                    "Test Key Nested = @{Nested @{Test}}";

            IniFile file = IniUtilities.LoadIniFileContent(iniFileContent, new IniOptions());

            file.ValueBinding.Bind(
                new Dictionary <string, string>()
            {
                { "First Tester", "Source Value 1" },
                { "Second Tester", "Source Value 2" },
                { "Third Tester", "Source Value 3" },
                { "Fourth Tester", "Source Value 4" },
                { "Test", "Tester" }
            });

            var bindedSection = file.Sections["Binding Section"];

            Assert.AreEqual("Source Value 1", bindedSection.Keys["Test Key 1"].Value);
            Assert.AreEqual("Source Value 2 and Source Value 3", bindedSection.Keys["Test Key 2-3"].Value);
            Assert.AreEqual("{Source Value 3, Source Value 3, Source Value 4}", bindedSection.Keys["Test Key 3-3-4"].Value);
            Assert.AreEqual("@{Unknown}", bindedSection.Keys["Test Key X"].Value);
            Assert.AreEqual("@{Nested Tester}", bindedSection.Keys["Test Key Nested"].Value);

            file.ValueBinding.Bind(new KeyValuePair <string, string>("Nested Tester", "Nested Source Value"));
            Assert.AreEqual("Nested Source Value", bindedSection.Keys["Test Key Nested"].Value);
        }
Esempio n. 3
0
        public void Bug4()
        {
            string iniFileContent = "suffix= LLC © 2016" + Environment.NewLine +
                                    "suffix=LLC © 2016  ";
            IniFile file =
                IniUtilities.LoadIniFileContent(iniFileContent,
                                                new IniOptions()
            {
                Encoding = System.Text.Encoding.UTF8
            });

            Assert.AreEqual(" LLC © 2016", file.Sections[0].Keys[0].Value);
            Assert.AreEqual("LLC © 2016  ", file.Sections[0].Keys[1].Value);

            iniFileContent = "suffix = LLC © 2016" + Environment.NewLine +
                             "suffix =   LLC © 2016  ";
            file = IniUtilities.LoadIniFileContent(iniFileContent,
                                                   new IniOptions()
            {
                Encoding = System.Text.Encoding.UTF8
            });

            Assert.AreEqual("LLC © 2016", file.Sections[0].Keys[0].Value);
            Assert.AreEqual("  LLC © 2016  ", file.Sections[0].Keys[1].Value);
        }
Esempio n. 4
0
        public void ReadCustomTest()
        {
            string iniFileContent = "#Section's trailing comment." + Environment.NewLine +
                                    "{Section's name}#Section's leading comment." + Environment.NewLine +
                                    "#Key's trailing comment." + Environment.NewLine +
                                    "Key's name : Key's value#Key's leading comment.";

            IniOptions options = new IniOptions()
            {
                CommentStarter = IniCommentStarter.Hash,
                SectionWrapper = IniSectionWrapper.CurlyBrackets,
                KeyDelimiter   = IniKeyDelimiter.Colon
            };
            IniFile file = IniUtilities.LoadIniFileContent(iniFileContent, options);

            Assert.AreEqual(1, file.Sections.Count);
            Assert.AreEqual("Section's trailing comment.", file.Sections[0].TrailingComment.Text);
            Assert.AreEqual("Section's name", file.Sections[0].Name);
            Assert.AreEqual("Section's leading comment.", file.Sections[0].LeadingComment.Text);

            Assert.AreEqual(1, file.Sections[0].Keys.Count);
            Assert.AreEqual("Key's trailing comment.", file.Sections[0].Keys[0].TrailingComment.Text);
            Assert.AreEqual("Key's name", file.Sections[0].Keys[0].Name);
            Assert.AreEqual("Key's value", file.Sections[0].Keys[0].Value);
            Assert.AreEqual("Key's leading comment.", file.Sections[0].Keys[0].LeadingComment.Text);
        }
Esempio n. 5
0
        public void ReadEmptyLinesTest()
        {
            string iniFileContent = Environment.NewLine +
                                    "  \t  " + Environment.NewLine +
                                    "[Section]" + Environment.NewLine +
                                    Environment.NewLine +
                                    Environment.NewLine +
                                    "  \t  " + Environment.NewLine +
                                    "Key = Value" + Environment.NewLine +
                                    Environment.NewLine +
                                    "  \t  " + Environment.NewLine +
                                    ";" + Environment.NewLine +
                                    "[Section]" + Environment.NewLine +
                                    Environment.NewLine +
                                    "  \t  " + Environment.NewLine +
                                    Environment.NewLine +
                                    ";" + Environment.NewLine +
                                    "Key = Value";

            IniFile file = IniUtilities.LoadIniFileContent(iniFileContent, new IniOptions());

            Assert.AreEqual(2, file.Sections[0].LeadingComment.EmptyLinesBefore);
            Assert.AreEqual(3, file.Sections[0].Keys[0].LeadingComment.EmptyLinesBefore);
            Assert.AreEqual(2, file.Sections[1].TrailingComment.EmptyLinesBefore);
            Assert.AreEqual(3, file.Sections[1].Keys[0].TrailingComment.EmptyLinesBefore);
        }
Esempio n. 6
0
        public void ReadSectionEdgeCasesTest()
        {
            string iniFileContent = "[" + Environment.NewLine +
                                    "]" + Environment.NewLine +
                                    "[]" + Environment.NewLine +
                                    "[;]" + Environment.NewLine +
                                    "[;;]" + Environment.NewLine +
                                    "[[]]" + Environment.NewLine +
                                    "[[]];" + Environment.NewLine +
                                    "[[;]]" + Environment.NewLine +
                                    "[[;]];";

            IniFile file = IniUtilities.LoadIniFileContent(iniFileContent, new IniOptions());

            Assert.AreEqual(7, file.Sections.Count);
            Assert.AreEqual(string.Empty, file.Sections[0].Name);
            Assert.AreEqual(";", file.Sections[1].Name);
            Assert.AreEqual(";;", file.Sections[2].Name);
            Assert.AreEqual("[]", file.Sections[3].Name);
            Assert.AreEqual("[]", file.Sections[4].Name);
            Assert.AreEqual(string.Empty, file.Sections[4].LeadingComment.Text);
            Assert.AreEqual("[;]", file.Sections[5].Name);
            Assert.AreEqual("[;]", file.Sections[6].Name);
            Assert.AreEqual(string.Empty, file.Sections[6].LeadingComment.Text);
        }
Esempio n. 7
0
        public void Bug1()
        {
            string iniFileContent = "[Segment [A]]" + Environment.NewLine +
                                    "[Segment [A]];[Comment];" + Environment.NewLine +

                                    "[Segment [A][1]]" + Environment.NewLine +
                                    "[Segment [A][1]]  ;[Comment][1];" + Environment.NewLine +

                                    "[Segment;A]" + Environment.NewLine +
                                    "[Segment[A;B]]" + Environment.NewLine +
                                    "[Segment[A;B]];" + Environment.NewLine +
                                    "[Segment[A;B]]    ;[Comment];" + Environment.NewLine +

                                    "[Segment[A;B]]AB;Invalid comment" + Environment.NewLine +
                                    "[Segment[A;B]][[;Invalid comment" + Environment.NewLine +
                                    "[Segment[A;B;]";

            IniOptions options = new IniOptions();
            IniFile    file    = IniUtilities.LoadIniFileContent(iniFileContent, options);

            Assert.AreEqual("Segment [A]", file.Sections[0].Name);
            Assert.AreEqual("Segment [A]", file.Sections[1].Name);
            Assert.AreEqual("[Comment];", file.Sections[1].LeadingComment.Text);

            Assert.AreEqual("Segment [A][1]", file.Sections[2].Name);
            Assert.AreEqual("Segment [A][1]", file.Sections[3].Name);
            Assert.AreEqual("[Comment][1];", file.Sections[3].LeadingComment.Text);
            Assert.AreEqual(2, file.Sections[3].LeadingComment.LeftIndentation);

            Assert.AreEqual("Segment;A", file.Sections[4].Name);
            Assert.AreEqual(null, file.Sections[4].LeadingComment.Text);
            Assert.AreEqual("Segment[A;B]", file.Sections[5].Name);
            Assert.AreEqual(null, file.Sections[5].LeadingComment.Text);
            Assert.AreEqual("Segment[A;B]", file.Sections[6].Name);
            Assert.AreEqual(string.Empty, file.Sections[6].LeadingComment.Text);
            Assert.AreEqual("Segment[A;B]", file.Sections[7].Name);
            Assert.AreEqual("[Comment];", file.Sections[7].LeadingComment.Text);
            Assert.AreEqual(4, file.Sections[7].LeadingComment.LeftIndentation);

            Assert.AreEqual("Segment[A;B]", file.Sections[8].Name);
            Assert.AreEqual(null, file.Sections[8].LeadingComment.Text);
            Assert.AreEqual("Segment[A;B]", file.Sections[9].Name);
            Assert.AreEqual(null, file.Sections[9].LeadingComment.Text);
            Assert.AreEqual("Segment[A;B;", file.Sections[10].Name);

            string[] lines = IniUtilities.SaveIniFileContent(file, options);

            Assert.AreEqual("[Segment [A]]", lines[0]);
            Assert.AreEqual("[Segment [A]];[Comment];", lines[1]);
            Assert.AreEqual("[Segment [A][1]]", lines[2]);
            Assert.AreEqual("[Segment [A][1]]  ;[Comment][1];", lines[3]);
            Assert.AreEqual("[Segment;A]", lines[4]);
            Assert.AreEqual("[Segment[A;B]]", lines[5]);
            Assert.AreEqual("[Segment[A;B]];", lines[6]);
            Assert.AreEqual("[Segment[A;B]]    ;[Comment];", lines[7]);
            Assert.AreEqual("[Segment[A;B]]", lines[8]);
            Assert.AreEqual("[Segment[A;B]]", lines[9]);
            Assert.AreEqual("[Segment[A;B;]", lines[10]);
        }
Esempio n. 8
0
        public void Bug5()
        {
            string iniFileContent = "[Section 1]" + Environment.NewLine +
                                    "Key 1.1=Value 1.1";
            IniFile file = IniUtilities.LoadIniFileContent(iniFileContent, new IniOptions());

            Assert.AreEqual("Value 1.1", file.Sections["Section 1"].Keys["Key 1.1"].Value);
        }
Esempio n. 9
0
        public void Bug3()
        {
            string iniFileContent = "[sektion]" + Environment.NewLine +
                                    "key=\"Data Source=server;Initial Catalog=catalog;Integrated Security=SSPI\"";
            IniFile file = IniUtilities.LoadIniFileContent(iniFileContent, new IniOptions());

            IniKey key = file.Sections["sektion"].Keys["key"];

            Assert.AreEqual("\"Data Source=server;Initial Catalog=catalog;Integrated Security=SSPI\"", key.Value);
            Assert.IsNull(key.LeadingComment.Text);
        }
Esempio n. 10
0
        public void ReadUTF8EncodingTest()
        {
            string iniFileContent = "[Καλημέρα κόσμε]" + Environment.NewLine +
                                    "こんにちは 世界 = ¥ £ € $ ¢ ₡ ₢ ₣ ₤ ₥ ₦ ₧ ₨ ₩ ₪ ₫ ₭ ₮ ₯ ₹";

            IniFile file = IniUtilities.LoadIniFileContent(iniFileContent, new IniOptions()
            {
                Encoding = Encoding.UTF8
            });

            Assert.AreEqual("Καλημέρα κόσμε", file.Sections[0].Name);
            Assert.AreEqual("こんにちは 世界", file.Sections[0].Keys[0].Name);
            Assert.AreEqual("¥ £ € $ ¢ ₡ ₢ ₣ ₤ ₥ ₦ ₧ ₨ ₩ ₪ ₫ ₭ ₮ ₯ ₹", file.Sections[0].Keys[0].Value);
        }
Esempio n. 11
0
        public void ReadValueEdgeCasesTest()
        {
            string iniFileContent = "[Section]" + Environment.NewLine +
                                    "Key=" + Environment.NewLine +
                                    "Key=;" + Environment.NewLine +
                                    "Key= " + Environment.NewLine +
                                    "Key= ;" + Environment.NewLine +
                                    "Key =" + Environment.NewLine +
                                    "Key =;" + Environment.NewLine +
                                    "Key = " + Environment.NewLine +
                                    "Key = ;" + Environment.NewLine +
                                    "Key=;Test" + Environment.NewLine +
                                    "Key = ;Test";

            IniFile file = IniUtilities.LoadIniFileContent(iniFileContent, new IniOptions());

            Assert.IsEmpty(file.Sections[0].Keys[0].Value);
            Assert.IsNull(file.Sections[0].Keys[0].LeadingComment.Text);
            Assert.AreEqual(0, file.Sections[0].Keys[0].LeadingComment.LeftIndentation);
            Assert.IsEmpty(file.Sections[0].Keys[1].Value);
            Assert.IsEmpty(file.Sections[0].Keys[1].LeadingComment.Text);
            Assert.AreEqual(0, file.Sections[0].Keys[1].LeadingComment.LeftIndentation);
            Assert.AreEqual(" ", file.Sections[0].Keys[2].Value);
            Assert.IsNull(file.Sections[0].Keys[2].LeadingComment.Text);
            Assert.AreEqual(0, file.Sections[0].Keys[2].LeadingComment.LeftIndentation);
            Assert.AreEqual(" ", file.Sections[0].Keys[3].Value);
            Assert.IsEmpty(file.Sections[0].Keys[3].LeadingComment.Text);
            Assert.AreEqual(1, file.Sections[0].Keys[3].LeadingComment.LeftIndentation);
            Assert.IsEmpty(file.Sections[0].Keys[4].Value);
            Assert.IsNull(file.Sections[0].Keys[4].LeadingComment.Text);
            Assert.AreEqual(0, file.Sections[0].Keys[4].LeadingComment.LeftIndentation);
            Assert.IsEmpty(file.Sections[0].Keys[5].Value);
            Assert.IsEmpty(file.Sections[0].Keys[5].LeadingComment.Text);
            Assert.AreEqual(0, file.Sections[0].Keys[5].LeadingComment.LeftIndentation);
            Assert.IsEmpty(file.Sections[0].Keys[6].Value);
            Assert.IsNull(file.Sections[0].Keys[6].LeadingComment.Text);
            Assert.AreEqual(0, file.Sections[0].Keys[6].LeadingComment.LeftIndentation);
            Assert.IsEmpty(file.Sections[0].Keys[7].Value);
            Assert.IsEmpty(file.Sections[0].Keys[7].LeadingComment.Text);
            Assert.AreEqual(0, file.Sections[0].Keys[7].LeadingComment.LeftIndentation);
            Assert.IsEmpty(file.Sections[0].Keys[8].Value);
            Assert.AreEqual("Test", file.Sections[0].Keys[8].LeadingComment.Text);
            Assert.AreEqual(0, file.Sections[0].Keys[8].LeadingComment.LeftIndentation);
            Assert.IsEmpty(file.Sections[0].Keys[9].Value);
            Assert.AreEqual("Test", file.Sections[0].Keys[9].LeadingComment.Text);
            Assert.AreEqual(0, file.Sections[0].Keys[9].LeadingComment.LeftIndentation);
        }
Esempio n. 12
0
        public void ReadCommentEdgeCasesTest()
        {
            string iniFileContent = ";" + Environment.NewLine +
                                    ";Section's trailing comment;" + Environment.NewLine +
                                    "[Section]" + Environment.NewLine +
                                    "[Section];" + Environment.NewLine +
                                    "[Section]  ;" + Environment.NewLine +
                                    ";" + Environment.NewLine +
                                    ";Key's trailing comment;" + Environment.NewLine +
                                    "Key = Value  " + Environment.NewLine +
                                    "Key = Value;" + Environment.NewLine +
                                    "Key = Value  ;";

            IniFile file = IniUtilities.LoadIniFileContent(iniFileContent, new IniOptions());

            Assert.AreEqual(Environment.NewLine + "Section's trailing comment;", file.Sections[0].TrailingComment.Text);
            Assert.AreEqual("Section", file.Sections[0].Name);
            Assert.IsNull(file.Sections[0].LeadingComment.Text);
            Assert.AreEqual(0, file.Sections[0].LeadingComment.LeftIndentation);

            Assert.AreEqual("Section", file.Sections[1].Name);
            Assert.IsEmpty(file.Sections[1].LeadingComment.Text);
            Assert.AreEqual(0, file.Sections[1].LeadingComment.LeftIndentation);

            Assert.AreEqual("Section", file.Sections[2].Name);
            Assert.IsEmpty(file.Sections[2].LeadingComment.Text);
            Assert.AreEqual(2, file.Sections[2].LeadingComment.LeftIndentation);

            Assert.AreEqual(Environment.NewLine + "Key's trailing comment;", file.Sections[2].Keys[0].TrailingComment.Text);
            Assert.AreEqual("Key", file.Sections[2].Keys[0].Name);
            Assert.AreEqual("Value", file.Sections[2].Keys[0].Value);
            Assert.IsNull(file.Sections[2].Keys[0].LeadingComment.Text);
            Assert.AreEqual(0, file.Sections[2].Keys[0].LeadingComment.LeftIndentation);

            Assert.AreEqual("Key", file.Sections[2].Keys[1].Name);
            Assert.AreEqual("Value", file.Sections[2].Keys[1].Value);
            Assert.IsEmpty(file.Sections[2].Keys[1].LeadingComment.Text);
            Assert.AreEqual(0, file.Sections[2].Keys[1].LeadingComment.LeftIndentation);

            Assert.AreEqual("Key", file.Sections[2].Keys[2].Name);
            Assert.AreEqual("Value", file.Sections[2].Keys[2].Value);
            Assert.IsEmpty(file.Sections[2].Keys[2].LeadingComment.Text);
            Assert.AreEqual(2, file.Sections[2].Keys[2].LeadingComment.LeftIndentation);
        }
Esempio n. 13
0
        public void ReadQuotedValue()
        {
            string iniFileContent = "key1 = \"Test;Test\"" + Environment.NewLine +
                                    "key2 = \"Test;Test\";" + Environment.NewLine +
                                    "key3 = \"Test;Test" + Environment.NewLine +
                                    "key4 = \"Test;Test;Test\"Test;Test;Test";

            IniFile    file    = IniUtilities.LoadIniFileContent(iniFileContent, new IniOptions());
            IniSection section = file.Sections[0];

            Assert.AreEqual("\"Test;Test\"", file.Sections[0].Keys[0].Value);
            Assert.IsNull(file.Sections[0].Keys[0].LeadingComment.Text);
            Assert.AreEqual("\"Test;Test\"", file.Sections[0].Keys[1].Value);
            Assert.AreEqual(string.Empty, file.Sections[0].Keys[1].LeadingComment.Text);
            Assert.AreEqual("\"Test", file.Sections[0].Keys[2].Value);
            Assert.AreEqual("Test", file.Sections[0].Keys[2].LeadingComment.Text);
            Assert.AreEqual("\"Test;Test;Test\"Test", file.Sections[0].Keys[3].Value);
            Assert.AreEqual("Test;Test", file.Sections[0].Keys[3].LeadingComment.Text);
        }
Esempio n. 14
0
        public void ReadGlobalSectionTest()
        {
            string iniFileContent = ";Trailing comment1" + Environment.NewLine +
                                    "Key1 = Value1" + Environment.NewLine +
                                    ";Trailing comment2" + Environment.NewLine +
                                    "Key2 = Value2";

            IniFile file = IniUtilities.LoadIniFileContent(iniFileContent, new IniOptions());

            Assert.AreEqual(1, file.Sections.Count);
            Assert.AreEqual(IniSection.GlobalSectionName, file.Sections[0].Name);

            Assert.AreEqual(2, file.Sections[0].Keys.Count);
            Assert.AreEqual("Trailing comment1", file.Sections[0].Keys[0].TrailingComment.Text);
            Assert.AreEqual("Key1", file.Sections[0].Keys[0].Name);
            Assert.AreEqual("Value1", file.Sections[0].Keys[0].Value);
            Assert.AreEqual("Trailing comment2", file.Sections[0].Keys[1].TrailingComment.Text);
            Assert.AreEqual("Key2", file.Sections[0].Keys[1].Name);
            Assert.AreEqual("Value2", file.Sections[0].Keys[1].Value);
        }
Esempio n. 15
0
        public void ReadDefaultTest()
        {
            string iniFileContent = ";Section's trailing comment." + Environment.NewLine +
                                    "[Section's name];Section's leading comment." + Environment.NewLine +
                                    ";Key's trailing comment." + Environment.NewLine +
                                    "Key's name = Key's value;Key's leading comment.";

            IniFile file = IniUtilities.LoadIniFileContent(iniFileContent, new IniOptions());

            Assert.AreEqual(1, file.Sections.Count);
            Assert.AreEqual("Section's trailing comment.", file.Sections[0].TrailingComment.Text);
            Assert.AreEqual("Section's name", file.Sections[0].Name);
            Assert.AreEqual("Section's leading comment.", file.Sections[0].LeadingComment.Text);

            Assert.AreEqual(1, file.Sections[0].Keys.Count);
            Assert.AreEqual("Key's trailing comment.", file.Sections[0].Keys[0].TrailingComment.Text);
            Assert.AreEqual("Key's name", file.Sections[0].Keys[0].Name);
            Assert.AreEqual("Key's value", file.Sections[0].Keys[0].Value);
            Assert.AreEqual("Key's leading comment.", file.Sections[0].Keys[0].LeadingComment.Text);
        }