< Summary

Information
Class: TeleFlow.Telegram.Internal.TelegramBotUsernameNormalizer
Assembly: TeleFlow.Telegram.Client
File(s): /_/src/TeleFlow.Telegram.Client/Internal/TelegramBotUsernameNormalizer.cs
Line coverage
100%
Covered lines: 23
Uncovered lines: 0
Coverable lines: 23
Total lines: 55
Line coverage: 100%
Branch coverage
92%
Covered branches: 24
Total branches: 26
Branch coverage: 92.3%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
TryNormalize(...)92.85%1414100%
IsTelegramUsernameCharacter(...)91.66%1212100%

File(s)

/_/src/TeleFlow.Telegram.Client/Internal/TelegramBotUsernameNormalizer.cs

#LineLine coverage
 1namespace TeleFlow.Telegram.Internal;
 2
 3internal static class TelegramBotUsernameNormalizer
 4{
 5    private const int MinLength = 5;
 6    private const int MaxLength = 32;
 7
 8    public static bool TryNormalize(
 9        string? botUsername,
 10        out string? normalizedUsername,
 11        out string? error)
 12    {
 104113        if (botUsername is null)
 14        {
 94415            normalizedUsername = null;
 94416            error = null;
 94417            return true;
 18        }
 19
 9720        var trimmed = botUsername.Trim();
 9721        if (trimmed.StartsWith('@'))
 22        {
 823            trimmed = trimmed[1..];
 24        }
 25
 9726        if (trimmed.Length is < MinLength or > MaxLength)
 27        {
 328            normalizedUsername = null;
 329            error = $"Telegram bot username must be between {MinLength} and {MaxLength} characters.";
 330            return false;
 31        }
 32
 186233        foreach (var character in trimmed)
 34        {
 83835            if (!IsTelegramUsernameCharacter(character))
 36            {
 237                normalizedUsername = null;
 238                error = "Telegram bot username can contain only ASCII letters, digits, and underscores.";
 239                return false;
 40            }
 41        }
 42
 9243        normalizedUsername = trimmed;
 9244        error = null;
 9245        return true;
 46    }
 47
 48    private static bool IsTelegramUsernameCharacter(char character)
 49    {
 83850        return character is >= 'A' and <= 'Z' ||
 83851               character is >= 'a' and <= 'z' ||
 83852               character is >= '0' and <= '9' ||
 83853               character == '_';
 54    }
 55}