Skip to content

Commit 654a95c

Browse files
committed
Introduce method for suspending fakes temporarily to avoid errors like issue #4476. Specific fix for Date/Now and Time/Now for the timebeing.
1 parent adab10c commit 654a95c

File tree

4 files changed

+45
-2
lines changed

4 files changed

+45
-2
lines changed

Rubberduck.Main/ComClientLibrary/UnitTesting/Fakes/Date.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,16 @@ public Date()
1818
public void DateCallback(IntPtr retVal)
1919
{
2020
OnCallBack(true);
21-
if (!TrySetReturnValue()) // specific invocation
21+
if (!TrySetReturnValue())
2222
{
23-
TrySetReturnValue(true); // any invocation
23+
TrySetReturnValue(true);
2424
}
2525
if (PassThrough)
2626
{
27+
FakesProvider.SuspendFake("Now");
2728
var nativeCall = Marshal.GetDelegateForFunctionPointer<DateDelegate>(NativeFunctionAddress);
2829
nativeCall(retVal);
30+
FakesProvider.ResumeFake("Now");
2931
return;
3032
}
3133
Marshal.GetNativeVariantForObject(ReturnValue ?? 0, retVal);

Rubberduck.Main/ComClientLibrary/UnitTesting/Fakes/Time.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@ public void TimeCallback(IntPtr retVal)
2424
}
2525
if (PassThrough)
2626
{
27+
FakesProvider.SuspendFake("Now");
2728
var nativeCall = Marshal.GetDelegateForFunctionPointer<TimeDelegate>(NativeFunctionAddress);
2829
nativeCall(retVal);
30+
FakesProvider.ResumeFake("Now");
2931
return;
3032
}
3133
Marshal.GetNativeVariantForObject(ReturnValue ?? 0, retVal);

Rubberduck.Main/ComClientLibrary/UnitTesting/FakesProvider.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,30 @@ public void StartTest()
3737
CodeIsUnderTest = true;
3838
}
3939

40+
public static void SuspendFake(String typename)
41+
{
42+
foreach (var fake in ActiveFakes.Values)
43+
{
44+
if (fake.GetType().Name == typename)
45+
{
46+
fake.DisableHook();
47+
return;
48+
}
49+
}
50+
}
51+
52+
public static void ResumeFake(String typename)
53+
{
54+
foreach (var fake in ActiveFakes.Values)
55+
{
56+
if (fake.GetType().Name == typename)
57+
{
58+
fake.DisableHook();
59+
return;
60+
}
61+
}
62+
}
63+
4064
public void StopTest()
4165
{
4266
foreach (var fake in ActiveFakes.Values)

Rubberduck.Main/ComClientLibrary/UnitTesting/StubBase.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,21 @@ protected void OnCallBack(bool trackNoParams = false)
5353
}
5454
}
5555

56+
public void DisableHook()
57+
{
58+
foreach (var hook in _hooks)
59+
{
60+
hook.ThreadACL.SetExclusiveACL(new[] { 0 });
61+
}
62+
}
63+
public void EnableHook()
64+
{
65+
foreach (var hook in _hooks)
66+
{
67+
hook.ThreadACL.SetInclusiveACL(new[] { 0 });
68+
}
69+
}
70+
5671
public virtual void Dispose()
5772
{
5873
foreach (var hook in _hooks)

0 commit comments

Comments
 (0)