From fe2792e3f0c7ab87e52ab32460004aff69e2fc1a Mon Sep 17 00:00:00 2001 From: Mariusz Date: Sat, 9 Apr 2016 22:04:38 +0200 Subject: [PATCH] #15 - added test of chain call od extension methods. --- Lecture/Lecture/ExtensionMethods.cs | 5 ++++- Lecture/Lesson2UnitTest/ExtensionMethodsUnitTest.cs | 10 ++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/Lecture/Lecture/ExtensionMethods.cs b/Lecture/Lecture/ExtensionMethods.cs index e347c8e7..7850b91c 100644 --- a/Lecture/Lecture/ExtensionMethods.cs +++ b/Lecture/Lecture/ExtensionMethods.cs @@ -13,7 +13,10 @@ public static int WordCount(this String str) { return str.Split(new char[] { ' ', '.', '?' }, StringSplitOptions.RemoveEmptyEntries).Length; } - + public static int MethodA(this int i) + { + return -i; + } /// /// Determines whether [contains] [the specified value]. /// diff --git a/Lecture/Lesson2UnitTest/ExtensionMethodsUnitTest.cs b/Lecture/Lesson2UnitTest/ExtensionMethodsUnitTest.cs index 3e1e461a..b7302e3a 100644 --- a/Lecture/Lesson2UnitTest/ExtensionMethodsUnitTest.cs +++ b/Lecture/Lesson2UnitTest/ExtensionMethodsUnitTest.cs @@ -15,6 +15,14 @@ public void InstanceCallTestMethod() Assert.AreEqual(3, _TestString.WordCount()); //To enable extension methods for a particular type, the definition must be visible. Assert.AreEqual(3, ExtensionMethods.WordCount(_TestString)); //Typical method call can also be in use. } + [TestMethod] + public void SequentialCallTestMethod() + { + string _TestString = "Hello Extension Methods"; + int _WordCountResult = _TestString.WordCount(); + Assert.AreEqual(3, _WordCountResult); + Assert.AreEqual(-3, _WordCountResult.MethodA()); + } /// /// Mies the test method. /// @@ -42,6 +50,8 @@ public void MyTestMethod() Assert.AreEqual(1, (int)c.MethodA(1)); // C.MethodA(object) Assert.AreSame(_inputString, c.MethodA(_inputString)); // C.MethodA(object) Assert.AreNotSame(new AnyClass(), c.MethodA(new AnyClass())); + + //How to use it } private class AnyClass {