public void Cache_reportsIfItemWasRetrievedFromCache() { Func <int, string> creator = key => { return(key.ToString()); }; bool wasRetrievedFromCache; var cache = new RecentItemsCache <int, string>(7); cache.GetItem(0, creator, out wasRetrievedFromCache); Assert.AreEqual(false, wasRetrievedFromCache, "Adding a new item to cache should not report it as retrieved from cache."); cache.GetItem(0, creator, out wasRetrievedFromCache); Assert.AreEqual(true, wasRetrievedFromCache, "Retrieving a cached item should report that it was retrieved from cache."); }
public void NullInput_key() { // Attempt to cache a key,value pair with null key var cache = new RecentItemsCache <string, string>(7); cache.GetItem(null, key => { return("value"); }); }
public void NullInput_key() { // Attempt to cache a key,value pair with null key var cache = new RecentItemsCache <string, string>(7); Assert.That(() => cache.GetItem(null, key => { return("value"); }), Throws.TypeOf <ArgumentNullException>()); }
public void NullInput_value() { // Cache a key,value pair with null value. var cache = new RecentItemsCache <int, string>(7); Assert.DoesNotThrow(() => { cache.GetItem(0, key => { return(null); }); }, "Null value should not throw exception"); }
/// <summary> /// Add key,value pair to cache, checking if it was newly added. /// Will assert if computed if not expected, or did not compute if expected to. /// </summary> /// <param name="cache"></param> /// <param name="key"></param> /// <param name="creator">Delegate to create the value from key</param> /// <param name="expectToCompute"> /// Whether should expect to need to compute the value from key using creator /// </param> private void AddItem <K, V>(RecentItemsCache <K, V> cache, K key, Func <K, V> creator, bool expectToCompute) { bool didCompute = false; Assert.AreEqual(creator(key), cache.GetItem(key, key1 => { didCompute = true; return(creator(key1)); }), String.Format("wrong value for {0} cached key given", didCompute?"newly":"previously")); string errorMessage; if (expectToCompute) { errorMessage = "Expected to compute value from key since it was not expected to be in the cache, but it was in the cache."; } else { errorMessage = "Did not expect to compute value from key since expected it to be in the cache, but it was not in the cache."; } Assert.AreEqual(expectToCompute, didCompute, errorMessage); }