Skip to content

Commit 75175e7

Browse files
committed
Added tests for the new public methods in UnitSystem
- updated the comments and invalid arguments checks - added a few tests for those cases a few question marks remain (marked with TODOs)
1 parent 33d5a51 commit 75175e7

File tree

2 files changed

+62
-4
lines changed

2 files changed

+62
-4
lines changed

UnitsNet.Tests/UnitSystemTests.cs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Copyright 2013 Andreas Gullberg Larsen ([email protected]). Maintained at https://github.com/angularsen/UnitsNet.
33

44
using System;
5+
using System.Linq;
56
using UnitsNet.Units;
67
using Xunit;
78

@@ -149,5 +150,55 @@ public void SIUnitSystemHasCorrectBaseUnits()
149150
Assert.Equal(AmountOfSubstanceUnit.Mole, UnitSystem.SI.BaseUnits.Amount);
150151
Assert.Equal(LuminousIntensityUnit.Candela, UnitSystem.SI.BaseUnits.LuminousIntensity);
151152
}
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+
152203
}
153204
}

UnitsNet/UnitSystem.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.Collections.Generic;
66
using System.Linq;
7+
using JetBrains.Annotations;
78
using UnitsNet.Units;
89

910
namespace UnitsNet
@@ -125,11 +126,13 @@ public override int GetHashCode()
125126
/// The default UnitInfo for the given quantity type, if such an association exists,
126127
/// and <see langword="null" /> otherwise.
127128
/// </returns>
128-
/// <exception cref="T:System.Collections.Generic.KeyNotFoundException">
129-
/// The given <paramref name="quantityType" /> is not available in this unit system.
129+
/// <exception cref="ArgumentException">
130+
/// Quantity type can not be undefined.
130131
/// </exception>
131132
public UnitInfo GetDefaultUnitInfo(QuantityType quantityType)
132133
{
134+
if (quantityType == QuantityType.Undefined)
135+
throw new ArgumentException("Quantity type can not be undefined.", nameof(quantityType));
133136
return _defaultUnits.Value[(int)quantityType - 1];
134137
}
135138

@@ -145,11 +148,15 @@ public UnitInfo GetDefaultUnitInfo(QuantityType quantityType)
145148
/// A new UnitSystem that defines <paramref name="defaultUnitInfo" /> as the default unit for
146149
/// <paramref name="quantityType" />
147150
/// </returns>
148-
/// <exception cref="T:System.ArgumentNullException">
149-
/// <paramref name="quantityType" /> is <see langword="null" />.
151+
/// <exception cref="ArgumentException">
152+
/// Quantity type can not be undefined.
150153
/// </exception>
151154
public UnitSystem WithDefaultUnit(QuantityType quantityType, UnitInfo defaultUnitInfo, BaseUnits baseUnits = null)
152155
{
156+
if (quantityType == QuantityType.Undefined)
157+
throw new ArgumentException("Quantity type can not be undefined.", nameof(quantityType));
158+
159+
// TODO any way to check if UnitInfo is of QuantityType?
153160
var newBaseUnits = baseUnits ?? BaseUnits;
154161

155162
var newDefaultUnits = _defaultUnits.Value.ToArray();

0 commit comments

Comments
 (0)