/
BuiltInDelegateTests.cs
135 lines (114 loc) · 3.78 KB
/
BuiltInDelegateTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace DelegatesLambdasAndExpressions
{
[TestClass]
public class BuiltInDelegateTests
{
public class TestClass
{
public TestArg Arg { get; set; }
}
public class TestArg { }
object testEventSender;
EventArgs testEventArgs;
void MyEventHandler(object sender, EventArgs eventArgs)
{
testEventSender = sender;
testEventArgs = eventArgs;
}
[TestMethod]
public void EventHandlerTest()
{
object sender = "My Sender";
var eventArgs = new EventArgs();
EventHandler eventHandler = MyEventHandler;
eventHandler(sender, eventArgs);
Assert.AreEqual(sender, testEventSender);
Assert.AreEqual(eventArgs, testEventArgs);
}
bool StringIsNotNull(string value)
{
return value != null;
}
[TestMethod]
public void PredicateTest()
{
Predicate<string> stringIsNotNullPredicate = StringIsNotNull;
var result = stringIsNotNullPredicate("I'm not null");
Assert.IsTrue(result);
}
int MyIntComparison(int x, int y)
{
return x.CompareTo(y);
}
[TestMethod]
public void ComparisonTest()
{
var x = 3; var y = 7;
Comparison<int> useCompareTo = MyIntComparison;
var result = useCompareTo(x, y);
Assert.AreEqual(x.CompareTo(y), result);
}
TestClass TestClassConverter(TestArg arg)
{
return new TestClass {Arg = arg};
}
[TestMethod]
public void ConverterTest()
{
var genericTestArg = new TestArg();
Converter<TestArg, TestClass> convertTestArgToTestClass = TestClassConverter;
var result = convertTestArgToTestClass(genericTestArg);
Assert.AreEqual(genericTestArg, result.Arg);
}
bool testActionExecuted;
void MyAction()
{
testActionExecuted = true;
}
[TestMethod]
public void ActionTest()
{
testActionExecuted = false;
Action action = MyAction;
action();
Assert.IsTrue(testActionExecuted);
}
TestArg testGenericDelegateArg;
void MyActionWithArg(TestArg arg)
{
testGenericDelegateArg = arg;
}
[TestMethod]
public void ActionWithArgTest()
{
testGenericDelegateArg = null;
var genericTestArg = new TestArg();
Action<TestArg> actionWithTestArg = MyActionWithArg;
actionWithTestArg(genericTestArg);
Assert.AreEqual(genericTestArg, testGenericDelegateArg);
}
TestClass genericNoArgFunctionReturnValue;
TestClass MyNoArgFunction()
{
return genericNoArgFunctionReturnValue;
}
[TestMethod]
public void FunctionTest()
{
genericNoArgFunctionReturnValue = new TestClass();
Func<TestClass> noArgFunction = MyNoArgFunction;
var result = noArgFunction();
Assert.AreEqual(genericNoArgFunctionReturnValue, result);
}
[TestMethod]
public void FunctionWithArgTest()
{
var genericTestArg = new TestArg();
Func<TestArg, TestClass> functionWithArg = TestClassConverter;
var result = functionWithArg(genericTestArg);
Assert.AreEqual(genericTestArg, result.Arg);
}
}
}