diff --git a/aiogram/api/types/inline_keyboard_markup.py b/aiogram/api/types/inline_keyboard_markup.py index 43fcec9e..d5dcb5ef 100644 --- a/aiogram/api/types/inline_keyboard_markup.py +++ b/aiogram/api/types/inline_keyboard_markup.py @@ -18,16 +18,18 @@ class InlineKeyboardMarkup(MutableTelegramObject): Source: https://core.telegram.org/bots/api#inlinekeyboardmarkup """ - row_width: int = 3 - inline_keyboard: List[List[InlineKeyboardButton]] = [] + inline_keyboard: List[List[InlineKeyboardButton]] = list() """Array of button rows, each represented by an Array of InlineKeyboardButton objects""" + row_width: int = 3 + """Wight of row. New buttons will fit this limit.""" + def add(self, *args: InlineKeyboardButton) -> InlineKeyboardMarkup: """ Add buttons + :param args: :return: self - :rtype: :obj:`types.InlineKeyboardMarkup` """ row = [] for index, button in enumerate(args, start=1): @@ -42,9 +44,9 @@ class InlineKeyboardMarkup(MutableTelegramObject): def row(self, *args: InlineKeyboardButton) -> InlineKeyboardMarkup: """ Add row + :param args: :return: self - :rtype: :obj:`types.InlineKeyboardMarkup` """ btn_array = [] for button in args: @@ -55,9 +57,9 @@ class InlineKeyboardMarkup(MutableTelegramObject): def insert(self, button: InlineKeyboardButton) -> InlineKeyboardMarkup: """ Insert button to last row + :param button: :return: self - :rtype: :obj:`types.InlineKeyboardMarkup` """ if self.inline_keyboard and len(self.inline_keyboard[-1]) < self.row_width: self.inline_keyboard[-1].append(button) diff --git a/tests/test_api/test_types/test_inline_keyboard_markup.py b/tests/test_api/test_types/test_inline_keyboard_markup.py new file mode 100644 index 00000000..fd357ee8 --- /dev/null +++ b/tests/test_api/test_types/test_inline_keyboard_markup.py @@ -0,0 +1,54 @@ +import pytest + +from aiogram.api.types import InlineKeyboardButton, InlineKeyboardMarkup + +test_row_width = [1, 2, 3, 10] +test_inline_keyboard = [ + [], + [ + [ + InlineKeyboardButton(text="foo", url="https://google.com"), + InlineKeyboardButton(text="foo", url="https://google.com"), + ] + ], +] + + +class TestInlineKeyboardMarkup: + @pytest.mark.parametrize("row_width", test_row_width) + def test_keyboard_markup_add(self, row_width): + kwargs = dict(row_width=row_width, inline_keyboard=[]) + + button = InlineKeyboardButton(text="foo", url="https://google.com") + + keyboard_markup = InlineKeyboardMarkup(**kwargs) + + api_method = keyboard_markup.add(button) + + assert button in api_method.inline_keyboard[0] + + @pytest.mark.parametrize("row_width", test_row_width) + def test_keyboard_markup_row(self, row_width): + kwargs = dict(row_width=row_width, inline_keyboard=[]) + buttons = [ + InlineKeyboardButton(text="foo", url="https://google.com"), + InlineKeyboardButton(text="bar", callback_data="test"), + ] + + keyboard_markup = InlineKeyboardMarkup(**kwargs) + + api_method = keyboard_markup.row(*buttons) + + assert buttons == api_method.inline_keyboard[0] + + @pytest.mark.parametrize("row_width", test_row_width) + @pytest.mark.parametrize("inline_keyboard", test_inline_keyboard) + def test_keyboard_markup_insert(self, row_width, inline_keyboard): + kwargs = dict(row_width=row_width, inline_keyboard=inline_keyboard) + button = InlineKeyboardButton(text="foo", url="https://google.com") + + keyboard_markup = InlineKeyboardMarkup(**kwargs) + + api_method = keyboard_markup.insert(button) + + assert button in api_method.inline_keyboard[0]