From 7bde463b2ae61be7689ac0e852ae04fef22f147e Mon Sep 17 00:00:00 2001 From: Markus Ewald Date: Sun, 20 Jul 2014 23:18:44 +0000 Subject: [PATCH] Changing and adding options works perfectly now git-svn-id: file:///srv/devel/repo-conversion/nusu@305 d2e56fa2-650e-0410-a79f-9358c0239efd --- .../ConfigurationFileStore.Parsing.cs | 4 +- .../Settings/ConfigurationFileStore.Test.cs | 65 ++++++++++++++++--- Source/Settings/ConfigurationFileStore.cs | 14 ++-- 3 files changed, 67 insertions(+), 16 deletions(-) diff --git a/Source/Settings/ConfigurationFileStore.Parsing.cs b/Source/Settings/ConfigurationFileStore.Parsing.cs index 6b15417..491e3fa 100644 --- a/Source/Settings/ConfigurationFileStore.Parsing.cs +++ b/Source/Settings/ConfigurationFileStore.Parsing.cs @@ -19,11 +19,11 @@ License along with this library #endregion using System; +using System.Collections.Generic; using System.Diagnostics; using System.IO; using Nuclex.Support.Parsing; -using System.Collections.Generic; namespace Nuclex.Support.Settings { @@ -160,7 +160,7 @@ namespace Nuclex.Support.Settings { // We have enough information to know that this is an assignment of some kind Option option = new Option() { - LineIndex = state.Category.Lines.Count - 1, + LineIndex = state.Category.Lines.Count, OptionName = new StringSegment( line, firstCharacterIndex, nameEndIndex - firstCharacterIndex + 1 ) diff --git a/Source/Settings/ConfigurationFileStore.Test.cs b/Source/Settings/ConfigurationFileStore.Test.cs index ceab195..9a0c39d 100644 --- a/Source/Settings/ConfigurationFileStore.Test.cs +++ b/Source/Settings/ConfigurationFileStore.Test.cs @@ -23,6 +23,7 @@ License along with this library using System; using System.Collections.Generic; using System.IO; +using System.Text; using NUnit.Framework; @@ -32,15 +33,6 @@ namespace Nuclex.Support.Settings { [TestFixture] internal class ConfigurationFileStoreTest { - /// Loads a configuration file from a string - /// Contents of the configuration file - /// The configuration file loaded from the string - private static ConfigurationFileStore load(string fileContents) { - using(var reader = new StringReader(fileContents)) { - return ConfigurationFileStore.Parse(reader); - } - } - /// /// Verifies that loading an empty file doesn't lead to an exception /// @@ -209,6 +201,23 @@ namespace Nuclex.Support.Settings { ); } + /// + /// Verifies that text placed after the closing quote will also be part of + /// an option's value + /// + [Test] + public void OptionValuesCanBeChanged() { + string fileContents = "test = 123 ; comment"; + ConfigurationFileStore configurationFile = load(fileContents); + + configurationFile.Set(null, "test", "hello world"); + + Assert.That( + save(configurationFile), + Contains.Substring("hello world").And.ContainsSubstring("comment") + ); + } + /// /// Verifies that options can be added to the configuration file /// @@ -220,6 +229,44 @@ namespace Nuclex.Support.Settings { Assert.That(configurationFile.Get(null, "test"), Is.EqualTo("123")); } + /// + /// Verifies that options can be added to the configuration file + /// + [Test] + public void CategoriesCanBeAdded() { + var configurationFile = new ConfigurationFileStore(); + + configurationFile.Set("general", "sol", "42"); + + Assert.That( + configurationFile.EnumerateCategories(), Is.EquivalentTo(new string[] { "general" }) + ); + Assert.That(save(configurationFile), Contains.Substring("[general]")); + } + + /// Loads a configuration file from a string + /// Contents of the configuration file + /// The configuration file loaded from the string + private static ConfigurationFileStore load(string fileContents) { + using(var reader = new StringReader(fileContents)) { + return ConfigurationFileStore.Parse(reader); + } + } + + /// Saves a configuration file into a string + /// Configuration file that will be saved + /// Contents of the configuration file + private static string save(ConfigurationFileStore configurationFile) { + var builder = new StringBuilder(); + + using(var writer = new StringWriter(builder)) { + configurationFile.Save(writer); + writer.Flush(); + } + + return builder.ToString(); + } + } } // namespace Nuclex.Support.Settings diff --git a/Source/Settings/ConfigurationFileStore.cs b/Source/Settings/ConfigurationFileStore.cs index 5bdb88f..d78c121 100644 --- a/Source/Settings/ConfigurationFileStore.cs +++ b/Source/Settings/ConfigurationFileStore.cs @@ -20,6 +20,7 @@ License along with this library using System; using System.Collections.Generic; +using System.Globalization; using System.IO; using System.Text; @@ -176,17 +177,20 @@ namespace Nuclex.Support.Settings { optionMightExist = false; } + string valueAsString = (string)Convert.ChangeType( + value, typeof(string), CultureInfo.InvariantCulture + ); Option targetOption; if(optionMightExist) { if(targetCategory.OptionLookup.TryGetValue(optionName, out targetOption)) { - return; + changeOption(targetCategory, targetOption, valueAsString); + } else { + createOption(targetCategory, optionName, valueAsString); } + } else { + createOption(targetCategory, optionName, valueAsString); } - - // Append at bottom of category - - } /// Removes the option with the specified name