using System; using UnitTestSharp; namespace Glass.UnitTests { public class InMemoryKeyValueStoreTests : TestFixture { public class GetKeysTests : TestFixture { public void Empty_GivesNothing() { CheckEqual(0, new InMemoryKeyValueStore().GetKeys().Count); } public void GivesEveryKeySet() { var store = new InMemoryKeyValueStore(); store.Set("a", "1"); store.Set("b", "2"); CheckEqual(2, store.GetKeys().Count); CheckTrue(((System.Collections.Generic.List)store.GetKeys()).Contains("a")); CheckTrue(((System.Collections.Generic.List)store.GetKeys()).Contains("b")); } public void ListIsACopy() { var store = new InMemoryKeyValueStore(); store.Set("a", "1"); var keys = (System.Collections.Generic.List)store.GetKeys(); keys.Clear(); CheckEqual(1, store.GetKeys().Count); } } public class GetTests : TestFixture { public void SetKey_GivesItsValue() { var store = new InMemoryKeyValueStore(); store.Set("a", "1"); CheckEqual("1", store.Get("a")); } public void MissingKey_GivesNull() { CheckNull(new InMemoryKeyValueStore().Get("a")); } public void Null_Throws() { var store = new InMemoryKeyValueStore(); CheckThrow(typeof(ArgumentNullException)); store.Get(null); } } public class SetTests : TestFixture { public void ExistingKey_IsReplaced() { var store = new InMemoryKeyValueStore(); store.Set("a", "1"); store.Set("a", "2"); CheckEqual("2", store.Get("a")); CheckEqual(1, store.GetKeys().Count); } public void EmptyValue_IsKept() { var store = new InMemoryKeyValueStore(); store.Set("a", ""); CheckEqual("", store.Get("a")); } public void NullKey_Throws() { var store = new InMemoryKeyValueStore(); CheckThrow(typeof(ArgumentNullException)); store.Set(null, "1"); } public void NullValue_Throws() { var store = new InMemoryKeyValueStore(); CheckThrow(typeof(ArgumentNullException)); store.Set("a", null); } } public class RemoveTests : TestFixture { public void ExistingKey_Forgets() { var store = new InMemoryKeyValueStore(); store.Set("a", "1"); store.Remove("a"); CheckNull(store.Get("a")); CheckEqual(0, store.GetKeys().Count); } public void MissingKey_DoesNothing() { var store = new InMemoryKeyValueStore(); store.Set("a", "1"); store.Remove("b"); CheckEqual(1, store.GetKeys().Count); } public void Null_Throws() { var store = new InMemoryKeyValueStore(); CheckThrow(typeof(ArgumentNullException)); store.Remove(null); } } } }