Skip to content

Commit 18213d9

Browse files
authored
Merge pull request #6056 from tommy9/fakesFixes
Fakes fixes
2 parents bffd6ff + a30aeed commit 18213d9

File tree

6 files changed

+93
-3
lines changed

6 files changed

+93
-3
lines changed

Rubberduck.Main/ComClientLibrary/UnitTesting/FakeBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ protected bool TrySetReturnValue(bool any = false)
7575

7676
#region IFake
7777

78-
private static readonly List<ReturnValueInfo> ReturnValues = new List<ReturnValueInfo>();
78+
private readonly List<ReturnValueInfo> ReturnValues = new List<ReturnValueInfo>();
7979
public virtual void Returns(object value, int invocation = FakesProvider.AllInvocations)
8080
{
8181
ReturnValues.Add(new ReturnValueInfo(invocation, string.Empty, string.Empty, value));

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(typeof(Now));
2728
var nativeCall = Marshal.GetDelegateForFunctionPointer<DateDelegate>(NativeFunctionAddress);
2829
nativeCall(retVal);
30+
FakesProvider.ResumeFake(typeof(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(typeof(Now));
2728
var nativeCall = Marshal.GetDelegateForFunctionPointer<TimeDelegate>(NativeFunctionAddress);
2829
nativeCall(retVal);
30+
FakesProvider.ResumeFake(typeof(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(Type type)
41+
{
42+
foreach (var fake in ActiveFakes.Values)
43+
{
44+
if (fake.GetType() == type)
45+
{
46+
fake.DisableHook();
47+
return;
48+
}
49+
}
50+
}
51+
52+
public static void ResumeFake(Type type)
53+
{
54+
foreach (var fake in ActiveFakes.Values)
55+
{
56+
if (fake.GetType() == type)
57+
{
58+
fake.EnableHook();
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)

RubberduckTests/IntegrationTests/FakeTests.bas

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -729,3 +729,50 @@ TestExit:
729729
TestFail:
730730
Assert.Fail "Test raised an error: #" & Err.Number & " - " & Err.Description
731731
End Sub
732+
733+
'@TestMethod
734+
Public Sub TestIssue4476()
735+
On Error GoTo TestFail
736+
737+
'Arrange:
738+
Fakes.Now.PassThrough = True
739+
Fakes.Date.PassThrough = True
740+
Dim retVal As Variant
741+
742+
'Act:
743+
retVal = Now
744+
retVal = Date '<== KA-BOOOM
745+
retVal = Now 'ensure fake reinstated
746+
747+
'Assert:
748+
Fakes.Now.Verify.Exactly 2
749+
Fakes.Date.Verify.Once
750+
751+
TestExit:
752+
Exit Sub
753+
TestFail:
754+
Assert.Fail "Test raised an error: #" & Err.Number & " - " & Err.Description
755+
End Sub
756+
757+
'@TestMethod
758+
Public Sub TestIssue5944()
759+
On Error GoTo TestFail
760+
761+
Fakes.InputBox.Returns 20
762+
Fakes.MsgBox.Returns 20
763+
764+
Dim inputBoxReturnValue As String
765+
Dim msgBoxReturnValue As Integer
766+
767+
inputBoxReturnValue = InputBox("Dummy")
768+
msgBoxReturnValue = MsgBox("Dummy")
769+
770+
Fakes.MsgBox.Verify.Once
771+
Fakes.InputBox.Verify.Once
772+
773+
TestExit:
774+
Exit Sub
775+
TestFail:
776+
Assert.Fail "Test raised an error: #" & Err.Number & " - " & Err.Description
777+
End Sub
778+

0 commit comments

Comments
 (0)