using Connected; using Connected.Notifications.Events; using Connected.ServiceModel; using Connected.Services; using Moq; namespace Common.Types; internal static class TestUtils { public static bool IsAssignableToGenericType(this Type type) { var genericType = typeof(TGenericType); if (!genericType.IsGenericType) throw new ArgumentException("The passed type must be a generic type.", nameof(TGenericType)); return type.IsAssignableToGenericType(genericType); } public static bool IsAssignableToGenericType(this Type type, Type genericType) { if (!genericType.IsGenericType) throw new ArgumentException("The passed type must be a generic type.", nameof(genericType)); var interfaceTypes = type.GetInterfaces(); foreach (var interfaceType in interfaceTypes) { if (interfaceType.IsGenericType && interfaceType.GetGenericTypeDefinition() == genericType) return true; } if (type.IsGenericType && type.GetGenericTypeDefinition() == genericType) return true; var baseType = type.BaseType; if (baseType is null) return false; return baseType.IsAssignableToGenericType(genericType); } public static bool SequenceEquivalent(this IEnumerable firstSequence, IEnumerable secondSequence) { var diff1 = firstSequence.Except(secondSequence); var diff2 = secondSequence.Except(firstSequence); return !diff1.Any() && !diff2.Any(); } public static void SetArguments(this ServiceOperation operation, TArgs args) where TArgs : IDto { typeof(ServiceOperation).GetProperty(nameof(operation.Arguments))!.SetValue(operation, args); } public static Moq.Language.Flow.ISetup SetupEvent(this Moq.Language.ISetupConditionResult setup, string @event) { return setup.Setup(e => e.Enqueue(It.IsAny(), It.IsAny(), @event, It.IsAny())); } public static Moq.Language.Flow.ISetup SetupEvent(this Mock setup, string @event) { return setup.Setup(e => e.Enqueue(It.IsAny(), It.IsAny(), @event, It.IsAny())); } public static void VerifyEvent(this Mock setup, string @event) { setup.Verify(e => e.Enqueue(It.IsAny(), It.IsAny(), @event, It.IsAny()), Times.Exactly(1)); } public static void SetupIEnumerable(this Mock mock, IEnumerable data) { mock.As>() .Setup(e => e.GetEnumerator()) .Returns(data.GetEnumerator()); } public static class AssertExtensions { public static async Task Throws(Task action) { Exception exception = null; try { await action; } catch (Exception ex) { exception = ex; } if (exception is null) Assert.Fail("Expected exception, none was thrown."); } internal static Task Throws(Func value) { return Throws(value()); } } }