-
-
Notifications
You must be signed in to change notification settings - Fork 810
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for Moq compatibility with F# events (#712)
There are two old Google Code issues which were resolved with commit 44070a9, however no documentation or unit tests were ever added for these issues: * Moq fails to mock events defined in F#: https://code.google.com/archive/p/moq/issues/238 * Can't raise events on mocked Interop interfaces: https://code.google.com/archive/p/moq/issues/226 This commit adds unit tests for the first of these two issues by add- ing an F# project to the solution, which allows us to define F# types for use in unit tests.
- Loading branch information
Showing
12 changed files
with
201 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD. | ||
// All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. | ||
|
||
namespace Moq.Tests.FSharpTypes | ||
|
||
open System; | ||
|
||
[<AbstractClass>] | ||
type HasAbstractActionEvent = | ||
[<CLIEvent>] | ||
abstract member Event: IDelegateEvent<Action> |
11 changes: 11 additions & 0 deletions
11
tests/Moq.Tests.FSharpTypes/HasAbstractEventHandlerEvent.fs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD. | ||
// All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. | ||
|
||
namespace Moq.Tests.FSharpTypes | ||
|
||
open System; | ||
|
||
[<AbstractClass>] | ||
type HasAbstractEventHandlerEvent = | ||
[<CLIEvent>] | ||
abstract member Event: IEvent<EventHandler, EventArgs> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD. | ||
// All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. | ||
|
||
namespace Moq.Tests.FSharpTypes | ||
|
||
open System; | ||
|
||
type HasActionEvent() = | ||
let event = new DelegateEvent<Action>(); | ||
|
||
[<CLIEvent>] | ||
member this.Event = event.Publish |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD. | ||
// All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. | ||
|
||
namespace Moq.Tests.FSharpTypes | ||
|
||
open System; | ||
|
||
type IHasActionEvent = | ||
[<CLIEvent>] | ||
abstract member Event: IDelegateEvent<Action> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD. | ||
// All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. | ||
|
||
namespace Moq.Tests.FSharpTypes | ||
|
||
open System; | ||
|
||
type IHasEventHandlerEvent = | ||
[<CLIEvent>] | ||
abstract member Event: IEvent<EventHandler, EventArgs> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<Import Project="$(BuildDirectory)AssemblyInfo.props" /> | ||
<Import Project="$(BuildDirectory)SignAssembly.props" /> | ||
|
||
<PropertyGroup> | ||
<TargetFrameworks>net46;netcoreapp2.0</TargetFrameworks> | ||
<DebugSymbols>True</DebugSymbols> | ||
<DebugType>full</DebugType> | ||
<IsPackable>False</IsPackable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<Compile Include="HasAbstractActionEvent.fs" /> | ||
<Compile Include="HasAbstractEventHandlerEvent.fs" /> | ||
<Compile Include="HasActionEvent.fs" /> | ||
<Compile Include="IHasActionEvent.fs" /> | ||
<Compile Include="IHasEventHandlerEvent.fs" /> | ||
</ItemGroup> | ||
|
||
<Target Name="Test" DependsOnTargets="Build" /> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD. | ||
// All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection; | ||
|
||
using Moq.Tests.FSharpTypes; | ||
|
||
using Xunit; | ||
|
||
namespace Moq.Tests | ||
{ | ||
public class FSharpCompatibilityFixture | ||
{ | ||
public static IEnumerable<object[]> AbstractFSharpEvents | ||
{ | ||
get | ||
{ | ||
yield return new object[] { typeof(HasAbstractActionEvent).GetEvent(nameof(HasAbstractActionEvent.Event)) }; | ||
yield return new object[] { typeof(HasAbstractEventHandlerEvent).GetEvent(nameof(HasAbstractEventHandlerEvent.Event)) }; | ||
yield return new object[] { typeof(IHasActionEvent).GetEvent(nameof(IHasActionEvent.Event)) }; | ||
yield return new object[] { typeof(IHasEventHandlerEvent).GetEvent(nameof(IHasEventHandlerEvent.Event)) }; | ||
} | ||
} | ||
|
||
public static IEnumerable<object[]> NonAbstractFSharpEvents | ||
{ | ||
get | ||
{ | ||
yield return new object[] { typeof(HasActionEvent).GetEvent(nameof(HasActionEvent.Event)) }; | ||
} | ||
} | ||
|
||
[Theory(Skip = "See https://github.com/Microsoft/visualfsharp/issues/5834.")] | ||
[MemberData(nameof(AbstractFSharpEvents))] | ||
public void Abstract_FSharp_event_has_accessors_marked_as_specialname(EventInfo @event) | ||
{ | ||
Assert.All(@event.GetAccessors(), accessor => Assert.True(accessor.IsAbstract)); | ||
Assert.All(@event.GetAccessors(), accessor => Assert.True(accessor.IsSpecialName, "Accessor not marked as `specialname`.")); | ||
} | ||
|
||
[Theory] | ||
[MemberData(nameof(NonAbstractFSharpEvents))] | ||
public void Non_abstract_FSharp_event_has_accessors_marked_as_specialname(EventInfo @event) | ||
{ | ||
Assert.All(@event.GetAccessors(), accessor => Assert.False(accessor.IsAbstract)); | ||
Assert.All(@event.GetAccessors(), accessor => Assert.True(accessor.IsSpecialName, "Accessor not marked as `specialname`.")); | ||
} | ||
|
||
[Fact] | ||
public void Can_subscribe_to_and_raise_abstract_FSharp_event() | ||
{ | ||
var mock = new Mock<IHasEventHandlerEvent>(); | ||
var eventRaiseCount = 0; | ||
EventHandler handler = (sender, e) => ++eventRaiseCount; | ||
|
||
mock.Object.Event += handler; | ||
mock.Raise(x => x.Event += null, EventArgs.Empty); | ||
|
||
Assert.Equal(1, eventRaiseCount); | ||
} | ||
|
||
[Fact] | ||
public void Can_unsubscribe_from_FSharp_event() | ||
{ | ||
var mock = new Mock<IHasEventHandlerEvent>(); | ||
var eventRaiseCount = 0; | ||
EventHandler handler = (sender, e) => ++eventRaiseCount; | ||
|
||
mock.Object.Event += handler; | ||
mock.Raise(x => x.Event += null, EventArgs.Empty); | ||
|
||
mock.Object.Event -= handler; | ||
mock.Raise(x => x.Event += null, EventArgs.Empty); | ||
|
||
Assert.Equal(1, eventRaiseCount); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Copyright (c) 2007, Clarius Consulting, Manas Technology Solutions, InSTEDD. | ||
// All rights reserved. Licensed under the BSD 3-Clause License; see License.txt. | ||
|
||
using System.Collections.Generic; | ||
using System.Reflection; | ||
|
||
namespace Moq.Tests | ||
{ | ||
public static class ReflectionExtensions | ||
{ | ||
public static IEnumerable<MethodInfo> GetAccessors(this EventInfo @event, bool nonPublic = false) | ||
{ | ||
yield return @event.GetAddMethod(nonPublic); | ||
yield return @event.GetRemoveMethod(nonPublic); | ||
} | ||
} | ||
} |