Achieved full test coverage for the configuration file store and the windows registry settings store

git-svn-id: file:///srv/devel/repo-conversion/nusu@320 d2e56fa2-650e-0410-a79f-9358c0239efd
This commit is contained in:
Markus Ewald 2014-07-22 20:02:33 +00:00
parent 58504f4a8a
commit 40492c8b21
4 changed files with 115 additions and 7 deletions

View File

@ -401,6 +401,21 @@ namespace Nuclex.Support.Settings {
Assert.That(() => load(fileContents), Throws.Exception); Assert.That(() => load(fileContents), Throws.Exception);
} }
/// <summary>
/// Verifies that attempting to cast a value to an incompatible data type causes
/// a FormatException to be thrown
/// </summary>
[Test]
public void ImpossibleCastCausesFormatException() {
string fileContents = "fail = yesnomaybe";
ConfigurationFileStore configurationFile = load(fileContents);
Assert.That(
() => configurationFile.Get<bool>(null, "fail"),
Throws.Exception.AssignableTo<FormatException>()
);
}
/// <summary> /// <summary>
/// Verifies that configuration files containing duplicate option names can not /// Verifies that configuration files containing duplicate option names can not
/// be used with the configuration file store /// be used with the configuration file store

View File

@ -24,8 +24,6 @@ using System.Globalization;
using System.IO; using System.IO;
using System.Text; using System.Text;
using Nuclex.Support.Parsing;
namespace Nuclex.Support.Settings { namespace Nuclex.Support.Settings {
/// <summary>Represents an ini- or cfg-like configuration file</summary> /// <summary>Represents an ini- or cfg-like configuration file</summary>

View File

@ -86,6 +86,21 @@ namespace Nuclex.Support.Settings {
); );
} }
/// <summary>Verifies that new instances of the registry store can be created</summary>
[Test]
public void RegistryHivesCanBeOpened() {
Assert.That(
() => {
using(
var store = new WindowsRegistryStore(
RegistryHive.LocalMachine, "SOFTWARE", writable: false
)
) { }
},
Throws.Nothing
);
}
/// <summary>Verifies that booleans can be stored in the registry</summary> /// <summary>Verifies that booleans can be stored in the registry</summary>
[Test] [Test]
public void BooleansCanBeStored() { public void BooleansCanBeStored() {
@ -138,6 +153,60 @@ namespace Nuclex.Support.Settings {
} }
} }
/// <summary>Verifies that long integers can be stored in the registry</summary>
[Test]
public void LongIntegersCanBeStored() {
using(var context = new TestContext()) {
context.Store.Set(null, "test", long.MaxValue);
Assert.That(context.Store.Get<long>(null, "test"), Is.EqualTo(long.MaxValue));
context.Store.Set(null, "test", long.MinValue);
Assert.That(context.Store.Get<long>(null, "test"), Is.EqualTo(long.MinValue));
}
}
/// <summary>Verifies that string arrays can be stored in the registry</summary>
[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<string[]>(null, "test"), Is.EquivalentTo(english));
context.Store.Set(null, "test", german);
Assert.That(context.Store.Get<string[]>(null, "test"), Is.EquivalentTo(german));
}
}
/// <summary>Verifies that byte arrays can be stored in the registry</summary>
[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<byte[]>(null, "test"), Is.EquivalentTo(ascending));
context.Store.Set(null, "test", descending);
Assert.That(context.Store.Get<byte[]>(null, "test"), Is.EquivalentTo(descending));
}
}
/// <summary>Verifies that strings can be stored in the registry</summary>
[Test]
public void ValuesCanBeStoredInCategories() {
using(var context = new TestContext()) {
context.Store.Set("main", "test", "hello world");
string value;
Assert.That(context.Store.TryGet<string>(null, "test", out value), Is.False);
Assert.That(context.Store.Get<string>("main", "test"), Is.EqualTo("hello world"));
}
}
/// <summary>Verifies that the subkeys of a registry key can be enumerated</summary> /// <summary>Verifies that the subkeys of a registry key can be enumerated</summary>
[Test] [Test]
public void CategoriesCanBeEnumerated() { public void CategoriesCanBeEnumerated() {
@ -252,6 +321,34 @@ namespace Nuclex.Support.Settings {
} }
} }
/// <summary>
/// Verifies that the store identifies the types of values stored in
/// a registry when they are enumerated
/// </summary>
[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<int>(null, "0", 123);
context.Store.Set<long>(null, "1", 456L);
context.Store.Set<byte[]>(null, "2", new byte[] { 7, 8, 9 });
context.Store.Set<string>(null, "3", "text");
context.Store.Set<string[]>(null, "4", new string[] { "many", "words" });
var optionInfos = new List<OptionInfo>(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 } // namespace Nuclex.Support.Settings

View File

@ -26,8 +26,6 @@ using System.Globalization;
using Microsoft.Win32; using Microsoft.Win32;
using Nuclex.Support.Parsing;
namespace Nuclex.Support.Settings { namespace Nuclex.Support.Settings {
/// <summary>Stores settings in the registry on Windows operating systems</summary> /// <summary>Stores settings in the registry on Windows operating systems</summary>
@ -242,7 +240,7 @@ namespace Nuclex.Support.Settings {
case RegistryValueKind.QWord: { return typeof(long); } case RegistryValueKind.QWord: { return typeof(long); }
case RegistryValueKind.MultiString: { return typeof(string[]); } case RegistryValueKind.MultiString: { return typeof(string[]); }
case RegistryValueKind.ExpandString: case RegistryValueKind.ExpandString:
case RegistryValueKind.String: { return typeof(string); } case RegistryValueKind.String:
case RegistryValueKind.Unknown: case RegistryValueKind.Unknown:
case RegistryValueKind.None: case RegistryValueKind.None:
default: { return typeof(string); } default: { return typeof(string); }