Beispiel #1
0
        /// <summary>
        /// Converts the current distance to a distance of another type.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <returns>
        /// A converted Distance
        /// </returns>
        public override Distance ConvertTo(Type type)
        {
            if (this.GetType() == type)
            {
                return(Distance.Build(type, this.Value));
            }

            if (typeof(MetricDistance).IsAssignableFrom(type))
            {
                int first  = (int)this.GetType().GetField("Factor").GetValue(null);
                int second = (int)type.GetField("Factor").GetValue(null);

                int factor = Math.Max(first, second) - Math.Min(first, second);
                if (second > first)
                {
                    factor *= -1;
                }

                return(Distance.Build(type, this.Value * (decimal)Math.Pow(10, factor)));
            }
            else
            {
                return(Distance.Build <Yards>(this.ConvertTo <Meters>().Value *MetricDistance.MeterToYardRatio).ConvertTo(type));
            }
        }
Beispiel #2
0
        public void When_comparing_distances_GreaterOrEqualTo_Then_works(decimal d1, Type t1, decimal d2, Type t2, bool expected)
        {
            var distance1 = Distance.Build(t1, d1);
            var distance2 = Distance.Build(t2, d2);

            Assert.That(distance1 >= distance2, Is.EqualTo(expected));
        }
Beispiel #3
0
        public void When_building_Then_returns_proper_distance()
        {
            var actual = Distance.Build <Kilometers>(25);

            Assert.That(actual, Is.InstanceOf <Kilometers>());
            Assert.That(actual.Value, Is.EqualTo(25));
        }
        /// <summary>
        /// Converts the current distance to a distance of another type.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <returns>
        /// A converted Distance
        /// </returns>
        /// <exception cref="System.NotSupportedException">
        ///     If the type is not supported for conversion.
        /// </exception>
        public override Distance ConvertTo(Type type)
        {
            if (this.GetType() == type)
            {
                return(Distance.Build(type, this.Value));
            }

            if (typeof(ImperialDistance).IsAssignableFrom(type))
            {
                if (!ConversionFactors.Values.ContainsKey(this.GetType()))
                {
                    throw new NotSupportedException(string.Format(Properties.Strings.ImperialDistance_TypeConversionNotSupported, this.ToString(), type.Name));
                }
                if (!ConversionFactors.Values.ContainsKey(type))
                {
                    throw new NotSupportedException(string.Format(Properties.Strings.ImperialDistance_TypeConversionNotSupported, this.ToString(), type.Name));
                }
                return((Distance)Activator.CreateInstance(type, this.Value * ConversionFactors.Values[this.GetType()][type]));
            }
            else
            {
                return(Distance.Build <Meters>(this.ConvertTo <Yards>().Value / MetricDistance.MeterToYardRatio).ConvertTo(type));
            }
        }