Example #1
0
        public void Delegates_MagicClass_NoModifierRegisteredResultsInUnchangedValue()
        {
            var testObject = new MagicClass(10);
            var actual = testObject.ApplyModifiers();

            Assert.AreEqual(10, actual);
        }
Example #2
0
        public void Delegates_MagicClass_ModifiersAlterValueCorrectly()
        {
            var testObject = new MagicClass(5);

            testObject.ValueModifier += delegate (int result)
            {
                return result + 10;
            };

            testObject.ValueModifier += delegate (int result)
            {
                return (result + 2) / 2;
            };

            var actual = testObject.ApplyModifiers();

            Assert.AreEqual(13, actual);

            // Explanation:
            // 1. At the beginning there was an initial value equaled to 5.
            // 2. Then the second callback function was applied to the previous result so the new result equals 3, i. e. (5 + 2) / 2 = 3.
            // 3. Afterwards the first callback was applied to the previous result and the final result was returned (because no more modifier was registered), i. e. 3 + 10 = 13
        }