-
Notifications
You must be signed in to change notification settings - Fork 72
Expand file tree
/
Copy pathShimTests.cs
More file actions
198 lines (165 loc) · 6.92 KB
/
Copy pathShimTests.cs
File metadata and controls
198 lines (165 loc) · 6.92 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Threading;
using Pose.Exceptions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Pose.Tests
{
[TestClass]
public class ShimTests
{
[TestMethod]
public void TestReplace()
{
Shim shim = Shim.Replace(() => Console.WriteLine(""));
Assert.AreEqual(typeof(Console).GetMethod("WriteLine", new[] { typeof(string) }), shim.Original);
Assert.IsNull(shim.Replacement);
}
[TestMethod]
public void TestReplaceWithInstanceVariable()
{
ShimTests shimTests = new ShimTests();
Shim shim = Shim.Replace(() => shimTests.TestReplace());
Assert.AreEqual(typeof(ShimTests).GetMethod("TestReplace"), shim.Original);
Assert.AreSame(shimTests, shim.Instance);
Assert.IsNull(shim.Replacement);
}
[TestMethod]
public void TestShimReplaceWithInvalidSignature()
{
ShimTests shimTests = new ShimTests();
Shim shim = Shim.Replace(() => shimTests.TestReplace());
Assert.ThrowsException<InvalidShimSignatureException>(
() => Shim.Replace(() => shimTests.TestReplace()).With(() => { }));
Assert.ThrowsException<InvalidShimSignatureException>(
() => Shim.Replace(() => Console.WriteLine(Is.A<string>())).With(() => { }));
}
[TestMethod]
public void TestShimReplaceWith()
{
ShimTests shimTests = new ShimTests();
Action action = new Action(() => { });
Action<ShimTests> actionInstance = new Action<ShimTests>((s) => { });
Shim shim = Shim.Replace(() => Console.WriteLine()).With(action);
Shim shim1 = Shim.Replace(() => shimTests.TestReplace()).With(actionInstance);
Assert.AreEqual(typeof(Console).GetMethod("WriteLine", Type.EmptyTypes), shim.Original);
Assert.AreEqual(action, shim.Replacement);
Assert.AreEqual(typeof(ShimTests).GetMethod("TestReplace"), shim1.Original);
Assert.AreSame(shimTests, shim1.Instance);
Assert.AreEqual(actionInstance, shim1.Replacement);
}
[TestMethod]
public void TestReplacePropertyGetter()
{
Shim shim = Shim.Replace(() => Thread.CurrentThread.CurrentCulture);
Assert.AreEqual(typeof(Thread).GetProperty(nameof(Thread.CurrentCulture), typeof(CultureInfo)).GetMethod, shim.Original);
Assert.IsNull(shim.Replacement);
}
[TestMethod]
public void TestReplacePropertySetter()
{
Shim shim = Shim.Replace(() => Is.A<Thread>().CurrentCulture, true);
Assert.AreEqual(typeof(Thread).GetProperty(nameof(Thread.CurrentCulture), typeof(CultureInfo)).SetMethod, shim.Original);
Assert.IsNull(shim.Replacement);
}
[TestMethod]
public void TestReplacePropertySetterAction()
{
var getterExecuted = false;
var getterShim = Shim.Replace(() => Is.A<Thread>().CurrentCulture)
.With((Thread t) =>
{
getterExecuted = true;
return t.CurrentCulture;
});
var setterExecuted = false;
var setterShim = Shim.Replace(() => Is.A<Thread>().CurrentCulture, true)
.With((Thread t, CultureInfo value) =>
{
setterExecuted = true;
t.CurrentCulture = value;
});
var currentCultureProperty = typeof(Thread).GetProperty(nameof(Thread.CurrentCulture), typeof(CultureInfo));
Assert.AreEqual(currentCultureProperty.GetMethod, getterShim.Original);
Assert.AreEqual(currentCultureProperty.SetMethod, setterShim.Original);
PoseContext.Isolate(() =>
{
var oldCulture = Thread.CurrentThread.CurrentCulture;
Thread.CurrentThread.CurrentCulture = oldCulture;
}, getterShim, setterShim);
Assert.IsTrue(getterExecuted, "Getter not executed");
Assert.IsTrue(setterExecuted, "Setter not executed");
}
[TestMethod]
public void TestReplaceConstructor()
{
var dummyShim = Shim.Replace<DummyForConstructorBase>(() => new DummyForConstructor()).With(() => new DummyForConstructorReplacment());
bool? wasCalled = false;
PoseContext.Isolate(() =>
{
wasCalled = new DummyHolder().Dummy.ConstructorCalled;
}, dummyShim);
Assert.IsNotNull(wasCalled);
Assert.IsFalse(wasCalled.Value);
}
[TestMethod]
public void TestReplaceConstructorWithoutGeneric()
{
var dummyShim = Shim.Replace(() => new DummyForConstructor()).With(() => new DummyForConstructorReplacment());
bool? wasCalled = false;
PoseContext.Isolate(() =>
{
wasCalled = new DummyHolder().Dummy.ConstructorCalled;
}, dummyShim);
Assert.IsNotNull(wasCalled);
Assert.IsFalse(wasCalled.Value);
}
[TestMethod]
public void TestReplaceConstructorWithoutGeneric2()
{
var dummyShim = Shim.Replace(() => new DummyForConstructorReplacment()).With(() => new DummyForConstructorReplacment(true));
PoseContext.Isolate(() =>
{
var unused = new DummyForConstructorReplacment();
}, dummyShim);
}
[TestMethod]
public void TestReplaceConstructorWithIncorrectTypes()
{
List<string> Replacement() => new List<string>();
var exception = Assert.ThrowsException<InvalidShimSignatureException>(() => Shim.Replace<DummyForConstructorBase>(() => new DummyForConstructor()).With(Replacement));
Assert.AreEqual("Mismatched construction types", exception.Message);
}
private class DummyHolder
{
public DummyHolder()
{
Dummy = new DummyForConstructor();
}
public DummyForConstructorBase Dummy { get; }
}
private abstract class DummyForConstructorBase
{
public bool? ConstructorCalled { get; protected set; }
}
private class DummyForConstructor : DummyForConstructorBase
{
public DummyForConstructor()
{
ConstructorCalled = true;
}
}
private class DummyForConstructorReplacment : DummyForConstructorBase
{
public DummyForConstructorReplacment()
{
ConstructorCalled = false;
}
public DummyForConstructorReplacment(bool dummyFlag)
{
ConstructorCalled = false;
}
}
}
}