Skip to content

Commit ece4e22

Browse files
committed
Updated extensions to account for multiple interfaces
This was raised by Fijode on EcsRx, should fix the underlying issue once EcsRx handlers update.
1 parent a508aff commit ece4e22

6 files changed

Lines changed: 49 additions & 15 deletions

File tree

appveyor.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
version: 5.1.{build}
1+
version: 5.2.{build}
22
branches:
33
only:
44
- main

src/SystemsRx.Tests/SystemsRx/Handlers/ReactToEventSystemHandlerTests.cs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Linq;
23
using SystemsRx.Events;
34
using SystemsRx.Executor.Handlers.Conventional;
45
using SystemsRx.MicroRx.Subjects;
@@ -13,6 +14,8 @@ namespace SystemsRx.Tests.SystemsRx.Handlers
1314
{
1415
public class ReactToEventSystemHandlerTests
1516
{
17+
public interface MultipleOfSameInterface : IReactToEventSystem<int>, IReactToEventSystem<float>{}
18+
1619
[Fact]
1720
public void should_correctly_handle_systems()
1821
{
@@ -44,15 +47,30 @@ public void should_destroy_and_dispose_system()
4447
}
4548

4649
[Fact]
47-
public void should_get_event_type_from_system()
50+
public void should_get_event_types_from_system()
4851
{
4952
var mockEventSystem = Substitute.For<IEventSystem>();
5053
var mockSystem = Substitute.For<IReactToEventSystem<int>>();
5154

5255
var systemHandler = new ReactToEventSystemHandler(mockEventSystem);
53-
var actualType = systemHandler.GetEventTypeFromSystem(mockSystem);
56+
var actualTypes = systemHandler.GetEventTypesFromSystem(mockSystem).ToArray();
57+
58+
Assert.Equal(1, actualTypes.Length);
59+
Assert.Equal(typeof(int), actualTypes[0]);
60+
}
61+
62+
[Fact]
63+
public void should_get_multiple_event_types_from_system()
64+
{
65+
var mockEventSystem = Substitute.For<IEventSystem>();
66+
var mockSystem = Substitute.For<MultipleOfSameInterface>();
67+
68+
var systemHandler = new ReactToEventSystemHandler(mockEventSystem);
69+
var actualTypes = systemHandler.GetEventTypesFromSystem(mockSystem).ToArray();
5470

55-
Assert.Equal(typeof(int), actualType);
71+
Assert.Equal(2, actualTypes.Length);
72+
Assert.Contains(typeof(int), actualTypes);
73+
Assert.Contains(typeof(float), actualTypes);
5674
}
5775

5876
[Fact]
Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using SystemsRx.Extensions;
1+
using System.Linq;
2+
using SystemsRx.Extensions;
23
using SystemsRx.Systems.Conventional;
34
using NSubstitute;
45
using Xunit;
@@ -7,13 +8,27 @@ namespace SystemsRx.Tests.SystemsRx
78
{
89
public class ISystemExtensionTests
910
{
11+
public interface HasMultipleImplementations : IReactToEventSystem<int>, IReactToEventSystem<float>{}
12+
1013
[Fact]
1114
public void should_get_interface_generic_type_from_reactive_data_system()
1215
{
1316
var fakeSystem = Substitute.For<IReactToEventSystem<int>>();
14-
var genericType = fakeSystem.GetGenericDataType(typeof(IReactToEventSystem<>));
15-
var typesMatch = genericType == typeof(int);
16-
Assert.True(typesMatch);
17+
var genericTypes = fakeSystem.GetGenericDataTypes(typeof(IReactToEventSystem<>)).ToArray();
18+
19+
Assert.Equal(1, genericTypes.Length);
20+
Assert.Contains(typeof(int), genericTypes);
21+
}
22+
23+
[Fact]
24+
public void should_get_multiple_interface_generic_types_from_reactive_data_system()
25+
{
26+
var fakeSystem = Substitute.For<HasMultipleImplementations>();
27+
var genericTypes = fakeSystem.GetGenericDataTypes(typeof(IReactToEventSystem<>)).ToArray();
28+
29+
Assert.Equal(2, genericTypes.Length);
30+
Assert.Contains(typeof(int), genericTypes);
31+
Assert.Contains(typeof(float), genericTypes);
1732
}
1833
}
1934
}

src/SystemsRx/Executor/Handlers/Conventional/ReactToEventSystemHandler.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ public bool CanHandleSystem(ISystem system)
3232
public Type[] GetMatchingInterfaces(ISystem system)
3333
{ return system.GetGenericInterfacesFor(typeof(IReactToEventSystem<>)).ToArray(); }
3434

35-
public Type GetEventTypeFromSystem(ISystem system)
36-
{ return system.GetGenericDataType(typeof(IReactToEventSystem<>)); }
35+
public IEnumerable<Type> GetEventTypesFromSystem(ISystem system)
36+
{ return system.GetGenericDataTypes(typeof(IReactToEventSystem<>)); }
3737

3838
public void SetupSystem(ISystem system)
3939
{

src/SystemsRx/Extensions/ISystemExtensions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ public static IEnumerable<Type> GetGenericInterfacesFor(this ISystem system, Typ
2626
public static bool MatchesSystemTypeWithGeneric(this ISystem system, Type systemType)
2727
{ return GetGenericInterfacesFor(system, systemType).Any(); }
2828

29-
public static Type GetGenericDataType(this ISystem system, Type systemType)
29+
public static IEnumerable<Type> GetGenericDataTypes(this ISystem system, Type systemType)
3030
{
3131
var matchingInterface = GetGenericInterfaceType(system, systemType);
32-
return matchingInterface.GetGenericArguments()[0];
32+
return matchingInterface.Select(x => x.GetGenericArguments()[0]);
3333
}
3434

35-
public static Type GetGenericInterfaceType(this ISystem system, Type systemType)
35+
public static IEnumerable<Type> GetGenericInterfaceType(this ISystem system, Type systemType)
3636
{ return system.GetType().GetMatchingInterfaceGenericTypes(systemType); }
3737

3838
public static bool IsReactToEventSystem(this ISystem system)
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Linq;
34

45
namespace SystemsRx.Extensions
56
{
67
public static class TypeExtensions
78
{
8-
public static Type GetMatchingInterfaceGenericTypes(this Type type, Type genericType)
9+
public static IEnumerable<Type> GetMatchingInterfaceGenericTypes(this Type type, Type genericType)
910
{
1011
return type
1112
.GetInterfaces()
12-
.Single(x => x.IsGenericType && x.GetGenericTypeDefinition() == genericType);
13+
.Where(x => x.IsGenericType && x.GetGenericTypeDefinition() == genericType);
1314
}
1415
}
1516
}

0 commit comments

Comments
 (0)