|
2 | 2 | // Copyright 2013 Andreas Gullberg Larsen ([email protected]). Maintained at https://github.com/angularsen/UnitsNet.
|
3 | 3 |
|
4 | 4 | using System;
|
| 5 | +using System.Linq; |
5 | 6 | using UnitsNet.Units;
|
6 | 7 | using Xunit;
|
7 | 8 |
|
@@ -149,5 +150,55 @@ public void SIUnitSystemHasCorrectBaseUnits()
|
149 | 150 | Assert.Equal(AmountOfSubstanceUnit.Mole, UnitSystem.SI.BaseUnits.Amount);
|
150 | 151 | Assert.Equal(LuminousIntensityUnit.Candela, UnitSystem.SI.BaseUnits.LuminousIntensity);
|
151 | 152 | }
|
| 153 | + |
| 154 | + [Fact] |
| 155 | + public void GetDefaultUnitInfoThrowsExceptionForUndefinedQuantity() |
| 156 | + { |
| 157 | + Assert.Throws<ArgumentException>(() => UnitSystem.SI.GetDefaultUnitInfo(QuantityType.Undefined)); |
| 158 | + } |
| 159 | + |
| 160 | + [Fact] |
| 161 | + public void GetDefaultUnitInfoReturnsNullForQuantitiesWithNoDefaultUnits() |
| 162 | + { |
| 163 | + // TODO do we expect to preserve this behavior? |
| 164 | + // AmplitudeRatio might be unitless- but there are (more than one) ways to express ratios. |
| 165 | + Assert.Null(UnitSystem.SI.GetDefaultUnitInfo(AmplitudeRatio.QuantityType)); |
| 166 | + } |
| 167 | + |
| 168 | + [Fact] |
| 169 | + public void WithDefaultUnitThrowsIfQuantityTypeIsUndefined() |
| 170 | + { |
| 171 | + Assert.Throws<ArgumentException>(() => UnitSystem.SI.WithDefaultUnit(QuantityType.Undefined, null)); |
| 172 | + } |
| 173 | + |
| 174 | + [Fact] |
| 175 | + public void WithDefaultUnitUsesOldBaseUnitsIfNotSpecified() |
| 176 | + { |
| 177 | + var myDefaultLengthUnit = Length.Info.UnitInfos.First(x => x.Value == LengthUnit.Millimeter); |
| 178 | + |
| 179 | + var newSI = UnitSystem.SI.WithDefaultUnit(QuantityType.Length, myDefaultLengthUnit, (BaseUnits) null); |
| 180 | + |
| 181 | + Assert.Equal(UnitSystem.SI, newSI); // currently comparing using BaseUnits |
| 182 | + } |
| 183 | + |
| 184 | + [Theory] |
| 185 | + [InlineData(LengthUnit.Undefined, MassUnit.Kilogram, DurationUnit.Second, ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela)] |
| 186 | + [InlineData(LengthUnit.Meter, MassUnit.Undefined, DurationUnit.Second, ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela)] |
| 187 | + [InlineData(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Undefined, ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela)] |
| 188 | + [InlineData(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Second, ElectricCurrentUnit.Undefined, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela)] |
| 189 | + [InlineData(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Second, ElectricCurrentUnit.Ampere, TemperatureUnit.Undefined, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela)] |
| 190 | + [InlineData(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Second, ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Undefined, LuminousIntensityUnit.Candela)] |
| 191 | + [InlineData(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Second, ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Undefined)] |
| 192 | + public void WithDefaultUnitThrowsIfSpecifiedBaseUnitsNotFullyDefined(LengthUnit length, MassUnit mass, DurationUnit time, ElectricCurrentUnit current, |
| 193 | + TemperatureUnit temperature, AmountOfSubstanceUnit amount, LuminousIntensityUnit luminousIntensity) |
| 194 | + { |
| 195 | + var myDefaultLengthUnit = Length.Info.UnitInfos.First(x => x.Value == LengthUnit.Millimeter); |
| 196 | + |
| 197 | + var baseUnits = new BaseUnits(length, mass, time, current, temperature, amount, luminousIntensity); |
| 198 | + |
| 199 | + // TODO do we want to preserve this behavior? |
| 200 | + Assert.Throws<ArgumentException>(()=> UnitSystem.SI.WithDefaultUnit(QuantityType.Length, myDefaultLengthUnit, baseUnits)); |
| 201 | + } |
| 202 | + |
152 | 203 | }
|
153 | 204 | }
|
0 commit comments