예제 #1
0
        public async Task TestPercentageParser()
        {
            var argsParser = new ArgsParser();

            argsParser.AddArgumentParser(new PercentageParser());

            Percentage percentageOver100 = await argsParser.Parse <Percentage>(ImmutableList.Create("1234.5678%"));

            Percentage percentageExactly100 = await argsParser.Parse <Percentage>(ImmutableList.Create("100%"));

            Percentage percentageZero = await argsParser.Parse <Percentage>(ImmutableList.Create("0%"));

            Assert.That(percentageOver100.AsPercent, Is.EqualTo(1234.5678));
            Assert.That(percentageOver100.AsDecimal, Is.EqualTo(12.345678));
            Assert.That(percentageExactly100.AsPercent, Is.EqualTo(100.0));
            Assert.That(percentageExactly100.AsDecimal, Is.EqualTo(1.0));
            Assert.That(percentageZero.AsPercent, Is.EqualTo(0.0));
            Assert.That(percentageZero.AsDecimal, Is.EqualTo(0.0));

            ArgsParseFailure failureNoNumber = Assert.ThrowsAsync <ArgsParseFailure>(() =>
                                                                                     argsParser.Parse <Percentage>(ImmutableList.Create("abc%"))) !;
            ArgsParseFailure failureNoPercentSign = Assert.ThrowsAsync <ArgsParseFailure>(() =>
                                                                                          argsParser.Parse <Percentage>(ImmutableList.Create("1.23"))) !;
            ArgsParseFailure failureNegative = Assert.ThrowsAsync <ArgsParseFailure>(() =>
                                                                                     argsParser.Parse <Percentage>(ImmutableList.Create("-5%"))) !;

            Assert.That(failureNoNumber.Message, Is.EqualTo("did not recognize 'abc' as a decimal"));
            Assert.That(failureNoPercentSign.Message, Is.EqualTo("percentages must end in '%'"));
            Assert.That(failureNegative.Message, Is.EqualTo("percentage cannot be negative"));
        }
예제 #2
0
        public async Task TestTimeSpanParser()
        {
            var argsParser = new ArgsParser();

            argsParser.AddArgumentParser(new TimeSpanParser());

            var result1 = await argsParser.Parse <TimeSpan>(args : ImmutableList.Create("8w3d20h48m5s"));

            var result2 = await argsParser.Parse <TimeSpan>(args : ImmutableList.Create("90d"));

            var expected = new TimeSpan(
                days: 8 * 7 + 3,
                hours: 20,
                minutes: 48,
                seconds: 5);

            Assert.That(result1, Is.EqualTo(expected));
            Assert.That(result2, Is.EqualTo(TimeSpan.FromDays(90)));

            ArgsParseFailure ex1 = Assert.ThrowsAsync <ArgsParseFailure>(() => argsParser
                                                                         .Parse <TimeSpan>(args: ImmutableList.Create("5s3d"))) !;
            ArgsParseFailure ex2 = Assert.ThrowsAsync <ArgsParseFailure>(() => argsParser
                                                                         .Parse <TimeSpan>(args: ImmutableList.Create("asdasdasd"))) !;

            Assert.IsTrue(ex1.Message.Contains("did not recognize '5s3d' as a duration"));
            Assert.IsTrue(ex2.Message.Contains("did not recognize 'asdasdasd' as a duration"));
        }
예제 #3
0
        public async Task TestAnyOrderParser()
        {
            var argsParser = new ArgsParser();

            argsParser.AddArgumentParser(new AnyOrderParser(argsParser));
            argsParser.AddArgumentParser(new StringParser());
            argsParser.AddArgumentParser(new SignedIntParser());

            var args1 = ImmutableList.Create("123", "foo");
            var args2 = ImmutableList.Create("foo", "123");

            (SignedInt int1, string string1) = await argsParser.Parse <AnyOrder <SignedInt, string> >(args1);

            (SignedInt int2, string string2) = await argsParser.Parse <AnyOrder <SignedInt, string> >(args2);

            Assert.That((int)int1, Is.EqualTo(123));
            Assert.That((int)int2, Is.EqualTo(123));
            Assert.That(string1, Is.EqualTo("foo"));
            Assert.That(string2, Is.EqualTo("foo"));

            ArgsParseFailure ex = Assert.ThrowsAsync <ArgsParseFailure>(() => argsParser
                                                                        .Parse <AnyOrder <SignedInt, string> >(ImmutableList.Create("foo", "bar"))) !;

            Assert.That(ex.Failures.Count, Is.EqualTo(2));
            Assert.That(ex.Message,
                        Is.EqualTo("did not recognize 'foo' as a number, or did not recognize 'bar' as a number"));
        }
