Exemplo n.º 1
0
        public void HandlerReturnTypeRestriction()
        {
            var service = new CallbackQueryService(new Container(), Substitute.For <ITelegramBotClient>());

            Assert.Null(Record.Exception(() =>
            {
                service.Register("example", typeof(HandlerReturnsMessageResponse));
            }));
            Assert.Null(Record.Exception(() =>
            {
                service.Register("example2", typeof(HandlerReturnsTaskOfMessageResponse));
            }));

            var exception = Assert.Throws <InvalidOperationException>(() =>
            {
                service.Register("oops", typeof(HandlerReturnsUnsupportedTypeA));
            });

            Assert.Equal("The return type of handler should be of type 'Task' or 'Task<string>'", exception.Message);

            exception = Assert.Throws <InvalidOperationException>(() =>
            {
                service.Register("oops", typeof(HandlerReturnsUnsupportedTypeB));
            });

            Assert.Equal("The return type of handler should be of type 'Task' or 'Task<string>'", exception.Message);
        }
Exemplo n.º 2
0
        public async Task HandlerCompilation()
        {
            var service = new CallbackQueryService(new Container(), Substitute.For <ITelegramBotClient>());

            service.Register("example", typeof(ExampleAction));

            var action        = new ExampleAction();
            var callbackQuery = new CallbackQuery();

            var node = service.RegisteredActions["example"];

            var response = await Assert.IsType <Task <string> >(node.Handler(action, callbackQuery, Array.Empty <string>()));

            Assert.Equal("First", response);

            node = node.Next !;
            Assert.NotNull(node);

            response = await Assert.IsType <Task <string> >(node.Handler(action, callbackQuery, new[] { "arg" }));

            Assert.Equal("Arg: arg", response);

            node = node.Next !;
            Assert.NotNull(node);

            response = await Assert.IsType <Task <string> >(node.Handler(action, callbackQuery, Array.Empty <string>()));

            Assert.Equal("fallback", response);

            Assert.Null(node.Next);
        }
Exemplo n.º 3
0
        public void SpecialHandlerArgumentTypes()
        {
            var service = new CallbackQueryService(new Container(), Substitute.For <ITelegramBotClient>());

            Assert.Null(Record.Exception(() => service.Register("example", typeof(HandlerWithSpecialArgumentTypes))));

            var exception = Assert.Throws <InvalidOperationException>(() => service.Register("oops", typeof(HandlerWithUnsupportedArgumentTypes)));

            Assert.Equal("Unsupported parameter type 'Single' in handler 'Unsupported'", exception.Message);
        }
Exemplo n.º 4
0
        public void BanDuplicatedActionHandlers(Type actionType)
        {
            var exception = Assert.Throws <InvalidOperationException>(() =>
            {
                var service = new CallbackQueryService(new Container(), Substitute.For <ITelegramBotClient>());

                service.Register("test", actionType);
            });

            Assert.Equal($"There're duplicated action handler attributes in type '{actionType.Name}'", exception.Message);
        }
Exemplo n.º 5
0
        public void CommandTypeShouldHaveHandlers()
        {
            var exception = Assert.Throws <InvalidOperationException>(() =>
            {
                var service = new CallbackQueryService(new Container(), Substitute.For <ITelegramBotClient>());

                service.Register("test", typeof(WithoutHandlers));
            });

            Assert.Equal("There's no any action handlers in type 'WithoutHandlers'", exception.Message);
        }
Exemplo n.º 6
0
        public async Task HandleUnknownAction()
        {
            var botClient = Substitute.For <ITelegramBotClient>();
            var service   = new CallbackQueryService(new Container(), botClient);

            await service.HandleCallbackQueryAsync(new CallbackQuery()
            {
                Id = "6", Data = "unknown"
            });

            await botClient.Received().AnswerCallbackQueryAsync("6", "该选项已失效或者没有功能实现", true);
        }
