/// <summary>
 /// Gets the math ml.
 /// </summary>
 /// <param name="quantity">The quantity.</param>
 /// <param name="unitFormat">The unit format.</param>
 /// <param name="multiplicationSign">The multiplication sign.</param>
 /// <returns>The root math ml element.</returns>
 public static XElement ToMathML(
     this IQuantity quantity,
     UnitFormat unitFormat = UnitFormat.Default,
     MultiplicationSign multiplicationSign = MultiplicationSign.Invisible)
 {
     return QuantityToMathMLConverter.DefaultConverter.GetMathML(quantity, unitFormat, multiplicationSign);
 }
        public void Velocity_ToString_When_UnitModeIsSpecified_Then_ResultShouldBeExpected(
            UnitFormat unitFormat,
            string expected)
        {
            var testee = new Velocity(1, x => x.Kilo.Meters / x.Hours);

            var result = testee.ToString(unitFormat);

            result.Should().Be(expected);
        }
        public void Force_ToString_When_UnitModeIsSpecified_Then_ResultShouldBeExpected(
            UnitFormat unitFormat,
            string expected)
        {
            var testee = new Force(1, x => x.Newtons);

            var result = testee.ToString(unitFormat);

            result.Should().Be(expected);
        }
Beispiel #4
0
        /// <summary>
        /// The be approximately.
        /// </summary>
        /// <param name="quantityAssertions">
        /// The quantity assertions.
        /// </param>
        /// <param name="expectedValue">
        /// The expected value.
        /// </param>
        /// <param name="expectedUnit">
        /// The expected unit.
        /// </param>
        /// <param name="unitFormat">
        /// The unit format.
        /// </param>
        /// <param name="precision">
        /// The precision.
        /// </param>
        /// <returns>
        /// The <see cref="AndConstraint{T}"/>.
        /// </returns>
        public static AndConstraint <ComparableTypeAssertions <IQuantity> > BeApproximately(
            this ComparableTypeAssertions <IQuantity> quantityAssertions,
            double expectedValue,
            string expectedUnit,
            UnitFormat unitFormat,
            double precision)
        {
            var quantity      = (IQuantity)quantityAssertions.Subject;
            var quantityValue = quantity.Value;
            var quantityUnit  = UnitFormatHelper.GetNotation(quantity.Unit, unitFormat);

            Execute.Assertion.ForCondition(quantityValue == expectedValue || Math.Abs(quantityValue - expectedValue) < precision)
            .FailWith("The value: {0}, but found: {1}", expectedValue, quantityValue);
            AssertUnit(quantityUnit, expectedUnit);

            return(new AndConstraint <ComparableTypeAssertions <IQuantity> >(quantityAssertions));
        }
Beispiel #5
0
        public static string FormatUnits(double value, UnitFormat unitFormat, int unitPrecision)
        {
            var prefix = Math.Sign(value) < 0 ? "-" : "";

            value = Math.Abs(value);
            switch (unitFormat)
            {
            case UnitFormat.Architectural:
                return(string.Concat(prefix, FormatArchitectural(value, unitPrecision)));

            case UnitFormat.Metric:
                return(string.Concat(prefix, FormatMetric(value, unitPrecision)));

            default:
                throw new ArgumentException("value");
            }
        }
        /// <summary>
        /// The be.
        /// </summary>
        /// <param name="quantityAssertions">
        /// The quantity assertions.
        /// </param>
        /// <param name="expectedValue">
        /// The expected value.
        /// </param>
        /// <param name="expectedUnit">
        /// The expected unit.
        /// </param>
        /// <param name="unitFormat">
        /// The unit format.
        /// </param>
        /// <returns>
        /// The <see cref="AndConstraint{T}"/>.
        /// </returns>
        public static AndConstraint<ComparableTypeAssertions<IQuantity>> Be(
            this ComparableTypeAssertions<IQuantity> quantityAssertions,
            double expectedValue,
            string expectedUnit,
            UnitFormat unitFormat)
        {
            var quantity = (IQuantity)quantityAssertions.Subject;
            var quantityValue = quantity.Value;
            var quantityUnit = UnitHelper.GetNotation(quantity.Unit, unitFormat);
            Execute.Assertion.ForCondition(quantityValue.Equals(expectedValue))
                .FailWith(
                    "The value: {0}, but found: {1}",
                    expectedValue.ToString("N20"),
                    quantityValue.ToString("N20"));
            AssertUnit(quantityUnit, expectedUnit);

            return new AndConstraint<ComparableTypeAssertions<IQuantity>>(quantityAssertions);
        }