예제 #4
0
        public async Task TestOneOfParser()
        {
            var argsParser = new ArgsParser();

            argsParser.AddArgumentParser(new OneOfParser(argsParser));
            argsParser.AddArgumentParser(new StringParser());
            argsParser.AddArgumentParser(new SignedIntParser());
            argsParser.AddArgumentParser(new InstantParser());

            OneOf <SignedInt, string> result1 =
                await argsParser.Parse <OneOf <SignedInt, string> >(ImmutableList.Create("123"));

            OneOf <SignedInt, string> result2 =
                await argsParser.Parse <OneOf <SignedInt, string> >(ImmutableList.Create("foo"));

            Assert.IsTrue(result1.Item1.IsPresent);
            Assert.IsFalse(result1.Item2.IsPresent);
            Assert.That((int)result1.Item1.Value, Is.EqualTo(123));
            Assert.IsFalse(result2.Item1.IsPresent);
            Assert.IsTrue(result2.Item2.IsPresent);
            Assert.That(result2.Item2.Value, Is.EqualTo("foo"));

            ArgsParseFailure exUnrecognized = Assert.ThrowsAsync <ArgsParseFailure>(() => argsParser
                                                                                    .Parse <OneOf <SignedInt, Instant> >(ImmutableList.Create("foo"))) !;

            Assert.That(exUnrecognized.Failures.Count, Is.EqualTo(2));
            const string errorText = "did not recognize 'foo' as a number, or did not recognize 'foo' as a UTC-instant";

            Assert.That(exUnrecognized.Message, Is.EqualTo(errorText));
            ArgsParseFailure exTooManyArgs = Assert.ThrowsAsync <ArgsParseFailure>(() => argsParser
                                                                                   .Parse <OneOf <SignedInt, SignedInt> >(ImmutableList.Create("123", "234"))) !;

            Assert.That(exTooManyArgs.Message, Is.EqualTo("too many arguments"));
        }
예제 #5
0
        public async Task TestHexColorParser()
        {
            var argsParser = new ArgsParser();

            argsParser.AddArgumentParser(new HexColorParser());

            Assert.That((await argsParser
                         .Parse <HexColor>(args: ImmutableList.Create("#abcdef"))).StringWithHash, Is.EqualTo("#ABCDEF"));
            Assert.That((await argsParser
                         .Parse <HexColor>(args: ImmutableList.Create("abcdef"))).StringWithHash, Is.EqualTo("#ABCDEF"));
            Assert.That((await argsParser
                         .Parse <HexColor>(args: ImmutableList.Create("#0f9D5a"))).StringWithHash, Is.EqualTo("#0F9D5A"));

            ArgsParseFailure ex1 = Assert.ThrowsAsync <ArgsParseFailure>(() => argsParser
                                                                         .Parse <HexColor>(ImmutableList.Create("blabla"))) !;
            ArgsParseFailure ex2 = Assert.ThrowsAsync <ArgsParseFailure>(() => argsParser
                                                                         .Parse <HexColor>(ImmutableList.Create("#abc"))) !;
            ArgsParseFailure ex3 = Assert.ThrowsAsync <ArgsParseFailure>(() => argsParser
                                                                         .Parse <HexColor>(ImmutableList.Create("#bcdefg"))) !;

            Assert.That(ex1.Message, Is.EqualTo("'blabla' is not a valid hex color"));
            Assert.That(ex2.Message, Is.EqualTo("'#abc' must be a 6-character hex code consisting of 0-9 and A-F, " +
                                                "for example '#FF0000' for pure red."));
            Assert.That(ex3.Message, Is.EqualTo("'#bcdefg' must be a 6-character hex code consisting of 0-9 and A-F, " +
                                                "for example '#FF0000' for pure red."));
        }
