|
| 1 | +using System.Reflection; |
| 2 | +using Telegram.Bot.Types; |
| 3 | +using TelegramBot.Attributes; |
| 4 | +using TelegramBot.Handlers; |
| 5 | + |
| 6 | +namespace TelegramBot.Tests; |
| 7 | + |
| 8 | +public class TextCommandHandlerTests |
| 9 | +{ |
| 10 | + [Fact] |
| 11 | + public void ExactCommandRouteMatchesParameterlessMethod() |
| 12 | + { |
| 13 | + var handler = CreateHandler("/start", nameof(CommandController.Start)); |
| 14 | + |
| 15 | + Assert.Equal(Method(nameof(CommandController.Start)), handler.GetMethodInfo()); |
| 16 | + Assert.Null(handler.GetArguments()); |
| 17 | + } |
| 18 | + |
| 19 | + [Fact] |
| 20 | + public void CommandRouteConvertsTypedArguments() |
| 21 | + { |
| 22 | + var handler = CreateHandler("/limit 42 true", nameof(CommandController.SetLimit)); |
| 23 | + object[] args = handler.GetArguments()!; |
| 24 | + |
| 25 | + Assert.Equal(Method(nameof(CommandController.SetLimit)), handler.GetMethodInfo()); |
| 26 | + Assert.Collection( |
| 27 | + args, |
| 28 | + value => Assert.Equal(42, Assert.IsType<int>(value)), |
| 29 | + value => Assert.True(Assert.IsType<bool>(value))); |
| 30 | + } |
| 31 | + |
| 32 | + [Fact] |
| 33 | + public void CommandRouteKeepsQuotedArgumentTogether() |
| 34 | + { |
| 35 | + var handler = CreateHandler("/say \"hello world\"", nameof(CommandController.Say)); |
| 36 | + |
| 37 | + Assert.Equal(Method(nameof(CommandController.Say)), handler.GetMethodInfo()); |
| 38 | + Assert.Equal(["hello world"], handler.GetArguments()); |
| 39 | + } |
| 40 | + |
| 41 | + [Fact] |
| 42 | + public void NonCommandTextDoesNotMatchCommandRoute() |
| 43 | + { |
| 44 | + var handler = CreateHandler("start", nameof(CommandController.Start)); |
| 45 | + |
| 46 | + Assert.Null(handler.GetMethodInfo()); |
| 47 | + Assert.Null(handler.GetArguments()); |
| 48 | + } |
| 49 | + |
| 50 | + [Fact] |
| 51 | + public void ArgumentConversionFailureDoesNotMatchCommandRoute() |
| 52 | + { |
| 53 | + var handler = CreateHandler("/limit not-a-number true", nameof(CommandController.SetLimit)); |
| 54 | + |
| 55 | + Assert.Null(handler.GetMethodInfo()); |
| 56 | + } |
| 57 | + |
| 58 | + [Fact] |
| 59 | + public void EqualCommandRoutesThrowAmbiguousMatchException() |
| 60 | + { |
| 61 | + var exception = Assert.Throws<AmbiguousMatchException>(() => |
| 62 | + CreateHandler( |
| 63 | + "/duplicate", |
| 64 | + nameof(CommandController.DuplicateOne), |
| 65 | + nameof(CommandController.DuplicateTwo))); |
| 66 | + |
| 67 | + Assert.Contains("/duplicate", exception.Message); |
| 68 | + } |
| 69 | + |
| 70 | + private static TextCommandHandler CreateHandler(string text, params string[] methodNames) |
| 71 | + { |
| 72 | + return new TextCommandHandler(methodNames.Select(Method).ToArray(), CreateUpdate(text)); |
| 73 | + } |
| 74 | + |
| 75 | + private static MethodInfo Method(string name) |
| 76 | + { |
| 77 | + return typeof(CommandController).GetMethod( |
| 78 | + name, |
| 79 | + BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)!; |
| 80 | + } |
| 81 | + |
| 82 | + private static Update CreateUpdate(string text) |
| 83 | + { |
| 84 | + return new Update |
| 85 | + { |
| 86 | + Message = new Message |
| 87 | + { |
| 88 | + Text = text, |
| 89 | + }, |
| 90 | + }; |
| 91 | + } |
| 92 | + |
| 93 | + private sealed class CommandController |
| 94 | + { |
| 95 | + [TextCommand("/start")] |
| 96 | + public void Start() |
| 97 | + { |
| 98 | + } |
| 99 | + |
| 100 | + [TextCommand("/limit")] |
| 101 | + public void SetLimit(int limit, bool enabled) |
| 102 | + { |
| 103 | + } |
| 104 | + |
| 105 | + [TextCommand("/say")] |
| 106 | + public void Say(string text) |
| 107 | + { |
| 108 | + } |
| 109 | + |
| 110 | + [TextCommand("/duplicate")] |
| 111 | + public void DuplicateOne() |
| 112 | + { |
| 113 | + } |
| 114 | + |
| 115 | + [TextCommand("/duplicate")] |
| 116 | + public void DuplicateTwo() |
| 117 | + { |
| 118 | + } |
| 119 | + } |
| 120 | +} |
0 commit comments