public void ConvertFromStringWithHourSpecifier()
        {
            var tsc = new TimeSpanConverter();

            var timeSpan = tsc.ConvertFrom("1H");

            Assert.IsNotNull(timeSpan);
            Assert.IsTrue(timeSpan is TimeSpan);
            Assert.AreEqual(TimeSpan.FromHours(1), (TimeSpan)timeSpan);

            tsc.ConvertFrom("1h");
            Assert.IsNotNull(timeSpan);
            Assert.IsTrue(timeSpan is TimeSpan);
            Assert.AreEqual(TimeSpan.FromHours(1), (TimeSpan)timeSpan);
        }
        public void ConvertFromStringWithMilliSecondSpecifier()
        {
            var tsc      = new TimeSpanConverter();
            var timeSpan = tsc.ConvertFrom("100ms");

            Assert.IsNotNull(timeSpan);
            Assert.IsTrue(timeSpan is TimeSpan);
            Assert.AreEqual(TimeSpan.FromMilliseconds(100), (TimeSpan)timeSpan);
        }
        public void ConvertFrom()
        {
            var tsc      = new TimeSpanConverter();
            var timeSpan = tsc.ConvertFrom("1000");

            Assert.IsNotNull(timeSpan);
            Assert.IsTrue(timeSpan is TimeSpan);
            Assert.AreEqual(TimeSpan.Parse("1000"), (TimeSpan)timeSpan);
        }
        public void BaseConvertFrom()
        {
            var tsc      = new TimeSpanConverter();
            var timeSpan = tsc.ConvertFrom("00:00:10");

            Assert.IsNotNull(timeSpan);
            Assert.IsTrue(timeSpan is TimeSpan);
            Assert.AreEqual(TimeSpan.FromSeconds(10), (TimeSpan)timeSpan);
        }
        public void ConvertFromStringWithMinuteSpecifier()
        {
            var tsc      = new TimeSpanConverter();
            var timeSpan = tsc.ConvertFrom("2m");

            Assert.IsNotNull(timeSpan);
            Assert.IsTrue(timeSpan is TimeSpan);
            Assert.AreEqual(TimeSpan.FromMinutes(2), (TimeSpan)timeSpan);
        }
Esempio n. 6
0
 public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
 {
     try
     {
         var converter = new TimeSpanConverter();
         return(converter.ConvertFrom(value));
     }
     catch
     {
         return(value);//can't convert, let databinding try to handle it
     }
 }
Esempio n. 7
0
        /// <summary>
        /// Parses the specified handler string.
        /// </summary>
        /// <param name="retryExpressionString">The handler string.</param>
        /// <returns></returns>
        protected virtual RetryExceptionHandler Parse(string retryExpressionString)
        {
            ParsedAdviceExpression parsedAdviceExpression = ParseAdviceExpression(retryExpressionString);

            if (!parsedAdviceExpression.Success)
            {
                log.Warn("Could not parse retry expression " + retryExpressionString);
                return(null);
            }

            RetryExceptionHandler handler = new RetryExceptionHandler(parsedAdviceExpression.ExceptionNames);

            handler.ConstraintExpressionText = parsedAdviceExpression.ConstraintExpression;
            handler.ActionExpressionText     = parsedAdviceExpression.AdviceExpression;

            Match match = GetMatchForActionExpression(parsedAdviceExpression.ActionExpressionText, delayRegex);

            if (match.Success)
            {
                handler.MaximumRetryCount = int.Parse(match.Groups[1].Value.Trim());
                handler.IsDelayBased      = true;

                try
                {
                    string ts = match.Groups[3].Value.Trim();
                    handler.DelayTimeSpan = (TimeSpan)timeSpanConverter.ConvertFrom(null, null, ts);
                } catch (Exception)
                {
                    log.Warn("Could not parse timespan " + match.Groups[3].Value.Trim());
                    return(null);
                }
                return(handler);
            }
            else
            {
                match = GetMatchForActionExpression(parsedAdviceExpression.ActionExpressionText, rateRegex);
                if (match.Success)
                {
                    handler.MaximumRetryCount   = int.Parse(match.Groups[1].Value.Trim());
                    handler.IsDelayBased        = false;
                    handler.DelayRateExpression = match.Groups[3].Value.Trim();
                    return(handler);
                }
                else
                {
                    return(null);
                }
            }
        }
Esempio n. 8
0
        /// <summary>
        /// Converts the given value to the <see cref="Duration"/> type.
        /// </summary>
        /// <param name="context">
        /// An <see cref="ITypeDescriptorContext"/> that provides a format context.
        /// </param>
        /// <param name="culture">
        /// The <see cref="CultureInfo"/> to use as the current culture.
        /// </param>
        /// <param name="value">
        /// The object to convert.
        /// </param>
        /// <returns>The returned <see cref="Duration"/>.</returns>
        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            if (value is string stringValue)
            {
                stringValue = stringValue.Trim();

                if (stringValue.Equals("Automatic", StringComparison.OrdinalIgnoreCase))
                {
                    return(Duration.Automatic);
                }
                else if (stringValue.Equals("Forever", StringComparison.OrdinalIgnoreCase))
                {
                    return(Duration.Forever);
                }
            }

            TimeSpan duration = (TimeSpan)TimeSpanConverter.ConvertFrom(context, culture, value);

            return(new Duration(duration));
        }
 public void ConvertFromNullReference()
 {
     try
     {
         var tsc = new TimeSpanConverter();
         tsc.ConvertFrom(null);
     }
     catch (NotSupportedException)
     {
         //.net throws NotSupportedException
         if (SystemUtils.MonoRuntime)
         {
             Assert.Fail("NotSupportedException not expected on mono");
         }
     }
     catch (NullReferenceException)
     {
         //mono throws NullReferenceException
         if (!SystemUtils.MonoRuntime)
         {
             Assert.Fail("NRE not expected on .net");
         }
     }
 }
        public void ConvertFromStringMalformed()
        {
            var tsc = new TimeSpanConverter();

            tsc.ConvertFrom("15a");
        }
        public void ConvertFromNonSupportedOptionBails()
        {
            var tsc = new TimeSpanConverter();

            tsc.ConvertFrom(12);
        }