예제 #6
0
        public async Task TestPkmnSpeciesParser()
        {
            const string speciesId   = "79317";
            const string speciesName = "Uniquamon";
            var          argsParser  = new ArgsParser();
            PkmnSpecies  species     = PkmnSpecies.RegisterName(speciesId, speciesName);

            argsParser.AddArgumentParser(new PkmnSpeciesParser(new[] { species }));

            PkmnSpecies resultById = await argsParser.Parse <PkmnSpecies>(args : ImmutableList.Create("#" + speciesId));

            PkmnSpecies resultByPaddedId = await argsParser.Parse <PkmnSpecies>(args : ImmutableList.Create("#0" + speciesId));

            PkmnSpecies resultByName1 = await argsParser.Parse <PkmnSpecies>(args : ImmutableList.Create(speciesName));

            PkmnSpecies resultByName2 = await argsParser.Parse <PkmnSpecies>(args : ImmutableList.Create("uNiQuAmOn"));

            Assert.AreEqual(species, resultById);
            Assert.AreEqual(species, resultByPaddedId);
            Assert.AreEqual(species, resultByName1);
            Assert.AreEqual(species, resultByName2);

            ArgsParseFailure exNotPrefixed = Assert.ThrowsAsync <ArgsParseFailure>(() => argsParser
                                                                                   .Parse <PkmnSpecies>(args: ImmutableList.Create(speciesId)));

            Assert.AreEqual("Please prefix with '#' to supply and pokedex number", exNotPrefixed.Message);
            ArgsParseFailure exUnknown = Assert.ThrowsAsync <ArgsParseFailure>(() => argsParser
                                                                               .Parse <PkmnSpecies>(args: ImmutableList.Create("unknown")));

            Assert.AreEqual(
                "No pokemon with the name 'unknown' was recognized. Please supply a valid name, " +
                "or prefix with '#' to supply and pokedex number instead", exUnknown.Message);
        }
예제 #7
0
            public void TestBadgesUserNotFound()
            {
                _argsParser.AddArgumentParser(new PkmnSpeciesParser(Array.Empty <PkmnSpecies>()));
                User             user      = MockUser("MockUser");
                ArgsParseFailure exception = Assert.ThrowsAsync <ArgsParseFailure>(() => _badgeCommands.Badges(
                                                                                       new CommandContext(MockMessage(user), ImmutableList.Create("@someone_unknown"), _argsParser)));

                Assert.AreEqual("did not recognize a user with the name 'someone_unknown'", exception.Message);
            }
예제 #8
0
        public void TestSelectUnknownBadge()
        {
            _argsParser.AddArgumentParser(new PkmnSpeciesParser(new PkmnSpecies[0]));
            User             user    = MockUser("MockUser");
            ArgsParseFailure failure = Assert.ThrowsAsync <ArgsParseFailure>(() =>
                                                                             _badgeCommands.SelectBadge(new CommandContext(MockMessage(user),
                                                                                                                           ImmutableList.Create("#123"), _argsParser)));

            Assert.AreEqual("did not recognize species '#123'", failure.Message);
        }
예제 #9
0
        public void TestTooManyArguments()
        {
            var argsParser = new ArgsParser();

            argsParser.AddArgumentParser(new NonNegativeIntParser());

            ArgsParseFailure ex = Assert.ThrowsAsync <ArgsParseFailure>(() => argsParser
                                                                        .Parse <NonNegativeInt>(args: ImmutableList.Create("123", "234"))) !;

            Assert.That(ex.Message, Is.EqualTo("too many arguments"));
        }
예제 #10
0
            public void rejects_negative_if_not_signed()
            {
                ArgsParseFailure exPokeyen = Assert.ThrowsAsync <ArgsParseFailure>(() => _argsParser
                                                                                   .Parse <Pokeyen>(args: ImmutableList.Create("P-11"))) !;

                Assert.That(exPokeyen.Message, Is.EqualTo("'P-11' cannot be less than P0"));
                ArgsParseFailure exTokens = Assert.ThrowsAsync <ArgsParseFailure>(() => _argsParser
                                                                                  .Parse <Tokens>(args: ImmutableList.Create("T-22"))) !;

                Assert.That(exTokens.Message, Is.EqualTo("'T-22' cannot be less than T0"));
            }
