예제 #1
0
        [InlineData("-1s", true)]          // tests format "-%s's'"
        public void TestTimeSpanParse(string input, bool isNegative)
        {
            var reader = new TimeSpanTypeReader();
            var result = reader.ReadAsync(null, input, null).Result;

            Assert.True(result.IsSuccess);

            var actual = (TimeSpan)result.BestMatch;

            Assert.True(actual != TimeSpan.Zero);

            if (isNegative)
            {
                Assert.True(actual < TimeSpan.Zero);

                Assert.True(actual.Seconds == 0 || actual.Seconds == -1);
                Assert.True(actual.Minutes == 0 || actual.Minutes == -2);
                Assert.True(actual.Hours == 0 || actual.Hours == -3);
                Assert.True(actual.Days == 0 || actual.Days == -4);
            }
            else
            {
                Assert.True(actual > TimeSpan.Zero);

                Assert.True(actual.Seconds == 0 || actual.Seconds == 1);
                Assert.True(actual.Minutes == 0 || actual.Minutes == 2);
                Assert.True(actual.Hours == 0 || actual.Hours == 3);
                Assert.True(actual.Days == 0 || actual.Days == 4);
            }
        }
예제 #2
0
        public void TryParseTimeSpan_GivenInvalidInput_ReturnsFalse(string input)
        {
            var uut = new TimeSpanTypeReader();

            var succeeded = uut.TryParseTimeSpan(input, out _);

            succeeded.ShouldBeFalse();
        }
예제 #3
0
        public void TryParseTimeSpan_GivenValidInput_SuccessfullyParses(string input, TimeSpan expected)
        {
            var uut = new TimeSpanTypeReader();

            var succeeded = uut.TryParseTimeSpan(input, out var actual);

            succeeded.ShouldBeTrue();
            actual.ShouldBe(expected);
        }
예제 #4
0
        public Task TempMute(
            [Summary("The user to be muted.")]
            IGuildUser subject,
            [Summary("The duration of the mute.")]
            string durationString,
            [Summary("The reason for the mute.")]
            [Remainder]
            string reason)
        {
            // TODO: Remove when we port to 2.0
            var duration = TimeSpanTypeReader.Read(durationString);

            if (!duration.HasValue)
            {
                throw new ArgumentException("Invalid Timespan Format");
            }

            return(ModerationService.CreateInfractionAsync(InfractionType.Mute, subject.Id, reason, duration.Value));
        }
예제 #5
0
        /// <summary>
        ///     Initializes a new <see cref="CommandService" /> class with the provided configuration.
        /// </summary>
        /// <param name="config">The configuration class.</param>
        /// <exception cref="InvalidOperationException">
        ///     The <see cref="RunMode" /> cannot be set to <see cref="RunMode.Default" />.
        /// </exception>
        public CommandService(CommandServiceConfig config)
        {
            _caseSensitive         = config.CaseSensitiveCommands;
            _throwOnError          = config.ThrowOnError;
            _ignoreExtraArgs       = config.IgnoreExtraArgs;
            _separatorChar         = config.SeparatorChar;
            _defaultRunMode        = config.DefaultRunMode;
            _quotationMarkAliasMap = (config.QuotationMarkAliasMap ?? new Dictionary <char, char>()).ToImmutableDictionary();
            if (_defaultRunMode == RunMode.Default)
            {
                throw new InvalidOperationException("The default run mode cannot be set to Default.");
            }

            _moduleLock      = new SemaphoreSlim(1, 1);
            _typedModuleDefs = new ConcurrentDictionary <Type, ModuleInfo>();
            _moduleDefs      = new HashSet <ModuleInfo>();
            _map             = new CommandMap(this);
            _typeReaders     = new ConcurrentDictionary <Type, ConcurrentDictionary <Type, TypeReader> >();

            _defaultTypeReaders = new ConcurrentDictionary <Type, TypeReader>();

            foreach (var type in PrimitiveParsers.SupportedTypes)
            {
                _defaultTypeReaders[type] = PrimitiveTypeReader.Create(type);
                _defaultTypeReaders[typeof(Nullable <>).MakeGenericType(type)] = NullableTypeReader.Create(type, _defaultTypeReaders[type]);
            }

            var tsreader = new TimeSpanTypeReader();

            _defaultTypeReaders[typeof(TimeSpan)]  = tsreader;
            _defaultTypeReaders[typeof(TimeSpan?)] = NullableTypeReader.Create(typeof(TimeSpan), tsreader);

            _defaultTypeReaders[typeof(string)] =
                new PrimitiveTypeReader <string>((string x, out string y) =>
            {
                y = x;
                return(true);
            }, 0);
        }
예제 #6
0
 public static TimeSpan?ToTime(this string s)
 => TimeSpanTypeReader.ParseTime(s);