< Summary

Information
Class: TeleFlow.Telegram.ReplyKeyboard
Assembly: TeleFlow.Framework
File(s): /_/src/TeleFlow.Framework/ReplyKeyboard.cs
Line coverage
97%
Covered lines: 40
Uncovered lines: 1
Coverable lines: 41
Total lines: 111
Line coverage: 97.5%
Branch coverage
83%
Covered branches: 5
Total branches: 6
Branch coverage: 83.3%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor()100%11100%
Create()100%11100%
Button(...)100%11100%
Button(...)50%2280%
Row()100%22100%
Persistent(...)100%11100%
Resize(...)100%11100%
OneTime(...)100%11100%
Placeholder(...)100%11100%
Selective(...)100%11100%
ToMarkup()100%22100%
get_CurrentRow()100%11100%

File(s)

/_/src/TeleFlow.Framework/ReplyKeyboard.cs

#LineLine coverage
 1using TeleFlow.Telegram.Internal;
 2using TeleFlow.Telegram.Schema.Types;
 3
 4namespace TeleFlow.Telegram;
 5
 6public sealed class ReplyKeyboard
 7{
 58    private readonly List<List<KeyboardButton>> _rows = [[]];
 9    private bool? _isPersistent;
 10    private bool? _resizeKeyboard;
 11    private bool? _oneTimeKeyboard;
 12    private string? _inputFieldPlaceholder;
 13    private bool? _selective;
 14
 15    private ReplyKeyboard()
 16    {
 517    }
 18
 19    public static ReplyKeyboard Create()
 20    {
 521        return new ReplyKeyboard();
 22    }
 23
 24    public ReplyKeyboard Button(string text)
 25    {
 626        ArgumentException.ThrowIfNullOrWhiteSpace(text);
 27
 628        CurrentRow.Add(new KeyboardButton { Text = text });
 629        return this;
 30    }
 31
 32    public ReplyKeyboard Button(KeyboardButton button)
 33    {
 134        ArgumentNullException.ThrowIfNull(button);
 35
 136        if (string.IsNullOrWhiteSpace(button.Text))
 37        {
 038            throw new ArgumentException("Reply keyboard button text must not be empty.", nameof(button));
 39        }
 40
 141        CurrentRow.Add(button);
 142        return this;
 43    }
 44
 45    public ReplyKeyboard Row()
 46    {
 347        if (CurrentRow.Count > 0)
 48        {
 249            _rows.Add([]);
 50        }
 51
 352        return this;
 53    }
 54
 55    public ReplyKeyboard Persistent(bool value = true)
 56    {
 157        _isPersistent = value;
 158        return this;
 59    }
 60
 61    public ReplyKeyboard Resize(bool value = true)
 62    {
 263        _resizeKeyboard = value;
 264        return this;
 65    }
 66
 67    public ReplyKeyboard OneTime(bool value = true)
 68    {
 169        _oneTimeKeyboard = value;
 170        return this;
 71    }
 72
 73    public ReplyKeyboard Placeholder(string placeholder)
 74    {
 275        TelegramKeyboardPlaceholderValidator.ValidateReplyKeyboardInputFieldPlaceholder(placeholder);
 76
 177        _inputFieldPlaceholder = placeholder;
 178        return this;
 79    }
 80
 81    public ReplyKeyboard Selective(bool value = true)
 82    {
 183        _selective = value;
 184        return this;
 85    }
 86
 87    public ReplyKeyboardMarkup ToMarkup()
 88    {
 489        var rows = _rows
 690            .Where(static row => row.Count > 0)
 491            .Select(static row => row.ToArray())
 492            .ToArray();
 93
 494        if (rows.Length == 0)
 95        {
 196            throw new InvalidOperationException("Reply keyboard must contain at least one button.");
 97        }
 98
 399        return new ReplyKeyboardMarkup
 3100        {
 3101            Keyboard = rows,
 3102            IsPersistent = _isPersistent,
 3103            ResizeKeyboard = _resizeKeyboard,
 3104            OneTimeKeyboard = _oneTimeKeyboard,
 3105            InputFieldPlaceholder = _inputFieldPlaceholder,
 3106            Selective = _selective
 3107        };
 108    }
 109
 10110    private List<KeyboardButton> CurrentRow => _rows[^1];
 111}