Пример #1
0
        public void TryParseFlags_ReturnsFalse_WhenUsingLargeNumber()
        {
            ColorFlagEnum result;

            Assert.IsFalse(FlagEnums.TryParseFlags("128", out result));
            Assert.AreEqual(default(ColorFlagEnum), result);
        }
Пример #2
0
        public void TryParseFlags_ReturnsFalse_WhenUsingUndefinedString()
        {
            ColorFlagEnum result;

            Assert.IsFalse(FlagEnums.TryParseFlags("Turquoise", out result));
            Assert.AreEqual(default(ColorFlagEnum), result);
        }
Пример #3
0
        public void TryParseFlags_ReturnsTrue_WhenUsingInvalidNumber()
        {
            ColorFlagEnum result;

            Assert.IsTrue(FlagEnums.TryParseFlags("16", out result));
            Assert.AreEqual((ColorFlagEnum)16, result);
        }
Пример #4
0
        public void TryParseFlags_ReturnsValidValue_WhenUsingValidNumber()
        {
            ColorFlagEnum result;

            Assert.IsTrue(FlagEnums.TryParseFlags("0", out result));
            Assert.AreEqual(ColorFlagEnum.Black, result);
            Assert.IsTrue(FlagEnums.TryParseFlags("1", out result));
            Assert.AreEqual(ColorFlagEnum.Red, result);
            Assert.IsTrue(FlagEnums.TryParseFlags("+2", out result));
            Assert.AreEqual(ColorFlagEnum.Green, result);
            Assert.IsTrue(FlagEnums.TryParseFlags("3", out result));
            Assert.AreEqual(ColorFlagEnum.Red | ColorFlagEnum.Green, result);
            Assert.IsTrue(FlagEnums.TryParseFlags("4", out result));
            Assert.AreEqual(ColorFlagEnum.Blue, result);
            Assert.IsTrue(FlagEnums.TryParseFlags("+5", out result));
            Assert.AreEqual(ColorFlagEnum.Red | ColorFlagEnum.Blue, result);
            Assert.IsTrue(FlagEnums.TryParseFlags("6", out result));
            Assert.AreEqual(ColorFlagEnum.Green | ColorFlagEnum.Blue, result);
            Assert.IsTrue(FlagEnums.TryParseFlags("7", out result));
            Assert.AreEqual(ColorFlagEnum.Red | ColorFlagEnum.Green | ColorFlagEnum.Blue, result);
            Assert.IsTrue(FlagEnums.TryParseFlags("8", out result));
            Assert.AreEqual(ColorFlagEnum.UltraViolet, result);
            Assert.IsTrue(FlagEnums.TryParseFlags(" Blue ,UltraViolet,+2 , Red", out result));
            Assert.AreEqual(ColorFlagEnum.Red | ColorFlagEnum.Green | ColorFlagEnum.Blue | ColorFlagEnum.UltraViolet, result);
            Assert.IsTrue(FlagEnums.TryParseFlags("15", out result));
            Assert.AreEqual(ColorFlagEnum.All, result);
        }
Пример #5
0
        public void TryParseFlags_ReturnsFalse_WhenUsingWhiteSpaceString()
        {
            ColorFlagEnum result;

            Assert.IsFalse(FlagEnums.TryParseFlags(" ", out result));
            Assert.AreEqual(default(ColorFlagEnum), result);
        }
