Пример #1
0
        public void RemoveSection_ShouldRemoveSectionIfFound()
        {
            var doc = IniDocument
                      .Builder()
                      .WithHeader(Comment.Builder().AppendLine("header comment").Build())
                      .WithFooter(Comment.Builder().AppendLine("footer comment").Build())
                      .AppendSection(Section
                                     .Builder()
                                     .WithComment(Comment
                                                  .Builder()
                                                  .AppendLine("section comment")
                                                  .Build())
                                     .WithName("section")
                                     .AppendProperty(Property
                                                     .Builder()
                                                     .WithComment(Comment
                                                                  .Builder()
                                                                  .AppendLine("property comment")
                                                                  .Build())
                                                     .WithKey("foo")
                                                     .Build())
                                     .AppendProperty(Property
                                                     .Builder()
                                                     .IsEnable(false)
                                                     .WithKey("foo")
                                                     .Build())
                                     .Build())
                      .AppendSection(Section
                                     .Builder()
                                     .IsEnable(false)
                                     .WithName("Things")
                                     .AppendProperty(Property
                                                     .Builder()
                                                     .WithKey("foo")
                                                     .Build())
                                     .AppendProperty(Property
                                                     .Builder()
                                                     .WithKey("fizz")
                                                     .WithValue("buzz")
                                                     .Build())
                                     .Build())
                      .Build();

            var actual = IniDocument
                         .Builder(doc)
                         .RemoveSection("Things")
                         .RemoveSection(" ")
                         .RemoveSection("")
                         .RemoveSection(null)
                         .Build();

            Assert.Equal(1, actual.SectionCount);
            Assert.Equal("section", actual.Sections()[0].Name);
        }
Пример #2
0
        public void Sections_ShouldReturnResultBasedOnStatusFilter()
        {
            var doc = IniDocument
                      .Builder()
                      .WithHeader(Comment.Builder().AppendLine("header comment").Build())
                      .WithFooter(Comment.Builder().AppendLine("footer comment").Build())
                      .AppendSection(Section
                                     .Builder()
                                     .WithComment(Comment
                                                  .Builder()
                                                  .AppendLine("section comment")
                                                  .Build())
                                     .WithName("section")
                                     .AppendProperty(Property
                                                     .Builder()
                                                     .WithComment(Comment
                                                                  .Builder()
                                                                  .AppendLine("property comment")
                                                                  .Build())
                                                     .WithKey("foo")
                                                     .Build())
                                     .AppendProperty(Property
                                                     .Builder()
                                                     .IsEnable(false)
                                                     .WithKey("foo")
                                                     .Build())
                                     .Build())
                      .AppendSection(Section
                                     .Builder()
                                     .IsEnable(false)
                                     .WithName("Things")
                                     .AppendProperty(Property
                                                     .Builder()
                                                     .WithKey("foo")
                                                     .Build())
                                     .AppendProperty(Property
                                                     .Builder()
                                                     .WithKey("fizz")
                                                     .WithValue("buzz")
                                                     .Build())
                                     .Build())
                      .Build();

            Assert.False(doc.IsEmpty);
            Assert.Equal(2, doc.Sections().Count);
            Assert.Equal(2, doc.Sections(Status.All).Count);
            Assert.Equal(2, doc.Sections((Status)999).Count);
            Assert.Equal(1, doc.Sections(Status.Enabled).Count);
            Assert.Equal(1, doc.Sections(Status.Disabled).Count);
        }
Пример #3
0
        public void CommentBuilder_FluentApi_ShouldCreateComment()
        {
            var actual = Comment
                         .Builder("Comment line 1", "Comment line 2", "Comment line 3")
                         .Build();

            var expected = new StringBuilder()
                           .AppendLine("Comment line 1".AsComment())
                           .AppendLine("Comment line 2".AsComment())
                           .AppendLine("Comment line 3".AsComment())
                           .ToString().Trim();

            Assert.Equal(expected, actual.ToString());
        }
Пример #4
0
        public void ShouldReturn1LineComment()
        {
            var comment = Comment
                          .Builder()
                          .AppendLine("Comment line 0")
                          .Build();

            var expected = new StringBuilder()
                           .AppendLine("Comment line 0".AsComment())
                           .ToString().Trim();

            Assert.Equal(1, comment.LineCount);
            Assert.Equal(expected, comment.ToString());
        }
