Exemplo n.º 1
0
        public void RemoveWorks()
        {
            EditorConfigFile editorConfigFile = PrepareTest("remove", out var file, out var workingFile);

            const int TestSection = 0;
            var       sectionName = editorConfigFile.Sections[TestSection].Name;

            // Find aftercomment property line
            editorConfigFile.TryGetProperty("aftercomment", editorConfigFile.Sections[TestSection], out var afterProp).Should().BeTrue();
            var lineNumber = afterProp !.LineNumber;

            // Remove beforecommment
            editorConfigFile.TryGetProperty("beforecomment", editorConfigFile.Sections[TestSection], out var prop).Should().BeTrue();
            using (var editContext = editorConfigFile.Edit(new EditorConfigFileOptions {
                EndSectionWithBlankLineOrComment = false
            }))
            {
                editContext.Sections[sectionName].Remove(prop !).Should().BeTrue();

                editContext.SaveChanges();
            }

            editorConfigFile.TryGetProperty("beforecomment", editorConfigFile.Sections[TestSection], out _).Should().BeFalse();

            // Confirm aftercomment has moved up a line
            editorConfigFile.TryGetProperty("aftercomment", editorConfigFile.Sections[TestSection], out var updatedProperty).Should().BeTrue();
            updatedProperty !.LineNumber.Should().Be(lineNumber - 1);

            // Confirm the file is one line shorter
            var fileLength        = File.ReadAllLines(file).Length;
            var workingFileLength = File.ReadAllLines(workingFile).Length;

            workingFileLength.Should().Be(fileLength - 1);
        }
Exemplo n.º 2
0
        public void AddWorks()
        {
            EditorConfigFile editorConfigFile = PrepareTest("add", out var file, out var workingFile);

            const int    TestSection       = 0;
            const string TestPropertyKey   = "testProperty";
            const string TestPropertyValue = "testValue";

            var section     = editorConfigFile.Sections[TestSection];
            var sectionName = section.Name;

            // Find aftercomment property line
            editorConfigFile.TryGetProperty("aftercomment", section, out var prop).Should().BeTrue();
            var lineNumber = prop !.LineNumber;

            var iniProperty = new IniPropertyData(TestPropertyKey, TestPropertyValue);

            using (var editContext = editorConfigFile.Edit(new EditorConfigFileOptions {
                EndSectionWithBlankLineOrComment = false
            }))
            {
                editContext.Sections[sectionName].Add(iniProperty);

                editContext.SaveChanges();
            }

            editorConfigFile.TryGetProperty(TestPropertyKey, editorConfigFile.Sections[TestSection], out var updatedProperty).Should().BeTrue();
            updatedProperty !.Data.Value.Should().Be(TestPropertyValue);

            // Use the original value rather than re-reading the property. This ensures that the file is otherwise unchanged
            updatedProperty !.LineNumber.Should().Be(lineNumber + 1);

            // Confirm the file is one line longer
            var fileLength        = File.ReadAllLines(file).Length;
            var workingFileLength = File.ReadAllLines(workingFile).Length;

            workingFileLength.Should().Be(fileLength + 1);
        }