Пример #6
0
        public void TryParseFlags_ReturnsValidValue_WhenUsingValidName()
        {
            ColorFlagEnum result;

            Assert.IsTrue(FlagEnums.TryParseFlags("Black", out result));
            Assert.AreEqual(ColorFlagEnum.Black, result);
            Assert.IsTrue(FlagEnums.TryParseFlags("Red", out result));
            Assert.AreEqual(ColorFlagEnum.Red, result);
            Assert.IsTrue(FlagEnums.TryParseFlags("Green", out result));
            Assert.AreEqual(ColorFlagEnum.Green, result);
            Assert.IsTrue(FlagEnums.TryParseFlags("Red, Green", out result));
            Assert.AreEqual(ColorFlagEnum.Red | ColorFlagEnum.Green, result);
            Assert.IsTrue(FlagEnums.TryParseFlags("Blue", out result));
            Assert.AreEqual(ColorFlagEnum.Blue, result);
            Assert.IsTrue(FlagEnums.TryParseFlags("Blue , Red", out result));
            Assert.AreEqual(ColorFlagEnum.Red | ColorFlagEnum.Blue, result);
            Assert.IsTrue(FlagEnums.TryParseFlags("Blue , Green", out result));
            Assert.AreEqual(ColorFlagEnum.Green | ColorFlagEnum.Blue, result);
            Assert.IsTrue(FlagEnums.TryParseFlags("Blue , Green , Red", out result));
            Assert.AreEqual(ColorFlagEnum.Red | ColorFlagEnum.Green | ColorFlagEnum.Blue, result);
            Assert.IsTrue(FlagEnums.TryParseFlags("UltraViolet", out result));
            Assert.AreEqual(ColorFlagEnum.UltraViolet, result);
            Assert.IsTrue(FlagEnums.TryParseFlags(" Blue ,UltraViolet,Green , Red", out result));
            Assert.AreEqual(ColorFlagEnum.Red | ColorFlagEnum.Green | ColorFlagEnum.Blue | ColorFlagEnum.UltraViolet, result);
        }
Пример #7
0
        public void TryParseFlags_ReturnsFalse_WhenUsingEmptyString()
        {
            ColorFlagEnum result;

            Assert.IsFalse(FlagEnums.TryParseFlags(string.Empty, out result));
            Assert.AreEqual(default(ColorFlagEnum), result);
        }
Пример #8
0
        public void TryParseFlags_UsesDefaultDelimiter_WhenUsingEmptyStringDelimiter()
        {
            ColorFlagEnum result0;
            ColorFlagEnum result1;

            Assert.AreEqual(FlagEnums.TryParseFlags("Red, Green", out result0), FlagEnums.TryParseFlags("Red, Green", string.Empty, out result1));
            Assert.AreEqual(result0, result1);
        }
Пример #9
0
        public void TryParseFlags_UsesDefaultDelimiter_WhenUsingNullDelimiter()
        {
            ColorFlagEnum result0;
            ColorFlagEnum result1;

            Assert.AreEqual(FlagEnums.TryParseFlags("Red, Green", out result0), FlagEnums.TryParseFlags("Red, Green", ignoreCase: false, null, out result1));
            Assert.AreEqual(result0, result1);
        }
Пример #10
0
        public void TryParseFlags_ReturnsValidValue_WhenUsingValidStringWithCustomDelimiter()
        {
            ColorFlagEnum result;

            Assert.IsTrue(FlagEnums.TryParseFlags("Red", "|", out result));
            Assert.AreEqual(ColorFlagEnum.Red, result);
            Assert.IsTrue(FlagEnums.TryParseFlags("1", "|", out result));
            Assert.AreEqual(ColorFlagEnum.Red, result);
            Assert.IsTrue(FlagEnums.TryParseFlags("Blue | Red", "|", out result));
            Assert.AreEqual(ColorFlagEnum.Red | ColorFlagEnum.Blue, result);
            Assert.IsTrue(FlagEnums.TryParseFlags("Blue | Green", "|", out result));
            Assert.AreEqual(ColorFlagEnum.Green | ColorFlagEnum.Blue, result);
            Assert.IsTrue(FlagEnums.TryParseFlags("Blue | Green | Red", "|", out result));
            Assert.AreEqual(ColorFlagEnum.Red | ColorFlagEnum.Green | ColorFlagEnum.Blue, result);
            Assert.IsTrue(FlagEnums.TryParseFlags(" Blue |UltraViolet|2 | Red", " | ", out result));
            Assert.AreEqual(ColorFlagEnum.Red | ColorFlagEnum.Green | ColorFlagEnum.Blue | ColorFlagEnum.UltraViolet, result);
        }
