public void WhenHandlerWithTokenIsPreservedUponBuild()
        {
            Func <CallRecordingConnection, object, CancellationToken, Task> handler =
                (connection, message, token) =>
            {
                connection.RecordCall(1, message, token);
                return(TaskFactory());
            };

            var result = _sut.When(handler).Build();

            Assert.That(
                result.Count(_ =>
            {
                if (_.Message == typeof(object))
                {
                    var msg      = new object();
                    var recorder = new CallRecordingConnection();
                    var token    = new CancellationToken();
                    _.Handler(recorder, msg, token).Wait();
                    return(recorder.RecordedCalls.SequenceEqual(new[]
                    {
                        new Tuple <int, object, CancellationToken>(
                            1, msg, token)
                    }));
                }
                return(false);
            }),
                Is.EqualTo(1));
        }
        public void WhenSyncHandlerIsPreservedUponBuild()
        {
            Action <CallRecordingConnection, object> handler =
                (connection, message) =>
            {
                connection.RecordCall(1, message, CancellationToken.None);
            };

            var result = _sut.When(handler).Build();

            Assert.That(
                result.Count(_ =>
            {
                if (_.Message == typeof(object))
                {
                    var msg      = new object();
                    var recorder = new CallRecordingConnection();
                    _.Handler(recorder, msg, CancellationToken.None).Wait();
                    return(recorder.RecordedCalls.SequenceEqual(new[]
                    {
                        new Tuple <int, object, CancellationToken>(
                            1, msg, CancellationToken.None)
                    }));
                }
                return(false);
            }),
                Is.EqualTo(1));
        }
        public async Task ProjectAsyncMessageCausesExpectedCalls(
            ConnectedProjectionHandlerResolver <CallRecordingConnection> resolver,
            object message,
            Tuple <int, object, CancellationToken>[] expectedCalls)
        {
            var connection = new CallRecordingConnection();
            var sut        = SutFactory(resolver);

            await sut.ProjectAsync(connection, message);

            Assert.That(connection.RecordedCalls, Is.EquivalentTo(expectedCalls));
        }
        public void WhenHandlerWithoutTokenPreservesPreviouslyRegisteredHandlersUponBuild()
        {
            Func <CallRecordingConnection, object, Task> handler1 =
                (connection, message) =>
            {
                connection.RecordCall(1, message, CancellationToken.None);
                return(TaskFactory());
            };

            Func <CallRecordingConnection, object, Task> handler2 =
                (connection, message) =>
            {
                connection.RecordCall(2, message, CancellationToken.None);
                return(TaskFactory());
            };

            var result = _sut.When(handler1).When(handler2).Build();

            Assert.That(
                result.Count(_ =>
            {
                if (_.Message == typeof(object))
                {
                    var msg      = new object();
                    var recorder = new CallRecordingConnection();
                    _.Handler(recorder, msg, CancellationToken.None).Wait();
                    return(recorder.RecordedCalls.SequenceEqual(new[]
                    {
                        new Tuple <int, object, CancellationToken>(
                            1, msg, CancellationToken.None)
                    }));
                }
                return(false);
            }),
                Is.EqualTo(1));
        }
        public void WhenHandlerWithoutTokenIsPreservedUponBuild()
        {
            Func<CallRecordingConnection,object, Task> handler =
                (connection, message) =>
                {
                    connection.RecordCall(1, message, CancellationToken.None);
                    return TaskFactory();
                };

            var result = _sut.When(handler).Build();

            Assert.That(
                result.Count(_ =>
                {
                    if (_.Message == typeof (object))
                    {
                        var msg = new object();
                        var recorder = new CallRecordingConnection();
                        _.Handler(recorder, msg, CancellationToken.None).Wait();
                        return recorder.RecordedCalls.SequenceEqual(new[]
                        {
                            new Tuple<int, object, CancellationToken>(
                                1, msg, CancellationToken.None)
                        });
                    }
                    return false;
                }),
                Is.EqualTo(1));
        }
        public void WhenHandlerWithTokenPreservesPreviouslyRegisteredHandlersUponBuild()
        {
            Func<CallRecordingConnection, object, CancellationToken, Task> handler1 =
                (connection, message, token) =>
                {
                    connection.RecordCall(1, message, token);
                    return TaskFactory();
                };

            Func<CallRecordingConnection, object, CancellationToken, Task> handler2 =
                (connection, message, token) =>
                {
                    connection.RecordCall(2, message, token);
                    return TaskFactory();
                };

            var result = _sut.When(handler1).When(handler2).Build();

            Assert.That(
                result.Count(_ =>
                {
                    if (_.Message == typeof(object))
                    {
                        var msg = new object();
                        var recorder = new CallRecordingConnection();
                        var token = new CancellationToken();
                        _.Handler(recorder, msg, token).Wait();
                        return recorder.RecordedCalls.SequenceEqual(new[]
                        {
                            new Tuple<int, object, CancellationToken>(
                                1, msg, token)
                        });
                    }
                    return false;
                }),
                Is.EqualTo(1));
        }