Skip to content

Commit c8d3fb6

Browse files
committed
Add UnitsHelper with tests
1 parent 42e8019 commit c8d3fb6

File tree

2 files changed

+195
-0
lines changed

2 files changed

+195
-0
lines changed

UnitsNet.Tests/UnitsHelperTest.cs

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
// Copyright (c) 2013 Andreas Gullberg Larsen ([email protected]).
2+
// https://github.com/angularsen/UnitsNet
3+
//
4+
// Permission is hereby granted, free of charge, to any person obtaining a copy
5+
// of this software and associated documentation files (the "Software"), to deal
6+
// in the Software without restriction, including without limitation the rights
7+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
// copies of the Software, and to permit persons to whom the Software is
9+
// furnished to do so, subject to the following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included in
12+
// all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
// THE SOFTWARE.
21+
22+
using System;
23+
using System.Linq;
24+
using UnitsNet.Units;
25+
using Xunit;
26+
27+
namespace UnitsNet.Tests
28+
{
29+
public class UnitsHelperTest
30+
{
31+
[Fact]
32+
public void Quantities_ReturnsKnownQuantityTypes()
33+
{
34+
var knownQuantities = new[] {QuantityType.Length, QuantityType.Force, QuantityType.Mass};
35+
36+
Assert.Superset(
37+
knownQuantities.ToHashSet(),
38+
UnitsHelper.Quantities.ToHashSet());
39+
}
40+
41+
[Fact]
42+
public void QuantityNames_ReturnsKnownNames()
43+
{
44+
var knownNames = new[] {"Length", "Force", "Mass"};
45+
46+
Assert.Superset(
47+
knownNames.ToHashSet(),
48+
UnitsHelper.QuantityNames.ToHashSet());
49+
}
50+
51+
[Fact]
52+
public void GetUnitNamesForQuantity_ReturnsKnownUnitNames()
53+
{
54+
var knownLengthUnitNames = new[] {"Meter", "Centimeter", "Kilometer"};
55+
var knownMassUnitNames = new[] {"Kilogram", "Gram", "Tonne"};
56+
57+
Assert.Superset(
58+
knownLengthUnitNames.ToHashSet(),
59+
UnitsHelper.GetUnitNamesForQuantity(QuantityType.Length).ToHashSet());
60+
61+
Assert.Superset(
62+
knownMassUnitNames.ToHashSet(),
63+
UnitsHelper.GetUnitNamesForQuantity(QuantityType.Mass).ToHashSet());
64+
}
65+
66+
[Fact]
67+
public void GetUnitEnumValuesForQuantity_ReturnsKnownValues()
68+
{
69+
var knownLengthUnits = new object[] {LengthUnit.Meter, LengthUnit.Centimeter, LengthUnit.Kilometer};
70+
var knownMassUnits = new object[] {MassUnit.Kilogram, MassUnit.Gram, MassUnit.Tonne};
71+
72+
Assert.Superset(
73+
knownLengthUnits.ToHashSet(),
74+
UnitsHelper.GetUnitEnumValuesForQuantity(QuantityType.Length).ToHashSet());
75+
76+
Assert.Superset(
77+
knownMassUnits.ToHashSet(),
78+
UnitsHelper.GetUnitEnumValuesForQuantity(QuantityType.Mass).ToHashSet());
79+
}
80+
81+
[Theory]
82+
[InlineData(QuantityType.Length, typeof(LengthUnit))]
83+
[InlineData(QuantityType.Mass, typeof(MassUnit))]
84+
[InlineData(QuantityType.Force, typeof(ForceUnit))]
85+
public void GetUnitType_ReturnsUnitTypeMatchingGivenQuantity(QuantityType quantityType, Type expectedUnitEnumType)
86+
{
87+
Assert.Equal(expectedUnitEnumType, UnitsHelper.GetUnitType(quantityType));
88+
}
89+
}
90+
}

