Skip to content

Commit 246b1cb

Browse files
authored
Support partial unit systems (#1690)
Extracted from #1544 because representing a unit system for only the dimensions an application uses is useful independently of QuantityValue. Moving it out gives the API change focused review and reduces the original PR's scope. Changes: - Allow UnitSystem to be constructed from any BaseUnits value defining at least one dimension. - Continue rejecting null and fully undefined base-unit sets. - Clarify the constructor documentation and exception message. Tests: - Change the seven previously rejected one-dimension-missing cases to assert successful construction. - Add a single-dimension UnitSystem case. - Add a dedicated assertion that BaseUnits.Undefined remains invalid.
1 parent d761370 commit 246b1cb

2 files changed

Lines changed: 13 additions & 4 deletions

File tree

UnitsNet.Tests/UnitSystemTests.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,20 @@ public void ConstructorThrowsArgumentNullExceptionForNullBaseUnits()
3434
[InlineData(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Second, ElectricCurrentUnit.Ampere, null, AmountOfSubstanceUnit.Mole, LuminousIntensityUnit.Candela)]
3535
[InlineData(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Second, ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, null, LuminousIntensityUnit.Candela)]
3636
[InlineData(LengthUnit.Meter, MassUnit.Kilogram, DurationUnit.Second, ElectricCurrentUnit.Ampere, TemperatureUnit.Kelvin, AmountOfSubstanceUnit.Mole, null)]
37-
public void ConstructorThrowsArgumentExceptionWithUndefinedUnits(LengthUnit? length, MassUnit? mass, DurationUnit? time, ElectricCurrentUnit? current,
37+
[InlineData(LengthUnit.Meter, null, null, null, null, null, null)]
38+
public void ConstructorSupportsPartialDimensions(LengthUnit? length, MassUnit? mass, DurationUnit? time, ElectricCurrentUnit? current,
3839
TemperatureUnit? temperature, AmountOfSubstanceUnit? amount, LuminousIntensityUnit? luminousIntensity)
3940
{
4041
var baseUnits = new BaseUnits(length, mass, time, current, temperature, amount, luminousIntensity);
41-
Assert.Throws<ArgumentException>(() => new UnitSystem(baseUnits));
42+
var unitSystem = new UnitSystem(baseUnits);
43+
44+
Assert.Equal(baseUnits, unitSystem.BaseUnits);
45+
}
46+
47+
[Fact]
48+
public void ConstructorThrowsArgumentExceptionWithUndefinedUnits()
49+
{
50+
Assert.Throws<ArgumentException>(() => new UnitSystem(BaseUnits.Undefined));
4251
}
4352

4453
[Fact]

UnitsNet/UnitSystem.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ public sealed class UnitSystem : IEquatable<UnitSystem>
1616
/// <summary>
1717
/// Creates an instance of a unit system with the specified base units.
1818
/// </summary>
19-
/// <param name="baseUnits">The base units for the unit system.</param>
19+
/// <param name="baseUnits">One or more base units that define the unit system.</param>
2020
public UnitSystem(BaseUnits baseUnits)
2121
{
2222
if (baseUnits is null) throw new ArgumentNullException(nameof(baseUnits));
23-
if (!baseUnits.IsFullyDefined) throw new ArgumentException("A unit system must have all base units defined.", nameof(baseUnits));
23+
if (baseUnits == BaseUnits.Undefined) throw new ArgumentException("A unit system must define at least one base unit.", nameof(baseUnits));
2424

2525
BaseUnits = baseUnits;
2626
}

0 commit comments

Comments
 (0)