예제 #11
0
        public void TestErrorMessageFromRelevantSuccess()
        {
            var argsParser = new ArgsParser();

            argsParser.AddArgumentParser(new NonNegativeIntParser());
            argsParser.AddArgumentParser(new OptionalParser(argsParser));

            ArgsParseFailure ex = Assert.ThrowsAsync <ArgsParseFailure>(() => argsParser
                                                                        .Parse <Optional <NonNegativeInt> >(args: ImmutableList.Create("abc"))) !;

            Assert.That(ex.Message, Is.Not.EqualTo("too many arguments"));
            Assert.That(ex.Message, Is.EqualTo("did not recognize 'abc' as a number"));
        }
예제 #12
0
        public void TestNoDuplicateErrorMessages()
        {
            var argsParser = new ArgsParser();

            argsParser.AddArgumentParser(new NonNegativeIntParser());
            argsParser.AddArgumentParser(new AnyOrderParser(argsParser));

            ArgsParseFailure ex = Assert.ThrowsAsync <ArgsParseFailure>(() => argsParser
                                                                        .Parse <AnyOrder <NonNegativeInt, NonNegativeInt> >(ImmutableList.Create("1", "x"))) !;

            Assert.That(ex.Message, Is.EqualTo("did not recognize 'x' as a number"));
            // this is how it used to be:
            Assert.That(ex.Message, Is.Not.EqualTo("did not recognize 'x' as a number, or did not recognize 'x' as a number"));
        }
예제 #13
0
        public async Task TestBoolParser()
        {
            var argsParser = new ArgsParser();

            argsParser.AddArgumentParser(new BoolParser());

            Assert.That(await argsParser.Parse <bool>(ImmutableList.Create("True")), Is.True);
            Assert.That(await argsParser.Parse <bool>(ImmutableList.Create("YES")), Is.True);
            Assert.That(await argsParser.Parse <bool>(ImmutableList.Create("false")), Is.False);
            Assert.That(await argsParser.Parse <bool>(ImmutableList.Create("nO")), Is.False);

            ArgsParseFailure failure = Assert.ThrowsAsync <ArgsParseFailure>(async() =>
                                                                             await argsParser.Parse <bool>(ImmutableList.Create("?"))) !;

            Assert.That(failure.Message, Is.EqualTo("Did not recognize '?' as a boolean"));
        }
예제 #14
0
        public async Task TestUserParser()
        {
            const string username    = "******";
            const string displayName = "名前";
            var          origUser    = new User(
                id: "1234567890", name: username, twitchDisplayName: displayName, simpleName: username,
                color: null,
                firstActiveAt: Instant.FromUnixTimeSeconds(0), lastActiveAt: Instant.FromUnixTimeSeconds(0),
                lastMessageAt: null, pokeyen: 0, tokens: 0);
            var userRepoMock = new Mock <IUserRepo>();

            userRepoMock
            .Setup(r => r.FindBySimpleName(username))
            .ReturnsAsync(origUser);
            userRepoMock
            .Setup(r => r.FindByDisplayName(displayName))
            .ReturnsAsync(origUser);
            var argsParser = new ArgsParser();

            argsParser.AddArgumentParser(new UserParser(userRepoMock.Object));

            var resultUser = await argsParser.Parse <User>(args : ImmutableList.Create(username));

            Assert.That(resultUser, Is.EqualTo(origUser));
            var resultUserPrefixed = await argsParser.Parse <User>(args : ImmutableList.Create('@' + username));

            Assert.That(resultUserPrefixed, Is.EqualTo(origUser));
            var resultUserDisplayName = await argsParser.Parse <User>(args : ImmutableList.Create(displayName));

            Assert.That(resultUserDisplayName, Is.EqualTo(origUser));

            ArgsParseFailure ex = Assert.ThrowsAsync <ArgsParseFailure>(() => argsParser
                                                                        .Parse <User>(args: ImmutableList.Create("some_unknown_name"))) !;

            Assert.That(ex.Message, Is.EqualTo("did not recognize a user with the name 'some_unknown_name'"));
            ArgsParseFailure exUserPrefixed = Assert.ThrowsAsync <ArgsParseFailure>(() => argsParser
                                                                                    .Parse <User>(args: ImmutableList.Create("@some_unknown_name"))) !;

            Assert.That(exUserPrefixed.Message,
                        Is.EqualTo("did not recognize a user with the name 'some_unknown_name'"));
            ArgsParseFailure exDisplayName = Assert.ThrowsAsync <ArgsParseFailure>(() => argsParser
                                                                                   .Parse <User>(args: ImmutableList.Create("なまえ"))) !;

            Assert.That(exDisplayName.Message, Is.EqualTo("did not recognize a user with the name 'なまえ'"));
        }