Пример #11
0
        public void TryParseFlags_ReturnsValidValue_WhenUsingValidStringWithCustomDelimiterWhileIgnoringCase()
        {
            ColorFlagEnum result;

            Assert.IsTrue(FlagEnums.TryParseFlags("black", true, "|", out result));
            Assert.AreEqual(ColorFlagEnum.Black, result);
            Assert.IsTrue(FlagEnums.TryParseFlags("0", true, "|", out result));
            Assert.AreEqual(ColorFlagEnum.Black, result);
            Assert.IsTrue(FlagEnums.TryParseFlags("ReD", true, "|", out result));
            Assert.AreEqual(ColorFlagEnum.Red, result);
            Assert.IsTrue(FlagEnums.TryParseFlags("1", true, "|", out result));
            Assert.AreEqual(ColorFlagEnum.Red, result);
            Assert.IsTrue(FlagEnums.TryParseFlags("GrEeN", true, "|", out result));
            Assert.AreEqual(ColorFlagEnum.Green, result);
            Assert.IsTrue(FlagEnums.TryParseFlags("2", true, "|", out result));
            Assert.AreEqual(ColorFlagEnum.Green, result);
            Assert.IsTrue(FlagEnums.TryParseFlags("RED|green", true, "| ", out result));
            Assert.AreEqual(ColorFlagEnum.Red | ColorFlagEnum.Green, result);
            Assert.IsTrue(FlagEnums.TryParseFlags("3", true, "|", out result));
            Assert.AreEqual(ColorFlagEnum.Red | ColorFlagEnum.Green, result);
            Assert.IsTrue(FlagEnums.TryParseFlags("blue", true, "|", out result));
            Assert.AreEqual(ColorFlagEnum.Blue, result);
            Assert.IsTrue(FlagEnums.TryParseFlags("4", true, "|", out result));
            Assert.AreEqual(ColorFlagEnum.Blue, result);
            Assert.IsTrue(FlagEnums.TryParseFlags("BLUE | red", true, "|", out result));
            Assert.AreEqual(ColorFlagEnum.Red | ColorFlagEnum.Blue, result);
            Assert.IsTrue(FlagEnums.TryParseFlags("5", true, "|", out result));
            Assert.AreEqual(ColorFlagEnum.Red | ColorFlagEnum.Blue, result);
            Assert.IsTrue(FlagEnums.TryParseFlags("BlUe| GReen", true, " | ", out result));
            Assert.AreEqual(ColorFlagEnum.Green | ColorFlagEnum.Blue, result);
            Assert.IsTrue(FlagEnums.TryParseFlags("6", true, "|", out result));
            Assert.AreEqual(ColorFlagEnum.Green | ColorFlagEnum.Blue, result);
            Assert.IsTrue(FlagEnums.TryParseFlags("BluE |    greEn |Red", true, "|", out result));
            Assert.AreEqual(ColorFlagEnum.Red | ColorFlagEnum.Green | ColorFlagEnum.Blue, result);
            Assert.IsTrue(FlagEnums.TryParseFlags("7", true, "|", out result));
            Assert.AreEqual(ColorFlagEnum.Red | ColorFlagEnum.Green | ColorFlagEnum.Blue, result);
            Assert.IsTrue(FlagEnums.TryParseFlags("UltRaVioLet", true, "|", out result));
            Assert.AreEqual(ColorFlagEnum.UltraViolet, result);
            Assert.IsTrue(FlagEnums.TryParseFlags("8", true, "|", out result));
            Assert.AreEqual(ColorFlagEnum.UltraViolet, result);
            Assert.IsTrue(FlagEnums.TryParseFlags(" GREEN |blue|UltraViolet | 1", true, " |", out result));
            Assert.AreEqual(ColorFlagEnum.Red | ColorFlagEnum.Green | ColorFlagEnum.Blue | ColorFlagEnum.UltraViolet, result);
            Assert.IsTrue(FlagEnums.TryParseFlags("15", true, "|", out result));
            Assert.AreEqual(ColorFlagEnum.Red | ColorFlagEnum.Green | ColorFlagEnum.Blue | ColorFlagEnum.UltraViolet, result);
        }
