Changing and adding options works perfectly now

git-svn-id: file:///srv/devel/repo-conversion/nusu@305 d2e56fa2-650e-0410-a79f-9358c0239efd
This commit is contained in:
Markus Ewald 2014-07-20 23:18:44 +00:00
parent 73a17b7a3e
commit 7bde463b2a
3 changed files with 67 additions and 16 deletions

View File

@ -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
)

View File

@ -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 {
/// <summary>Loads a configuration file from a string</summary>
/// <param name="fileContents">Contents of the configuration file</param>
/// <returns>The configuration file loaded from the string</returns>
private static ConfigurationFileStore load(string fileContents) {
using(var reader = new StringReader(fileContents)) {
return ConfigurationFileStore.Parse(reader);
}
}
/// <summary>
/// Verifies that loading an empty file doesn't lead to an exception
/// </summary>
@ -209,6 +201,23 @@ namespace Nuclex.Support.Settings {
);
}
/// <summary>
/// Verifies that text placed after the closing quote will also be part of
/// an option's value
/// </summary>
[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")
);
}
/// <summary>
/// Verifies that options can be added to the configuration file
/// </summary>
@ -220,6 +229,44 @@ namespace Nuclex.Support.Settings {
Assert.That(configurationFile.Get<string>(null, "test"), Is.EqualTo("123"));
}
/// <summary>
/// Verifies that options can be added to the configuration file
/// </summary>
[Test]
public void CategoriesCanBeAdded() {
var configurationFile = new ConfigurationFileStore();
configurationFile.Set<string>("general", "sol", "42");
Assert.That(
configurationFile.EnumerateCategories(), Is.EquivalentTo(new string[] { "general" })
);
Assert.That(save(configurationFile), Contains.Substring("[general]"));
}
/// <summary>Loads a configuration file from a string</summary>
/// <param name="fileContents">Contents of the configuration file</param>
/// <returns>The configuration file loaded from the string</returns>
private static ConfigurationFileStore load(string fileContents) {
using(var reader = new StringReader(fileContents)) {
return ConfigurationFileStore.Parse(reader);
}
}
/// <summary>Saves a configuration file into a string</summary>
/// <param name="configurationFile">Configuration file that will be saved</param>
/// <returns>Contents of the configuration file</returns>
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

View File

@ -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
}
/// <summary>Removes the option with the specified name</summary>