-
Notifications
You must be signed in to change notification settings - Fork 408
Expand file tree
/
Copy pathIQuantityTests.cs
More file actions
126 lines (104 loc) · 4.12 KB
/
IQuantityTests.cs
File metadata and controls
126 lines (104 loc) · 4.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Licensed under MIT No Attribution, see LICENSE file at the root.
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
namespace UnitsNet.Tests;
// ReSharper disable once InconsistentNaming
public partial class IQuantityTests
{
[Fact]
public void As_GivenWrongUnitType_ThrowsArgumentException()
{
Assert.All(Quantity.Infos.Select(x => x.Zero), quantity => { Assert.Throws<ArgumentException>(() => quantity.As(ComparisonType.Absolute)); });
}
[Fact]
public void ToUnit_GivenWrongUnitType_ThrowsArgumentException()
{
Assert.All(Quantity.Infos.Select(x => x.Zero), quantity => { Assert.Throws<ArgumentException>(() => quantity.ToUnit(ComparisonType.Absolute)); });
}
[Fact]
public virtual void ToUnit_UnitSystem_SI_ReturnsQuantityInSIUnits()
{
var quantity = new Mass(1, Mass.BaseUnit);
MassUnit expectedUnit = Mass.Info.GetDefaultUnit(UnitSystem.SI);
var expectedValue = quantity.As(expectedUnit);
Assert.Multiple(() =>
{
IQuantity<MassUnit> quantityToConvert = quantity;
IQuantity<MassUnit> 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(() =>
{
IQuantity<MassUnit> quantity = new Mass(1, Mass.BaseUnit);
Assert.Throws<ArgumentNullException>(() => quantity.ToUnit(nullUnitSystem));
}, () =>
{
IQuantity quantity = new Mass(1, Mass.BaseUnit);
Assert.Throws<ArgumentNullException>(() => quantity.ToUnit(nullUnitSystem));
});
}
[Fact]
public void UnitInfo_ReturnsUnitInfoForQuantityUnit()
{
var length = new Length(3.0, LengthUnit.Centimeter);
IQuantity quantity = length;
UnitInfo unitInfo = quantity.UnitInfo;
Assert.Equal(nameof(LengthUnit.Centimeter), unitInfo.Name);
Assert.Equal(quantity.UnitKey, unitInfo.UnitKey);
}
[Fact]
public void UnitInfo_Zero_ReturnsBaseUnitInfo()
{
IQuantity quantity = Length.Info.Zero;
UnitInfo unitInfo = quantity.UnitInfo;
Assert.Equal(Length.Info.BaseUnitInfo.UnitKey, unitInfo.UnitKey);
}
[Fact]
public void UnitInfo_TypedQuantity_ReturnsTypedUnitInfo()
{
IQuantity<LengthUnit> quantity = new Length(3.0, LengthUnit.Centimeter);
UnitInfo<LengthUnit> unitInfo = quantity.UnitInfo;
Assert.Equal(LengthUnit.Centimeter, unitInfo.Value);
Assert.Equal(nameof(LengthUnit.Centimeter), unitInfo.Name);
}
[Fact]
public void UnitInfo_MatchesUnit()
{
Assert.All(Quantity.Infos.Select(x => x.Zero), quantity =>
{
Assert.Equal(quantity.Unit, quantity.UnitInfo.Value);
});
}
[Fact]
public void ToUnit_UnitSystem_ThrowsArgumentExceptionIfNotSupported()
{
var unsupportedUnitSystem = new UnitSystem(new BaseUnits(
(LengthUnit)(-1),
(MassUnit)(-1),
(DurationUnit)(-1),
(ElectricCurrentUnit)(-1),
(TemperatureUnit)(-1),
(AmountOfSubstanceUnit)(-1),
(LuminousIntensityUnit)(-1)));
Assert.Multiple(() =>
{
IQuantity<MassUnit> quantity = new Mass(1, Mass.BaseUnit);
Assert.Throws<ArgumentException>(() => quantity.ToUnit(unsupportedUnitSystem));
}, () =>
{
IQuantity quantity = new Mass(1, Mass.BaseUnit);
Assert.Throws<ArgumentException>(() => quantity.ToUnit(unsupportedUnitSystem));
});
}
}