Example #1
0
        public void FirstLoad_CallsLoadEventsInOrder()
        {
            var lazy = new LazyGetSetTester <object>(null, null).LazyGetSet;

            int eventsCalledCount    = 0,
                valueCachedCallOrder = 0,
                valueGetCallOrder    = 0,
                valueSetCallOrder    = 0;

            lazy.ValueCached += (sender, value) => valueCachedCallOrder = ++eventsCalledCount;

            lazy.ValueGet += (sender, value) => valueGetCallOrder = ++eventsCalledCount;

            lazy.ValueSet += (sender, value) => valueSetCallOrder = ++eventsCalledCount;

            var val = lazy.Value;

            Assert.AreEqual(1, valueCachedCallOrder);
            Assert.AreEqual(2, valueGetCallOrder);
            Assert.AreEqual(0, valueSetCallOrder);

            lazy.Value = val;

            Assert.AreEqual(2, valueGetCallOrder);
            Assert.AreEqual(3, valueSetCallOrder);
            Assert.AreEqual(4, valueCachedCallOrder);
        }
Example #2
0
        public void SetLazyValue_WithNull_SetsCachedValue()
        {
            var lazy = new LazyGetSetTester <object>(null, o => { });

            lazy.LazyGetSet.Value = null;

            Assert.AreEqual(null, lazy.LazyGetSet.Value);
        }
Example #3
0
        public void GetLazyValue_WithNull_LoadsValue()
        {
            var lazy = new LazyGetSetTester <object>(() => null);

            var val = lazy.LazyGetSet.Value;

            Assert.AreEqual(null, val);
        }
Example #4
0
        public void GetLazyValue_WithGetFunc_CallsGetFunc()
        {
            var lazy = new LazyGetSetTester <object>();

            _ = lazy.LazyGetSet.Value;

            Assert.GreaterOrEqual(1, lazy.GetCount);
        }
Example #5
0
        public void SetLazyValue_WithSetAction_CallsSetAction()
        {
            var lazy = new LazyGetSetTester <object>(null, null);

            lazy.LazyGetSet.Value = null;

            Assert.GreaterOrEqual(1, lazy.SetCount);
        }
Example #6
0
        public void GetSetLazy_WithDifferentValues_UpdatesAndUsesCache()
        {
            var firstVal  = 167;
            var secondVal = "asdf";

            var lazy = new LazyGetSetTester <object>(() => firstVal, null);

            Assert.AreEqual(firstVal, lazy.LazyGetSet.Value);
            Assert.AreEqual(1, lazy.GetCount);

            lazy.LazyGetSet.Value = secondVal;
            Assert.AreEqual(1, lazy.GetCount);

            Assert.AreEqual(secondVal, lazy.LazyGetSet.Value);
            Assert.AreEqual(1, lazy.GetCount);
        }
Example #7
0
        public void ClearCache_WhenItWasPreviouslyCached_SetsIsCachedToFalseAndReloadsOnNextRequest()
        {
            var lazy = new LazyGetSetTester <object>();

            _ = lazy.LazyGetSet.Value;

            Assert.IsTrue(lazy.LazyGetSet.IsCached);
            Assert.AreEqual(1, lazy.GetCount);

            lazy.LazyGetSet.ClearCache();

            Assert.IsFalse(lazy.LazyGetSet.IsCached);

            _ = lazy.LazyGetSet.Value;

            Assert.IsTrue(lazy.LazyGetSet.IsCached);
            Assert.AreEqual(2, lazy.GetCount);
        }
Example #8
0
        public void SetLazyValue_WithoutSetAction_ThrowsInvalidOperation()
        {
            var lazy = new LazyGetSetTester <object>();

            Assert.Throws <NotSupportedException>(() => lazy.LazyGetSet.Value = null);
        }