Пример #5
0
        public void WithComment_ShouldReplaceTheCommentWithTheGivenOne()
        {
            var property = Property
                           .Builder()
                           .WithKey("foo")
                           .WithComment(Comment.Builder().AppendLine("line1").Build())
                           .Build();

            Assert.Equal("line1".AsComment(), property.Comment.ToString());

            property = Property
                       .Builder(property)
                       .WithComment(Comment.Builder().AppendLine("updated line1").AppendLine("line2").Build())
                       .Build();

            var expected = new StringBuilder()
                           .AppendLine("updated line1".AsComment())
                           .AppendLine("line2".AsComment())
                           .ToString().Trim();

            Assert.Equal(expected, property.Comment.ToString());

            var emptyPropComment = Property
                                   .Builder(property)
                                   .WithComment(null)
                                   .Build();

            Assert.Equal("", emptyPropComment.Comment.ToString());
            Assert.True(emptyPropComment.Comment.IsEmpty);

            var emptyPropComment2 = Property
                                    .Builder(property)
                                    .WithComment(Comment.Builder().Build())
                                    .Build();

            Assert.Equal("", emptyPropComment2.Comment.ToString());
            Assert.True(emptyPropComment2.Comment.IsEmpty);
        }
Пример #6
0
        public void ShouldReturnCommentWithAllValidLinesFromTheGivenText()
        {
            var text = new StringBuilder()
                       .AppendLine("Comment line 0")
                       .AppendLine()
                       .AppendLine("#;#Comment line 1")
                       .AppendLine(" Comment line 2;;")
                       .ToString().Trim();

            var comment = Comment
                          .Builder()
                          .Parse(text)
                          .Build();

            var expected = new StringBuilder()
                           .AppendLine("Comment line 0".AsComment())
                           .AppendLine("Comment line 1".AsComment())
                           .AppendLine("Comment line 2".AsComment())
                           .ToString().Trim();

            Assert.Equal(3, comment.LineCount);
            Assert.Equal(expected, comment.ToString());
        }
Пример #7
0
        public void ToString_ShouldReturnResultAccordingToTheFiltersUsed()
        {
            var property = Property
                           .Builder()
                           .WithKey("foo")
                           .WithValue("bar")
                           .WithComment(Comment
                                        .Builder()
                                        .AppendLine("prop comment")
                                        .Build())
                           .IsEnable(false)
                           .Build();

            var expected = new StringBuilder()
                           .AppendLine("prop comment".AsComment())
                           .AppendLine("foo=bar".AsDisabled())
                           .ToString().Trim();

            Assert.Equal(expected, property.ToString());
            Assert.Equal(expected, property.ToString(Filters.None));
            Assert.Equal("foo=bar".AsDisabled(), property.ToString(Filters.TrimComment));
            Assert.Equal("", property.ToString(Filters.TrimDisabled));
        }
Пример #8
0
        public void SectionBuilderClone_ShouldCreateADeepCopyOfTheGivenSection()
        {
            var section = Section
                          .Builder()
                          .WithComment(Comment.Builder().AppendLine("original section").Build())
                          .WithName("Person")
                          .AppendProperty(Property
                                          .Builder()
                                          .WithKey("name")
                                          .WithValue("joy")
                                          .Build())
                          .Build();

            section = Section
                      .Builder(section)
                      .AppendProperty(Property
                                      .Builder()
                                      .WithComment(Comment.Builder().AppendLine("person age").Build())
                                      .WithKey("age")
                                      .WithValue("17")
                                      .Build())
                      .Build();
            Assert.Equal(2, section.PropertyCount);
        }