Пример #12
0
        public void TryParseFlags_ReturnsValidValue_WhenUsingValidStringWhileIgnoringCase()
        {
            ColorFlagEnum result;

            Assert.IsTrue(FlagEnums.TryParseFlags("black", true, out result));
            Assert.AreEqual(ColorFlagEnum.Black, result);
            Assert.IsTrue(FlagEnums.TryParseFlags("0", true, out result));
            Assert.AreEqual(ColorFlagEnum.Black, result);
            Assert.IsTrue(FlagEnums.TryParseFlags("ReD", true, out result));
            Assert.AreEqual(ColorFlagEnum.Red, result);
            Assert.IsTrue(FlagEnums.TryParseFlags("1", true, out result));
            Assert.AreEqual(ColorFlagEnum.Red, result);
            Assert.IsTrue(FlagEnums.TryParseFlags("GrEeN", true, out result));
            Assert.AreEqual(ColorFlagEnum.Green, result);
            Assert.IsTrue(FlagEnums.TryParseFlags("2", true, out result));
            Assert.AreEqual(ColorFlagEnum.Green, result);
            Assert.IsTrue(FlagEnums.TryParseFlags("3", true, out result));
            Assert.AreEqual(ColorFlagEnum.Red | ColorFlagEnum.Green, result);
            Assert.IsTrue(FlagEnums.TryParseFlags("blue", true, out result));
            Assert.AreEqual(ColorFlagEnum.Blue, result);
            Assert.IsTrue(FlagEnums.TryParseFlags("4", true, out result));
            Assert.AreEqual(ColorFlagEnum.Blue, result);
            Assert.IsTrue(FlagEnums.TryParseFlags("BLUE , red", true, out result));
            Assert.AreEqual(ColorFlagEnum.Red | ColorFlagEnum.Blue, result);
            Assert.IsTrue(FlagEnums.TryParseFlags("5", true, out result));
            Assert.AreEqual(ColorFlagEnum.Red | ColorFlagEnum.Blue, result);
            Assert.IsTrue(FlagEnums.TryParseFlags("BlUe, GReen", true, out result));
            Assert.AreEqual(ColorFlagEnum.Green | ColorFlagEnum.Blue, result);
            Assert.IsTrue(FlagEnums.TryParseFlags("6", true, out result));
            Assert.AreEqual(ColorFlagEnum.Green | ColorFlagEnum.Blue, result);
            Assert.IsTrue(FlagEnums.TryParseFlags("Red ,    BluE ,greEn", true, out result));
            Assert.AreEqual(ColorFlagEnum.Red | ColorFlagEnum.Green | ColorFlagEnum.Blue, result);
            Assert.IsTrue(FlagEnums.TryParseFlags("7", true, out result));
            Assert.AreEqual(ColorFlagEnum.Red | ColorFlagEnum.Green | ColorFlagEnum.Blue, result);
            Assert.IsTrue(FlagEnums.TryParseFlags("UltRaVioLet", true, out result));
            Assert.AreEqual(ColorFlagEnum.UltraViolet, result);
            Assert.IsTrue(FlagEnums.TryParseFlags("8", true, out result));
            Assert.AreEqual(ColorFlagEnum.UltraViolet, result);
            Assert.IsTrue(FlagEnums.TryParseFlags(" GREEN ,blue,UltraViolet , 1", true, out result));
            Assert.AreEqual(ColorFlagEnum.Red | ColorFlagEnum.Green | ColorFlagEnum.Blue | ColorFlagEnum.UltraViolet, result);
            Assert.IsTrue(FlagEnums.TryParseFlags("15", true, out result));
            Assert.AreEqual(ColorFlagEnum.Red | ColorFlagEnum.Green | ColorFlagEnum.Blue | ColorFlagEnum.UltraViolet, result);
        }
        public async Task <(string, bool, string)> Handle(CallbackQuery data, object context = default, CancellationToken cancellationToken = default)
        {
            var callback = data.Data.Split(':');

            if (callback[0] != "set")
            {
                return(null, false, null);
            }

            if (!await myChatInfo.CandEditPoll(data.Message.Chat, data.From?.Id, cancellationToken))
            {
                return("You can't edit the poll.", true, null);
            }

            var chatId      = data.Message.Chat.Id;
            var settingsSet = myContext.Set <Settings>();

            var allSettings = await settingsSet.GetSettings(chatId).AsTracking().ToListAsync(cancellationToken);

            Settings settings = null;

            switch (callback.ElementAtOrDefault(1))
            {
            case "list":
                return(await Return(await SettingsList(chatId, cancellationToken)));

            case var identifier when int.TryParse(identifier, out var id):
                settings = allSettings.FirstOrDefault(setting => setting.Id == id);

                break;
            }

            if (settings == null)
            {
                // create a new
                settings = new Settings {
                    Chat = chatId, Order = allSettings.Select(_ => _.Order).DefaultIfEmpty(-1).Max() + 1, Format = VoteEnum.Standard
                };
                await settingsSet.AddAsync(settings, cancellationToken);
            }

            string message = null;

            switch (callback.ElementAtOrDefault(2) ?? "")
            {
            case "default":
                settings.Order = allSettings.Select(_ => _.Order).DefaultIfEmpty(1).Min() - 1;
                await myContext.SaveChangesAsync(cancellationToken);

                return(await Return(await SettingsList(chatId, cancellationToken), "Default poll format is changed."));

            case "delete":
                settingsSet.Remove(settings);
                await myContext.SaveChangesAsync(cancellationToken);

                return(await Return(await SettingsList(chatId, cancellationToken), "Poll format is deleted."));

            case var format when FlagEnums.TryParseFlags(format, out VoteEnum toggleVotes, EnumFormat.DecimalValue):
                settings.Format = FlagEnums.ToggleFlags(settings.Format, toggleVotes);

                // adjust ⁺¹
                if (settings.Format.HasAnyFlags(VoteEnum.Plus) && !settings.Format.HasAnyFlags(VoteEnum.Countable))
                {
                    settings.Format = settings.Format.RemoveFlags(VoteEnum.Plus);
                }

                if (await myContext.SaveChangesAsync(cancellationToken) > 0)
                {
                    message = "Poll format is changed.";
                }
                goto default;

            default:
                var buttons = new []
                {
                    VoteEnum.Team,
                    VoteEnum.TeamHarmony,
                    VoteEnum.Plus1,
                    VoteEnum.Remotely,
                    VoteEnum.Invitation,
                    VoteEnum.MayBe,
                    VoteEnum.Yes,
                    VoteEnum.Thumbs,
                    VoteEnum.HarryPotter,
                    VoteEnum.Cancel,
                    VoteEnum.Share
                }
                .Select(format => new []
                {
                    InlineKeyboardButton.WithCallbackData($"{(settings.Format.HasAllFlags(format) ? '☑' : '☐')} {format.Format(new StringBuilder())}", $"{ID}:{settings.Id}:{format:D}")
                });

                if (allSettings.FirstOrDefault() is Settings existingDefault && existingDefault != settings)
                {
                    buttons = buttons.Append(new[]
                    {
                        InlineKeyboardButton.WithCallbackData("Make as a default", $"{ID}:{settings.Id}:default")
                    });
                }

                buttons = buttons
                          .Append(new [] { InlineKeyboardButton.WithCallbackData("Delete", $"{ID}:{settings.Id}:delete") })
                          .Append(new [] { InlineKeyboardButton.WithCallbackData("Back", $"{ID}:list") });

                return(await Return((
                                        settings.Format.Format(new StringBuilder("Selected poll format:").AppendLine()).ToTextMessageContent(),
                                        new InlineKeyboardMarkup(buttons)), message));
            }

            async Task <(string, bool, string)> Return((InputTextMessageContent content, InlineKeyboardMarkup replyMarkup) pass, string notification = "")
            {
                await myTelegramBotClient.EditMessageTextAsync(data.Message.Chat, data.Message.MessageId,
                                                               pass.content.MessageText, pass.content.ParseMode, pass.content.DisableWebPagePreview, pass.replyMarkup, cancellationToken);

                return(notification, false, null);
            }
        }