UnitsNet/UnitsHelper.cs

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
// Copyright (c) 2013 Andreas Gullberg Larsen ([email protected]).
2+
// https://github.com/angularsen/UnitsNet
3+
//
4+
// Permission is hereby granted, free of charge, to any person obtaining a copy
5+
// of this software and associated documentation files (the "Software"), to deal
6+
// in the Software without restriction, including without limitation the rights
7+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
// copies of the Software, and to permit persons to whom the Software is
9+
// furnished to do so, subject to the following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included in
12+
// all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
// THE SOFTWARE.
21+
22+
using System;
23+
using System.Collections.Generic;
24+
using System.Linq;
25+
using System.Reflection;
26+
using UnitsNet.Units;
27+
28+
namespace UnitsNet
29+
{
30+
/// <summary>
31+
/// This class helps enumerating quantities and units at runtime.
32+
/// </summary>
33+
public static class UnitsHelper
34+
{
35+
private static readonly string UnitEnumNamespace = typeof(LengthUnit).Namespace;
36+
37+
private static readonly Type[] UnitEnumTypes = Assembly.GetAssembly(typeof(Length))
38+
.GetExportedTypes()
39+
.Where(t => t.IsEnum && t.Namespace == UnitEnumNamespace)
40+
.ToArray();
41+
42+
static UnitsHelper()
43+
{
44+
var quantityTypes = Enum.GetValues(typeof(QuantityType)).Cast<QuantityType>().ToArray();
45+
Quantities = quantityTypes;
46+
QuantityNames = quantityTypes.Select(q => q.ToString()).ToArray();
47+
}
48+
49+
/// <summary>
50+
/// All enum values of <see cref="QuantityType"/>, such as <see cref="QuantityType.Length"/> and <see cref="QuantityType.Mass"/>.
51+
/// </summary>
52+
public static QuantityType[] Quantities { get; }
53+
54+
/// <summary>
55+
/// All enum value names of <see cref="QuantityType"/>, such as "Length" and "Mass".
56+
/// </summary>
57+
public static string[] QuantityNames { get; }
58+
59+
/// <summary>
60+
/// Returns the enum values for the given <paramref name="quantity"/>, excluding the Undefined=0 value.
61+
/// You can then use this for dynamic parsing in <see cref="UnitParser.Parse"/>.
62+
/// If you only need the unit names, use <see cref="GetUnitNamesForQuantity"/> instead.
63+
/// </summary>
64+
/// <example>
65+
/// Given <see cref="QuantityType.Length"/> it will return all enum values of <see cref="LengthUnit"/>,
66+
/// such as <see cref="LengthUnit.Centimeter"/> and <see cref="LengthUnit.Meter"/>.
67+
/// </example>
68+
/// <param name="quantity">The quantity type.</param>
69+
/// <returns>Unit enum values.</returns>
70+
public static IEnumerable<object> GetUnitEnumValuesForQuantity(QuantityType quantity)
71+
{
72+
// QuantityType.Length => "UnitsNet.Units.LengthUnit"
73+
Type unitEnumType = GetUnitType(quantity);
74+
75+
// Skip Undefined, which is only really used to help catch uninitialized values
76+
return Enum.GetValues(unitEnumType).Cast<object>().Skip(1).ToArray();
77+
}
78+
79+
/// <summary>
80+
/// Returns the enum value names for the given <paramref name="quantity"/>.
81+
/// For example, given <see cref="QuantityType.Length"/> it will return the names
82+
/// of all enum values of <see cref="LengthUnit"/>, such as "Centimeter" and "Meter".
83+
/// </summary>
84+
/// <param name="quantity">The quantity type.</param>
85+
/// <returns>Unit enum values.</returns>
86+
public static IEnumerable<string> GetUnitNamesForQuantity(QuantityType quantity)
87+
{
88+
return GetUnitEnumValuesForQuantity(quantity).Select(unitEnumValue => unitEnumValue.ToString());
89+
}
90+
91+
/// <summary>
92+
/// Returns the enum type for the given <paramref name="quantity"/>.
93+
/// </summary>
94+
/// <example>
95+
/// Given <see cref="QuantityType.Length"/> it will return <see cref="LengthUnit"/>.
96+
/// </example>
97+
/// <param name="quantity"></param>
98+
/// <returns></returns>
99+
public static Type GetUnitType(QuantityType quantity)
100+
{
101+
return UnitEnumTypes
102+
.First(t => t.FullName == $"{UnitEnumNamespace}.{quantity}Unit");
103+
}
104+
}
105+
}

0 commit comments

Comments
 (0)