Example #1
0
        public void OnlyActionForTheSameIndexedParameterTypeIsCalledOnSwitch()
        {
            foreach (var(result, value, valueType) in AllResultsToTest())
            {
                var resultType = result.GetType();

                var delegateMonitor = new DelegateMonitor();

                var voidDelegates = GetAllTypes(resultType)
                                    .Select(genericType => DelegateCreator.CreateDelegate(
                                                delegateMonitor,
                                                nameof(DelegateMonitor.NoOperation),
                                                true,
                                                typeof(Action <>),
                                                genericType))
                                    .Cast <object>()
                                    .ToArray();

                var switchMethod = resultType.GetMethods().Single(method => method.Name == "Switch" && !method.IsGenericMethod);

                if (switchMethod == null)
                {
                    throw new InvalidOperationException("Switch method not found");
                }

                switchMethod.Invoke(result, voidDelegates);

                Assert.AreEqual(1, delegateMonitor.TotalCalls);

                Assert.AreEqual(1, delegateMonitor.GetCalls(valueType));
            }
        }
Example #2
0
        public void WhenConstructedWithFailureTypeTryMapWillReturnFalseAndProvideDefaultValue()
        {
            foreach (var resultType in AllResultTypesToTest())
            {
                var successType = GetSuccessType(resultType);
                foreach (var failureType in GetFailureTypes(resultType))
                {
                    foreach (var value in TestData.GetPossibleValues(failureType))
                    {
                        var result = CreateResult(resultType, failureType, value);

                        var delegateMonitor = new DelegateMonitor();

                        var genericTryMapMethod = resultType.GetMethod("TryMap");

                        if (genericTryMapMethod == null)
                        {
                            throw new MissingMethodException("TryMap method is expected to be implemented");
                        }

                        var tryMapMethod = genericTryMapMethod.MakeGenericMethod(typeof(string));

                        var createStringDelegate =
                            DelegateCreator.CreateDelegate(
                                delegateMonitor,
                                nameof(DelegateMonitor.CreateString),
                                true,
                                typeof(Func <,>),
                                successType,
                                typeof(string));

                        var parameters   = new object?[] { createStringDelegate, null };
                        var isSuccessful = tryMapMethod.Invoke(result, parameters);

                        if (isSuccessful == null)
                        {
                            throw new InvalidOperationException("TryMap should only return true or false");
                        }

                        Assert.IsFalse((bool)isSuccessful);

                        var mapResult = parameters[1];

                        Assert.IsNull(mapResult);

                        Assert.AreEqual(0, delegateMonitor.TotalCalls);
                    }
                }
            }
        }
        public void OnlyFuncForTheSameIndexedParameterTypeIsCalledOnMap()
        {
            foreach (var(voidResult, isSuccess, value, valueType) in AllVoidResultsToTest())
            {
                var voidResultType = voidResult.GetType();

                var delegateMonitor = new DelegateMonitor();

                var failureCreateStringDelegates = GetFailureTypes(voidResultType)
                                                   .Select(genericType =>
                                                           DelegateCreator.CreateDelegate(
                                                               delegateMonitor,
                                                               nameof(DelegateMonitor.CreateString),
                                                               true,
                                                               typeof(Func <,>),
                                                               genericType,
                                                               typeof(string)));

                var successCreateStringDelegate = DelegateCreator.CreateDelegate(
                    delegateMonitor,
                    nameof(DelegateMonitor.CreateString),
                    false,
                    typeof(Func <>),
                    typeof(string));

                var createStringDelegates = failureCreateStringDelegates
                                            .Prepend(successCreateStringDelegate)
                                            .Cast <object>()
                                            .ToArray();

                var genericMapMethod = voidResultType.GetMethods().Single(method => method.Name == "Map" && method.IsGenericMethod);

                if (genericMapMethod == null)
                {
                    throw new InvalidOperationException("Map method not found");
                }

                var mapMethod = genericMapMethod.MakeGenericMethod(typeof(string));

                var mapResult = mapMethod.Invoke(voidResult, createStringDelegates);

                Assert.AreEqual(isSuccess ? "void" : value?.ToString(), mapResult);

                Assert.AreEqual(1, delegateMonitor.TotalCalls);

                Assert.AreEqual(1, isSuccess ? delegateMonitor.GetCalls() : delegateMonitor.GetCalls(valueType !));
            }
        }
        public void OnlyFuncForTheSameIndexedParameterTypeIsCalledOnMap()
        {
            foreach (var(union, value, valueType) in AllUnionsToTest())
            {
                var unionType = union.GetType();

                var delegateMonitor = new DelegateMonitor();

                var createStringDelegates = GetAllTypes(unionType)
                                            .Select(genericType => DelegateCreator.CreateDelegate(
                                                        delegateMonitor,
                                                        nameof(DelegateMonitor.CreateString),
                                                        true,
                                                        typeof(Func <,>),
                                                        genericType,
                                                        typeof(string)))
                                            .Cast <object>()
                                            .ToArray();

                var genericMapMethod = unionType.GetMethods().Single(method => method.Name == "Map" && method.IsGenericMethod);

                if (genericMapMethod == null)
                {
                    throw new InvalidOperationException("Map method not found");
                }

                var mapMethod = genericMapMethod.MakeGenericMethod(typeof(string));

                var mapResult = mapMethod.Invoke(union, createStringDelegates);

                Assert.AreEqual(value?.ToString(), mapResult);

                Assert.AreEqual(1, delegateMonitor.TotalCalls);

                Assert.AreEqual(1, delegateMonitor.GetCalls(valueType));
            }
        }
 public void SetUp()
 {
     _delegateMonitor = new DelegateMonitor();
 }