|
3 | 3 |
|
4 | 4 | using System;
|
5 | 5 | using System.Globalization;
|
| 6 | +using UnitsNet.Units; |
6 | 7 | using Xunit;
|
7 | 8 |
|
8 | 9 | namespace UnitsNet.Tests
|
9 | 10 | {
|
10 | 11 | public class QuantityIFormattableTests
|
11 | 12 | {
|
12 | 13 | private static readonly Length MyLength = Length.FromFeet(1.2345678);
|
| 14 | + |
| 15 | + private static readonly CultureInfo AmericanCulture = CultureInfo.GetCultureInfo("en-US"); |
| 16 | + private static readonly CultureInfo NorwegianCulture = CultureInfo.GetCultureInfo("nb-NO"); |
| 17 | + private static readonly CultureInfo RussianCulture = CultureInfo.GetCultureInfo("ru-RU"); |
13 | 18 |
|
14 | 19 | [Fact]
|
15 | 20 | public void GFormatStringEqualsToString()
|
@@ -46,5 +51,278 @@ public void UnsupportedFormatStringThrowsException()
|
46 | 51 | {
|
47 | 52 | Assert.Throws<FormatException>(() => MyLength.ToString("z"));
|
48 | 53 | }
|
| 54 | + |
| 55 | + // The default, parameterless ToString() method represents the result with all significant digits, without a group separator. |
| 56 | + [Theory] |
| 57 | +#if NET |
| 58 | + [InlineData(double.MinValue, "-1.7976931348623157E+308 m")] |
| 59 | +#else |
| 60 | + [InlineData(double.MinValue, "-1.79769313486232E+308 m")] |
| 61 | +#endif |
| 62 | + [InlineData(-0.819999999999, "-0.819999999999 m")] |
| 63 | + [InlineData(-0.111234, "-0.111234 m")] |
| 64 | + [InlineData(-0.1, "-0.1 m")] |
| 65 | + [InlineData(-0.0000012345, "-1.2345E-06 m")] |
| 66 | + [InlineData(-0.000001, "-1E-06 m")] |
| 67 | + [InlineData(0, "0 m")] |
| 68 | + [InlineData(0.000001, "1E-06 m")] |
| 69 | + [InlineData(0.0000012345, "1.2345E-06 m")] |
| 70 | + [InlineData(0.1, "0.1 m")] |
| 71 | + [InlineData(0.111234, "0.111234 m")] |
| 72 | + [InlineData(0.819999999999, "0.819999999999 m")] |
| 73 | +#if NET |
| 74 | + [InlineData(double.MaxValue, "1.7976931348623157E+308 m")] |
| 75 | +#else |
| 76 | + [InlineData(double.MaxValue, "1.79769313486232E+308 m")] |
| 77 | +#endif |
| 78 | + public void DefaultToStringFormatting(double value, string expected) |
| 79 | + { |
| 80 | + string actual = Length.FromMeters(value).ToString(AmericanCulture); |
| 81 | + Assert.Equal(expected, actual); |
| 82 | + } |
| 83 | + [Theory] |
| 84 | + [InlineData("de-DE")] |
| 85 | + [InlineData("da-DK")] |
| 86 | + [InlineData("es-AR")] |
| 87 | + [InlineData("es-ES")] |
| 88 | + [InlineData("it-IT")] |
| 89 | + [InlineData("en-CA")] |
| 90 | + [InlineData("en-US")] |
| 91 | + [InlineData("ar-EG")] |
| 92 | + [InlineData("en-GB")] |
| 93 | + [InlineData("es-MX")] |
| 94 | + public void RadixPointCultureFormatting(string cultureName) |
| 95 | + { |
| 96 | + CultureInfo culture = CultureInfo.GetCultureInfo(cultureName); |
| 97 | + string ds = culture.NumberFormat.NumberDecimalSeparator; |
| 98 | + Assert.Equal($"0{ds}12 m", Length.FromMeters(0.12).ToString(culture)); |
| 99 | + } |
| 100 | + |
| 101 | + [Theory] |
| 102 | + [InlineData("de-DE")] |
| 103 | + [InlineData("da-DK")] |
| 104 | + [InlineData("es-AR")] |
| 105 | + [InlineData("es-ES")] |
| 106 | + [InlineData("it-IT")] |
| 107 | + [InlineData("en-CA")] |
| 108 | + [InlineData("en-US")] |
| 109 | + [InlineData("ar-EG")] |
| 110 | + [InlineData("en-GB")] |
| 111 | + [InlineData("es-MX")] |
| 112 | + public void ToString_SFormat_DecimalSeparator_ForCulture(string cultureName) |
| 113 | + { |
| 114 | + CultureInfo culture = CultureInfo.GetCultureInfo(cultureName); |
| 115 | + string ds = culture.NumberFormat.NumberDecimalSeparator; |
| 116 | + Assert.Equal($"0{ds}12 m", Length.FromMeters(0.12).ToString("s2", culture)); |
| 117 | + } |
| 118 | + |
| 119 | + [Theory] |
| 120 | + [InlineData("en-CA")] |
| 121 | + [InlineData("en-GB")] |
| 122 | + [InlineData("en-US")] |
| 123 | + [InlineData("ar-EG")] |
| 124 | + [InlineData("es-MX")] |
| 125 | + [InlineData("nn-NO")] |
| 126 | + [InlineData("fr-FR")] |
| 127 | + [InlineData("de-DE")] |
| 128 | + [InlineData("da-DK")] |
| 129 | + [InlineData("es-AR")] |
| 130 | + [InlineData("es-ES")] |
| 131 | + [InlineData("it-IT")] |
| 132 | + public void ToString_WithCultureWithoutGroupingSeparator(string cultureName) |
| 133 | + { |
| 134 | + CultureInfo culture = CultureInfo.GetCultureInfo(cultureName); |
| 135 | + Assert.Equal("1111 m", Length.FromMeters(1111).ToString(culture)); |
| 136 | + } |
| 137 | + |
| 138 | + [Theory] |
| 139 | + [InlineData("en-CA")] |
| 140 | + [InlineData("en-GB")] |
| 141 | + [InlineData("en-US")] |
| 142 | + [InlineData("ar-EG")] |
| 143 | + [InlineData("es-MX")] |
| 144 | + [InlineData("nn-NO")] |
| 145 | + [InlineData("fr-FR")] |
| 146 | + [InlineData("de-DE")] |
| 147 | + [InlineData("da-DK")] |
| 148 | + [InlineData("es-AR")] |
| 149 | + [InlineData("es-ES")] |
| 150 | + [InlineData("it-IT")] |
| 151 | + public void ToString_SFormat_UsesGroupingSeparator_ForCulture(string cultureName) |
| 152 | + { |
| 153 | + CultureInfo culture = CultureInfo.GetCultureInfo(cultureName); |
| 154 | + string gs = culture.NumberFormat.NumberGroupSeparator; |
| 155 | + |
| 156 | + Assert.Equal($"1{gs}111 m", Length.FromMeters(1111).ToString("S", culture)); |
| 157 | + } |
| 158 | + |
| 159 | + [Theory] |
| 160 | + [InlineData("en-CA")] |
| 161 | + [InlineData("en-GB")] |
| 162 | + [InlineData("en-US")] |
| 163 | + [InlineData("ar-EG")] |
| 164 | + [InlineData("es-MX")] |
| 165 | + [InlineData("nn-NO")] |
| 166 | + [InlineData("fr-FR")] |
| 167 | + [InlineData("de-DE")] |
| 168 | + [InlineData("da-DK")] |
| 169 | + [InlineData("es-AR")] |
| 170 | + [InlineData("es-ES")] |
| 171 | + [InlineData("it-IT")] |
| 172 | + public void FeetInches_UseGroupingSeparator_ForCulture(string cultureName) |
| 173 | + { |
| 174 | + CultureInfo culture = CultureInfo.GetCultureInfo(cultureName); |
| 175 | + string gs = culture.NumberFormat.NumberGroupSeparator; |
| 176 | + |
| 177 | + // Feet/Inch and Stone/Pound combinations are only used (customarily) in the US, UK and maybe Ireland - all English speaking countries. |
| 178 | + // FeetInches returns a whole number of feet, with the remainder expressed (rounded) in inches. Same for StonePounds. |
| 179 | + Assert.Equal($"3{gs}333 st 7 lb", Mass.FromStonePounds(3333, 7).StonePounds.ToString(culture)); |
| 180 | + } |
| 181 | + |
| 182 | + [Theory] |
| 183 | + [InlineData("en-CA")] |
| 184 | + [InlineData("en-GB")] |
| 185 | + [InlineData("en-US")] |
| 186 | + [InlineData("ar-EG")] |
| 187 | + [InlineData("es-MX")] |
| 188 | + [InlineData("nn-NO")] |
| 189 | + [InlineData("fr-FR")] |
| 190 | + [InlineData("de-DE")] |
| 191 | + [InlineData("da-DK")] |
| 192 | + [InlineData("es-AR")] |
| 193 | + [InlineData("es-ES")] |
| 194 | + [InlineData("it-IT")] |
| 195 | + public void StonePounds_UseGroupingSeparator_ForCulture(string cultureName) |
| 196 | + { |
| 197 | + CultureInfo culture = CultureInfo.GetCultureInfo(cultureName); |
| 198 | + string gs = culture.NumberFormat.NumberGroupSeparator; |
| 199 | + |
| 200 | + // Feet/Inch and Stone/Pound combinations are only used (customarily) in the US, UK and maybe Ireland - all English speaking countries. |
| 201 | + // FeetInches returns a whole number of feet, with the remainder expressed (rounded) in inches. Same for StonePounds. |
| 202 | + Assert.Equal($"3{gs}333 st 7 lb", Mass.FromStonePounds(3333, 7).StonePounds.ToString(culture)); |
| 203 | + } |
| 204 | + |
| 205 | + // Due to rounding, the values will result in the same string representation regardless of the number of significant digits (up to a certain point) |
| 206 | + [Theory] |
| 207 | + [InlineData(-0.819999999999, "S", "-0.819999999999 m")] |
| 208 | + [InlineData(-0.819999999999, "s2", "-0.82 m")] |
| 209 | + [InlineData(-0.819999999999, "s4", "-0.82 m")] |
| 210 | + [InlineData(-0.8, "s4", "-0.8 m")] |
| 211 | + [InlineData(0.819999999999, "S", "0.819999999999 m")] |
| 212 | + [InlineData(0.819999999999, "s", "0.819999999999 m")] |
| 213 | + [InlineData(0.819999999999, "s2", "0.82 m")] |
| 214 | + [InlineData(0.819999999999, "s4", "0.82 m")] |
| 215 | + [InlineData(0.8, "s4", "0.8 m")] |
| 216 | + [InlineData(0.00299999999, "s2", "0.003 m")] |
| 217 | + [InlineData(0.00299999999, "s4", "0.003 m")] |
| 218 | + [InlineData(0.0003000001, "s2", "3e-04 m")] |
| 219 | + [InlineData(0.0003000001, "s4", "3e-04 m")] |
| 220 | + public void ToString_SFormat_RoundsToSignificantDigitsAfterRadix(double value, |
| 221 | + string significantDigitsAfterRadixFormatString, string expected) |
| 222 | + { |
| 223 | + string actual = Length.FromMeters(value).ToString(significantDigitsAfterRadixFormatString, AmericanCulture); |
| 224 | + Assert.Equal(expected, actual); |
| 225 | + } |
| 226 | + |
| 227 | + // Any value in the interval (-inf ≤ x < 1e-03] is formatted in scientific notation |
| 228 | + [Theory] |
| 229 | + [InlineData(double.MinValue, "-1.8e+308 m")] |
| 230 | + [InlineData(1.23e-120, "1.23e-120 m")] |
| 231 | + [InlineData(0.0000111, "1.11e-05 m")] |
| 232 | + [InlineData(1.99e-4, "1.99e-04 m")] |
| 233 | + public void ToString_SFormat_BelowMilli_UsesScientificNotation(double value, string expected) |
| 234 | + { |
| 235 | + string actual = Length.FromMeters(value).ToString("s2", AmericanCulture); |
| 236 | + Assert.Equal(expected, actual); |
| 237 | + } |
| 238 | + |
| 239 | + // Any value in the interval [1e-03 ≤ x < 1e+03] is formatted in fixed point notation. |
| 240 | + [Theory] |
| 241 | + [InlineData(1e-3, "0.001 m")] |
| 242 | + [InlineData(1.1, "1.1 m")] |
| 243 | + [InlineData(999.99, "999.99 m")] |
| 244 | + public void ToString_SFormat_BetweenMilliAndKilo_UsesFixedPointFormat(double value, string expected) |
| 245 | + { |
| 246 | + string actual = Length.FromMeters(value).ToString("s2",AmericanCulture); |
| 247 | + Assert.Equal(expected, actual); |
| 248 | + } |
| 249 | + |
| 250 | + // Any value in the interval [1e+03 ≤ x < 1e+06] is formatted in fixed point notation with digit grouping. |
| 251 | + [Theory] |
| 252 | + [InlineData(1000, "1,000 m")] |
| 253 | + [InlineData(11000, "11,000 m")] |
| 254 | + [InlineData(111000, "111,000 m")] |
| 255 | + [InlineData(999999.99, "999,999.99 m")] |
| 256 | + public void ToString_SFormat_From1e3To1e5_UsesFixedPointFormatWithDigitGrouping(double value, string expected) |
| 257 | + { |
| 258 | + string actual = Length.FromMeters(value).ToString("s2",AmericanCulture); |
| 259 | + Assert.Equal(expected, actual); |
| 260 | + } |
| 261 | + |
| 262 | + // Any value in the interval [1e+06 ≤ x ≤ +inf) is formatted in scientific notation. |
| 263 | + [Theory] |
| 264 | + [InlineData(1e6, "1e+06 m")] |
| 265 | + [InlineData(11100000, "1.11e+07 m")] |
| 266 | + [InlineData(double.MaxValue, "1.8e+308 m")] |
| 267 | + public void ToString_SFormat_Above1e6_UsesScientificNotation(double value, string expected) |
| 268 | + { |
| 269 | + string actual = Length.FromMeters(value).ToString("s2",AmericanCulture); |
| 270 | + Assert.Equal(expected, actual); |
| 271 | + } |
| 272 | + |
| 273 | + [Fact] |
| 274 | + public void AllUnitsImplementToStringForInvariantCulture() |
| 275 | + { |
| 276 | + Assert.Equal("1 °", Angle.FromDegrees(1).ToString(CultureInfo.InvariantCulture)); |
| 277 | + Assert.Equal("1 m²", Area.FromSquareMeters(1).ToString(CultureInfo.InvariantCulture)); |
| 278 | + Assert.Equal("1 V", ElectricPotential.FromVolts(1).ToString(CultureInfo.InvariantCulture)); |
| 279 | + Assert.Equal("1 N", Force.FromNewtons(1).ToString(CultureInfo.InvariantCulture)); |
| 280 | + Assert.Equal("1 m", Length.FromMeters(1).ToString(CultureInfo.InvariantCulture)); |
| 281 | + Assert.Equal("1 kg", Mass.FromKilograms(1).ToString(CultureInfo.InvariantCulture)); |
| 282 | + Assert.Equal("1 Pa", Pressure.FromPascals(1).ToString(CultureInfo.InvariantCulture)); |
| 283 | + Assert.Equal("1 rad/s", RotationalSpeed.FromRadiansPerSecond(1).ToString(CultureInfo.InvariantCulture)); |
| 284 | + Assert.Equal("1 K", Temperature.FromKelvins(1).ToString(CultureInfo.InvariantCulture)); |
| 285 | + Assert.Equal("1 N·m", Torque.FromNewtonMeters(1).ToString(CultureInfo.InvariantCulture)); |
| 286 | + Assert.Equal("1 m³", Volume.FromCubicMeters(1).ToString(CultureInfo.InvariantCulture)); |
| 287 | + Assert.Equal("1 m³/s", VolumeFlow.FromCubicMetersPerSecond(1).ToString(CultureInfo.InvariantCulture)); |
| 288 | + |
| 289 | + Assert.Equal("2 ft 3 in", Length.FromFeetInches(2, 3).FeetInches.ToString(CultureInfo.InvariantCulture)); |
| 290 | + Assert.Equal("3 st 7 lb", Mass.FromStonePounds(3, 7).StonePounds.ToString(CultureInfo.InvariantCulture)); |
| 291 | + } |
| 292 | + |
| 293 | + [Fact] |
| 294 | + public void ToString_WithNorwegianCulture() |
| 295 | + { |
| 296 | + Assert.Equal("1 °", Angle.FromDegrees(1).ToUnit(AngleUnit.Degree).ToString(NorwegianCulture)); |
| 297 | + Assert.Equal("1 m²", Area.FromSquareMeters(1).ToUnit(AreaUnit.SquareMeter).ToString(NorwegianCulture)); |
| 298 | + Assert.Equal("1 V", ElectricPotential.FromVolts(1).ToUnit(ElectricPotentialUnit.Volt).ToString(NorwegianCulture)); |
| 299 | + Assert.Equal("1 m³/s", VolumeFlow.FromCubicMetersPerSecond(1).ToUnit(VolumeFlowUnit.CubicMeterPerSecond).ToString(NorwegianCulture)); |
| 300 | + Assert.Equal("1 N", Force.FromNewtons(1).ToUnit(ForceUnit.Newton).ToString(NorwegianCulture)); |
| 301 | + Assert.Equal("1 m", Length.FromMeters(1).ToUnit(LengthUnit.Meter).ToString(NorwegianCulture)); |
| 302 | + Assert.Equal("1 kg", Mass.FromKilograms(1).ToUnit(MassUnit.Kilogram).ToString(NorwegianCulture)); |
| 303 | + Assert.Equal("1 Pa", Pressure.FromPascals(1).ToUnit(PressureUnit.Pascal).ToString(NorwegianCulture)); |
| 304 | + Assert.Equal("1 rad/s", RotationalSpeed.FromRadiansPerSecond(1).ToUnit(RotationalSpeedUnit.RadianPerSecond).ToString(NorwegianCulture)); |
| 305 | + Assert.Equal("1 K", Temperature.FromKelvins(1).ToUnit(TemperatureUnit.Kelvin).ToString(NorwegianCulture)); |
| 306 | + Assert.Equal("1 N·m", Torque.FromNewtonMeters(1).ToUnit(TorqueUnit.NewtonMeter).ToString(NorwegianCulture)); |
| 307 | + Assert.Equal("1 m³", Volume.FromCubicMeters(1).ToUnit(VolumeUnit.CubicMeter).ToString(NorwegianCulture)); |
| 308 | + } |
| 309 | + |
| 310 | + [Fact] |
| 311 | + public void ToString_WithRussianCulture() |
| 312 | + { |
| 313 | + Assert.Equal("1 °", Angle.FromDegrees(1).ToUnit(AngleUnit.Degree).ToString(RussianCulture)); |
| 314 | + Assert.Equal("1 м²", Area.FromSquareMeters(1).ToUnit(AreaUnit.SquareMeter).ToString(RussianCulture)); |
| 315 | + Assert.Equal("1 В", ElectricPotential.FromVolts(1).ToUnit(ElectricPotentialUnit.Volt).ToString(RussianCulture)); |
| 316 | + Assert.Equal("1 м³/с", VolumeFlow.FromCubicMetersPerSecond(1).ToUnit(VolumeFlowUnit.CubicMeterPerSecond).ToString(RussianCulture)); |
| 317 | + Assert.Equal("1 Н", Force.FromNewtons(1).ToUnit(ForceUnit.Newton).ToString(RussianCulture)); |
| 318 | + Assert.Equal("1 м", Length.FromMeters(1).ToUnit(LengthUnit.Meter).ToString(RussianCulture)); |
| 319 | + Assert.Equal("1 кг", Mass.FromKilograms(1).ToUnit(MassUnit.Kilogram).ToString(RussianCulture)); |
| 320 | + Assert.Equal("1 Па", Pressure.FromPascals(1).ToUnit(PressureUnit.Pascal).ToString(RussianCulture)); |
| 321 | + Assert.Equal("1 рад/с", RotationalSpeed.FromRadiansPerSecond(1).ToUnit(RotationalSpeedUnit.RadianPerSecond).ToString(RussianCulture)); |
| 322 | + Assert.Equal("1 K", Temperature.FromKelvins(1).ToUnit(TemperatureUnit.Kelvin).ToString(RussianCulture)); |
| 323 | + Assert.Equal("1 Н·м", Torque.FromNewtonMeters(1).ToUnit(TorqueUnit.NewtonMeter).ToString(RussianCulture)); |
| 324 | + Assert.Equal("1 м³", Volume.FromCubicMeters(1).ToUnit(VolumeUnit.CubicMeter).ToString(RussianCulture)); |
| 325 | + } |
| 326 | + |
49 | 327 | }
|
50 | 328 | }
|
0 commit comments