Skip to content

Commit 4dcc1ac

Browse files
committed
mostly cosmetic modifications
- renamed some tests, added comments here and there - WithDefaultUnit : added a check for the compatibility between QuantityType and UnitInfo passed in (also added a test for this)
1 parent 75175e7 commit 4dcc1ac

File tree

3 files changed

+31
-18
lines changed

3 files changed

+31
-18
lines changed

UnitsNet.Tests/CustomCode/LengthTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ public void Constructor_UnitSystemSI_AssignsSIUnit()
188188
}
189189

190190
[Fact]
191-
public void Constructor_UnitSystemMySmallSI_AssignsMillimiters()
191+
public void Constructor_UnitSystemWithMillimeters_ConstructsWithMillimeters()
192192
{
193193
var myDefaultLengthUnit = Length.Info.UnitInfos.First(x => x.Value == LengthUnit.Millimeter);
194194
var myUnitSystem = UnitSystem.SI.WithDefaultUnit(QuantityType.Length, myDefaultLengthUnit);

UnitsNet.Tests/UnitSystemTests.cs

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -152,27 +152,38 @@ public void SIUnitSystemHasCorrectBaseUnits()
152152
}
153153

154154
[Fact]
155-
public void GetDefaultUnitInfoThrowsExceptionForUndefinedQuantity()
155+
public void GetDefaultUnitInfo_GivenUndefinedQuantity_ThrowsArgumentException()
156156
{
157157
Assert.Throws<ArgumentException>(() => UnitSystem.SI.GetDefaultUnitInfo(QuantityType.Undefined));
158158
}
159159

160160
[Fact]
161-
public void GetDefaultUnitInfoReturnsNullForQuantitiesWithNoDefaultUnits()
161+
public void GetDefaultUnitInfo_GivenQuantityWithNoDefaultUnits_ReturnsNull()
162162
{
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));
163+
// we cannot simply rely on something like AmplitudeRatio not having a default base unit definition (as this is expected to change soon)
164+
var unitSystemWithNoDefaultLengthUnit = UnitSystem.SI.WithDefaultUnit(QuantityType.Length, null); // we can however force the dissociation
165+
166+
Assert.Null(unitSystemWithNoDefaultLengthUnit.GetDefaultUnitInfo(QuantityType.Length));
166167
}
167168

168169
[Fact]
169-
public void WithDefaultUnitThrowsIfQuantityTypeIsUndefined()
170+
public void WithDefaultUnit_GivenUndefinedQuantityType_ThrowsArgumentException()
170171
{
171-
Assert.Throws<ArgumentException>(() => UnitSystem.SI.WithDefaultUnit(QuantityType.Undefined, null));
172+
var anyUnitInfo = Length.Info.UnitInfos.First();
173+
174+
Assert.Throws<ArgumentException>(() => UnitSystem.SI.WithDefaultUnit(QuantityType.Undefined, anyUnitInfo));
175+
}
176+
177+
[Fact]
178+
public void WithDefaultUnit_GivenIncompatibleUnitAndQuantityType_ThrowsArgumentException()
179+
{
180+
var nonMassUnit = Length.Info.UnitInfos.First();
181+
182+
Assert.Throws<ArgumentException>(() => UnitSystem.SI.WithDefaultUnit(QuantityType.Mass, nonMassUnit));
172183
}
173184

