|
| 1 | +// Copyright (c) 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). |
| 2 | +// https://github.com/angularsen/UnitsNet |
| 3 | +// |
| 4 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | +// of this software and associated documentation files (the "Software"), to deal |
| 6 | +// in the Software without restriction, including without limitation the rights |
| 7 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | +// copies of the Software, and to permit persons to whom the Software is |
| 9 | +// furnished to do so, subject to the following conditions: |
| 10 | +// |
| 11 | +// The above copyright notice and this permission notice shall be included in |
| 12 | +// all copies or substantial portions of the Software. |
| 13 | +// |
| 14 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 20 | +// THE SOFTWARE. |
| 21 | + |
| 22 | +using System.Globalization; |
| 23 | +using UnitsNet.Units; |
| 24 | +using Xunit; |
| 25 | + |
| 26 | +namespace UnitsNet.Tests |
| 27 | +{ |
| 28 | + public class ToStringTests |
| 29 | + { |
| 30 | + [Fact] |
| 31 | + public void ReturnsTheOriginalValueAndUnit() |
| 32 | + { |
| 33 | + var oldCulture = UnitSystem.DefaultCulture; |
| 34 | + try |
| 35 | + { |
| 36 | + UnitSystem.DefaultCulture = CultureInfo.InvariantCulture; |
| 37 | + Assert.Equal("5 kg", Mass.FromKilograms(5).ToString()); |
| 38 | + Assert.Equal("5,000 g", Mass.FromGrams(5000).ToString()); |
| 39 | + Assert.Equal("1e-04 long tn", Mass.FromLongTons(1e-4).ToString()); |
| 40 | + Assert.Equal("3.46e-04 dN/m", ForcePerLength.FromDecinewtonsPerMeter(0.00034567).ToString()); |
| 41 | + Assert.Equal("0.0069 dB", Level.FromDecibels(0.0069).ToString()); |
| 42 | + Assert.Equal("0.011 kWh/kg", SpecificEnergy.FromKilowattHoursPerKilogram(0.011).ToString()); |
| 43 | +// Assert.Equal("0.1 MJ/kg·C", SpecificEntropy.FromMegajoulesPerKilogramDegreeCelsius(0.1).ToString()); |
| 44 | + Assert.Equal("0.1 MJ/kg.C", SpecificEntropy.FromMegajoulesPerKilogramDegreeCelsius(0.1).ToString()); |
| 45 | + Assert.Equal("5 cm", Length.FromCentimeters(5).ToString()); |
| 46 | + } |
| 47 | + finally |
| 48 | + { |
| 49 | + UnitSystem.DefaultCulture = oldCulture; |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + [Fact] |
| 54 | + public void ConvertsToTheGivenUnit() |
| 55 | + { |
| 56 | + var oldCulture = UnitSystem.DefaultCulture; |
| 57 | + try |
| 58 | + { |
| 59 | + UnitSystem.DefaultCulture = CultureInfo.InvariantCulture; |
| 60 | + Assert.Equal("5,000 g", Mass.FromKilograms(5).ToString(MassUnit.Gram)); |
| 61 | + Assert.Equal("5 kg", Mass.FromGrams(5000).ToString(MassUnit.Kilogram)); |
| 62 | + Assert.Equal("0.05 m", Length.FromCentimeters(5).ToString(LengthUnit.Meter)); |
| 63 | + Assert.Equal("1.97 in", Length.FromCentimeters(5).ToString(LengthUnit.Inch)); |
| 64 | + } |
| 65 | + finally |
| 66 | + { |
| 67 | + UnitSystem.DefaultCulture = oldCulture; |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + [Fact] |
| 72 | + public void FormatsNumberUsingGivenCulture() |
| 73 | + { |
| 74 | + var oldCulture = UnitSystem.DefaultCulture; |
| 75 | + try |
| 76 | + { |
| 77 | + UnitSystem.DefaultCulture = CultureInfo.InvariantCulture; |
| 78 | + Assert.Equal("0.05 m", Length.FromCentimeters(5).ToString(LengthUnit.Meter, null)); |
| 79 | + Assert.Equal("0.05 m", Length.FromCentimeters(5).ToString(LengthUnit.Meter, CultureInfo.InvariantCulture)); |
| 80 | + Assert.Equal("0,05 m", Length.FromCentimeters(5).ToString(LengthUnit.Meter, new CultureInfo("nb-NO"))); |
| 81 | + } |
| 82 | + finally |
| 83 | + { |
| 84 | + UnitSystem.DefaultCulture = oldCulture; |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + [Fact] |
| 89 | + public void FormatsNumberUsingGivenDigitsAfterRadix() |
| 90 | + { |
| 91 | + var oldCulture = UnitSystem.DefaultCulture; |
| 92 | + try |
| 93 | + { |
| 94 | + UnitSystem.DefaultCulture = CultureInfo.InvariantCulture; |
| 95 | + Assert.Equal("0.05 m", Length.FromCentimeters(5).ToString(LengthUnit.Meter, null, 4)); |
| 96 | + Assert.Equal("1.97 in", Length.FromCentimeters(5).ToString(LengthUnit.Inch, null, 2)); |
| 97 | + Assert.Equal("1.9685 in", Length.FromCentimeters(5).ToString(LengthUnit.Inch, null, 4)); |
| 98 | + } |
| 99 | + finally |
| 100 | + { |
| 101 | + UnitSystem.DefaultCulture = oldCulture; |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | +} |
0 commit comments