Beispiel #7
0
        /// <summary>
        /// The be.
        /// </summary>
        /// <param name="quantityAssertions">
        /// The quantity assertions.
        /// </param>
        /// <param name="expectedValue">
        /// The expected value.
        /// </param>
        /// <param name="expectedUnit">
        /// The expected unit.
        /// </param>
        /// <param name="unitFormat">
        /// The unit format.
        /// </param>
        /// <returns>
        /// The <see cref="AndConstraint{T}"/>.
        /// </returns>
        public static AndConstraint <ComparableTypeAssertions <IQuantity> > Be(
            this ComparableTypeAssertions <IQuantity> quantityAssertions,
            double expectedValue,
            string expectedUnit,
            UnitFormat unitFormat)
        {
            var quantity      = (IQuantity)quantityAssertions.Subject;
            var quantityValue = quantity.Value;
            var quantityUnit  = UnitFormatHelper.GetNotation(quantity.Unit, unitFormat);

            Execute.Assertion.ForCondition(quantityValue.Equals(expectedValue))
            .FailWith(
                "The value: {0}, but found: {1}",
                expectedValue.ToString("N20"),
                quantityValue.ToString("N20"));
            AssertUnit(quantityUnit, expectedUnit);

            return(new AndConstraint <ComparableTypeAssertions <IQuantity> >(quantityAssertions));
        }
Beispiel #8
0
        public DrawingSettings(string path, UnitFormat unitFormat, int unitPrecision)
        {
            this.fileName      = path;
            this.unitFormat    = unitFormat;
            this.unitPrecision = unitPrecision < 0 ? 0 : unitPrecision;

            switch (unitFormat)
            {
            case UnitFormat.Architectural:
                // only allowable values are 0, 2, 4, 8, 16, 32
                this.unitPrecision = AllowedArchitecturalPrecisions.Where(x => x <= this.unitPrecision).Max();
                break;

            case UnitFormat.Metric:
                // only allowable values are [0, 16]
                this.unitPrecision = Math.Max(0, this.unitPrecision);
                this.unitPrecision = Math.Min(16, this.unitPrecision);
                break;
            }
        }
Beispiel #9
0
        public void Quantity_Multiplication_Then_ResultShouldBeExpected(
            double lhs,
            string lhsUnit,
            double rhs,
            string rhsUnit,
            UnitFormat unitFormat,
            double expectedValue,
            string expectedUnit)
        {
            var testee1 = new Quantity(
                lhs,
                UnitSystem.GetUnitFrom(lhsUnit, ParseSettings.DefaultInvariantCulture).Value);
            var testee2 = new Quantity(
                rhs,
                UnitSystem.GetUnitFrom(rhsUnit, ParseSettings.DefaultInvariantCulture).Value);

            var result = testee1 * testee2;

            result.Should().Be(expectedValue, expectedUnit, unitFormat);
        }
        /// <summary>
        /// Gets the math ml.
        /// </summary>
        /// <param name="quantity">The quantity.</param>
        /// <param name="unitFormat">The unit format.</param>
        /// <param name="multiplicationSign">The multiplication sign.</param>
        /// <returns>The root <see cref="XElement"/> node.</returns>
        public XElement GetMathML(
            IQuantity quantity,
            UnitFormat unitFormat = UnitFormat.Default,
            MultiplicationSign multiplicationSign = MultiplicationSign.Invisible)
        {
            var mn = new XElement(MathML.Mn, quantity.Value);
            var mrow = new XElement(MathML.Mrow, mn);
            var unitElement = mrow;
            if (unitFormat.HasFlag(UnitFormat.SurroundInBrackets))
            {
                var mfenced = new XElement(MathML.Mfenced, MathML.OpenLeftBracket, MathML.CloseRightBrack);
                mrow.Add(mfenced);
                unitElement = mfenced;
            }

            ExpressionToMathMLVisitor.DefaultMathMLVisitor.Visit(
                quantity.Unit.GetExpression(),
                multiplicationSign,
                unitElement);
            return new XElement(MathML.Math, new XAttribute(MathML.NamespaceAlias, MathML.Namespace), mrow);
        }
Beispiel #11
0
        public DrawingSettings(string path, UnitFormat unitFormat, int unitPrecision, int anglePrecision)
        {
            FileName       = path;
            UnitFormat     = unitFormat;
            UnitPrecision  = unitPrecision < 0 ? 0 : unitPrecision;
            AnglePrecision = anglePrecision < 0 ? 0 : anglePrecision;

            switch (unitFormat)
            {
            case UnitFormat.Architectural:
                // only allowable values are 0, 2, 4, 8, 16, 32
                UnitPrecision = AllowedArchitecturalPrecisions.Where(x => x <= UnitPrecision).Max();
                break;

            case UnitFormat.Metric:
                // only allowable values are [0, 16]
                UnitPrecision = Math.Max(0, UnitPrecision);
                UnitPrecision = Math.Min(16, UnitPrecision);
                break;
            }
        }
        /// <summary>
        /// Gets the math ml.
        /// </summary>
        /// <param name="quantity">The quantity.</param>
        /// <param name="unitFormat">The unit format.</param>
        /// <param name="multiplicationSign">The multiplication sign.</param>
        /// <returns>The root <see cref="XElement"/> node.</returns>
        public XElement GetMathML(
            IQuantity quantity,
            UnitFormat unitFormat = UnitFormat.Default,
            MultiplicationSign multiplicationSign = MultiplicationSign.Invisible)
        {
            var mn          = new XElement(MathML.Mn, quantity.Value);
            var mrow        = new XElement(MathML.Mrow, mn);
            var unitElement = mrow;

            if (unitFormat.HasFlag(UnitFormat.SurroundInBrackets))
            {
                var mfenced = new XElement(MathML.Mfenced, MathML.OpenLeftBracket, MathML.CloseRightBrack);
                mrow.Add(mfenced);
                unitElement = mfenced;
            }

            ExpressionToMathMLVisitor.DefaultMathMLVisitor.Visit(
                quantity.Unit.GetExpression(),
                multiplicationSign,
                unitElement);
            return(new XElement(MathML.Math, new XAttribute(MathML.NamespaceAlias, MathML.Namespace), mrow));
        }
 internal UnitDefinitionDescriptor(UnitFormat format)
 {
     Format = format;
 }
 /// <summary>
 /// Returns a <see cref="string" /> that represents this instance.
 /// </summary>
 /// <param name="unitFormat">The unit format.</param>
 /// <param name="formatProvider">The format provider.</param>
 /// <returns>
 /// A <see cref="string" /> that represents this instance.
 /// </returns>
 public string ToString(UnitFormat unitFormat, IFormatProvider formatProvider)
 {
     return this.ToString(unitFormat, null, formatProvider);
 }