Exemplo n.º 7
0
        public void BanDuplicatedRegistrations()
        {
            var service = new CallbackQueryService(new Container(), Substitute.For <ITelegramBotClient>());

            Assert.Null(Record.Exception(() =>
            {
                service.Register("example", typeof(DuplicatedRegistrationsExample));
            }));

            var exception = Assert.Throws <InvalidOperationException>(() =>
            {
                service.Register("example", typeof(DuplicatedRegistrationsExample));
            });

            Assert.Equal("Action with prefix 'example' is registered", exception.Message);
        }
Exemplo n.º 8
0
        public async Task ActionFallback()
        {
            var container = new Container();
            var botClient = Substitute.For <ITelegramBotClient>();
            var service   = new CallbackQueryService(container, botClient);

            container.Register <ActionBase, DispatchingExample>(serviceKey: "example");
            service.Register("example", typeof(DispatchingExample));

            await service.HandleCallbackQueryAsync(new CallbackQuery()
            {
                Id = "1", Data = "example 123"
            });

            await botClient.Received().AnswerCallbackQueryAsync("1", "Number: 123", true);

            await service.HandleCallbackQueryAsync(new CallbackQuery()
            {
                Id = "2", Data = "example abcd"
            });

            await botClient.Received().AnswerCallbackQueryAsync("2", "Letters: abcd", true);

            await service.HandleCallbackQueryAsync(new CallbackQuery()
            {
                Id = "3", Data = "example"
            });

            await botClient.Received().AnswerCallbackQueryAsync("3", "No argument", true);

            await service.HandleCallbackQueryAsync(new CallbackQuery()
            {
                Id = "4", Data = "example 5F625B56-8B66-4510-B5E6-8AF142C7EA16"
            });

            await botClient.Received().AnswerCallbackQueryAsync("4", "Guid: 5f625b56-8b66-4510-b5e6-8af142c7ea16", true);

            await service.HandleCallbackQueryAsync(new CallbackQuery()
            {
                Id = "5", Data = "example ???"
            });

            await botClient.Received().AnswerCallbackQueryAsync("5");

            await Task.CompletedTask;
        }
Exemplo n.º 9
0
        public void FallbackHandlerArgumentsRestriction()
        {
            var service = new CallbackQueryService(new Container(), Substitute.For <ITelegramBotClient>());

            Assert.Null(Record.Exception(() =>
            {
                service.Register("example", typeof(FallbackHandlerWithoutAnyArguments));
            }));
            Assert.Null(Record.Exception(() =>
            {
                service.Register("example2", typeof(FallbackHandlerWithOnlyMessageArgument));
            }));

            var exception = Assert.Throws <InvalidOperationException>(() =>
            {
                service.Register("test", typeof(FallbackHandlerWithUnexpectedArguments));
            });

            Assert.Equal("Fallback handler should have no arguments or only one parameter of type 'CallbackQuery'", exception.Message);
        }
Exemplo n.º 10
0
        public async Task ShouldCallHandlerWithMatchedArgumentCount()
        {
            const string ExceptionMessage = "The argument count doesn't match";

            var service = new CallbackQueryService(new Container(), Substitute.For <ITelegramBotClient>());

            service.Register("example", typeof(ExampleAction));

            var results = new List <object>();

            var action        = new ExampleAction();
            var callbackQuery = new CallbackQuery();

            var node = service.RegisteredActions["example"];

            Assert.Equal(ExceptionMessage, (await Assert.ThrowsAsync <ArgumentException>(() => node.Handler(action, callbackQuery, new[] { "arg" }))).Message);

            node = node.Next !;

            Assert.Equal(ExceptionMessage, (await Assert.ThrowsAsync <ArgumentException>(() => node.Handler(action, callbackQuery, Array.Empty <string>()))).Message);
            Assert.Equal(ExceptionMessage, (await Assert.ThrowsAsync <ArgumentException>(() => node.Handler(action, callbackQuery, new[] { "arg", "arg2" }))).Message);
        }