Пример #9
0
        public void ToString_ResultsShouldBeBasedOnFilter()
        {
            var section = Section
                          .Builder()
                          .WithComment(Comment.Builder().AppendLine("original section").Build())
                          .WithName("Person")
                          .AppendProperty(Property
                                          .Builder()
                                          .WithComment(Comment.Builder().AppendLine("person name").Build())
                                          .WithKey("name")
                                          .WithValue("joy")
                                          .Build())
                          .AppendProperty(Property
                                          .Builder()
                                          .IsEnable(false)
                                          .WithComment(Comment.Builder().AppendLine("person age").Build())
                                          .WithKey("age")
                                          .WithValue("17")
                                          .Build())
                          .Build();

            var expected = new StringBuilder()
                           .AppendLine("original section".AsComment())
                           .AppendLine("Person".AsSection())
                           .AppendLine("person name".AsComment())
                           .AppendLine("name=joy")
                           .AppendLine("person age".AsComment())
                           .AppendLine("age=17".AsDisabled())
                           .ToString().Trim();

            Assert.Equal(expected, section.ToString());
            Assert.Equal(expected, section.ToString(Filters.None));

            expected = new StringBuilder()
                       .AppendLine("Person".AsSection())
                       .AppendLine("name=joy")
                       .AppendLine("age=17".AsDisabled())
                       .ToString().Trim();

            Assert.Equal(expected, section.ToString(Filters.TrimComment));

            expected = new StringBuilder()
                       .AppendLine("original section".AsComment())
                       .AppendLine("Person".AsSection())
                       .AppendLine("person name".AsComment())
                       .AppendLine("name=joy")
                       .ToString().Trim();

            Assert.Equal(expected, section.ToString(Filters.TrimDisabled));

            expected = new StringBuilder()
                       .AppendLine("original section".AsComment())
                       .AppendLine("Person".AsSection())
                       .AppendLine("person name".AsComment())
                       .AppendLine("name=joy")
                       .ToString().Trim();

            Assert.Equal(expected, section.ToString(Filters.TrimDisabled));

            expected = new StringBuilder()
                       .AppendLine("Person".AsSection())
                       .AppendLine("name=joy")
                       .ToString().Trim();

            Assert.Equal(expected, section.ToString(Filters.TrimCommentDisabled));

            section = Section
                      .Builder(section)
                      .IsEnable(false)
                      .Build();
            Assert.Equal("", section.ToString(Filters.TrimDisabled));

            expected = new StringBuilder()
                       .AppendLine("Person".AsSection().AsDisabled())
                       .AppendLine("name=joy".AsDisabled())
                       .AppendLine("age=17".AsDisabled())
                       .ToString().Trim();
            Assert.Equal(expected, section.ToString(Filters.TrimComment));
        }
Пример #10
0
        public void RemovePropertyAt_ShouldRemoveTheGivenPropertyFromTheGivenSectionIfFound()
        {
            var doc = IniDocument
                      .Builder()
                      .WithHeader(Comment.Builder().AppendLine("header comment").Build())
                      .WithFooter(Comment.Builder().AppendLine("footer comment").Build())
                      .AppendSection(Section
                                     .Builder()
                                     .WithComment(Comment
                                                  .Builder()
                                                  .AppendLine("section comment")
                                                  .Build())
                                     .WithName("section")
                                     .AppendProperty(Property
                                                     .Builder()
                                                     .WithComment(Comment
                                                                  .Builder()
                                                                  .AppendLine("property comment")
                                                                  .Build())
                                                     .WithKey("foo")
                                                     .WithValue("bar")
                                                     .Build())
                                     .AppendProperty(Property
                                                     .Builder()
                                                     .IsEnable(false)
                                                     .WithKey("foo")
                                                     .WithValue("bared")
                                                     .Build())
                                     .Build())
                      .AppendSection(Section
                                     .Builder()
                                     .IsEnable(false)
                                     .WithName("Things")
                                     .AppendProperty(Property
                                                     .Builder()
                                                     .WithKey("foo")
                                                     .Build())
                                     .AppendProperty(Property
                                                     .Builder()
                                                     .WithKey("fizz")
                                                     .WithValue("buzz")
                                                     .Build())
                                     .Build())
                      .Build();

            Assert.Equal(2, doc.Sections()[0].PropertyCount);
            Assert.Equal(2, doc.Sections()[1].PropertyCount);

            var actual = IniDocument
                         .Builder(doc)
                         .RemovePropertyAt(null, -1)
                         .RemovePropertyAt("section", -999)
                         .RemovePropertyAt(null, 999)
                         .RemovePropertyAt("Things", 5)
                         .RemovePropertyAt("section", 1)
                         .RemovePropertyAt("Things", 0)
                         .Build();

            Assert.Equal(1, actual.Sections()[0].PropertyCount);
            Assert.Equal(1, actual.Sections()[1].PropertyCount);
            Assert.Collection(actual.Sections()[0].Properties(),
                              i =>
            {
                Assert.Equal("foo", i.Key);
                Assert.Equal("bar", i.Value);
            });
            Assert.Collection(actual.Sections()[1].Properties(),
                              i =>
            {
                Assert.Equal("fizz", i.Key);
                Assert.Equal("buzz", i.Value);
            });
        }