Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
3c61dd6
- QuantityValue implemented as a fractional number
lipchev Apr 13, 2025
e421a1b
adding the git-ignored files
lipchev Apr 13, 2025
53f00cd
changing the CultureInfo? parameters back to IFormatProvder?
lipchev Apr 17, 2025
e339d87
- `UnitAbbreviationsCahe` and `UnitParser`: replaced the IReadOnlyCol…
lipchev Apr 18, 2025
d3092bc
including the changes from https://github.com/angularsen/UnitsNet/pul…
lipchev Apr 19, 2025
5693259
Completed the QuantityFormatter test coverage (and some cosmetic chan…
lipchev Apr 19, 2025
b027df2
UnitKey: making the constructor internal and renaming the properties
lipchev Apr 19, 2025
e23973a
removing the QuantityFormatter overloads and re-organizing the extens…
lipchev Apr 20, 2025
fe0a4c9
- updating the implicit usings and cleanup some of the using statements
lipchev Apr 21, 2025
56b1df9
adding a few more tests
lipchev Apr 21, 2025
da9a3e9
removing some unnecessary using statements and replacing the target f…
lipchev Jul 25, 2025
345ae06
renaming the default quantity provider
lipchev Jul 25, 2025
3c84a32
removing Quantity.FromQuantityInfo
lipchev Jul 25, 2025
5907e18
avoiding the ToArray call when initializing the quantity info using a…
lipchev Jul 25, 2025
82edf5c
Merge remote-tracking branch 'upstream\master' into fractional-quanti…
lipchev Jul 25, 2025
b07cab1
Merge remote-tracking branch 'upstream\master' into fractional-quanti…
lipchev Jul 26, 2025
e27cc5b
removing the remnents of the IQuantityInfo<TUnit> interface and the u…
lipchev Jul 26, 2025
faa0c1e
removed the unit-not-found comment
lipchev Jul 26, 2025
eb22805
merging the removal of the EnumUtils (i'm keeping it for now)
lipchev Jul 26, 2025
ece337d
merging the AoT compatibility: https://github.com/angularsen/UnitsNet…
lipchev Jul 26, 2025
cdf2016
adding the missing TryGetUnitFromAbbreviation overload and the tests
lipchev Jul 26, 2025
b1130f4
merging from upstream: ✨Add more Gray units (#1568)
lipchev Jul 26, 2025
f425ebe
removed the EnumUtils and replacing the usages with the EnumHelper
lipchev Jul 26, 2025
c53e7e9
bumping the pre-release version to 6.0.0-pre017 (staying ahead of the…
lipchev Jul 26, 2025
e5664e7
merging from upstream (keeping ahead of the official nugget version: …
lipchev Jul 27, 2025
e43068f
cleanup some namespaces and other cosmetic changes
lipchev Jul 29, 2025
35619ee
merging from upstream and bumping the nugget versions (keeping ahead …
lipchev Jul 30, 2025
ffc88de
splitting the IQuantity interface definitions into separate files
lipchev Jul 30, 2025
f7118a4
- renamed IQuantityInstance to IQuantityOfType
lipchev Aug 3, 2025
192ecae
making the GenericMathExtensions class obsolete and replacing the rel…
lipchev Aug 3, 2025
75fe28c
remove the GenerateArithmetic from the Temperature.json
lipchev Aug 3, 2025
fb01d4f
- re-formatting the QuantityInfo and making the IQuantityInfo interna…
lipchev Aug 3, 2025
db8ee3e
cleanup and reformat the UnitInfo/IUnitDefinition
lipchev Aug 3, 2025
b8877e1
merging from upstream: Add prefix milli to torr unit (#1588)
lipchev Aug 3, 2025
8c31f4e
cosmetic changes
lipchev Aug 3, 2025
f94a4d0
refactored test: From_ValueAndUnit_ReturnsQuantityWithSameValueAndUnit
lipchev Aug 3, 2025
0133647
renamed the UnitsNetSetup.QuantityInfoLookup
lipchev Aug 9, 2025
338da80
Merge remote-tracking branch 'upstream\master' into fractional-quanti…
lipchev Aug 10, 2025
09fd680
synchronizing the changes in UnitKey
lipchev Aug 16, 2025
8634470
merged from upstream (ignoring the ToUnitUntyped bits)
lipchev Aug 16, 2025
6e229c7
- Constraint the Abs extension method to ILinearQuantity<TQuantity>
lipchev Aug 18, 2025
927b7e2
merged from upstream
lipchev Aug 18, 2025
e569dfa
updating the nugget packages
lipchev Aug 18, 2025
6d430a6
refactored the [Obsolete] IQuantity methods as extensions
lipchev Aug 18, 2025
f7fc596
exposing the operator IDivisionOperators<TSelf, TSelf, QuantityValue>
lipchev Aug 18, 2025
1538a52
bump the nugget version to 6.0.0-pre019
lipchev Aug 19, 2025
14ee96e
making the TQuantity Create(double, UnitKey) method public (IQuantity…
lipchev Aug 19, 2025
48d50a9
removing the Dimensions instance property
lipchev Aug 19, 2025
2724c42
merged from upstream
lipchev Aug 19, 2025
3b3287d
removed the Dimensions from the HowMuch
lipchev Aug 19, 2025
ab3402b
completing the implementation of the HowMuch in the Configuration sample
lipchev Aug 19, 2025
f549040
merged from upstream
lipchev Sep 22, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
UnitKey: making the constructor internal and renaming the properties
  • Loading branch information
lipchev committed Apr 19, 2025
commit b027df28ba95491a4d5225be097b74ea3e0a88ef
4 changes: 2 additions & 2 deletions UnitsNet.Benchmark/Enums/GetBoxedEnumValuesBenchmarks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public int GetEnumValues()
foreach (Enum unitValue in Enum.GetValues(unitType).Cast<Enum>())
{
var unitKey = (UnitKey)unitValue;
total += unitKey.UnitValue;
total += unitKey.UnitEnumValue;
}
}

Expand All @@ -39,7 +39,7 @@ public int GetEnumValuesWithDictionary()
{
foreach (UnitInfo unitInfo in _quantityInfos[unitType].UnitInfos)
{
total += unitInfo.UnitKey.UnitValue;
total += unitInfo.UnitKey.UnitEnumValue;
}
}

Expand Down
8 changes: 4 additions & 4 deletions UnitsNet.Benchmark/Enums/UnitKeyEqualsBenchmarks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ public class UnitKeyEqualsBenchmarks

private static readonly UnitKey UnitKey = UnitKey.ForUnit(VolumeUnit.CubicMeter);
private static readonly UnitKey OtherUnitKey = UnitKey.ForUnit(VolumeUnit.AcreFoot);
private readonly Type OtherUnitType = UnitKey.UnitType;
private readonly int OtherUnitValue = UnitKey.UnitValue;
private readonly Type OtherUnitType = UnitKey.UnitEnumType;
private readonly int OtherUnitValue = UnitKey.UnitEnumValue;

private readonly Type UnitType = UnitKey.UnitType;
private readonly int UnitValue = UnitKey.UnitValue;
private readonly Type UnitType = UnitKey.UnitEnumType;
private readonly int UnitValue = UnitKey.UnitEnumValue;

[Benchmark(Baseline = true)]
public bool EqualsRecord()
Expand Down
4 changes: 2 additions & 2 deletions UnitsNet.Benchmark/Enums/UnitKeyHashCodeBenchmarks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ public class UnitKeyHashCodeBenchmarks

private static readonly UnitKey UnitKey = UnitKey.ForUnit(VolumeUnit.CubicMeter);

private readonly Type UnitType = UnitKey.UnitType;
private readonly int UnitValue = UnitKey.UnitValue;
private readonly Type UnitType = UnitKey.UnitEnumType;
private readonly int UnitValue = UnitKey.UnitEnumValue;

[Benchmark(Baseline = true)]
public int GetHashCodeRecord()
Expand Down
2 changes: 1 addition & 1 deletion UnitsNet.Benchmark/Enums/UnitKeyToEnumBenchmarks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public int ManualCast()
var total = 0;
for (var i = 0; i < NbIterations; i++)
{
if ((MassUnit)unitKey.UnitValue == MassUnit.Gram)
if ((MassUnit)unitKey.UnitEnumValue == MassUnit.Gram)
{
total++;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public override void Write(Utf8JsonWriter writer, IQuantity quantity, JsonSerial
WriteValueProperty(writer, quantity, options);
}

if (quantity.UnitKey.UnitValue != quantity.QuantityInfo.BaseUnitInfo.UnitKey.UnitValue)
if (quantity.UnitKey.UnitEnumValue != quantity.QuantityInfo.BaseUnitInfo.UnitKey.UnitEnumValue)
{
// write the Unit property
writer.WritePropertyName(unitProperty);
Expand Down
2 changes: 1 addition & 1 deletion UnitsNet.Tests/QuantityConversionOptionsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void SetConversionUnits_ShouldAddUnitConversionMapping()
options.SetConversionUnits(LengthUnit.Meter, MassUnit.Kilogram);

Assert.Contains(options.ConversionUnits, mapping =>
mapping.FromUnitKey.UnitType == typeof(LengthUnit) && mapping.ToUnitKey.UnitType == typeof(MassUnit));
mapping.FromUnitKey.UnitEnumType == typeof(LengthUnit) && mapping.ToUnitKey.UnitEnumType == typeof(MassUnit));
}

[Fact]
Expand Down
12 changes: 6 additions & 6 deletions UnitsNet.Tests/QuantityConversionsBuilderExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,7 @@ public void GetConversionFunctions_WithCustomConversionsAndCustomCachingOptions_
UnitConversionMapping unitConversionKey = pair.Key;
CustomQuantityConversionExpressionMapping expressionMapping = pair.Value;
ConversionExpression conversionExpression = expressionMapping.ConversionExpression;
var conversionKey = new QuantityConversionKey(unitConversionKey.FromUnitKey, unitConversionKey.ToUnitKey.UnitType);
var conversionKey = new QuantityConversionKey(unitConversionKey.FromUnitKey, unitConversionKey.ToUnitKey.UnitEnumType);
Assert.Contains(conversionKey, conversionExpressions);
Assert.Equal(expressionMapping.TargetUnit.UnitKey, conversionExpressions[conversionKey].TargetUnit);
Assert.Equal(conversionExpression.Evaluate(10), conversionExpressions[conversionKey].Convert(10));
Expand All @@ -657,7 +657,7 @@ public void GetConversionFunctions_WithCustomConversionsAndCustomCachingOptions_
() =>
{
// all generated conversions from a DensityUnit are a function of the custom conversion expressions in our customConversionFromDensity
Assert.All(conversionExpressions.Where(x => x.Key.FromUnit.UnitType == typeof(DensityUnit)),
Assert.All(conversionExpressions.Where(x => x.Key.FromUnit.UnitEnumType == typeof(DensityUnit)),
pair =>
{
QuantityConversionKey conversionKey = pair.Key;
Expand All @@ -681,7 +681,7 @@ public void GetConversionFunctions_WithCustomConversionsAndCustomCachingOptions_
() =>
{
// all generated conversions from a SpecificVolumeUnit are a function of the custom conversion expressions in our customConversionFromSpecificVolume
Assert.All(conversionExpressions.Where(x => x.Key.FromUnit.UnitType == typeof(SpecificVolumeUnit)),
Assert.All(conversionExpressions.Where(x => x.Key.FromUnit.UnitEnumType == typeof(SpecificVolumeUnit)),
pair =>
{
QuantityConversionKey conversionKey = pair.Key;
Expand Down Expand Up @@ -772,7 +772,7 @@ public void GetConversionFunctions_WithUnitConversionMappings_ReturnsTheSpecifie
Assert.Equal(expectedValue, conversionExpressions[conversionKey].Convert(valueToConvert));
Assert.Equal(toUnit.UnitKey, conversionExpressions[conversionKey].TargetUnit);
// this maybe a bit redundant but that's the only covering line for the ResultType property (its only used for the default equality contract)
Assert.Equal(conversionKey.ResultType, conversionExpressions[conversionKey].TargetUnit.UnitType);
Assert.Equal(conversionKey.ResultType, conversionExpressions[conversionKey].TargetUnit.UnitEnumType);
if (reduceConstants)
{
Assert.True(IsReduced(conversionExpressions[conversionKey].Convert(1)));
Expand Down Expand Up @@ -846,15 +846,15 @@ public void GetConversionFunctions_WithUnitConversionMappingsAndCustomFunctions_
{
Assert.All(conversionMappingOptions.ConversionExpressions, pair =>
{
var conversionKey = new QuantityConversionKey(pair.Key.FromUnitKey, pair.Key.ToUnitKey.UnitType);
var conversionKey = new QuantityConversionKey(pair.Key.FromUnitKey, pair.Key.ToUnitKey.UnitEnumType);
Assert.True(conversionExpressions.ContainsKey(conversionKey));
Assert.Equal(pair.Value.ConversionExpression.Evaluate(10), conversionExpressions[conversionKey].Convert(10));
});
}, () =>
{
Assert.All(conversionMappingOptions.CustomUnitMappings, pair =>
{
var conversionKey = new QuantityConversionKey(pair.Key.FromUnitKey, pair.Key.ToUnitKey.UnitType);
var conversionKey = new QuantityConversionKey(pair.Key.FromUnitKey, pair.Key.ToUnitKey.UnitEnumType);
UnitInfo sourceUnit = pair.Value.SourceUnit;
UnitInfo targetUnit = pair.Value.TargetUnit;
QuantityValue valueToConvert = 10;
Expand Down
12 changes: 12 additions & 0 deletions UnitsNet.Tests/UnitAbbreviationsCacheTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,12 @@ public void GetDefaultAbbreviationFallsBackToInvariantCulture()
// Assert
Assert.Equal("Invariant abbreviation for Unit1", abbreviation);
}

[Fact]
public void GetDefaultAbbreviation_WithNullUnitType_ThrowsArgumentNullException()
{
Assert.Throws<ArgumentNullException>(() => UnitAbbreviationsCache.Default.GetDefaultAbbreviation(null!, 1));
}

[Fact]
public void GetDefaultAbbreviationThrowsUnitNotFoundExceptionIfNoneExist()
Expand Down Expand Up @@ -178,6 +184,12 @@ public void GetAllUnitAbbreviationsForQuantity_WithQuantityWithoutAbbreviations_
Assert.Empty(unitAbbreviationsCache.GetAllUnitAbbreviationsForQuantity(typeof(HowMuchUnit)));
}

[Fact]
public void GetAllUnitAbbreviationsForQuantity_WithNullUnitType_ThrowsArgumentNullException()
{
Assert.Throws<ArgumentNullException>(() => UnitAbbreviationsCache.Default.GetAllUnitAbbreviationsForQuantity(null!));
}

[Fact]
public void GetAllUnitAbbreviationsForQuantity_WithInvalidUnitType_ThrowsArgumentException()
{
Expand Down
51 changes: 37 additions & 14 deletions UnitsNet.Tests/UnitKeyTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@ public enum TestUnit
public void Constructor_ShouldCreateUnitKey(int unitValue)
{
var unitKey = new UnitKey(typeof(TestUnit), unitValue);
Assert.Equal(typeof(TestUnit), unitKey.UnitType);
Assert.Equal(unitValue, unitKey.UnitValue);
Assert.Equal(typeof(TestUnit), unitKey.UnitEnumType);
Assert.Equal(unitValue, unitKey.UnitEnumValue);
}

[Fact]
public void Constructor_WithNullType_ShouldNotThrow()
{
var unitKey = new UnitKey(null!, 0);
Assert.Null(unitKey.UnitType);
Assert.Equal(0, unitKey.UnitValue);
Assert.Null(unitKey.UnitEnumType);
Assert.Equal(0, unitKey.UnitEnumValue);
}

[Theory]
Expand All @@ -42,8 +42,8 @@ public void Constructor_WithNullType_ShouldNotThrow()
public void ForUnit_ShouldCreateUnitKey(TestUnit unit)
{
var unitKey = UnitKey.ForUnit(unit);
Assert.Equal(typeof(TestUnit), unitKey.UnitType);
Assert.Equal((int)unit, unitKey.UnitValue);
Assert.Equal(typeof(TestUnit), unitKey.UnitEnumType);
Assert.Equal((int)unit, unitKey.UnitEnumValue);
}

[Theory]
Expand All @@ -53,8 +53,31 @@ public void ForUnit_ShouldCreateUnitKey(TestUnit unit)
public void Create_ShouldCreateUnitKey(int unitValue)
{
var unitKey = UnitKey.Create<TestUnit>(unitValue);
Assert.Equal(typeof(TestUnit), unitKey.UnitType);
Assert.Equal(unitValue, unitKey.UnitValue);
Assert.Equal(typeof(TestUnit), unitKey.UnitEnumType);
Assert.Equal(unitValue, unitKey.UnitEnumValue);
}

[Theory]
[InlineData(typeof(TestUnit), 1)]
[InlineData(typeof(TestUnit), 2)]
[InlineData(typeof(TestUnit), 3)]
public void Create_WithUnitTypeAndUnitValue_ShouldCreateUnitKey(Type unitType, int unitValue)
{
var unitKey = UnitKey.Create(unitType, unitValue);
Assert.Equal(unitType, unitKey.UnitEnumType);
Assert.Equal(unitValue, unitKey.UnitEnumValue);
}

[Fact]
public void Create_WithNullUnitType_ShouldThrowArgumentNullException()
{
Assert.Throws<ArgumentNullException>(() => UnitKey.Create(null!, 0));
}

[Fact]
public void Create_WithNonEnumType_ShouldThrowArgumentException()
{
Assert.Throws<ArgumentException>(() => UnitKey.Create(typeof(int), 1));
}

[Theory]
Expand All @@ -64,8 +87,8 @@ public void Create_ShouldCreateUnitKey(int unitValue)
public void ImplicitConversion_ShouldCreateUnitKey(TestUnit unit)
{
UnitKey unitKey = unit;
Assert.Equal(typeof(TestUnit), unitKey.UnitType);
Assert.Equal((int)unit, unitKey.UnitValue);
Assert.Equal(typeof(TestUnit), unitKey.UnitEnumType);
Assert.Equal((int)unit, unitKey.UnitEnumValue);
}

[Theory]
Expand Down Expand Up @@ -94,8 +117,8 @@ public void ToUnit_ShouldReturnEnum(TestUnit unit)
public void Default_InitializesWithoutAType()
{
var defaultUnitKey = default(UnitKey);
Assert.Null(defaultUnitKey.UnitType);
Assert.Equal(0, defaultUnitKey.UnitValue);
Assert.Null(defaultUnitKey.UnitEnumType);
Assert.Equal(0, defaultUnitKey.UnitEnumValue);
}

[Fact]
Expand Down Expand Up @@ -139,7 +162,7 @@ public void Deconstruct_ShouldReturnTheUnitTypeAndUnitValue()
[InlineData(TestUnit.Unit1, "TestUnit.Unit1")]
[InlineData(TestUnit.Unit2, "TestUnit.Unit2")]
[InlineData(TestUnit.Unit3, "TestUnit.Unit3")]
[InlineData((TestUnit)(-1), "UnitType: UnitsNet.Tests.UnitKeyTest+TestUnit, UnitValue = -1")]
[InlineData((TestUnit)(-1), "UnitEnumType: UnitsNet.Tests.UnitKeyTest+TestUnit, UnitEnumValue = -1")]
public void GetDebuggerDisplay_ShouldReturnCorrectString(TestUnit unit, string expectedDisplay)
{
var unitKey = UnitKey.ForUnit(unit);
Expand All @@ -154,6 +177,6 @@ public void GetDebuggerDisplayWithDefault_ShouldReturnCorrectString()
var defaultUnitKey = default(UnitKey);
var display = defaultUnitKey.GetType().GetMethod("GetDebuggerDisplay", BindingFlags.NonPublic | BindingFlags.Instance)!
.Invoke(defaultUnitKey, null);
Assert.Equal("UnitType: , UnitValue = 0", display);
Assert.Equal("UnitEnumType: , UnitEnumValue = 0", display);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -265,9 +265,9 @@ private static IEnumerable<ConversionEntry> GetConversionsTo(this QuantityInfo f
{
// filter the customUnitConversions for specified conversion direction
Type sourceUnitType = fromQuantity.UnitType;
var unitMappings = customUnitConversions.CustomUnitMappings.Where(x => x.Key.FromUnitKey.UnitType == sourceUnitType)
var unitMappings = customUnitConversions.CustomUnitMappings.Where(x => x.Key.FromUnitKey.UnitEnumType == sourceUnitType)
.ToDictionary(x => x.Key.FromUnitKey, pair => pair.Value);
var conversionExpressions = customUnitConversions.ConversionExpressions.Where(x => x.Key.FromUnitKey.UnitType == sourceUnitType)
var conversionExpressions = customUnitConversions.ConversionExpressions.Where(x => x.Key.FromUnitKey.UnitEnumType == sourceUnitType)
.ToDictionary(x => x.Key.FromUnitKey, pair => pair.Value);
// if there are any custom conversionExpressions we ignore the caching mode and calculate an expression for all units
if (conversionExpressions.Count != 0)
Expand Down Expand Up @@ -332,7 +332,7 @@ private static IEnumerable<ConversionEntry> GetConversionsWithAllUnits(this Quan
{
if (toQuantity.TryGetConversionFrom(fromUnit, conversionExpression, reduceConstants, out QuantityConversionFunction conversionFunction))
{
yield return new ConversionEntry(new QuantityConversionKey(fromUnit.UnitKey, conversionFunction.TargetUnit.UnitType), conversionFunction);
yield return new ConversionEntry(new QuantityConversionKey(fromUnit.UnitKey, conversionFunction.TargetUnit.UnitEnumType), conversionFunction);
}
else
{
Expand Down
Loading