#region Apache License 2.0 /* Nuclex .NET Framework Copyright (C) 2002-2024 Markus Ewald / Nuclex Development Labs Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ #endregion // Apache License 2.0 #if UNITTEST using System; using System.Collections.Generic; using NUnit.Framework; namespace Nuclex.Support.Settings { /// Unit tests for the memory settings store [TestFixture] internal class MemoryStoreTest { /// Verifies that constructed a memory store throws an exception [Test] public void CanBeCreated() { Assert.That(() => new MemoryStore(), Throws.Nothing); } /// /// Verifies that it's possible to enumerate the options in a non-existing category /// [Test] public void NonExistingCategoriesCanBeEnumerated() { var memoryStore = new MemoryStore(); Assert.That(memoryStore.EnumerateOptions("doesn't exist"), Is.Empty); } /// /// Verifies that accessing an option that doesn't exist throws an exception /// [Test] public void AccessingNonExistingOptionThrowsException() { var memoryStore = new MemoryStore(); Assert.That( () => memoryStore.Get(null, "doesn't exist"), Throws.Exception.AssignableTo() ); } /// /// Verifies that accessing a category that doesn't exist throws an exception /// [Test] public void AccessingNonExistingCategoryThrowsException() { var memoryStore = new MemoryStore(); memoryStore.Set(null, "test", "123"); Assert.That( () => memoryStore.Get("doesn't exist", "test"), Throws.Exception.AssignableTo() ); } /// /// Verifies that settings can be stored in the memory store /// [Test] public void SettingsCanBeAssignedAndRetrieved() { var memoryStore = new MemoryStore(); memoryStore.Set("general", "sol", "42"); Assert.That(memoryStore.Get("general", "sol"), Is.EqualTo("42")); } /// /// Verifies that it's possible to remove options from the memory store /// [Test] public void OptionsCanBeRemoved() { var memoryStore = new MemoryStore(); memoryStore.Set(null, "test", null); Assert.That(memoryStore.Remove(null, "test"), Is.True); string value; Assert.That(memoryStore.TryGet(null, "test", out value), Is.False); } /// /// Verifies that it's not an error to remove an option from a non-existing category /// [Test] public void CanRemoveOptionFromNonExistingCategory() { var memoryStore = new MemoryStore(); Assert.That(memoryStore.Remove("nothing", "first"), Is.False); } /// /// Verifies that it's not an error to remove a non-existing option /// [Test] public void CanRemoveNonExistingOption() { var memoryStore = new MemoryStore(); Assert.That(memoryStore.Remove(null, "first"), Is.False); } /// /// Verifies that the root category is not part of the enumerated categories /// [Test] public void RootCategoryIsNotEnumerated() { var memoryStore = new MemoryStore(); Assert.That(memoryStore.EnumerateCategories(), Is.Empty); } /// /// Verifies that the root category is not part of the enumerated categories /// [Test] public void OptionsInRootCategoryCanBeEnumerated() { var memoryStore = new MemoryStore(); string[] optionNames = new string[] { "first", "second" }; memoryStore.Set(null, optionNames[0], 1); memoryStore.Set(null, optionNames[1], 2); var optionInfos = new List(memoryStore.EnumerateOptions()); Assert.That(optionInfos.Count, Is.EqualTo(2)); var enumeratedOptionNames = new List() { optionInfos[0].Name, optionInfos[1].Name }; Assert.That(enumeratedOptionNames, Is.EquivalentTo(optionNames)); } /// /// Verifies that the root category is not part of the enumerated categories /// [Test] public void CategoriesCanBeCreated() { var memoryStore = new MemoryStore(); memoryStore.Set(null, "not", "used"); memoryStore.Set("test", "message", "hello world"); Assert.That(memoryStore.EnumerateCategories(), Is.EquivalentTo(new string[] { "test" })); } } } // namespace Nuclex.Support.Settings #endif // UNITTEST