diff --git a/Source/Settings/ConfigurationFileStore.Test.cs b/Source/Settings/ConfigurationFileStore.Test.cs index a096af1..8e6cb67 100644 --- a/Source/Settings/ConfigurationFileStore.Test.cs +++ b/Source/Settings/ConfigurationFileStore.Test.cs @@ -313,7 +313,7 @@ namespace Nuclex.Support.Settings { configurationFile.Set(null, "test", null); Assert.That(configurationFile.Remove(null, "test"), Is.True); - + string value; Assert.That(configurationFile.TryGet(null, "test", out value), Is.False); } @@ -401,6 +401,21 @@ namespace Nuclex.Support.Settings { Assert.That(() => load(fileContents), Throws.Exception); } + /// + /// Verifies that attempting to cast a value to an incompatible data type causes + /// a FormatException to be thrown + /// + [Test] + public void ImpossibleCastCausesFormatException() { + string fileContents = "fail = yesnomaybe"; + ConfigurationFileStore configurationFile = load(fileContents); + + Assert.That( + () => configurationFile.Get(null, "fail"), + Throws.Exception.AssignableTo() + ); + } + /// /// Verifies that configuration files containing duplicate option names can not /// be used with the configuration file store @@ -415,7 +430,7 @@ namespace Nuclex.Support.Settings { public void BooleanLiteralsAreUnderstood(string fileContents, bool expectedValue) { ConfigurationFileStore configurationFile = load(fileContents); - if(expectedValue) { + if(expectedValue) { Assert.That(configurationFile.Get(null, "value"), Is.True); } else { Assert.That(configurationFile.Get(null, "value"), Is.False); diff --git a/Source/Settings/ConfigurationFileStore.cs b/Source/Settings/ConfigurationFileStore.cs index a6136b5..fde8d84 100644 --- a/Source/Settings/ConfigurationFileStore.cs +++ b/Source/Settings/ConfigurationFileStore.cs @@ -24,8 +24,6 @@ using System.Globalization; using System.IO; using System.Text; -using Nuclex.Support.Parsing; - namespace Nuclex.Support.Settings { /// Represents an ini- or cfg-like configuration file diff --git a/Source/Settings/WindowsRegistryStore.Test.cs b/Source/Settings/WindowsRegistryStore.Test.cs index 7f4f2eb..dd85bed 100644 --- a/Source/Settings/WindowsRegistryStore.Test.cs +++ b/Source/Settings/WindowsRegistryStore.Test.cs @@ -86,6 +86,21 @@ namespace Nuclex.Support.Settings { ); } + /// Verifies that new instances of the registry store can be created + [Test] + public void RegistryHivesCanBeOpened() { + Assert.That( + () => { + using( + var store = new WindowsRegistryStore( + RegistryHive.LocalMachine, "SOFTWARE", writable: false + ) + ) { } + }, + Throws.Nothing + ); + } + /// Verifies that booleans can be stored in the registry [Test] public void BooleansCanBeStored() { @@ -138,6 +153,60 @@ namespace Nuclex.Support.Settings { } } + /// Verifies that long integers can be stored in the registry + [Test] + public void LongIntegersCanBeStored() { + using(var context = new TestContext()) { + context.Store.Set(null, "test", long.MaxValue); + Assert.That(context.Store.Get(null, "test"), Is.EqualTo(long.MaxValue)); + + context.Store.Set(null, "test", long.MinValue); + Assert.That(context.Store.Get(null, "test"), Is.EqualTo(long.MinValue)); + } + } + + /// Verifies that string arrays can be stored in the registry + [Test] + public void StringArraysCanBeStored() { + string[] english = new string[] { "one", "two", "three" }; + string[] german = new string[] { "eins", "zwei", "drei" }; + + using(var context = new TestContext()) { + context.Store.Set(null, "test", english); + Assert.That(context.Store.Get(null, "test"), Is.EquivalentTo(english)); + + context.Store.Set(null, "test", german); + Assert.That(context.Store.Get(null, "test"), Is.EquivalentTo(german)); + } + } + + /// Verifies that byte arrays can be stored in the registry + [Test] + public void ByteArraysCanBeStored() { + byte[] ascending = new byte[] { 1, 2, 3 }; + byte[] descending = new byte[] { 9, 8, 7 }; + + using(var context = new TestContext()) { + context.Store.Set(null, "test", ascending); + Assert.That(context.Store.Get(null, "test"), Is.EquivalentTo(ascending)); + + context.Store.Set(null, "test", descending); + Assert.That(context.Store.Get(null, "test"), Is.EquivalentTo(descending)); + } + } + + /// Verifies that strings can be stored in the registry + [Test] + public void ValuesCanBeStoredInCategories() { + using(var context = new TestContext()) { + context.Store.Set("main", "test", "hello world"); + + string value; + Assert.That(context.Store.TryGet(null, "test", out value), Is.False); + Assert.That(context.Store.Get("main", "test"), Is.EqualTo("hello world")); + } + } + /// Verifies that the subkeys of a registry key can be enumerated [Test] public void CategoriesCanBeEnumerated() { @@ -252,6 +321,34 @@ namespace Nuclex.Support.Settings { } } + /// + /// Verifies that the store identifies the types of values stored in + /// a registry when they are enumerated + /// + [Test] + public void ValueTypesAreIdentifiedWhenEnumerating() { + Type[] types = new Type[] { + typeof(int), + typeof(long), + typeof(byte[]), + typeof(string), + typeof(string[]) + }; + using(var context = new TestContext()) { + context.Store.Set(null, "0", 123); + context.Store.Set(null, "1", 456L); + context.Store.Set(null, "2", new byte[] { 7, 8, 9 }); + context.Store.Set(null, "3", "text"); + context.Store.Set(null, "4", new string[] { "many", "words" }); + + var optionInfos = new List(context.Store.EnumerateOptions()); + for(int index = 0; index < optionInfos.Count; ++index) { + int typeIndex = int.Parse(optionInfos[index].Name); + Assert.That(optionInfos[index].OptionType, Is.EqualTo(types[typeIndex])); + } + } + } + } } // namespace Nuclex.Support.Settings diff --git a/Source/Settings/WindowsRegistryStore.cs b/Source/Settings/WindowsRegistryStore.cs index 86204df..5720fa9 100644 --- a/Source/Settings/WindowsRegistryStore.cs +++ b/Source/Settings/WindowsRegistryStore.cs @@ -26,8 +26,6 @@ using System.Globalization; using Microsoft.Win32; -using Nuclex.Support.Parsing; - namespace Nuclex.Support.Settings { /// Stores settings in the registry on Windows operating systems @@ -242,7 +240,7 @@ namespace Nuclex.Support.Settings { case RegistryValueKind.QWord: { return typeof(long); } case RegistryValueKind.MultiString: { return typeof(string[]); } case RegistryValueKind.ExpandString: - case RegistryValueKind.String: { return typeof(string); } + case RegistryValueKind.String: case RegistryValueKind.Unknown: case RegistryValueKind.None: default: { return typeof(string); }