예제 #15
0
            public void rejects_wrong_prefix()
            {
                ArgsParseFailure exPokeyen = Assert.ThrowsAsync <ArgsParseFailure>(() => _argsParser
                                                                                   .Parse <Pokeyen>(ImmutableList.Create("X11"))) !;

                Assert.That(exPokeyen.Message, Is.EqualTo("did not recognize 'X11' as a 'P'-prefixed number"));
                ArgsParseFailure exTokens = Assert.ThrowsAsync <ArgsParseFailure>(() => _argsParser
                                                                                  .Parse <Tokens>(ImmutableList.Create("X22"))) !;

                Assert.That(exTokens.Message, Is.EqualTo("did not recognize 'X22' as a 'T'-prefixed number"));
                ArgsParseFailure exSignedPokeyen = Assert.ThrowsAsync <ArgsParseFailure>(() => _argsParser
                                                                                         .Parse <SignedPokeyen>(ImmutableList.Create("X33"))) !;

                Assert.That(exSignedPokeyen.Message, Is.EqualTo("did not recognize 'X33' as a 'P'-prefixed number"));
                ArgsParseFailure exSignedTokens = Assert.ThrowsAsync <ArgsParseFailure>(() => _argsParser
                                                                                        .Parse <SignedTokens>(ImmutableList.Create("X44"))) !;

                Assert.That(exSignedTokens.Message, Is.EqualTo("did not recognize 'X44' as a 'T'-prefixed number"));
            }
예제 #16
0
        public void TestErrorMessageFromDeeplyNestedFailure()
        {
            var argsParser = new ArgsParser();

            argsParser.AddArgumentParser(new NonNegativeIntParser());
            argsParser.AddArgumentParser(new InstantParser());
            argsParser.AddArgumentParser(new AnyOrderParser(argsParser));
            argsParser.AddArgumentParser(new OptionalParser(argsParser));

            ArgsParseFailure ex = Assert.ThrowsAsync <ArgsParseFailure>(() => argsParser
                                                                        .Parse <AnyOrder <Optional <NonNegativeInt>, Optional <Instant> > >(args: ImmutableList.Create("X", "Y"))) !;

            Assert.That(ex.Message, Is.Not.EqualTo("too many arguments"));
            Assert.That(ex.Message, Is.EqualTo("did not recognize 'X' as a number, or did not recognize 'X' as a UTC-instant"));
            Assert.That(ex.Failures, Is.EqualTo(new[]
            {
                new Failure(ErrorRelevanceConfidence.Default, "did not recognize 'X' as a number"),
                new Failure(ErrorRelevanceConfidence.Default, "did not recognize 'X' as a UTC-instant"),
                new Failure(ErrorRelevanceConfidence.Unlikely, "too many arguments")
            }));
        }