Beispiel #15
0
        /// <summary>
        /// Gets the notation.
        /// </summary>
        /// <param name="unit">The quantity unit.</param>
        /// <param name="unitFormat">The unit format.</param>
        /// <returns>The notation.</returns>
        public static string GetNotation(IUnit unit, UnitFormat unitFormat)
        {
            Contract.Requires(unit != null);
            var notation = unit.Notation;
            if (unitFormat.HasFlag(UnitFormat.SurroundInBrackets))
            {
                return $"[{notation}]";
            }

            return notation;
        }
 /// <summary>
 /// Returns a <see cref="string" /> that represents this instance.
 /// </summary>
 /// <param name="unitFormat">The unit format.</param>
 /// <param name="format">The format.</param>
 /// <param name="formatProvider">The format provider.</param>
 /// <returns>
 /// A <see cref="string" /> that represents this instance.
 /// </returns>
 public string ToString(UnitFormat unitFormat, string format, IFormatProvider formatProvider)
 {
     return QuantityHelper.ToString(
         this.Unit.FormatValue(this.value, format, formatProvider),
         UnitFormatHelper.GetNotation(this.Unit, unitFormat));
 }
 /// <summary>
 /// Returns a <see cref="string" /> that represents this instance.
 /// </summary>
 /// <param name="unitFormat">The unit format.</param>
 /// <param name="format">The format.</param>
 /// <returns>
 /// A <see cref="string" /> that represents this instance.
 /// </returns>
 public string ToString(UnitFormat unitFormat, string format)
 {
     return this.ToString(unitFormat, format, CultureInfo.CurrentCulture);
 }
        /// <summary>
        /// The be approximately.
        /// </summary>
        /// <param name="quantityAssertions">
        /// The quantity assertions.
        /// </param>
        /// <param name="expectedValue">
        /// The expected value.
        /// </param>
        /// <param name="expectedUnit">
        /// The expected unit.
        /// </param>
        /// <param name="unitFormat">
        /// The unit format.
        /// </param>
        /// <param name="precision">
        /// The precision.
        /// </param>
        /// <returns>
        /// The <see cref="AndConstraint{T}"/>.
        /// </returns>
        public static AndConstraint<ComparableTypeAssertions<IQuantity>> BeApproximately(
            this ComparableTypeAssertions<IQuantity> quantityAssertions,
            double expectedValue,
            string expectedUnit,
            UnitFormat unitFormat,
            double precision)
        {
            var quantity = (IQuantity)quantityAssertions.Subject;
            var quantityValue = quantity.Value;
            var quantityUnit = UnitHelper.GetNotation(quantity.Unit, unitFormat);
            Execute.Assertion.ForCondition(quantityValue == expectedValue || Math.Abs(quantityValue - expectedValue) < precision)
                .FailWith("The value: {0}, but found: {1}", expectedValue, quantityValue);
            AssertUnit(quantityUnit, expectedUnit);

            return new AndConstraint<ComparableTypeAssertions<IQuantity>>(quantityAssertions);
        }
Beispiel #19
0
        /// <summary>
        /// 获取单位定义信息。
        /// </summary>
        /// <param name="format">给定的 <see cref="UnitFormat"/>。</param>
        /// <param name="notation">给定的 <see cref="UnitNotation"/>。</param>
        /// <returns>返回 <see cref="UnitDefinitionInfo"/>。</returns>
        public static UnitDefinitionInfo GetInfo(UnitFormat format, UnitNotation notation)
        {
            var info = GetDescriptor(format);

            return(notation == UnitNotation.BinarySystem ? info.BinaryInfo : info.DecimalInfo);
        }
Beispiel #20
0
 /// <summary>
 /// 获取指定单位格式的单位定义描述符。
 /// </summary>
 /// <param name="format">给定的 <see cref="UnitFormat"/>。</param>
 /// <returns>返回 <see cref="UnitDefinitionDescriptor"/>。</returns>
 public static UnitDefinitionDescriptor GetDescriptor(UnitFormat format)
 => SupportDescriptors.First(info => info.Format == format);