diff --git a/CodeGen/Generators/UnitsNetGen/QuantityGenerator.cs b/CodeGen/Generators/UnitsNetGen/QuantityGenerator.cs
index 1a1acbb388..0238c804ae 100644
--- a/CodeGen/Generators/UnitsNetGen/QuantityGenerator.cs
+++ b/CodeGen/Generators/UnitsNetGen/QuantityGenerator.cs
@@ -214,7 +214,10 @@ private void GenerateInstanceConstructors()
Writer.WL($@"
_unit = unit;
}}
-
+");
+ if (!_isDimensionless)
+ {
+ Writer.WL($@"
///
/// Creates an instance of the quantity with the given numeric value in units compatible with the given .
/// If multiple compatible units were found, the first match is used.
@@ -225,18 +228,11 @@ private void GenerateInstanceConstructors()
/// No unit was found for the given .
public {_quantity.Name}(double value, UnitSystem unitSystem)
{{
- if (unitSystem is null) throw new ArgumentNullException(nameof(unitSystem));
-
- var unitInfos = Info.GetUnitInfosFor(unitSystem.BaseUnits);
- var firstUnitInfo = unitInfos.FirstOrDefault();
-");
-
- Writer.WL(@"
- _value = value;");
- Writer.WL($@"
- _unit = firstUnitInfo?.Value ?? throw new ArgumentException(""No units were found for the given UnitSystem."", nameof(unitSystem));
+ _value = value;
+ _unit = Info.GetDefaultUnit(unitSystem);
}}
");
+ }
}
private void GenerateStaticProperties()
@@ -1000,34 +996,16 @@ public double As({_unitEnumName} unit)
}}
");
- Writer.WL($@"
+ Writer.WL( $@"
///
public double As(UnitSystem unitSystem)
{{
- if (unitSystem is null)
- throw new ArgumentNullException(nameof(unitSystem));
-
- var unitInfos = Info.GetUnitInfosFor(unitSystem.BaseUnits);
-
- var firstUnitInfo = unitInfos.FirstOrDefault();
- if (firstUnitInfo == null)
- throw new ArgumentException(""No units were found for the given UnitSystem."", nameof(unitSystem));
-
- return As(firstUnitInfo.Value);
+ return As(Info.GetDefaultUnit(unitSystem));
}}
");
Writer.WL($@"
- ///
- double IQuantity.As(Enum unit)
- {{
- if (!(unit is {_unitEnumName} typedUnit))
- throw new ArgumentException($""The given unit is of type {{unit.GetType()}}. Only {{typeof({_unitEnumName})}} is supported."", nameof(unit));
-
- return As(typedUnit);
- }}
-
///
/// Converts this {_quantity.Name} to another {_quantity.Name} with the unit representation .
///
@@ -1123,29 +1101,33 @@ private bool TryToUnit({_unitEnumName} unit, [NotNullWhen(true)] out {_quantity.
converted = convertedOrNull.Value;
return true;
}}
+");
+ Writer.WL($@"
+ ///
+ public {_quantity.Name} ToUnit(UnitSystem unitSystem)
+ {{
+ return ToUnit(Info.GetDefaultUnit(unitSystem));
+ }}
+");
- ///
- IQuantity IQuantity.ToUnit(Enum unit)
+ Writer.WL($@"
+ #region Explicit implementations
+
+ double IQuantity.As(Enum unit)
{{
- if (!(unit is {_unitEnumName} typedUnit))
+ if (unit is not {_unitEnumName} typedUnit)
throw new ArgumentException($""The given unit is of type {{unit.GetType()}}. Only {{typeof({_unitEnumName})}} is supported."", nameof(unit));
- return ToUnit(typedUnit, DefaultConversionFunctions);
+ return As(typedUnit);
}}
- ///
- public {_quantity.Name} ToUnit(UnitSystem unitSystem)
+ ///
+ IQuantity IQuantity.ToUnit(Enum unit)
{{
- if (unitSystem is null)
- throw new ArgumentNullException(nameof(unitSystem));
-
- var unitInfos = Info.GetUnitInfosFor(unitSystem.BaseUnits);
-
- var firstUnitInfo = unitInfos.FirstOrDefault();
- if (firstUnitInfo == null)
- throw new ArgumentException(""No units were found for the given UnitSystem."", nameof(unitSystem));
+ if (!(unit is {_unitEnumName} typedUnit))
+ throw new ArgumentException($""The given unit is of type {{unit.GetType()}}. Only {{typeof({_unitEnumName})}} is supported."", nameof(unit));
- return ToUnit(firstUnitInfo.Value);
+ return ToUnit(typedUnit, DefaultConversionFunctions);
}}
///
@@ -1158,6 +1140,8 @@ IQuantity IQuantity.ToUnit(Enum unit)
IQuantity<{_unitEnumName}> IQuantity<{_unitEnumName}>.ToUnit(UnitSystem unitSystem) => ToUnit(unitSystem);
#endregion
+
+ #endregion
");
}
diff --git a/CodeGen/Generators/UnitsNetGen/UnitTestBaseClassGenerator.cs b/CodeGen/Generators/UnitsNetGen/UnitTestBaseClassGenerator.cs
index 64d7775f50..6f8e55be76 100644
--- a/CodeGen/Generators/UnitsNetGen/UnitTestBaseClassGenerator.cs
+++ b/CodeGen/Generators/UnitsNetGen/UnitTestBaseClassGenerator.cs
@@ -50,6 +50,14 @@ internal class UnitTestBaseClassGenerator : GeneratorBase
/// Example: "LengthUnit.Centimeter".
///
private readonly string _otherOrBaseUnitFullName;
+
+ ///
+ /// Indicates whether the quantity is dimensionless.
+ ///
+ ///
+ /// A dimensionless quantity has all base dimensions (L, M, T, I, Θ, N, J) equal to zero.
+ ///
+ private readonly bool _isDimensionless;
///
/// Stores a mapping of culture names to their corresponding unique unit abbreviations.
@@ -98,6 +106,7 @@ public UnitTestBaseClassGenerator(Quantity quantity)
// Try to pick another unit, or fall back to base unit if only a single unit.
_otherOrBaseUnit = quantity.Units.Where(u => u != _baseUnit).DefaultIfEmpty(_baseUnit).First();
_otherOrBaseUnitFullName = $"{_unitEnumName}.{_otherOrBaseUnit.SingularName}";
+ _isDimensionless = quantity.BaseDimensions is { L: 0, M: 0, T: 0, I: 0, Θ: 0, N: 0, J: 0 };
var abbreviationsForCulture = new Dictionary>>();
foreach (Unit unit in quantity.Units)
@@ -257,6 +266,10 @@ public void Ctor_WithNaNValue_DoNotThrowsArgumentException()
Assert.Null(exception);
}}
+");
+ if (!_isDimensionless)
+ {
+ Writer.WL($@"
[Fact]
public void Ctor_NullAsUnitSystem_ThrowsArgumentNullException()
@@ -265,20 +278,24 @@ public void Ctor_NullAsUnitSystem_ThrowsArgumentNullException()
}}
[Fact]
- public void Ctor_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
+ public virtual void Ctor_SIUnitSystem_ReturnsQuantityWithSIUnits()
{{
- Func
- protected abstract bool SupportsSIUnitSystem { get; }
+ protected virtual bool SupportsSIUnitSystem { get; } = true;
+
+ ///
+ /// Gets a predefined instance of representing unsupported or invalid base units.
+ ///
+ ///
+ /// This property is initialized with invalid unit values, represented by negative enumerations of
+ /// , , , ,
+ /// , , and .
+ /// It is intended for use in scenarios where base units are not supported or need to be explicitly marked as invalid.
+ ///
+ protected static BaseUnits UnsupportedBaseUnits { get; } = new(
+ (LengthUnit)(-1),
+ (MassUnit)(-1),
+ (DurationUnit)(-1),
+ (ElectricCurrentUnit)(-1),
+ (TemperatureUnit)(-1),
+ (AmountOfSubstanceUnit)(-1),
+ (LuminousIntensityUnit)(-1));
}
}
diff --git a/UnitsNet.Tests/CustomCode/ThermalInsulanceTests.cs b/UnitsNet.Tests/CustomCode/ThermalInsulanceTests.cs
index 8ef94686e4..04ad2d5c2c 100644
--- a/UnitsNet.Tests/CustomCode/ThermalInsulanceTests.cs
+++ b/UnitsNet.Tests/CustomCode/ThermalInsulanceTests.cs
@@ -18,6 +18,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
+using Xunit;
namespace UnitsNet.Tests.CustomCode
{
@@ -30,5 +31,11 @@ public class ThermalInsulanceTests : ThermalInsulanceTestsBase
protected override double SquareMeterDegreesCelsiusPerWattInOneSquareMeterKelvinPerKilowatt => 1e-3;
protected override double SquareMeterKelvinsPerKilowattInOneSquareMeterKelvinPerKilowatt => 1;
protected override double SquareMeterKelvinsPerWattInOneSquareMeterKelvinPerKilowatt => 0.001;
+
+ [Fact(Skip = "See about changing the BaseUnit to SquareMeterKelvinPerWatt")]
+ public override void BaseUnit_HasSIBase()
+ {
+ base.BaseUnit_HasSIBase();
+ }
}
}
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/AbsorbedDoseOfIonizingRadiationTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/AbsorbedDoseOfIonizingRadiationTestsBase.g.cs
index c73f176c92..18ed31d7f7 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/AbsorbedDoseOfIonizingRadiationTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/AbsorbedDoseOfIonizingRadiationTestsBase.g.cs
@@ -152,18 +152,18 @@ public void Ctor_NullAsUnitSystem_ThrowsArgumentNullException()
}
[Fact]
- public void Ctor_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
+ public virtual void Ctor_SIUnitSystem_ReturnsQuantityWithSIUnits()
{
- Func TestCode = () => new AbsorbedDoseOfIonizingRadiation(value: 1, unitSystem: UnitSystem.SI);
- if (SupportsSIUnitSystem)
- {
- var quantity = (AbsorbedDoseOfIonizingRadiation) TestCode();
- Assert.Equal(1, quantity.Value);
- }
- else
- {
- Assert.Throws(TestCode);
- }
+ var quantity = new AbsorbedDoseOfIonizingRadiation(value: 1, unitSystem: UnitSystem.SI);
+ Assert.Equal(1, quantity.Value);
+ Assert.True(quantity.QuantityInfo.UnitInfos.First(x => x.Value == quantity.Unit).BaseUnits.IsSubsetOf(UnitSystem.SI.BaseUnits));
+ }
+
+ [Fact]
+ public void Ctor_UnitSystem_ThrowsArgumentExceptionIfNotSupported()
+ {
+ var unsupportedUnitSystem = new UnitSystem(UnsupportedBaseUnits);
+ Assert.Throws(() => new AbsorbedDoseOfIonizingRadiation(value: 1, unitSystem: unsupportedUnitSystem));
}
[Fact]
@@ -312,20 +312,109 @@ public void As()
}
[Fact]
- public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
+ public virtual void BaseUnit_HasSIBase()
+ {
+ var baseUnitInfo = AbsorbedDoseOfIonizingRadiation.Info.BaseUnitInfo;
+ Assert.True(baseUnitInfo.BaseUnits.IsSubsetOf(UnitSystem.SI.BaseUnits));
+ }
+
+ [Fact]
+ public virtual void As_UnitSystem_SI_ReturnsQuantityInSIUnits()
+ {
+ var quantity = new AbsorbedDoseOfIonizingRadiation(value: 1, unit: AbsorbedDoseOfIonizingRadiation.BaseUnit);
+ var expectedValue = quantity.As(AbsorbedDoseOfIonizingRadiation.Info.GetDefaultUnit(UnitSystem.SI));
+
+ var convertedValue = quantity.As(UnitSystem.SI);
+
+ Assert.Equal(expectedValue, convertedValue);
+ }
+
+ [Fact]
+ public void As_UnitSystem_ThrowsArgumentNullExceptionIfNull()
+ {
+ var quantity = new AbsorbedDoseOfIonizingRadiation(value: 1, unit: AbsorbedDoseOfIonizingRadiation.BaseUnit);
+ UnitSystem nullUnitSystem = null!;
+ Assert.Throws(() => quantity.As(nullUnitSystem));
+ }
+
+ [Fact]
+ public void As_UnitSystem_ThrowsArgumentExceptionIfNotSupported()
+ {
+ var quantity = new AbsorbedDoseOfIonizingRadiation(value: 1, unit: AbsorbedDoseOfIonizingRadiation.BaseUnit);
+ var unsupportedUnitSystem = new UnitSystem(UnsupportedBaseUnits);
+ Assert.Throws(() => quantity.As(unsupportedUnitSystem));
+ }
+
+ [Fact]
+ public virtual void ToUnit_UnitSystem_SI_ReturnsQuantityInSIUnits()
{
var quantity = new AbsorbedDoseOfIonizingRadiation(value: 1, unit: AbsorbedDoseOfIonizingRadiation.BaseUnit);
- Func AsWithSIUnitSystem = () => quantity.As(UnitSystem.SI);
+ var expectedUnit = AbsorbedDoseOfIonizingRadiation.Info.GetDefaultUnit(UnitSystem.SI);
+ var expectedValue = quantity.As(expectedUnit);
- if (SupportsSIUnitSystem)
+ Assert.Multiple(() =>
{
- var value = Convert.ToDouble(AsWithSIUnitSystem());
- Assert.Equal(1, value);
- }
- else
+ AbsorbedDoseOfIonizingRadiation quantityToConvert = quantity;
+
+ AbsorbedDoseOfIonizingRadiation convertedQuantity = quantityToConvert.ToUnit(UnitSystem.SI);
+
+ Assert.Equal(expectedUnit, convertedQuantity.Unit);
+ Assert.Equal(expectedValue, convertedQuantity.Value);
+ }, () =>
{
- Assert.Throws(AsWithSIUnitSystem);
- }
+ IQuantity quantityToConvert = quantity;
+
+ IQuantity convertedQuantity = quantityToConvert.ToUnit(UnitSystem.SI);
+
+ Assert.Equal(expectedUnit, convertedQuantity.Unit);
+ Assert.Equal(expectedValue, convertedQuantity.Value);
+ }, () =>
+ {
+ IQuantity quantityToConvert = quantity;
+
+ IQuantity convertedQuantity = quantityToConvert.ToUnit(UnitSystem.SI);
+
+ Assert.Equal(expectedUnit, convertedQuantity.Unit);
+ Assert.Equal(expectedValue, convertedQuantity.Value);
+ });
+ }
+
+ [Fact]
+ public void ToUnit_UnitSystem_ThrowsArgumentNullExceptionIfNull()
+ {
+ UnitSystem nullUnitSystem = null!;
+ Assert.Multiple(() =>
+ {
+ var quantity = new AbsorbedDoseOfIonizingRadiation(value: 1, unit: AbsorbedDoseOfIonizingRadiation.BaseUnit);
+ Assert.Throws(() => quantity.ToUnit(nullUnitSystem));
+ }, () =>
+ {
+ IQuantity quantity = new AbsorbedDoseOfIonizingRadiation(value: 1, unit: AbsorbedDoseOfIonizingRadiation.BaseUnit);
+ Assert.Throws(() => quantity.ToUnit(nullUnitSystem));
+ }, () =>
+ {
+ IQuantity quantity = new AbsorbedDoseOfIonizingRadiation(value: 1, unit: AbsorbedDoseOfIonizingRadiation.BaseUnit);
+ Assert.Throws(() => quantity.ToUnit(nullUnitSystem));
+ });
+ }
+
+ [Fact]
+ public void ToUnit_UnitSystem_ThrowsArgumentExceptionIfNotSupported()
+ {
+ var unsupportedUnitSystem = new UnitSystem(UnsupportedBaseUnits);
+ Assert.Multiple(() =>
+ {
+ var quantity = new AbsorbedDoseOfIonizingRadiation(value: 1, unit: AbsorbedDoseOfIonizingRadiation.BaseUnit);
+ Assert.Throws(() => quantity.ToUnit(unsupportedUnitSystem));
+ }, () =>
+ {
+ IQuantity quantity = new AbsorbedDoseOfIonizingRadiation(value: 1, unit: AbsorbedDoseOfIonizingRadiation.BaseUnit);
+ Assert.Throws(() => quantity.ToUnit(unsupportedUnitSystem));
+ }, () =>
+ {
+ IQuantity quantity = new AbsorbedDoseOfIonizingRadiation(value: 1, unit: AbsorbedDoseOfIonizingRadiation.BaseUnit);
+ Assert.Throws(() => quantity.ToUnit(unsupportedUnitSystem));
+ });
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/AccelerationTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/AccelerationTestsBase.g.cs
index d8477708b6..0879a6b214 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/AccelerationTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/AccelerationTestsBase.g.cs
@@ -144,18 +144,18 @@ public void Ctor_NullAsUnitSystem_ThrowsArgumentNullException()
}
[Fact]
- public void Ctor_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
+ public virtual void Ctor_SIUnitSystem_ReturnsQuantityWithSIUnits()
{
- Func TestCode = () => new Acceleration(value: 1, unitSystem: UnitSystem.SI);
- if (SupportsSIUnitSystem)
- {
- var quantity = (Acceleration) TestCode();
- Assert.Equal(1, quantity.Value);
- }
- else
- {
- Assert.Throws(TestCode);
- }
+ var quantity = new Acceleration(value: 1, unitSystem: UnitSystem.SI);
+ Assert.Equal(1, quantity.Value);
+ Assert.True(quantity.QuantityInfo.UnitInfos.First(x => x.Value == quantity.Unit).BaseUnits.IsSubsetOf(UnitSystem.SI.BaseUnits));
+ }
+
+ [Fact]
+ public void Ctor_UnitSystem_ThrowsArgumentExceptionIfNotSupported()
+ {
+ var unsupportedUnitSystem = new UnitSystem(UnsupportedBaseUnits);
+ Assert.Throws(() => new Acceleration(value: 1, unitSystem: unsupportedUnitSystem));
}
[Fact]
@@ -292,20 +292,109 @@ public void As()
}
[Fact]
- public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
+ public virtual void BaseUnit_HasSIBase()
+ {
+ var baseUnitInfo = Acceleration.Info.BaseUnitInfo;
+ Assert.True(baseUnitInfo.BaseUnits.IsSubsetOf(UnitSystem.SI.BaseUnits));
+ }
+
+ [Fact]
+ public virtual void As_UnitSystem_SI_ReturnsQuantityInSIUnits()
+ {
+ var quantity = new Acceleration(value: 1, unit: Acceleration.BaseUnit);
+ var expectedValue = quantity.As(Acceleration.Info.GetDefaultUnit(UnitSystem.SI));
+
+ var convertedValue = quantity.As(UnitSystem.SI);
+
+ Assert.Equal(expectedValue, convertedValue);
+ }
+
+ [Fact]
+ public void As_UnitSystem_ThrowsArgumentNullExceptionIfNull()
+ {
+ var quantity = new Acceleration(value: 1, unit: Acceleration.BaseUnit);
+ UnitSystem nullUnitSystem = null!;
+ Assert.Throws(() => quantity.As(nullUnitSystem));
+ }
+
+ [Fact]
+ public void As_UnitSystem_ThrowsArgumentExceptionIfNotSupported()
+ {
+ var quantity = new Acceleration(value: 1, unit: Acceleration.BaseUnit);
+ var unsupportedUnitSystem = new UnitSystem(UnsupportedBaseUnits);
+ Assert.Throws(() => quantity.As(unsupportedUnitSystem));
+ }
+
+ [Fact]
+ public virtual void ToUnit_UnitSystem_SI_ReturnsQuantityInSIUnits()
{
var quantity = new Acceleration(value: 1, unit: Acceleration.BaseUnit);
- Func AsWithSIUnitSystem = () => quantity.As(UnitSystem.SI);
+ var expectedUnit = Acceleration.Info.GetDefaultUnit(UnitSystem.SI);
+ var expectedValue = quantity.As(expectedUnit);
- if (SupportsSIUnitSystem)
+ Assert.Multiple(() =>
{
- var value = Convert.ToDouble(AsWithSIUnitSystem());
- Assert.Equal(1, value);
- }
- else
+ Acceleration quantityToConvert = quantity;
+
+ Acceleration convertedQuantity = quantityToConvert.ToUnit(UnitSystem.SI);
+
+ Assert.Equal(expectedUnit, convertedQuantity.Unit);
+ Assert.Equal(expectedValue, convertedQuantity.Value);
+ }, () =>
{
- Assert.Throws(AsWithSIUnitSystem);
- }
+ IQuantity quantityToConvert = quantity;
+
+ IQuantity convertedQuantity = quantityToConvert.ToUnit(UnitSystem.SI);
+
+ Assert.Equal(expectedUnit, convertedQuantity.Unit);
+ Assert.Equal(expectedValue, convertedQuantity.Value);
+ }, () =>
+ {
+ IQuantity quantityToConvert = quantity;
+
+ IQuantity convertedQuantity = quantityToConvert.ToUnit(UnitSystem.SI);
+
+ Assert.Equal(expectedUnit, convertedQuantity.Unit);
+ Assert.Equal(expectedValue, convertedQuantity.Value);
+ });
+ }
+
+ [Fact]
+ public void ToUnit_UnitSystem_ThrowsArgumentNullExceptionIfNull()
+ {
+ UnitSystem nullUnitSystem = null!;
+ Assert.Multiple(() =>
+ {
+ var quantity = new Acceleration(value: 1, unit: Acceleration.BaseUnit);
+ Assert.Throws(() => quantity.ToUnit(nullUnitSystem));
+ }, () =>
+ {
+ IQuantity quantity = new Acceleration(value: 1, unit: Acceleration.BaseUnit);
+ Assert.Throws(() => quantity.ToUnit(nullUnitSystem));
+ }, () =>
+ {
+ IQuantity quantity = new Acceleration(value: 1, unit: Acceleration.BaseUnit);
+ Assert.Throws(() => quantity.ToUnit(nullUnitSystem));
+ });
+ }
+
+ [Fact]
+ public void ToUnit_UnitSystem_ThrowsArgumentExceptionIfNotSupported()
+ {
+ var unsupportedUnitSystem = new UnitSystem(UnsupportedBaseUnits);
+ Assert.Multiple(() =>
+ {
+ var quantity = new Acceleration(value: 1, unit: Acceleration.BaseUnit);
+ Assert.Throws(() => quantity.ToUnit(unsupportedUnitSystem));
+ }, () =>
+ {
+ IQuantity quantity = new Acceleration(value: 1, unit: Acceleration.BaseUnit);
+ Assert.Throws(() => quantity.ToUnit(unsupportedUnitSystem));
+ }, () =>
+ {
+ IQuantity quantity = new Acceleration(value: 1, unit: Acceleration.BaseUnit);
+ Assert.Throws(() => quantity.ToUnit(unsupportedUnitSystem));
+ });
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/AmountOfSubstanceTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/AmountOfSubstanceTestsBase.g.cs
index 4803afa163..49e464382a 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/AmountOfSubstanceTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/AmountOfSubstanceTestsBase.g.cs
@@ -156,18 +156,18 @@ public void Ctor_NullAsUnitSystem_ThrowsArgumentNullException()
}
[Fact]
- public void Ctor_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
+ public virtual void Ctor_SIUnitSystem_ReturnsQuantityWithSIUnits()
{
- Func TestCode = () => new AmountOfSubstance(value: 1, unitSystem: UnitSystem.SI);
- if (SupportsSIUnitSystem)
- {
- var quantity = (AmountOfSubstance) TestCode();
- Assert.Equal(1, quantity.Value);
- }
- else
- {
- Assert.Throws(TestCode);
- }
+ var quantity = new AmountOfSubstance(value: 1, unitSystem: UnitSystem.SI);
+ Assert.Equal(1, quantity.Value);
+ Assert.True(quantity.QuantityInfo.UnitInfos.First(x => x.Value == quantity.Unit).BaseUnits.IsSubsetOf(UnitSystem.SI.BaseUnits));
+ }
+
+ [Fact]
+ public void Ctor_UnitSystem_ThrowsArgumentExceptionIfNotSupported()
+ {
+ var unsupportedUnitSystem = new UnitSystem(UnsupportedBaseUnits);
+ Assert.Throws(() => new AmountOfSubstance(value: 1, unitSystem: unsupportedUnitSystem));
}
[Fact]
@@ -322,20 +322,109 @@ public void As()
}
[Fact]
- public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
+ public virtual void BaseUnit_HasSIBase()
+ {
+ var baseUnitInfo = AmountOfSubstance.Info.BaseUnitInfo;
+ Assert.True(baseUnitInfo.BaseUnits.IsSubsetOf(UnitSystem.SI.BaseUnits));
+ }
+
+ [Fact]
+ public virtual void As_UnitSystem_SI_ReturnsQuantityInSIUnits()
+ {
+ var quantity = new AmountOfSubstance(value: 1, unit: AmountOfSubstance.BaseUnit);
+ var expectedValue = quantity.As(AmountOfSubstance.Info.GetDefaultUnit(UnitSystem.SI));
+
+ var convertedValue = quantity.As(UnitSystem.SI);
+
+ Assert.Equal(expectedValue, convertedValue);
+ }
+
+ [Fact]
+ public void As_UnitSystem_ThrowsArgumentNullExceptionIfNull()
+ {
+ var quantity = new AmountOfSubstance(value: 1, unit: AmountOfSubstance.BaseUnit);
+ UnitSystem nullUnitSystem = null!;
+ Assert.Throws(() => quantity.As(nullUnitSystem));
+ }
+
+ [Fact]
+ public void As_UnitSystem_ThrowsArgumentExceptionIfNotSupported()
+ {
+ var quantity = new AmountOfSubstance(value: 1, unit: AmountOfSubstance.BaseUnit);
+ var unsupportedUnitSystem = new UnitSystem(UnsupportedBaseUnits);
+ Assert.Throws(() => quantity.As(unsupportedUnitSystem));
+ }
+
+ [Fact]
+ public virtual void ToUnit_UnitSystem_SI_ReturnsQuantityInSIUnits()
{
var quantity = new AmountOfSubstance(value: 1, unit: AmountOfSubstance.BaseUnit);
- Func AsWithSIUnitSystem = () => quantity.As(UnitSystem.SI);
+ var expectedUnit = AmountOfSubstance.Info.GetDefaultUnit(UnitSystem.SI);
+ var expectedValue = quantity.As(expectedUnit);
- if (SupportsSIUnitSystem)
+ Assert.Multiple(() =>
{
- var value = Convert.ToDouble(AsWithSIUnitSystem());
- Assert.Equal(1, value);
- }
- else
+ AmountOfSubstance quantityToConvert = quantity;
+
+ AmountOfSubstance convertedQuantity = quantityToConvert.ToUnit(UnitSystem.SI);
+
+ Assert.Equal(expectedUnit, convertedQuantity.Unit);
+ Assert.Equal(expectedValue, convertedQuantity.Value);
+ }, () =>
{
- Assert.Throws(AsWithSIUnitSystem);
- }
+ IQuantity quantityToConvert = quantity;
+
+ IQuantity convertedQuantity = quantityToConvert.ToUnit(UnitSystem.SI);
+
+ Assert.Equal(expectedUnit, convertedQuantity.Unit);
+ Assert.Equal(expectedValue, convertedQuantity.Value);
+ }, () =>
+ {
+ IQuantity quantityToConvert = quantity;
+
+ IQuantity convertedQuantity = quantityToConvert.ToUnit(UnitSystem.SI);
+
+ Assert.Equal(expectedUnit, convertedQuantity.Unit);
+ Assert.Equal(expectedValue, convertedQuantity.Value);
+ });
+ }
+
+ [Fact]
+ public void ToUnit_UnitSystem_ThrowsArgumentNullExceptionIfNull()
+ {
+ UnitSystem nullUnitSystem = null!;
+ Assert.Multiple(() =>
+ {
+ var quantity = new AmountOfSubstance(value: 1, unit: AmountOfSubstance.BaseUnit);
+ Assert.Throws(() => quantity.ToUnit(nullUnitSystem));
+ }, () =>
+ {
+ IQuantity quantity = new AmountOfSubstance(value: 1, unit: AmountOfSubstance.BaseUnit);
+ Assert.Throws(() => quantity.ToUnit(nullUnitSystem));
+ }, () =>
+ {
+ IQuantity quantity = new AmountOfSubstance(value: 1, unit: AmountOfSubstance.BaseUnit);
+ Assert.Throws(() => quantity.ToUnit(nullUnitSystem));
+ });
+ }
+
+ [Fact]
+ public void ToUnit_UnitSystem_ThrowsArgumentExceptionIfNotSupported()
+ {
+ var unsupportedUnitSystem = new UnitSystem(UnsupportedBaseUnits);
+ Assert.Multiple(() =>
+ {
+ var quantity = new AmountOfSubstance(value: 1, unit: AmountOfSubstance.BaseUnit);
+ Assert.Throws(() => quantity.ToUnit(unsupportedUnitSystem));
+ }, () =>
+ {
+ IQuantity quantity = new AmountOfSubstance(value: 1, unit: AmountOfSubstance.BaseUnit);
+ Assert.Throws(() => quantity.ToUnit(unsupportedUnitSystem));
+ }, () =>
+ {
+ IQuantity quantity = new AmountOfSubstance(value: 1, unit: AmountOfSubstance.BaseUnit);
+ Assert.Throws(() => quantity.ToUnit(unsupportedUnitSystem));
+ });
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/AmplitudeRatioTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/AmplitudeRatioTestsBase.g.cs
index 4f81f7b51e..f5f8083341 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/AmplitudeRatioTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/AmplitudeRatioTestsBase.g.cs
@@ -97,27 +97,6 @@ public void Ctor_WithNaNValue_DoNotThrowsArgumentException()
Assert.Null(exception);
}
- [Fact]
- public void Ctor_NullAsUnitSystem_ThrowsArgumentNullException()
- {
- Assert.Throws(() => new AmplitudeRatio(value: 1, unitSystem: null));
- }
-
- [Fact]
- public void Ctor_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
- {
- Func TestCode = () => new AmplitudeRatio(value: 1, unitSystem: UnitSystem.SI);
- if (SupportsSIUnitSystem)
- {
- var quantity = (AmplitudeRatio) TestCode();
- Assert.Equal(1, quantity.Value);
- }
- else
- {
- Assert.Throws(TestCode);
- }
- }
-
[Fact]
public void AmplitudeRatio_QuantityInfo_ReturnsQuantityInfoDescribingQuantity()
{
@@ -192,20 +171,70 @@ public void As()
}
[Fact]
- public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
+ public void As_UnitSystem_ReturnsValueInDimensionlessUnit()
+ {
+ var quantity = new AmplitudeRatio(value: 1, unit: AmplitudeRatioUnit.DecibelVolt);
+
+ var convertedValue = quantity.As(UnitSystem.SI);
+
+ Assert.Equal(quantity.Value, convertedValue);
+ }
+
+ [Fact]
+ public void As_UnitSystem_ThrowsArgumentNullExceptionIfNull()
{
var quantity = new AmplitudeRatio(value: 1, unit: AmplitudeRatio.BaseUnit);
- Func AsWithSIUnitSystem = () => quantity.As(UnitSystem.SI);
+ UnitSystem nullUnitSystem = null!;
+ Assert.Throws(() => quantity.As(nullUnitSystem));
+ }
- if (SupportsSIUnitSystem)
+ [Fact]
+ public void ToUnitSystem_ReturnsValueInDimensionlessUnit()
+ {
+ Assert.Multiple(() =>
{
- var value = Convert.ToDouble(AsWithSIUnitSystem());
- Assert.Equal(1, value);
- }
- else
+ var quantity = new AmplitudeRatio(value: 1, unit: AmplitudeRatioUnit.DecibelVolt);
+
+ AmplitudeRatio convertedQuantity = quantity.ToUnit(UnitSystem.SI);
+
+ Assert.Equal(AmplitudeRatioUnit.DecibelVolt, convertedQuantity.Unit);
+ Assert.Equal(quantity.Value, convertedQuantity.Value);
+ }, () =>
{
- Assert.Throws(AsWithSIUnitSystem);
- }
+ IQuantity quantity = new AmplitudeRatio(value: 1, unit: AmplitudeRatioUnit.DecibelVolt);
+
+ IQuantity convertedQuantity = quantity.ToUnit(UnitSystem.SI);
+
+ Assert.Equal(AmplitudeRatioUnit.DecibelVolt, convertedQuantity.Unit);
+ Assert.Equal(quantity.Value, convertedQuantity.Value);
+ }, () =>
+ {
+ IQuantity quantity = new AmplitudeRatio(value: 1, unit: AmplitudeRatioUnit.DecibelVolt);
+
+ IQuantity convertedQuantity = quantity.ToUnit(UnitSystem.SI);
+
+ Assert.Equal(AmplitudeRatioUnit.DecibelVolt, convertedQuantity.Unit);
+ Assert.Equal(quantity.Value, convertedQuantity.Value);
+ });
+ }
+
+ [Fact]
+ public void ToUnit_UnitSystem_ThrowsArgumentNullExceptionIfNull()
+ {
+ UnitSystem nullUnitSystem = null!;
+ Assert.Multiple(() =>
+ {
+ var quantity = new AmplitudeRatio(value: 1, unit: AmplitudeRatio.BaseUnit);
+ Assert.Throws(() => quantity.ToUnit(nullUnitSystem));
+ }, () =>
+ {
+ IQuantity quantity = new AmplitudeRatio(value: 1, unit: AmplitudeRatio.BaseUnit);
+ Assert.Throws(() => quantity.ToUnit(nullUnitSystem));
+ }, () =>
+ {
+ IQuantity quantity = new AmplitudeRatio(value: 1, unit: AmplitudeRatio.BaseUnit);
+ Assert.Throws(() => quantity.ToUnit(nullUnitSystem));
+ });
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/AngleTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/AngleTestsBase.g.cs
index c4ff8b2806..e7055d0a73 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/AngleTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/AngleTestsBase.g.cs
@@ -141,27 +141,6 @@ public void Ctor_WithNaNValue_DoNotThrowsArgumentException()
Assert.Null(exception);
}
- [Fact]
- public void Ctor_NullAsUnitSystem_ThrowsArgumentNullException()
- {
- Assert.Throws(() => new Angle(value: 1, unitSystem: null));
- }
-
- [Fact]
- public void Ctor_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
- {
- Func TestCode = () => new Angle(value: 1, unitSystem: UnitSystem.SI);
- if (SupportsSIUnitSystem)
- {
- var quantity = (Angle) TestCode();
- Assert.Equal(1, quantity.Value);
- }
- else
- {
- Assert.Throws(TestCode);
- }
- }
-
[Fact]
public void Angle_QuantityInfo_ReturnsQuantityInfoDescribingQuantity()
{
@@ -302,20 +281,70 @@ public void As()
}
[Fact]
- public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
+ public void As_UnitSystem_ReturnsValueInDimensionlessUnit()
+ {
+ var quantity = new Angle(value: 1, unit: AngleUnit.Radian);
+
+ var convertedValue = quantity.As(UnitSystem.SI);
+
+ Assert.Equal(quantity.Value, convertedValue);
+ }
+
+ [Fact]
+ public void As_UnitSystem_ThrowsArgumentNullExceptionIfNull()
{
var quantity = new Angle(value: 1, unit: Angle.BaseUnit);
- Func AsWithSIUnitSystem = () => quantity.As(UnitSystem.SI);
+ UnitSystem nullUnitSystem = null!;
+ Assert.Throws(() => quantity.As(nullUnitSystem));
+ }
- if (SupportsSIUnitSystem)
+ [Fact]
+ public void ToUnitSystem_ReturnsValueInDimensionlessUnit()
+ {
+ Assert.Multiple(() =>
{
- var value = Convert.ToDouble(AsWithSIUnitSystem());
- Assert.Equal(1, value);
- }
- else
+ var quantity = new Angle(value: 1, unit: AngleUnit.Radian);
+
+ Angle convertedQuantity = quantity.ToUnit(UnitSystem.SI);
+
+ Assert.Equal(AngleUnit.Radian, convertedQuantity.Unit);
+ Assert.Equal(quantity.Value, convertedQuantity.Value);
+ }, () =>
{
- Assert.Throws(AsWithSIUnitSystem);
- }
+ IQuantity quantity = new Angle(value: 1, unit: AngleUnit.Radian);
+
+ IQuantity convertedQuantity = quantity.ToUnit(UnitSystem.SI);
+
+ Assert.Equal(AngleUnit.Radian, convertedQuantity.Unit);
+ Assert.Equal(quantity.Value, convertedQuantity.Value);
+ }, () =>
+ {
+ IQuantity quantity = new Angle(value: 1, unit: AngleUnit.Radian);
+
+ IQuantity convertedQuantity = quantity.ToUnit(UnitSystem.SI);
+
+ Assert.Equal(AngleUnit.Radian, convertedQuantity.Unit);
+ Assert.Equal(quantity.Value, convertedQuantity.Value);
+ });
+ }
+
+ [Fact]
+ public void ToUnit_UnitSystem_ThrowsArgumentNullExceptionIfNull()
+ {
+ UnitSystem nullUnitSystem = null!;
+ Assert.Multiple(() =>
+ {
+ var quantity = new Angle(value: 1, unit: Angle.BaseUnit);
+ Assert.Throws(() => quantity.ToUnit(nullUnitSystem));
+ }, () =>
+ {
+ IQuantity quantity = new Angle(value: 1, unit: Angle.BaseUnit);
+ Assert.Throws(() => quantity.ToUnit(nullUnitSystem));
+ }, () =>
+ {
+ IQuantity quantity = new Angle(value: 1, unit: Angle.BaseUnit);
+ Assert.Throws(() => quantity.ToUnit(nullUnitSystem));
+ });
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/AreaDensityTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/AreaDensityTestsBase.g.cs
index 206a8179d2..05d1bb83d4 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/AreaDensityTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/AreaDensityTestsBase.g.cs
@@ -100,18 +100,18 @@ public void Ctor_NullAsUnitSystem_ThrowsArgumentNullException()
}
[Fact]
- public void Ctor_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
+ public virtual void Ctor_SIUnitSystem_ReturnsQuantityWithSIUnits()
{
- Func TestCode = () => new AreaDensity(value: 1, unitSystem: UnitSystem.SI);
- if (SupportsSIUnitSystem)
- {
- var quantity = (AreaDensity) TestCode();
- Assert.Equal(1, quantity.Value);
- }
- else
- {
- Assert.Throws(TestCode);
- }
+ var quantity = new AreaDensity(value: 1, unitSystem: UnitSystem.SI);
+ Assert.Equal(1, quantity.Value);
+ Assert.True(quantity.QuantityInfo.UnitInfos.First(x => x.Value == quantity.Unit).BaseUnits.IsSubsetOf(UnitSystem.SI.BaseUnits));
+ }
+
+ [Fact]
+ public void Ctor_UnitSystem_ThrowsArgumentExceptionIfNotSupported()
+ {
+ var unsupportedUnitSystem = new UnitSystem(UnsupportedBaseUnits);
+ Assert.Throws(() => new AreaDensity(value: 1, unitSystem: unsupportedUnitSystem));
}
[Fact]
@@ -182,20 +182,109 @@ public void As()
}
[Fact]
- public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
+ public virtual void BaseUnit_HasSIBase()
+ {
+ var baseUnitInfo = AreaDensity.Info.BaseUnitInfo;
+ Assert.True(baseUnitInfo.BaseUnits.IsSubsetOf(UnitSystem.SI.BaseUnits));
+ }
+
+ [Fact]
+ public virtual void As_UnitSystem_SI_ReturnsQuantityInSIUnits()
+ {
+ var quantity = new AreaDensity(value: 1, unit: AreaDensity.BaseUnit);
+ var expectedValue = quantity.As(AreaDensity.Info.GetDefaultUnit(UnitSystem.SI));
+
+ var convertedValue = quantity.As(UnitSystem.SI);
+
+ Assert.Equal(expectedValue, convertedValue);
+ }
+
+ [Fact]
+ public void As_UnitSystem_ThrowsArgumentNullExceptionIfNull()
+ {
+ var quantity = new AreaDensity(value: 1, unit: AreaDensity.BaseUnit);
+ UnitSystem nullUnitSystem = null!;
+ Assert.Throws(() => quantity.As(nullUnitSystem));
+ }
+
+ [Fact]
+ public void As_UnitSystem_ThrowsArgumentExceptionIfNotSupported()
+ {
+ var quantity = new AreaDensity(value: 1, unit: AreaDensity.BaseUnit);
+ var unsupportedUnitSystem = new UnitSystem(UnsupportedBaseUnits);
+ Assert.Throws(() => quantity.As(unsupportedUnitSystem));
+ }
+
+ [Fact]
+ public virtual void ToUnit_UnitSystem_SI_ReturnsQuantityInSIUnits()
{
var quantity = new AreaDensity(value: 1, unit: AreaDensity.BaseUnit);
- Func AsWithSIUnitSystem = () => quantity.As(UnitSystem.SI);
+ var expectedUnit = AreaDensity.Info.GetDefaultUnit(UnitSystem.SI);
+ var expectedValue = quantity.As(expectedUnit);
- if (SupportsSIUnitSystem)
+ Assert.Multiple(() =>
{
- var value = Convert.ToDouble(AsWithSIUnitSystem());
- Assert.Equal(1, value);
- }
- else
+ AreaDensity quantityToConvert = quantity;
+
+ AreaDensity convertedQuantity = quantityToConvert.ToUnit(UnitSystem.SI);
+
+ Assert.Equal(expectedUnit, convertedQuantity.Unit);
+ Assert.Equal(expectedValue, convertedQuantity.Value);
+ }, () =>
{
- Assert.Throws(AsWithSIUnitSystem);
- }
+ IQuantity quantityToConvert = quantity;
+
+ IQuantity convertedQuantity = quantityToConvert.ToUnit(UnitSystem.SI);
+
+ Assert.Equal(expectedUnit, convertedQuantity.Unit);
+ Assert.Equal(expectedValue, convertedQuantity.Value);
+ }, () =>
+ {
+ IQuantity quantityToConvert = quantity;
+
+ IQuantity convertedQuantity = quantityToConvert.ToUnit(UnitSystem.SI);
+
+ Assert.Equal(expectedUnit, convertedQuantity.Unit);
+ Assert.Equal(expectedValue, convertedQuantity.Value);
+ });
+ }
+
+ [Fact]
+ public void ToUnit_UnitSystem_ThrowsArgumentNullExceptionIfNull()
+ {
+ UnitSystem nullUnitSystem = null!;
+ Assert.Multiple(() =>
+ {
+ var quantity = new AreaDensity(value: 1, unit: AreaDensity.BaseUnit);
+ Assert.Throws(() => quantity.ToUnit(nullUnitSystem));
+ }, () =>
+ {
+ IQuantity quantity = new AreaDensity(value: 1, unit: AreaDensity.BaseUnit);
+ Assert.Throws(() => quantity.ToUnit(nullUnitSystem));
+ }, () =>
+ {
+ IQuantity quantity = new AreaDensity(value: 1, unit: AreaDensity.BaseUnit);
+ Assert.Throws(() => quantity.ToUnit(nullUnitSystem));
+ });
+ }
+
+ [Fact]
+ public void ToUnit_UnitSystem_ThrowsArgumentExceptionIfNotSupported()
+ {
+ var unsupportedUnitSystem = new UnitSystem(UnsupportedBaseUnits);
+ Assert.Multiple(() =>
+ {
+ var quantity = new AreaDensity(value: 1, unit: AreaDensity.BaseUnit);
+ Assert.Throws(() => quantity.ToUnit(unsupportedUnitSystem));
+ }, () =>
+ {
+ IQuantity quantity = new AreaDensity(value: 1, unit: AreaDensity.BaseUnit);
+ Assert.Throws(() => quantity.ToUnit(unsupportedUnitSystem));
+ }, () =>
+ {
+ IQuantity quantity = new AreaDensity(value: 1, unit: AreaDensity.BaseUnit);
+ Assert.Throws(() => quantity.ToUnit(unsupportedUnitSystem));
+ });
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/AreaMomentOfInertiaTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/AreaMomentOfInertiaTestsBase.g.cs
index 38e613c388..9df6f7ac70 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/AreaMomentOfInertiaTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/AreaMomentOfInertiaTestsBase.g.cs
@@ -112,18 +112,18 @@ public void Ctor_NullAsUnitSystem_ThrowsArgumentNullException()
}
[Fact]
- public void Ctor_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
+ public virtual void Ctor_SIUnitSystem_ReturnsQuantityWithSIUnits()
{
- Func TestCode = () => new AreaMomentOfInertia(value: 1, unitSystem: UnitSystem.SI);
- if (SupportsSIUnitSystem)
- {
- var quantity = (AreaMomentOfInertia) TestCode();
- Assert.Equal(1, quantity.Value);
- }
- else
- {
- Assert.Throws(TestCode);
- }
+ var quantity = new AreaMomentOfInertia(value: 1, unitSystem: UnitSystem.SI);
+ Assert.Equal(1, quantity.Value);
+ Assert.True(quantity.QuantityInfo.UnitInfos.First(x => x.Value == quantity.Unit).BaseUnits.IsSubsetOf(UnitSystem.SI.BaseUnits));
+ }
+
+ [Fact]
+ public void Ctor_UnitSystem_ThrowsArgumentExceptionIfNotSupported()
+ {
+ var unsupportedUnitSystem = new UnitSystem(UnsupportedBaseUnits);
+ Assert.Throws(() => new AreaMomentOfInertia(value: 1, unitSystem: unsupportedUnitSystem));
}
[Fact]
@@ -212,20 +212,109 @@ public void As()
}
[Fact]
- public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
+ public virtual void BaseUnit_HasSIBase()
+ {
+ var baseUnitInfo = AreaMomentOfInertia.Info.BaseUnitInfo;
+ Assert.True(baseUnitInfo.BaseUnits.IsSubsetOf(UnitSystem.SI.BaseUnits));
+ }
+
+ [Fact]
+ public virtual void As_UnitSystem_SI_ReturnsQuantityInSIUnits()
+ {
+ var quantity = new AreaMomentOfInertia(value: 1, unit: AreaMomentOfInertia.BaseUnit);
+ var expectedValue = quantity.As(AreaMomentOfInertia.Info.GetDefaultUnit(UnitSystem.SI));
+
+ var convertedValue = quantity.As(UnitSystem.SI);
+
+ Assert.Equal(expectedValue, convertedValue);
+ }
+
+ [Fact]
+ public void As_UnitSystem_ThrowsArgumentNullExceptionIfNull()
+ {
+ var quantity = new AreaMomentOfInertia(value: 1, unit: AreaMomentOfInertia.BaseUnit);
+ UnitSystem nullUnitSystem = null!;
+ Assert.Throws(() => quantity.As(nullUnitSystem));
+ }
+
+ [Fact]
+ public void As_UnitSystem_ThrowsArgumentExceptionIfNotSupported()
+ {
+ var quantity = new AreaMomentOfInertia(value: 1, unit: AreaMomentOfInertia.BaseUnit);
+ var unsupportedUnitSystem = new UnitSystem(UnsupportedBaseUnits);
+ Assert.Throws(() => quantity.As(unsupportedUnitSystem));
+ }
+
+ [Fact]
+ public virtual void ToUnit_UnitSystem_SI_ReturnsQuantityInSIUnits()
{
var quantity = new AreaMomentOfInertia(value: 1, unit: AreaMomentOfInertia.BaseUnit);
- Func AsWithSIUnitSystem = () => quantity.As(UnitSystem.SI);
+ var expectedUnit = AreaMomentOfInertia.Info.GetDefaultUnit(UnitSystem.SI);
+ var expectedValue = quantity.As(expectedUnit);
- if (SupportsSIUnitSystem)
+ Assert.Multiple(() =>
{
- var value = Convert.ToDouble(AsWithSIUnitSystem());
- Assert.Equal(1, value);
- }
- else
+ AreaMomentOfInertia quantityToConvert = quantity;
+
+ AreaMomentOfInertia convertedQuantity = quantityToConvert.ToUnit(UnitSystem.SI);
+
+ Assert.Equal(expectedUnit, convertedQuantity.Unit);
+ Assert.Equal(expectedValue, convertedQuantity.Value);
+ }, () =>
{
- Assert.Throws(AsWithSIUnitSystem);
- }
+ IQuantity quantityToConvert = quantity;
+
+ IQuantity convertedQuantity = quantityToConvert.ToUnit(UnitSystem.SI);
+
+ Assert.Equal(expectedUnit, convertedQuantity.Unit);
+ Assert.Equal(expectedValue, convertedQuantity.Value);
+ }, () =>
+ {
+ IQuantity quantityToConvert = quantity;
+
+ IQuantity convertedQuantity = quantityToConvert.ToUnit(UnitSystem.SI);
+
+ Assert.Equal(expectedUnit, convertedQuantity.Unit);
+ Assert.Equal(expectedValue, convertedQuantity.Value);
+ });
+ }
+
+ [Fact]
+ public void ToUnit_UnitSystem_ThrowsArgumentNullExceptionIfNull()
+ {
+ UnitSystem nullUnitSystem = null!;
+ Assert.Multiple(() =>
+ {
+ var quantity = new AreaMomentOfInertia(value: 1, unit: AreaMomentOfInertia.BaseUnit);
+ Assert.Throws(() => quantity.ToUnit(nullUnitSystem));
+ }, () =>
+ {
+ IQuantity quantity = new AreaMomentOfInertia(value: 1, unit: AreaMomentOfInertia.BaseUnit);
+ Assert.Throws(() => quantity.ToUnit(nullUnitSystem));
+ }, () =>
+ {
+ IQuantity quantity = new AreaMomentOfInertia(value: 1, unit: AreaMomentOfInertia.BaseUnit);
+ Assert.Throws(() => quantity.ToUnit(nullUnitSystem));
+ });
+ }
+
+ [Fact]
+ public void ToUnit_UnitSystem_ThrowsArgumentExceptionIfNotSupported()
+ {
+ var unsupportedUnitSystem = new UnitSystem(UnsupportedBaseUnits);
+ Assert.Multiple(() =>
+ {
+ var quantity = new AreaMomentOfInertia(value: 1, unit: AreaMomentOfInertia.BaseUnit);
+ Assert.Throws(() => quantity.ToUnit(unsupportedUnitSystem));
+ }, () =>
+ {
+ IQuantity quantity = new AreaMomentOfInertia(value: 1, unit: AreaMomentOfInertia.BaseUnit);
+ Assert.Throws(() => quantity.ToUnit(unsupportedUnitSystem));
+ }, () =>
+ {
+ IQuantity quantity = new AreaMomentOfInertia(value: 1, unit: AreaMomentOfInertia.BaseUnit);
+ Assert.Throws(() => quantity.ToUnit(unsupportedUnitSystem));
+ });
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/AreaTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/AreaTestsBase.g.cs
index 64659ab0b7..b89886adb6 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/AreaTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/AreaTestsBase.g.cs
@@ -144,18 +144,18 @@ public void Ctor_NullAsUnitSystem_ThrowsArgumentNullException()
}
[Fact]
- public void Ctor_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
+ public virtual void Ctor_SIUnitSystem_ReturnsQuantityWithSIUnits()
{
- Func TestCode = () => new Area(value: 1, unitSystem: UnitSystem.SI);
- if (SupportsSIUnitSystem)
- {
- var quantity = (Area) TestCode();
- Assert.Equal(1, quantity.Value);
- }
- else
- {
- Assert.Throws(TestCode);
- }
+ var quantity = new Area(value: 1, unitSystem: UnitSystem.SI);
+ Assert.Equal(1, quantity.Value);
+ Assert.True(quantity.QuantityInfo.UnitInfos.First(x => x.Value == quantity.Unit).BaseUnits.IsSubsetOf(UnitSystem.SI.BaseUnits));
+ }
+
+ [Fact]
+ public void Ctor_UnitSystem_ThrowsArgumentExceptionIfNotSupported()
+ {
+ var unsupportedUnitSystem = new UnitSystem(UnsupportedBaseUnits);
+ Assert.Throws(() => new Area(value: 1, unitSystem: unsupportedUnitSystem));
}
[Fact]
@@ -292,20 +292,109 @@ public void As()
}
[Fact]
- public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
+ public virtual void BaseUnit_HasSIBase()
+ {
+ var baseUnitInfo = Area.Info.BaseUnitInfo;
+ Assert.True(baseUnitInfo.BaseUnits.IsSubsetOf(UnitSystem.SI.BaseUnits));
+ }
+
+ [Fact]
+ public virtual void As_UnitSystem_SI_ReturnsQuantityInSIUnits()
+ {
+ var quantity = new Area(value: 1, unit: Area.BaseUnit);
+ var expectedValue = quantity.As(Area.Info.GetDefaultUnit(UnitSystem.SI));
+
+ var convertedValue = quantity.As(UnitSystem.SI);
+
+ Assert.Equal(expectedValue, convertedValue);
+ }
+
+ [Fact]
+ public void As_UnitSystem_ThrowsArgumentNullExceptionIfNull()
+ {
+ var quantity = new Area(value: 1, unit: Area.BaseUnit);
+ UnitSystem nullUnitSystem = null!;
+ Assert.Throws(() => quantity.As(nullUnitSystem));
+ }
+
+ [Fact]
+ public void As_UnitSystem_ThrowsArgumentExceptionIfNotSupported()
+ {
+ var quantity = new Area(value: 1, unit: Area.BaseUnit);
+ var unsupportedUnitSystem = new UnitSystem(UnsupportedBaseUnits);
+ Assert.Throws(() => quantity.As(unsupportedUnitSystem));
+ }
+
+ [Fact]
+ public virtual void ToUnit_UnitSystem_SI_ReturnsQuantityInSIUnits()
{
var quantity = new Area(value: 1, unit: Area.BaseUnit);
- Func AsWithSIUnitSystem = () => quantity.As(UnitSystem.SI);
+ var expectedUnit = Area.Info.GetDefaultUnit(UnitSystem.SI);
+ var expectedValue = quantity.As(expectedUnit);
- if (SupportsSIUnitSystem)
+ Assert.Multiple(() =>
{
- var value = Convert.ToDouble(AsWithSIUnitSystem());
- Assert.Equal(1, value);
- }
- else
+ Area quantityToConvert = quantity;
+
+ Area convertedQuantity = quantityToConvert.ToUnit(UnitSystem.SI);
+
+ Assert.Equal(expectedUnit, convertedQuantity.Unit);
+ Assert.Equal(expectedValue, convertedQuantity.Value);
+ }, () =>
{
- Assert.Throws(AsWithSIUnitSystem);
- }
+ IQuantity quantityToConvert = quantity;
+
+ IQuantity convertedQuantity = quantityToConvert.ToUnit(UnitSystem.SI);
+
+ Assert.Equal(expectedUnit, convertedQuantity.Unit);
+ Assert.Equal(expectedValue, convertedQuantity.Value);
+ }, () =>
+ {
+ IQuantity quantityToConvert = quantity;
+
+ IQuantity convertedQuantity = quantityToConvert.ToUnit(UnitSystem.SI);
+
+ Assert.Equal(expectedUnit, convertedQuantity.Unit);
+ Assert.Equal(expectedValue, convertedQuantity.Value);
+ });
+ }
+
+ [Fact]
+ public void ToUnit_UnitSystem_ThrowsArgumentNullExceptionIfNull()
+ {
+ UnitSystem nullUnitSystem = null!;
+ Assert.Multiple(() =>
+ {
+ var quantity = new Area(value: 1, unit: Area.BaseUnit);
+ Assert.Throws(() => quantity.ToUnit(nullUnitSystem));
+ }, () =>
+ {
+ IQuantity quantity = new Area(value: 1, unit: Area.BaseUnit);
+ Assert.Throws(() => quantity.ToUnit(nullUnitSystem));
+ }, () =>
+ {
+ IQuantity quantity = new Area(value: 1, unit: Area.BaseUnit);
+ Assert.Throws(() => quantity.ToUnit(nullUnitSystem));
+ });
+ }
+
+ [Fact]
+ public void ToUnit_UnitSystem_ThrowsArgumentExceptionIfNotSupported()
+ {
+ var unsupportedUnitSystem = new UnitSystem(UnsupportedBaseUnits);
+ Assert.Multiple(() =>
+ {
+ var quantity = new Area(value: 1, unit: Area.BaseUnit);
+ Assert.Throws(() => quantity.ToUnit(unsupportedUnitSystem));
+ }, () =>
+ {
+ IQuantity quantity = new Area(value: 1, unit: Area.BaseUnit);
+ Assert.Throws(() => quantity.ToUnit(unsupportedUnitSystem));
+ }, () =>
+ {
+ IQuantity quantity = new Area(value: 1, unit: Area.BaseUnit);
+ Assert.Throws(() => quantity.ToUnit(unsupportedUnitSystem));
+ });
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/BitRateTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/BitRateTestsBase.g.cs
index 39871640de..576192179b 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/BitRateTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/BitRateTestsBase.g.cs
@@ -192,18 +192,18 @@ public void Ctor_NullAsUnitSystem_ThrowsArgumentNullException()
}
[Fact]
- public void Ctor_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
+ public virtual void Ctor_SIUnitSystem_ReturnsQuantityWithSIUnits()
{
- Func TestCode = () => new BitRate(value: 1, unitSystem: UnitSystem.SI);
- if (SupportsSIUnitSystem)
- {
- var quantity = (BitRate) TestCode();
- Assert.Equal(1, quantity.Value);
- }
- else
- {
- Assert.Throws(TestCode);
- }
+ var quantity = new BitRate(value: 1, unitSystem: UnitSystem.SI);
+ Assert.Equal(1, quantity.Value);
+ Assert.True(quantity.QuantityInfo.UnitInfos.First(x => x.Value == quantity.Unit).BaseUnits.IsSubsetOf(UnitSystem.SI.BaseUnits));
+ }
+
+ [Fact]
+ public void Ctor_UnitSystem_ThrowsArgumentExceptionIfNotSupported()
+ {
+ var unsupportedUnitSystem = new UnitSystem(UnsupportedBaseUnits);
+ Assert.Throws(() => new BitRate(value: 1, unitSystem: unsupportedUnitSystem));
}
[Fact]
@@ -412,20 +412,109 @@ public void As()
}
[Fact]
- public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
+ public virtual void BaseUnit_HasSIBase()
+ {
+ var baseUnitInfo = BitRate.Info.BaseUnitInfo;
+ Assert.True(baseUnitInfo.BaseUnits.IsSubsetOf(UnitSystem.SI.BaseUnits));
+ }
+
+ [Fact]
+ public virtual void As_UnitSystem_SI_ReturnsQuantityInSIUnits()
+ {
+ var quantity = new BitRate(value: 1, unit: BitRate.BaseUnit);
+ var expectedValue = quantity.As(BitRate.Info.GetDefaultUnit(UnitSystem.SI));
+
+ var convertedValue = quantity.As(UnitSystem.SI);
+
+ Assert.Equal(expectedValue, convertedValue);
+ }
+
+ [Fact]
+ public void As_UnitSystem_ThrowsArgumentNullExceptionIfNull()
+ {
+ var quantity = new BitRate(value: 1, unit: BitRate.BaseUnit);
+ UnitSystem nullUnitSystem = null!;
+ Assert.Throws(() => quantity.As(nullUnitSystem));
+ }
+
+ [Fact]
+ public void As_UnitSystem_ThrowsArgumentExceptionIfNotSupported()
+ {
+ var quantity = new BitRate(value: 1, unit: BitRate.BaseUnit);
+ var unsupportedUnitSystem = new UnitSystem(UnsupportedBaseUnits);
+ Assert.Throws(() => quantity.As(unsupportedUnitSystem));
+ }
+
+ [Fact]
+ public virtual void ToUnit_UnitSystem_SI_ReturnsQuantityInSIUnits()
{
var quantity = new BitRate(value: 1, unit: BitRate.BaseUnit);
- Func AsWithSIUnitSystem = () => quantity.As(UnitSystem.SI);
+ var expectedUnit = BitRate.Info.GetDefaultUnit(UnitSystem.SI);
+ var expectedValue = quantity.As(expectedUnit);
- if (SupportsSIUnitSystem)
+ Assert.Multiple(() =>
{
- var value = Convert.ToDouble(AsWithSIUnitSystem());
- Assert.Equal(1, value);
- }
- else
+ BitRate quantityToConvert = quantity;
+
+ BitRate convertedQuantity = quantityToConvert.ToUnit(UnitSystem.SI);
+
+ Assert.Equal(expectedUnit, convertedQuantity.Unit);
+ Assert.Equal(expectedValue, convertedQuantity.Value);
+ }, () =>
{
- Assert.Throws(AsWithSIUnitSystem);
- }
+ IQuantity quantityToConvert = quantity;
+
+ IQuantity convertedQuantity = quantityToConvert.ToUnit(UnitSystem.SI);
+
+ Assert.Equal(expectedUnit, convertedQuantity.Unit);
+ Assert.Equal(expectedValue, convertedQuantity.Value);
+ }, () =>
+ {
+ IQuantity quantityToConvert = quantity;
+
+ IQuantity convertedQuantity = quantityToConvert.ToUnit(UnitSystem.SI);
+
+ Assert.Equal(expectedUnit, convertedQuantity.Unit);
+ Assert.Equal(expectedValue, convertedQuantity.Value);
+ });
+ }
+
+ [Fact]
+ public void ToUnit_UnitSystem_ThrowsArgumentNullExceptionIfNull()
+ {
+ UnitSystem nullUnitSystem = null!;
+ Assert.Multiple(() =>
+ {
+ var quantity = new BitRate(value: 1, unit: BitRate.BaseUnit);
+ Assert.Throws(() => quantity.ToUnit(nullUnitSystem));
+ }, () =>
+ {
+ IQuantity quantity = new BitRate(value: 1, unit: BitRate.BaseUnit);
+ Assert.Throws(() => quantity.ToUnit(nullUnitSystem));
+ }, () =>
+ {
+ IQuantity quantity = new BitRate(value: 1, unit: BitRate.BaseUnit);
+ Assert.Throws(() => quantity.ToUnit(nullUnitSystem));
+ });
+ }
+
+ [Fact]
+ public void ToUnit_UnitSystem_ThrowsArgumentExceptionIfNotSupported()
+ {
+ var unsupportedUnitSystem = new UnitSystem(UnsupportedBaseUnits);
+ Assert.Multiple(() =>
+ {
+ var quantity = new BitRate(value: 1, unit: BitRate.BaseUnit);
+ Assert.Throws(() => quantity.ToUnit(unsupportedUnitSystem));
+ }, () =>
+ {
+ IQuantity quantity = new BitRate(value: 1, unit: BitRate.BaseUnit);
+ Assert.Throws(() => quantity.ToUnit(unsupportedUnitSystem));
+ }, () =>
+ {
+ IQuantity quantity = new BitRate(value: 1, unit: BitRate.BaseUnit);
+ Assert.Throws(() => quantity.ToUnit(unsupportedUnitSystem));
+ });
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/BrakeSpecificFuelConsumptionTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/BrakeSpecificFuelConsumptionTestsBase.g.cs
index 6043b6179f..13df892898 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/BrakeSpecificFuelConsumptionTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/BrakeSpecificFuelConsumptionTestsBase.g.cs
@@ -100,18 +100,18 @@ public void Ctor_NullAsUnitSystem_ThrowsArgumentNullException()
}
[Fact]
- public void Ctor_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
+ public virtual void Ctor_SIUnitSystem_ReturnsQuantityWithSIUnits()
{
- Func TestCode = () => new BrakeSpecificFuelConsumption(value: 1, unitSystem: UnitSystem.SI);
- if (SupportsSIUnitSystem)
- {
- var quantity = (BrakeSpecificFuelConsumption) TestCode();
- Assert.Equal(1, quantity.Value);
- }
- else
- {
- Assert.Throws(TestCode);
- }
+ var quantity = new BrakeSpecificFuelConsumption(value: 1, unitSystem: UnitSystem.SI);
+ Assert.Equal(1, quantity.Value);
+ Assert.True(quantity.QuantityInfo.UnitInfos.First(x => x.Value == quantity.Unit).BaseUnits.IsSubsetOf(UnitSystem.SI.BaseUnits));
+ }
+
+ [Fact]
+ public void Ctor_UnitSystem_ThrowsArgumentExceptionIfNotSupported()
+ {
+ var unsupportedUnitSystem = new UnitSystem(UnsupportedBaseUnits);
+ Assert.Throws(() => new BrakeSpecificFuelConsumption(value: 1, unitSystem: unsupportedUnitSystem));
}
[Fact]
@@ -182,20 +182,109 @@ public void As()
}
[Fact]
- public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
+ public virtual void BaseUnit_HasSIBase()
+ {
+ var baseUnitInfo = BrakeSpecificFuelConsumption.Info.BaseUnitInfo;
+ Assert.True(baseUnitInfo.BaseUnits.IsSubsetOf(UnitSystem.SI.BaseUnits));
+ }
+
+ [Fact]
+ public virtual void As_UnitSystem_SI_ReturnsQuantityInSIUnits()
+ {
+ var quantity = new BrakeSpecificFuelConsumption(value: 1, unit: BrakeSpecificFuelConsumption.BaseUnit);
+ var expectedValue = quantity.As(BrakeSpecificFuelConsumption.Info.GetDefaultUnit(UnitSystem.SI));
+
+ var convertedValue = quantity.As(UnitSystem.SI);
+
+ Assert.Equal(expectedValue, convertedValue);
+ }
+
+ [Fact]
+ public void As_UnitSystem_ThrowsArgumentNullExceptionIfNull()
+ {
+ var quantity = new BrakeSpecificFuelConsumption(value: 1, unit: BrakeSpecificFuelConsumption.BaseUnit);
+ UnitSystem nullUnitSystem = null!;
+ Assert.Throws(() => quantity.As(nullUnitSystem));
+ }
+
+ [Fact]
+ public void As_UnitSystem_ThrowsArgumentExceptionIfNotSupported()
+ {
+ var quantity = new BrakeSpecificFuelConsumption(value: 1, unit: BrakeSpecificFuelConsumption.BaseUnit);
+ var unsupportedUnitSystem = new UnitSystem(UnsupportedBaseUnits);
+ Assert.Throws(() => quantity.As(unsupportedUnitSystem));
+ }
+
+ [Fact]
+ public virtual void ToUnit_UnitSystem_SI_ReturnsQuantityInSIUnits()
{
var quantity = new BrakeSpecificFuelConsumption(value: 1, unit: BrakeSpecificFuelConsumption.BaseUnit);
- Func AsWithSIUnitSystem = () => quantity.As(UnitSystem.SI);
+ var expectedUnit = BrakeSpecificFuelConsumption.Info.GetDefaultUnit(UnitSystem.SI);
+ var expectedValue = quantity.As(expectedUnit);
- if (SupportsSIUnitSystem)
+ Assert.Multiple(() =>
{
- var value = Convert.ToDouble(AsWithSIUnitSystem());
- Assert.Equal(1, value);
- }
- else
+ BrakeSpecificFuelConsumption quantityToConvert = quantity;
+
+ BrakeSpecificFuelConsumption convertedQuantity = quantityToConvert.ToUnit(UnitSystem.SI);
+
+ Assert.Equal(expectedUnit, convertedQuantity.Unit);
+ Assert.Equal(expectedValue, convertedQuantity.Value);
+ }, () =>
{
- Assert.Throws(AsWithSIUnitSystem);
- }
+ IQuantity quantityToConvert = quantity;
+
+ IQuantity convertedQuantity = quantityToConvert.ToUnit(UnitSystem.SI);
+
+ Assert.Equal(expectedUnit, convertedQuantity.Unit);
+ Assert.Equal(expectedValue, convertedQuantity.Value);
+ }, () =>
+ {
+ IQuantity quantityToConvert = quantity;
+
+ IQuantity convertedQuantity = quantityToConvert.ToUnit(UnitSystem.SI);
+
+ Assert.Equal(expectedUnit, convertedQuantity.Unit);
+ Assert.Equal(expectedValue, convertedQuantity.Value);
+ });
+ }
+
+ [Fact]
+ public void ToUnit_UnitSystem_ThrowsArgumentNullExceptionIfNull()
+ {
+ UnitSystem nullUnitSystem = null!;
+ Assert.Multiple(() =>
+ {
+ var quantity = new BrakeSpecificFuelConsumption(value: 1, unit: BrakeSpecificFuelConsumption.BaseUnit);
+ Assert.Throws(() => quantity.ToUnit(nullUnitSystem));
+ }, () =>
+ {
+ IQuantity quantity = new BrakeSpecificFuelConsumption(value: 1, unit: BrakeSpecificFuelConsumption.BaseUnit);
+ Assert.Throws(() => quantity.ToUnit(nullUnitSystem));
+ }, () =>
+ {
+ IQuantity quantity = new BrakeSpecificFuelConsumption(value: 1, unit: BrakeSpecificFuelConsumption.BaseUnit);
+ Assert.Throws(() => quantity.ToUnit(nullUnitSystem));
+ });
+ }
+
+ [Fact]
+ public void ToUnit_UnitSystem_ThrowsArgumentExceptionIfNotSupported()
+ {
+ var unsupportedUnitSystem = new UnitSystem(UnsupportedBaseUnits);
+ Assert.Multiple(() =>
+ {
+ var quantity = new BrakeSpecificFuelConsumption(value: 1, unit: BrakeSpecificFuelConsumption.BaseUnit);
+ Assert.Throws(() => quantity.ToUnit(unsupportedUnitSystem));
+ }, () =>
+ {
+ IQuantity quantity = new BrakeSpecificFuelConsumption(value: 1, unit: BrakeSpecificFuelConsumption.BaseUnit);
+ Assert.Throws(() => quantity.ToUnit(unsupportedUnitSystem));
+ }, () =>
+ {
+ IQuantity quantity = new BrakeSpecificFuelConsumption(value: 1, unit: BrakeSpecificFuelConsumption.BaseUnit);
+ Assert.Throws(() => quantity.ToUnit(unsupportedUnitSystem));
+ });
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/CoefficientOfThermalExpansionTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/CoefficientOfThermalExpansionTestsBase.g.cs
index 2fc84d591a..79bc06a406 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/CoefficientOfThermalExpansionTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/CoefficientOfThermalExpansionTestsBase.g.cs
@@ -112,18 +112,18 @@ public void Ctor_NullAsUnitSystem_ThrowsArgumentNullException()
}
[Fact]
- public void Ctor_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
+ public virtual void Ctor_SIUnitSystem_ReturnsQuantityWithSIUnits()
{
- Func TestCode = () => new CoefficientOfThermalExpansion(value: 1, unitSystem: UnitSystem.SI);
- if (SupportsSIUnitSystem)
- {
- var quantity = (CoefficientOfThermalExpansion) TestCode();
- Assert.Equal(1, quantity.Value);
- }
- else
- {
- Assert.Throws(TestCode);
- }
+ var quantity = new CoefficientOfThermalExpansion(value: 1, unitSystem: UnitSystem.SI);
+ Assert.Equal(1, quantity.Value);
+ Assert.True(quantity.QuantityInfo.UnitInfos.First(x => x.Value == quantity.Unit).BaseUnits.IsSubsetOf(UnitSystem.SI.BaseUnits));
+ }
+
+ [Fact]
+ public void Ctor_UnitSystem_ThrowsArgumentExceptionIfNotSupported()
+ {
+ var unsupportedUnitSystem = new UnitSystem(UnsupportedBaseUnits);
+ Assert.Throws(() => new CoefficientOfThermalExpansion(value: 1, unitSystem: unsupportedUnitSystem));
}
[Fact]
@@ -212,20 +212,109 @@ public void As()
}
[Fact]
- public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
+ public virtual void BaseUnit_HasSIBase()
+ {
+ var baseUnitInfo = CoefficientOfThermalExpansion.Info.BaseUnitInfo;
+ Assert.True(baseUnitInfo.BaseUnits.IsSubsetOf(UnitSystem.SI.BaseUnits));
+ }
+
+ [Fact]
+ public virtual void As_UnitSystem_SI_ReturnsQuantityInSIUnits()
+ {
+ var quantity = new CoefficientOfThermalExpansion(value: 1, unit: CoefficientOfThermalExpansion.BaseUnit);
+ var expectedValue = quantity.As(CoefficientOfThermalExpansion.Info.GetDefaultUnit(UnitSystem.SI));
+
+ var convertedValue = quantity.As(UnitSystem.SI);
+
+ Assert.Equal(expectedValue, convertedValue);
+ }
+
+ [Fact]
+ public void As_UnitSystem_ThrowsArgumentNullExceptionIfNull()
+ {
+ var quantity = new CoefficientOfThermalExpansion(value: 1, unit: CoefficientOfThermalExpansion.BaseUnit);
+ UnitSystem nullUnitSystem = null!;
+ Assert.Throws(() => quantity.As(nullUnitSystem));
+ }
+
+ [Fact]
+ public void As_UnitSystem_ThrowsArgumentExceptionIfNotSupported()
+ {
+ var quantity = new CoefficientOfThermalExpansion(value: 1, unit: CoefficientOfThermalExpansion.BaseUnit);
+ var unsupportedUnitSystem = new UnitSystem(UnsupportedBaseUnits);
+ Assert.Throws(() => quantity.As(unsupportedUnitSystem));
+ }
+
+ [Fact]
+ public virtual void ToUnit_UnitSystem_SI_ReturnsQuantityInSIUnits()
{
var quantity = new CoefficientOfThermalExpansion(value: 1, unit: CoefficientOfThermalExpansion.BaseUnit);
- Func AsWithSIUnitSystem = () => quantity.As(UnitSystem.SI);
+ var expectedUnit = CoefficientOfThermalExpansion.Info.GetDefaultUnit(UnitSystem.SI);
+ var expectedValue = quantity.As(expectedUnit);
- if (SupportsSIUnitSystem)
+ Assert.Multiple(() =>
{
- var value = Convert.ToDouble(AsWithSIUnitSystem());
- Assert.Equal(1, value);
- }
- else
+ CoefficientOfThermalExpansion quantityToConvert = quantity;
+
+ CoefficientOfThermalExpansion convertedQuantity = quantityToConvert.ToUnit(UnitSystem.SI);
+
+ Assert.Equal(expectedUnit, convertedQuantity.Unit);
+ Assert.Equal(expectedValue, convertedQuantity.Value);
+ }, () =>
{
- Assert.Throws(AsWithSIUnitSystem);
- }
+ IQuantity quantityToConvert = quantity;
+
+ IQuantity convertedQuantity = quantityToConvert.ToUnit(UnitSystem.SI);
+
+ Assert.Equal(expectedUnit, convertedQuantity.Unit);
+ Assert.Equal(expectedValue, convertedQuantity.Value);
+ }, () =>
+ {
+ IQuantity quantityToConvert = quantity;
+
+ IQuantity convertedQuantity = quantityToConvert.ToUnit(UnitSystem.SI);
+
+ Assert.Equal(expectedUnit, convertedQuantity.Unit);
+ Assert.Equal(expectedValue, convertedQuantity.Value);
+ });
+ }
+
+ [Fact]
+ public void ToUnit_UnitSystem_ThrowsArgumentNullExceptionIfNull()
+ {
+ UnitSystem nullUnitSystem = null!;
+ Assert.Multiple(() =>
+ {
+ var quantity = new CoefficientOfThermalExpansion(value: 1, unit: CoefficientOfThermalExpansion.BaseUnit);
+ Assert.Throws(() => quantity.ToUnit(nullUnitSystem));
+ }, () =>
+ {
+ IQuantity quantity = new CoefficientOfThermalExpansion(value: 1, unit: CoefficientOfThermalExpansion.BaseUnit);
+ Assert.Throws(() => quantity.ToUnit(nullUnitSystem));
+ }, () =>
+ {
+ IQuantity quantity = new CoefficientOfThermalExpansion(value: 1, unit: CoefficientOfThermalExpansion.BaseUnit);
+ Assert.Throws(() => quantity.ToUnit(nullUnitSystem));
+ });
+ }
+
+ [Fact]
+ public void ToUnit_UnitSystem_ThrowsArgumentExceptionIfNotSupported()
+ {
+ var unsupportedUnitSystem = new UnitSystem(UnsupportedBaseUnits);
+ Assert.Multiple(() =>
+ {
+ var quantity = new CoefficientOfThermalExpansion(value: 1, unit: CoefficientOfThermalExpansion.BaseUnit);
+ Assert.Throws(() => quantity.ToUnit(unsupportedUnitSystem));
+ }, () =>
+ {
+ IQuantity quantity = new CoefficientOfThermalExpansion(value: 1, unit: CoefficientOfThermalExpansion.BaseUnit);
+ Assert.Throws(() => quantity.ToUnit(unsupportedUnitSystem));
+ }, () =>
+ {
+ IQuantity quantity = new CoefficientOfThermalExpansion(value: 1, unit: CoefficientOfThermalExpansion.BaseUnit);
+ Assert.Throws(() => quantity.ToUnit(unsupportedUnitSystem));
+ });
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/CompressibilityTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/CompressibilityTestsBase.g.cs
index 741d895284..0f79896816 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/CompressibilityTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/CompressibilityTestsBase.g.cs
@@ -116,18 +116,18 @@ public void Ctor_NullAsUnitSystem_ThrowsArgumentNullException()
}
[Fact]
- public void Ctor_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
+ public virtual void Ctor_SIUnitSystem_ReturnsQuantityWithSIUnits()
{
- Func TestCode = () => new Compressibility(value: 1, unitSystem: UnitSystem.SI);
- if (SupportsSIUnitSystem)
- {
- var quantity = (Compressibility) TestCode();
- Assert.Equal(1, quantity.Value);
- }
- else
- {
- Assert.Throws(TestCode);
- }
+ var quantity = new Compressibility(value: 1, unitSystem: UnitSystem.SI);
+ Assert.Equal(1, quantity.Value);
+ Assert.True(quantity.QuantityInfo.UnitInfos.First(x => x.Value == quantity.Unit).BaseUnits.IsSubsetOf(UnitSystem.SI.BaseUnits));
+ }
+
+ [Fact]
+ public void Ctor_UnitSystem_ThrowsArgumentExceptionIfNotSupported()
+ {
+ var unsupportedUnitSystem = new UnitSystem(UnsupportedBaseUnits);
+ Assert.Throws(() => new Compressibility(value: 1, unitSystem: unsupportedUnitSystem));
}
[Fact]
@@ -222,20 +222,109 @@ public void As()
}
[Fact]
- public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
+ public virtual void BaseUnit_HasSIBase()
+ {
+ var baseUnitInfo = Compressibility.Info.BaseUnitInfo;
+ Assert.True(baseUnitInfo.BaseUnits.IsSubsetOf(UnitSystem.SI.BaseUnits));
+ }
+
+ [Fact]
+ public virtual void As_UnitSystem_SI_ReturnsQuantityInSIUnits()
+ {
+ var quantity = new Compressibility(value: 1, unit: Compressibility.BaseUnit);
+ var expectedValue = quantity.As(Compressibility.Info.GetDefaultUnit(UnitSystem.SI));
+
+ var convertedValue = quantity.As(UnitSystem.SI);
+
+ Assert.Equal(expectedValue, convertedValue);
+ }
+
+ [Fact]
+ public void As_UnitSystem_ThrowsArgumentNullExceptionIfNull()
+ {
+ var quantity = new Compressibility(value: 1, unit: Compressibility.BaseUnit);
+ UnitSystem nullUnitSystem = null!;
+ Assert.Throws(() => quantity.As(nullUnitSystem));
+ }
+
+ [Fact]
+ public void As_UnitSystem_ThrowsArgumentExceptionIfNotSupported()
+ {
+ var quantity = new Compressibility(value: 1, unit: Compressibility.BaseUnit);
+ var unsupportedUnitSystem = new UnitSystem(UnsupportedBaseUnits);
+ Assert.Throws(() => quantity.As(unsupportedUnitSystem));
+ }
+
+ [Fact]
+ public virtual void ToUnit_UnitSystem_SI_ReturnsQuantityInSIUnits()
{
var quantity = new Compressibility(value: 1, unit: Compressibility.BaseUnit);
- Func AsWithSIUnitSystem = () => quantity.As(UnitSystem.SI);
+ var expectedUnit = Compressibility.Info.GetDefaultUnit(UnitSystem.SI);
+ var expectedValue = quantity.As(expectedUnit);
- if (SupportsSIUnitSystem)
+ Assert.Multiple(() =>
{
- var value = Convert.ToDouble(AsWithSIUnitSystem());
- Assert.Equal(1, value);
- }
- else
+ Compressibility quantityToConvert = quantity;
+
+ Compressibility convertedQuantity = quantityToConvert.ToUnit(UnitSystem.SI);
+
+ Assert.Equal(expectedUnit, convertedQuantity.Unit);
+ Assert.Equal(expectedValue, convertedQuantity.Value);
+ }, () =>
{
- Assert.Throws(AsWithSIUnitSystem);
- }
+ IQuantity quantityToConvert = quantity;
+
+ IQuantity convertedQuantity = quantityToConvert.ToUnit(UnitSystem.SI);
+
+ Assert.Equal(expectedUnit, convertedQuantity.Unit);
+ Assert.Equal(expectedValue, convertedQuantity.Value);
+ }, () =>
+ {
+ IQuantity quantityToConvert = quantity;
+
+ IQuantity convertedQuantity = quantityToConvert.ToUnit(UnitSystem.SI);
+
+ Assert.Equal(expectedUnit, convertedQuantity.Unit);
+ Assert.Equal(expectedValue, convertedQuantity.Value);
+ });
+ }
+
+ [Fact]
+ public void ToUnit_UnitSystem_ThrowsArgumentNullExceptionIfNull()
+ {
+ UnitSystem nullUnitSystem = null!;
+ Assert.Multiple(() =>
+ {
+ var quantity = new Compressibility(value: 1, unit: Compressibility.BaseUnit);
+ Assert.Throws(() => quantity.ToUnit(nullUnitSystem));
+ }, () =>
+ {
+ IQuantity quantity = new Compressibility(value: 1, unit: Compressibility.BaseUnit);
+ Assert.Throws(() => quantity.ToUnit(nullUnitSystem));
+ }, () =>
+ {
+ IQuantity quantity = new Compressibility(value: 1, unit: Compressibility.BaseUnit);
+ Assert.Throws(() => quantity.ToUnit(nullUnitSystem));
+ });
+ }
+
+ [Fact]
+ public void ToUnit_UnitSystem_ThrowsArgumentExceptionIfNotSupported()
+ {
+ var unsupportedUnitSystem = new UnitSystem(UnsupportedBaseUnits);
+ Assert.Multiple(() =>
+ {
+ var quantity = new Compressibility(value: 1, unit: Compressibility.BaseUnit);
+ Assert.Throws(() => quantity.ToUnit(unsupportedUnitSystem));
+ }, () =>
+ {
+ IQuantity quantity = new Compressibility(value: 1, unit: Compressibility.BaseUnit);
+ Assert.Throws(() => quantity.ToUnit(unsupportedUnitSystem));
+ }, () =>
+ {
+ IQuantity quantity = new Compressibility(value: 1, unit: Compressibility.BaseUnit);
+ Assert.Throws(() => quantity.ToUnit(unsupportedUnitSystem));
+ });
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/DensityTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/DensityTestsBase.g.cs
index 71a688e659..2693b0e659 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/DensityTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/DensityTestsBase.g.cs
@@ -312,18 +312,18 @@ public void Ctor_NullAsUnitSystem_ThrowsArgumentNullException()
}
[Fact]
- public void Ctor_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
+ public virtual void Ctor_SIUnitSystem_ReturnsQuantityWithSIUnits()
{
- Func TestCode = () => new Density(value: 1, unitSystem: UnitSystem.SI);
- if (SupportsSIUnitSystem)
- {
- var quantity = (Density) TestCode();
- Assert.Equal(1, quantity.Value);
- }
- else
- {
- Assert.Throws(TestCode);
- }
+ var quantity = new Density(value: 1, unitSystem: UnitSystem.SI);
+ Assert.Equal(1, quantity.Value);
+ Assert.True(quantity.QuantityInfo.UnitInfos.First(x => x.Value == quantity.Unit).BaseUnits.IsSubsetOf(UnitSystem.SI.BaseUnits));
+ }
+
+ [Fact]
+ public void Ctor_UnitSystem_ThrowsArgumentExceptionIfNotSupported()
+ {
+ var unsupportedUnitSystem = new UnitSystem(UnsupportedBaseUnits);
+ Assert.Throws(() => new Density(value: 1, unitSystem: unsupportedUnitSystem));
}
[Fact]
@@ -712,20 +712,109 @@ public void As()
}
[Fact]
- public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
+ public virtual void BaseUnit_HasSIBase()
+ {
+ var baseUnitInfo = Density.Info.BaseUnitInfo;
+ Assert.True(baseUnitInfo.BaseUnits.IsSubsetOf(UnitSystem.SI.BaseUnits));
+ }
+
+ [Fact]
+ public virtual void As_UnitSystem_SI_ReturnsQuantityInSIUnits()
+ {
+ var quantity = new Density(value: 1, unit: Density.BaseUnit);
+ var expectedValue = quantity.As(Density.Info.GetDefaultUnit(UnitSystem.SI));
+
+ var convertedValue = quantity.As(UnitSystem.SI);
+
+ Assert.Equal(expectedValue, convertedValue);
+ }
+
+ [Fact]
+ public void As_UnitSystem_ThrowsArgumentNullExceptionIfNull()
+ {
+ var quantity = new Density(value: 1, unit: Density.BaseUnit);
+ UnitSystem nullUnitSystem = null!;
+ Assert.Throws(() => quantity.As(nullUnitSystem));
+ }
+
+ [Fact]
+ public void As_UnitSystem_ThrowsArgumentExceptionIfNotSupported()
+ {
+ var quantity = new Density(value: 1, unit: Density.BaseUnit);
+ var unsupportedUnitSystem = new UnitSystem(UnsupportedBaseUnits);
+ Assert.Throws(() => quantity.As(unsupportedUnitSystem));
+ }
+
+ [Fact]
+ public virtual void ToUnit_UnitSystem_SI_ReturnsQuantityInSIUnits()
{
var quantity = new Density(value: 1, unit: Density.BaseUnit);
- Func AsWithSIUnitSystem = () => quantity.As(UnitSystem.SI);
+ var expectedUnit = Density.Info.GetDefaultUnit(UnitSystem.SI);
+ var expectedValue = quantity.As(expectedUnit);
- if (SupportsSIUnitSystem)
+ Assert.Multiple(() =>
{
- var value = Convert.ToDouble(AsWithSIUnitSystem());
- Assert.Equal(1, value);
- }
- else
+ Density quantityToConvert = quantity;
+
+ Density convertedQuantity = quantityToConvert.ToUnit(UnitSystem.SI);
+
+ Assert.Equal(expectedUnit, convertedQuantity.Unit);
+ Assert.Equal(expectedValue, convertedQuantity.Value);
+ }, () =>
{
- Assert.Throws(AsWithSIUnitSystem);
- }
+ IQuantity quantityToConvert = quantity;
+
+ IQuantity convertedQuantity = quantityToConvert.ToUnit(UnitSystem.SI);
+
+ Assert.Equal(expectedUnit, convertedQuantity.Unit);
+ Assert.Equal(expectedValue, convertedQuantity.Value);
+ }, () =>
+ {
+ IQuantity quantityToConvert = quantity;
+
+ IQuantity convertedQuantity = quantityToConvert.ToUnit(UnitSystem.SI);
+
+ Assert.Equal(expectedUnit, convertedQuantity.Unit);
+ Assert.Equal(expectedValue, convertedQuantity.Value);
+ });
+ }
+
+ [Fact]
+ public void ToUnit_UnitSystem_ThrowsArgumentNullExceptionIfNull()
+ {
+ UnitSystem nullUnitSystem = null!;
+ Assert.Multiple(() =>
+ {
+ var quantity = new Density(value: 1, unit: Density.BaseUnit);
+ Assert.Throws(() => quantity.ToUnit(nullUnitSystem));
+ }, () =>
+ {
+ IQuantity quantity = new Density(value: 1, unit: Density.BaseUnit);
+ Assert.Throws(() => quantity.ToUnit(nullUnitSystem));
+ }, () =>
+ {
+ IQuantity quantity = new Density(value: 1, unit: Density.BaseUnit);
+ Assert.Throws(() => quantity.ToUnit(nullUnitSystem));
+ });
+ }
+
+ [Fact]
+ public void ToUnit_UnitSystem_ThrowsArgumentExceptionIfNotSupported()
+ {
+ var unsupportedUnitSystem = new UnitSystem(UnsupportedBaseUnits);
+ Assert.Multiple(() =>
+ {
+ var quantity = new Density(value: 1, unit: Density.BaseUnit);
+ Assert.Throws(() => quantity.ToUnit(unsupportedUnitSystem));
+ }, () =>
+ {
+ IQuantity quantity = new Density(value: 1, unit: Density.BaseUnit);
+ Assert.Throws(() => quantity.ToUnit(unsupportedUnitSystem));
+ }, () =>
+ {
+ IQuantity quantity = new Density(value: 1, unit: Density.BaseUnit);
+ Assert.Throws(() => quantity.ToUnit(unsupportedUnitSystem));
+ });
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/DoseAreaProductTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/DoseAreaProductTestsBase.g.cs
index 3d117b22c9..31da791da1 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/DoseAreaProductTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/DoseAreaProductTestsBase.g.cs
@@ -168,18 +168,18 @@ public void Ctor_NullAsUnitSystem_ThrowsArgumentNullException()
}
[Fact]
- public void Ctor_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
+ public virtual void Ctor_SIUnitSystem_ReturnsQuantityWithSIUnits()
{
- Func TestCode = () => new DoseAreaProduct(value: 1, unitSystem: UnitSystem.SI);
- if (SupportsSIUnitSystem)
- {
- var quantity = (DoseAreaProduct) TestCode();
- Assert.Equal(1, quantity.Value);
- }
- else
- {
- Assert.Throws(TestCode);
- }
+ var quantity = new DoseAreaProduct(value: 1, unitSystem: UnitSystem.SI);
+ Assert.Equal(1, quantity.Value);
+ Assert.True(quantity.QuantityInfo.UnitInfos.First(x => x.Value == quantity.Unit).BaseUnits.IsSubsetOf(UnitSystem.SI.BaseUnits));
+ }
+
+ [Fact]
+ public void Ctor_UnitSystem_ThrowsArgumentExceptionIfNotSupported()
+ {
+ var unsupportedUnitSystem = new UnitSystem(UnsupportedBaseUnits);
+ Assert.Throws(() => new DoseAreaProduct(value: 1, unitSystem: unsupportedUnitSystem));
}
[Fact]
@@ -352,20 +352,109 @@ public void As()
}
[Fact]
- public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
+ public virtual void BaseUnit_HasSIBase()
+ {
+ var baseUnitInfo = DoseAreaProduct.Info.BaseUnitInfo;
+ Assert.True(baseUnitInfo.BaseUnits.IsSubsetOf(UnitSystem.SI.BaseUnits));
+ }
+
+ [Fact]
+ public virtual void As_UnitSystem_SI_ReturnsQuantityInSIUnits()
+ {
+ var quantity = new DoseAreaProduct(value: 1, unit: DoseAreaProduct.BaseUnit);
+ var expectedValue = quantity.As(DoseAreaProduct.Info.GetDefaultUnit(UnitSystem.SI));
+
+ var convertedValue = quantity.As(UnitSystem.SI);
+
+ Assert.Equal(expectedValue, convertedValue);
+ }
+
+ [Fact]
+ public void As_UnitSystem_ThrowsArgumentNullExceptionIfNull()
+ {
+ var quantity = new DoseAreaProduct(value: 1, unit: DoseAreaProduct.BaseUnit);
+ UnitSystem nullUnitSystem = null!;
+ Assert.Throws(() => quantity.As(nullUnitSystem));
+ }
+
+ [Fact]
+ public void As_UnitSystem_ThrowsArgumentExceptionIfNotSupported()
+ {
+ var quantity = new DoseAreaProduct(value: 1, unit: DoseAreaProduct.BaseUnit);
+ var unsupportedUnitSystem = new UnitSystem(UnsupportedBaseUnits);
+ Assert.Throws(() => quantity.As(unsupportedUnitSystem));
+ }
+
+ [Fact]
+ public virtual void ToUnit_UnitSystem_SI_ReturnsQuantityInSIUnits()
{
var quantity = new DoseAreaProduct(value: 1, unit: DoseAreaProduct.BaseUnit);
- Func AsWithSIUnitSystem = () => quantity.As(UnitSystem.SI);
+ var expectedUnit = DoseAreaProduct.Info.GetDefaultUnit(UnitSystem.SI);
+ var expectedValue = quantity.As(expectedUnit);
- if (SupportsSIUnitSystem)
+ Assert.Multiple(() =>
{
- var value = Convert.ToDouble(AsWithSIUnitSystem());
- Assert.Equal(1, value);
- }
- else
+ DoseAreaProduct quantityToConvert = quantity;
+
+ DoseAreaProduct convertedQuantity = quantityToConvert.ToUnit(UnitSystem.SI);
+
+ Assert.Equal(expectedUnit, convertedQuantity.Unit);
+ Assert.Equal(expectedValue, convertedQuantity.Value);
+ }, () =>
{
- Assert.Throws(AsWithSIUnitSystem);
- }
+ IQuantity quantityToConvert = quantity;
+
+ IQuantity convertedQuantity = quantityToConvert.ToUnit(UnitSystem.SI);
+
+ Assert.Equal(expectedUnit, convertedQuantity.Unit);
+ Assert.Equal(expectedValue, convertedQuantity.Value);
+ }, () =>
+ {
+ IQuantity quantityToConvert = quantity;
+
+ IQuantity convertedQuantity = quantityToConvert.ToUnit(UnitSystem.SI);
+
+ Assert.Equal(expectedUnit, convertedQuantity.Unit);
+ Assert.Equal(expectedValue, convertedQuantity.Value);
+ });
+ }
+
+ [Fact]
+ public void ToUnit_UnitSystem_ThrowsArgumentNullExceptionIfNull()
+ {
+ UnitSystem nullUnitSystem = null!;
+ Assert.Multiple(() =>
+ {
+ var quantity = new DoseAreaProduct(value: 1, unit: DoseAreaProduct.BaseUnit);
+ Assert.Throws(() => quantity.ToUnit(nullUnitSystem));
+ }, () =>
+ {
+ IQuantity quantity = new DoseAreaProduct(value: 1, unit: DoseAreaProduct.BaseUnit);
+ Assert.Throws(() => quantity.ToUnit(nullUnitSystem));
+ }, () =>
+ {
+ IQuantity quantity = new DoseAreaProduct(value: 1, unit: DoseAreaProduct.BaseUnit);
+ Assert.Throws(() => quantity.ToUnit(nullUnitSystem));
+ });
+ }
+
+ [Fact]
+ public void ToUnit_UnitSystem_ThrowsArgumentExceptionIfNotSupported()
+ {
+ var unsupportedUnitSystem = new UnitSystem(UnsupportedBaseUnits);
+ Assert.Multiple(() =>
+ {
+ var quantity = new DoseAreaProduct(value: 1, unit: DoseAreaProduct.BaseUnit);
+ Assert.Throws(() => quantity.ToUnit(unsupportedUnitSystem));
+ }, () =>
+ {
+ IQuantity quantity = new DoseAreaProduct(value: 1, unit: DoseAreaProduct.BaseUnit);
+ Assert.Throws(() => quantity.ToUnit(unsupportedUnitSystem));
+ }, () =>
+ {
+ IQuantity quantity = new DoseAreaProduct(value: 1, unit: DoseAreaProduct.BaseUnit);
+ Assert.Throws(() => quantity.ToUnit(unsupportedUnitSystem));
+ });
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/DurationTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/DurationTestsBase.g.cs
index 9cf6c4fd45..716638d9a6 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/DurationTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/DurationTestsBase.g.cs
@@ -136,18 +136,18 @@ public void Ctor_NullAsUnitSystem_ThrowsArgumentNullException()
}
[Fact]
- public void Ctor_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
+ public virtual void Ctor_SIUnitSystem_ReturnsQuantityWithSIUnits()
{
- Func TestCode = () => new Duration(value: 1, unitSystem: UnitSystem.SI);
- if (SupportsSIUnitSystem)
- {
- var quantity = (Duration) TestCode();
- Assert.Equal(1, quantity.Value);
- }
- else
- {
- Assert.Throws(TestCode);
- }
+ var quantity = new Duration(value: 1, unitSystem: UnitSystem.SI);
+ Assert.Equal(1, quantity.Value);
+ Assert.True(quantity.QuantityInfo.UnitInfos.First(x => x.Value == quantity.Unit).BaseUnits.IsSubsetOf(UnitSystem.SI.BaseUnits));
+ }
+
+ [Fact]
+ public void Ctor_UnitSystem_ThrowsArgumentExceptionIfNotSupported()
+ {
+ var unsupportedUnitSystem = new UnitSystem(UnsupportedBaseUnits);
+ Assert.Throws(() => new Duration(value: 1, unitSystem: unsupportedUnitSystem));
}
[Fact]
@@ -272,20 +272,109 @@ public void As()
}
[Fact]
- public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
+ public virtual void BaseUnit_HasSIBase()
+ {
+ var baseUnitInfo = Duration.Info.BaseUnitInfo;
+ Assert.True(baseUnitInfo.BaseUnits.IsSubsetOf(UnitSystem.SI.BaseUnits));
+ }
+
+ [Fact]
+ public virtual void As_UnitSystem_SI_ReturnsQuantityInSIUnits()
+ {
+ var quantity = new Duration(value: 1, unit: Duration.BaseUnit);
+ var expectedValue = quantity.As(Duration.Info.GetDefaultUnit(UnitSystem.SI));
+
+ var convertedValue = quantity.As(UnitSystem.SI);
+
+ Assert.Equal(expectedValue, convertedValue);
+ }
+
+ [Fact]
+ public void As_UnitSystem_ThrowsArgumentNullExceptionIfNull()
+ {
+ var quantity = new Duration(value: 1, unit: Duration.BaseUnit);
+ UnitSystem nullUnitSystem = null!;
+ Assert.Throws(() => quantity.As(nullUnitSystem));
+ }
+
+ [Fact]
+ public void As_UnitSystem_ThrowsArgumentExceptionIfNotSupported()
+ {
+ var quantity = new Duration(value: 1, unit: Duration.BaseUnit);
+ var unsupportedUnitSystem = new UnitSystem(UnsupportedBaseUnits);
+ Assert.Throws(() => quantity.As(unsupportedUnitSystem));
+ }
+
+ [Fact]
+ public virtual void ToUnit_UnitSystem_SI_ReturnsQuantityInSIUnits()
{
var quantity = new Duration(value: 1, unit: Duration.BaseUnit);
- Func AsWithSIUnitSystem = () => quantity.As(UnitSystem.SI);
+ var expectedUnit = Duration.Info.GetDefaultUnit(UnitSystem.SI);
+ var expectedValue = quantity.As(expectedUnit);
- if (SupportsSIUnitSystem)
+ Assert.Multiple(() =>
{
- var value = Convert.ToDouble(AsWithSIUnitSystem());
- Assert.Equal(1, value);
- }
- else
+ Duration quantityToConvert = quantity;
+
+ Duration convertedQuantity = quantityToConvert.ToUnit(UnitSystem.SI);
+
+ Assert.Equal(expectedUnit, convertedQuantity.Unit);
+ Assert.Equal(expectedValue, convertedQuantity.Value);
+ }, () =>
{
- Assert.Throws(AsWithSIUnitSystem);
- }
+ IQuantity quantityToConvert = quantity;
+
+ IQuantity convertedQuantity = quantityToConvert.ToUnit(UnitSystem.SI);
+
+ Assert.Equal(expectedUnit, convertedQuantity.Unit);
+ Assert.Equal(expectedValue, convertedQuantity.Value);
+ }, () =>
+ {
+ IQuantity quantityToConvert = quantity;
+
+ IQuantity convertedQuantity = quantityToConvert.ToUnit(UnitSystem.SI);
+
+ Assert.Equal(expectedUnit, convertedQuantity.Unit);
+ Assert.Equal(expectedValue, convertedQuantity.Value);
+ });
+ }
+
+ [Fact]
+ public void ToUnit_UnitSystem_ThrowsArgumentNullExceptionIfNull()
+ {
+ UnitSystem nullUnitSystem = null!;
+ Assert.Multiple(() =>
+ {
+ var quantity = new Duration(value: 1, unit: Duration.BaseUnit);
+ Assert.Throws(() => quantity.ToUnit(nullUnitSystem));
+ }, () =>
+ {
+ IQuantity quantity = new Duration(value: 1, unit: Duration.BaseUnit);
+ Assert.Throws(() => quantity.ToUnit(nullUnitSystem));
+ }, () =>
+ {
+ IQuantity quantity = new Duration(value: 1, unit: Duration.BaseUnit);
+ Assert.Throws(() => quantity.ToUnit(nullUnitSystem));
+ });
+ }
+
+ [Fact]
+ public void ToUnit_UnitSystem_ThrowsArgumentExceptionIfNotSupported()
+ {
+ var unsupportedUnitSystem = new UnitSystem(UnsupportedBaseUnits);
+ Assert.Multiple(() =>
+ {
+ var quantity = new Duration(value: 1, unit: Duration.BaseUnit);
+ Assert.Throws(() => quantity.ToUnit(unsupportedUnitSystem));
+ }, () =>
+ {
+ IQuantity quantity = new Duration(value: 1, unit: Duration.BaseUnit);
+ Assert.Throws(() => quantity.ToUnit(unsupportedUnitSystem));
+ }, () =>
+ {
+ IQuantity quantity = new Duration(value: 1, unit: Duration.BaseUnit);
+ Assert.Throws(() => quantity.ToUnit(unsupportedUnitSystem));
+ });
}
[Fact]
diff --git a/UnitsNet.Tests/GeneratedCode/TestsBase/DynamicViscosityTestsBase.g.cs b/UnitsNet.Tests/GeneratedCode/TestsBase/DynamicViscosityTestsBase.g.cs
index fe420c3483..4d1eaa3e17 100644
--- a/UnitsNet.Tests/GeneratedCode/TestsBase/DynamicViscosityTestsBase.g.cs
+++ b/UnitsNet.Tests/GeneratedCode/TestsBase/DynamicViscosityTestsBase.g.cs
@@ -128,18 +128,18 @@ public void Ctor_NullAsUnitSystem_ThrowsArgumentNullException()
}
[Fact]
- public void Ctor_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
+ public virtual void Ctor_SIUnitSystem_ReturnsQuantityWithSIUnits()
{
- Func TestCode = () => new DynamicViscosity(value: 1, unitSystem: UnitSystem.SI);
- if (SupportsSIUnitSystem)
- {
- var quantity = (DynamicViscosity) TestCode();
- Assert.Equal(1, quantity.Value);
- }
- else
- {
- Assert.Throws(TestCode);
- }
+ var quantity = new DynamicViscosity(value: 1, unitSystem: UnitSystem.SI);
+ Assert.Equal(1, quantity.Value);
+ Assert.True(quantity.QuantityInfo.UnitInfos.First(x => x.Value == quantity.Unit).BaseUnits.IsSubsetOf(UnitSystem.SI.BaseUnits));
+ }
+
+ [Fact]
+ public void Ctor_UnitSystem_ThrowsArgumentExceptionIfNotSupported()
+ {
+ var unsupportedUnitSystem = new UnitSystem(UnsupportedBaseUnits);
+ Assert.Throws(() => new DynamicViscosity(value: 1, unitSystem: unsupportedUnitSystem));
}
[Fact]
@@ -252,20 +252,109 @@ public void As()
}
[Fact]
- public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
+ public virtual void BaseUnit_HasSIBase()
+ {
+ var baseUnitInfo = DynamicViscosity.Info.BaseUnitInfo;
+ Assert.True(baseUnitInfo.BaseUnits.IsSubsetOf(UnitSystem.SI.BaseUnits));
+ }
+
+ [Fact]
+ public virtual void As_UnitSystem_SI_ReturnsQuantityInSIUnits()
+ {
+ var quantity = new DynamicViscosity(value: 1, unit: DynamicViscosity.BaseUnit);
+ var expectedValue = quantity.As(DynamicViscosity.Info.GetDefaultUnit(UnitSystem.SI));
+
+ var convertedValue = quantity.As(UnitSystem.SI);
+
+ Assert.Equal(expectedValue, convertedValue);
+ }
+
+ [Fact]
+ public void As_UnitSystem_ThrowsArgumentNullExceptionIfNull()
+ {
+ var quantity = new DynamicViscosity(value: 1, unit: DynamicViscosity.BaseUnit);
+ UnitSystem nullUnitSystem = null!;
+ Assert.Throws(() => quantity.As(nullUnitSystem));
+ }
+
+ [Fact]
+ public void As_UnitSystem_ThrowsArgumentExceptionIfNotSupported()
+ {
+ var quantity = new DynamicViscosity(value: 1, unit: DynamicViscosity.BaseUnit);
+ var unsupportedUnitSystem = new UnitSystem(UnsupportedBaseUnits);
+ Assert.Throws(() => quantity.As(unsupportedUnitSystem));
+ }
+
+ [Fact]
+ public virtual void ToUnit_UnitSystem_SI_ReturnsQuantityInSIUnits()
{
var quantity = new DynamicViscosity(value: 1, unit: DynamicViscosity.BaseUnit);
- Func AsWithSIUnitSystem = () => quantity.As(UnitSystem.SI);
+ var expectedUnit = DynamicViscosity.Info.GetDefaultUnit(UnitSystem.SI);
+ var expectedValue = quantity.As(expectedUnit);
- if (SupportsSIUnitSystem)
+ Assert.Multiple(() =>
{
- var value = Convert.ToDouble(AsWithSIUnitSystem());
- Assert.Equal(1, value);
- }
- else
+ DynamicViscosity quantityToConvert = quantity;
+
+ DynamicViscosity convertedQuantity = quantityToConvert.ToUnit(UnitSystem.SI);
+
+ Assert.Equal(expectedUnit, convertedQuantity.Unit);
+ Assert.Equal(expectedValue, convertedQuantity.Value);
+ }, () =>
{
- Assert.Throws(AsWithSIUnitSystem);
- }
+ IQuantity quantityToConvert = quantity;
+
+ IQuantity convertedQuantity = quantityToConvert.ToUnit(UnitSystem.SI);
+
+ Assert.Equal(expectedUnit, convertedQuantity.Unit);
+ Assert.Equal(expectedValue, convertedQuantity.Value);
+ }, () =>
+ {
+ IQuantity quantityToConvert = quantity;
+
+ IQuantity convertedQuantity = quantityToConvert.ToUnit(UnitSystem.SI);
+
+ Assert.Equal(expectedUnit, convertedQuantity.Unit);
+ Assert.Equal(expectedValue, convertedQuantity.Value);
+ });
+ }
+
+ [Fact]
+ public void ToUnit_UnitSystem_ThrowsArgumentNullExceptionIfNull()
+ {
+ UnitSystem nullUnitSystem = null!;
+ Assert.Multiple(() =>
+ {
+ var quantity = new DynamicViscosity(value: 1, unit: DynamicViscosity.BaseUnit);
+ Assert.Throws