174185
[Fact]
175-
public void WithDefaultUnitUsesOldBaseUnitsIfNotSpecified()
186+
public void WithDefaultUnit_GivenNullForBaseUnits_ReturnsUnitSystemWithOldBaseUnits()
176187
{
177188
var myDefaultLengthUnit = Length.Info.UnitInfos.First(x => x.Value == LengthUnit.Millimeter);
178189

@@ -189,14 +200,14 @@ public void WithDefaultUnitUsesOldBaseUnitsIfNotSpecified()
189200
[InlineData(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Second, ElectricCurrentUnit.Ampere, TemperatureUnit.Undefined, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela)]
190201
[InlineData(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Second, ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Undefined, LuminousIntensityUnit.Candela)]
191202
[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,
203+
public void WithDefaultUnit_GivenBaseUnitsNotFullyDefined_ThrowsArgumentException(LengthUnit length, MassUnit mass, DurationUnit time, ElectricCurrentUnit current,
193204
TemperatureUnit temperature, AmountOfSubstanceUnit amount, LuminousIntensityUnit luminousIntensity)
194205
{
195206
var myDefaultLengthUnit = Length.Info.UnitInfos.First(x => x.Value == LengthUnit.Millimeter);
196207

197208
var baseUnits = new BaseUnits(length, mass, time, current, temperature, amount, luminousIntensity);
198209

199-
// TODO do we want to preserve this behavior?
210+
// BaseUnits(obsolete) kept in order to avoid introducing a breaking change (just yet)
200211
Assert.Throws<ArgumentException>(()=> UnitSystem.SI.WithDefaultUnit(QuantityType.Length, myDefaultLengthUnit, baseUnits));
201212
}
202213

UnitsNet/UnitSystem.cs

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

44
using System;
5-
using System.Collections.Generic;
65
using System.Linq;
7-
using JetBrains.Annotations;
86
using UnitsNet.Units;
97

108
namespace UnitsNet
@@ -16,6 +14,7 @@ namespace UnitsNet
1614
/// </summary>
1715
public sealed class UnitSystem : IEquatable<UnitSystem>
1816
{
17+
// the array used for storing the default units in the current UnitSystem, ordered by QuantityType (excluding QuantityType.Undefined)
1918
private readonly Lazy<UnitInfo[]> _defaultUnits;
2019

2120
/// <summary>
@@ -133,7 +132,7 @@ public UnitInfo GetDefaultUnitInfo(QuantityType quantityType)
133132
{
134133
if (quantityType == QuantityType.Undefined)
135134
throw new ArgumentException("Quantity type can not be undefined.", nameof(quantityType));
136-
return _defaultUnits.Value[(int)quantityType - 1];
135+
return _defaultUnits.Value[(int) quantityType - 1]; // valid QuantityTypes start from 1 (0 == Undefined)
137136
}
138137

139138
/// <summary>
@@ -149,18 +148,21 @@ public UnitInfo GetDefaultUnitInfo(QuantityType quantityType)
149148
/// <paramref name="quantityType" />
150149
/// </returns>
151150
/// <exception cref="ArgumentException">
152-
/// Quantity type can not be undefined.
151+
/// Quantity type can not be undefined and must be compatible with the new default unit (e.g. cannot associate MassUnit with 'Meter')
153152
/// </exception>
154153
public UnitSystem WithDefaultUnit(QuantityType quantityType, UnitInfo defaultUnitInfo, BaseUnits baseUnits = null)
155154
{
156-
if (quantityType == QuantityType.Undefined)
155+
if (quantityType == QuantityType.Undefined) // redundant with the following test
157156
throw new ArgumentException("Quantity type can not be undefined.", nameof(quantityType));
158157

159-
// TODO any way to check if UnitInfo is of QuantityType?
158+
if (defaultUnitInfo != null && !Quantity.Infos.Any(x => x.QuantityType == quantityType && x.UnitInfos.Contains(defaultUnitInfo)))
159+
throw new ArgumentException("The unit provided was not found in the list of units for the specified quantity type");
160+
160161
var newBaseUnits = baseUnits ?? BaseUnits;
161162

163+
// create a copy of the current mappings, updating only the provided association
162164
var newDefaultUnits = _defaultUnits.Value.ToArray();
163-
newDefaultUnits[(int)quantityType - 1] = defaultUnitInfo;
165+
newDefaultUnits[(int) quantityType - 1] = defaultUnitInfo; // valid QuantityTypes start from 1 (0 == Undefined)
164166

165167
return new UnitSystem(newBaseUnits, newDefaultUnits);
166168
}

0 commit comments

Comments
 (0)