예제 #17
0
        public async Task TestPkmnSpeciesParserNameNormalization()
        {
            var         argsParser       = new ArgsParser();
            PkmnSpecies species          = PkmnSpecies.RegisterName("123", "'Mahina: -Pea.");
            Regex       removeCharsRegex = new Regex("[ '-.:]");

            string NormalizeName(string name) => removeCharsRegex.Replace(name, "");

            argsParser.AddArgumentParser(new PkmnSpeciesParser(new[] { species }, NormalizeName));

            PkmnSpecies resultById = await argsParser.Parse <PkmnSpecies>(args : ImmutableList.Create("#123"));

            PkmnSpecies result1 = await argsParser.Parse <PkmnSpecies>(args : ImmutableList.Create("mahina", "pea"));

            PkmnSpecies result2 = await argsParser.Parse <PkmnSpecies>(args : ImmutableList.Create("mahina:", "-pea"));

            PkmnSpecies result3 = await argsParser.Parse <PkmnSpecies>(args : ImmutableList.Create("MAHINA:pea"));

            PkmnSpecies result4 = await argsParser.Parse <PkmnSpecies>(args : ImmutableList.Create("mahinaPEA"));

            PkmnSpecies result5 = await argsParser.Parse <PkmnSpecies>(args : ImmutableList.Create("'mahina-pea."));

            Assert.AreEqual(species, resultById);
            Assert.AreEqual(species, result1);
            Assert.AreEqual(species, result2);
            Assert.AreEqual(species, result3);
            Assert.AreEqual(species, result4);
            Assert.AreEqual(species, result5);

            ArgsParseFailure exNotRecognized = Assert.ThrowsAsync <ArgsParseFailure>(() => argsParser
                                                                                     .Parse <PkmnSpecies>(args: ImmutableList.Create("mahina", "aaaaaa")));

            Assert.AreEqual(
                "No pokemon with the name 'mahina' was recognized. Please supply a valid name, " +
                "or prefix with '#' to supply and pokedex number instead", exNotRecognized.Message);
            ArgsParseFailure exNoAccidentalHashRemoval = Assert.ThrowsAsync <ArgsParseFailure>(() => argsParser
                                                                                               .Parse <PkmnSpecies>(args: ImmutableList.Create("#mahinapea")));

            Assert.AreEqual("did not recognize species '#mahinapea'", exNoAccidentalHashRemoval.Message);
        }
예제 #18
0
        public async Task TestManyOfParser()
        {
            var argsParser = new ArgsParser();

            argsParser.AddArgumentParser(new PositiveIntParser());
            argsParser.AddArgumentParser(new ManyOfParser(argsParser));

            ImmutableList <PositiveInt> threeInts = await argsParser
                                                    .Parse <ManyOf <PositiveInt> >(ImmutableList.Create("1", "3", "2"));

            CollectionAssert.AreEqual(new[] { 1, 3, 2 }, threeInts.Select(i => i.Number));

            ImmutableList <PositiveInt> zeroInts = await argsParser
                                                   .Parse <ManyOf <PositiveInt> >(ImmutableList.Create <string>());

            CollectionAssert.AreEqual(Array.Empty <int>(), zeroInts.Select(i => i.Number));

            ArgsParseFailure failure = Assert.ThrowsAsync <ArgsParseFailure>(() => argsParser
                                                                             .Parse <ManyOf <PositiveInt> >(ImmutableList.Create <string>("1", "c", "2"))) !;

            Assert.That(failure.Message, Is.EqualTo("did not recognize 'c' as a number"));
        }
예제 #19
0
        public async Task TestInstantParser()
        {
            var argsParser = new ArgsParser();

            argsParser.AddArgumentParser(new InstantParser());

            var result1 = await argsParser.Parse <Instant>(args : ImmutableList.Create("2020-03-22", "01:59:20Z"));

            var result2 = await argsParser.Parse <Instant>(args : ImmutableList.Create("2020-03-22T01:59:20Z"));

            Instant refInstant = InstantPattern.General.Parse("2020-03-22T01:59:20Z").Value;

            Assert.That(result1, Is.EqualTo(refInstant));
            Assert.That(result2, Is.EqualTo(refInstant));
            Assert.That(result2, Is.EqualTo(result1));

            ArgsParseFailure ex1 = Assert.ThrowsAsync <ArgsParseFailure>(() => argsParser
                                                                         .Parse <Instant>(ImmutableList.Create("2020-03-22T01:59:20+02"))) !;
            ArgsParseFailure ex2 = Assert.ThrowsAsync <ArgsParseFailure>(() => argsParser
                                                                         .Parse <Instant>(ImmutableList.Create("asdasdasd"))) !;

            Assert.That(ex1.Message, Is.EqualTo("did not recognize '2020-03-22T01:59:20+02' as a UTC-instant"));
            Assert.That(ex2.Message, Is.EqualTo("did not recognize 'asdasdasd' as a UTC-instant"));
        }