Skip to content

Commit b7725ed

Browse files
committed
Add tests
1 parent eb84751 commit b7725ed

File tree

2 files changed

+127
-3
lines changed

2 files changed

+127
-3
lines changed

UnitsNet.Tests/QuantityInfoTest.cs

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
// Copyright (c) 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com).
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.Diagnostics.CodeAnalysis;
24+
using UnitsNet.Units;
25+
using Xunit;
26+
27+
namespace UnitsNet.Tests
28+
{
29+
public class QuantityInfoTest
30+
{
31+
[Fact]
32+
public void Constructor_AssignsProperties()
33+
{
34+
var expectedZero = Length.FromCentimeters(10);
35+
var expectedUnits = new Enum[] {LengthUnit.Centimeter, LengthUnit.Kilometer};
36+
var expectedQuantityType = QuantityType.Length;
37+
38+
var info = new QuantityInfo(expectedQuantityType, expectedUnits, expectedZero);
39+
40+
Assert.Equal(expectedZero, info.Zero);
41+
Assert.Equal("Length", info.Name);
42+
Assert.Equal(expectedUnits, info.Units);
43+
Assert.Equal(new[]{"Centimeter", "Kilometer"}, info.UnitNames);
44+
Assert.Equal(expectedQuantityType, info.QuantityType);
45+
}
46+
47+
[Fact]
48+
public void GenericsConstructor_AssignsProperties()
49+
{
50+
var expectedZero = Length.FromCentimeters(10);
51+
var expectedUnits = new[] {LengthUnit.Centimeter, LengthUnit.Kilometer};
52+
var expectedQuantityType = QuantityType.Length;
53+
54+
var info = new QuantityInfo<LengthUnit>(expectedQuantityType, expectedUnits, expectedZero);
55+
56+
Assert.Equal(expectedZero, info.Zero);
57+
Assert.Equal("Length", info.Name);
58+
Assert.Equal(expectedUnits, info.Units);
59+
Assert.Equal(new[]{"Centimeter", "Kilometer"}, info.UnitNames);
60+
Assert.Equal(expectedQuantityType, info.QuantityType);
61+
}
62+
63+
[Fact]
64+
[SuppressMessage("ReSharper", "AssignNullToNotNullAttribute")]
65+
public void Constructor_GivenNullAsUnits_ThrowsArgumentNullException()
66+
{
67+
Assert.Throws<ArgumentNullException>(() => new QuantityInfo(QuantityType.Length, null, null));
68+
}
69+
70+
[Fact]
71+
[SuppressMessage("ReSharper", "AssignNullToNotNullAttribute")]
72+
public void GenericsConstructor_GivenNullAsUnits_ThrowsArgumentNullException()
73+
{
74+
Assert.Throws<ArgumentNullException>(() => new QuantityInfo<LengthUnit>(QuantityType.Length, null, null));
75+
}
76+
77+
[Fact]
78+
[SuppressMessage("ReSharper", "AssignNullToNotNullAttribute")]
79+
public void Constructor_GivenNullAsZero_ThrowsArgumentNullException()
80+
{
81+
Assert.Throws<ArgumentNullException>(() => new QuantityInfo(QuantityType.Length, new Enum[0], null));
82+
}
83+
84+
[Fact]
85+
[SuppressMessage("ReSharper", "AssignNullToNotNullAttribute")]
86+
public void GenericsConstructor_GivenNullAsZero_ThrowsArgumentNullException()
87+
{
88+
Assert.Throws<ArgumentNullException>(() => new QuantityInfo<LengthUnit>(QuantityType.Length, new LengthUnit[0], null));
89+
}
90+
}
91+
}

UnitsNet.Tests/QuantityTests.cs

+36-3
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
// Copyright (c) 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com).
22
// https://github.com/angularsen/UnitsNet
3-
//
3+
//
44
// Permission is hereby granted, free of charge, to any person obtaining a copy
55
// of this software and associated documentation files (the "Software"), to deal
66
// in the Software without restriction, including without limitation the rights
77
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
88
// copies of the Software, and to permit persons to whom the Software is
99
// furnished to do so, subject to the following conditions:
10-
//
10+
//
1111
// The above copyright notice and this permission notice shall be included in
1212
// all copies or substantial portions of the Software.
13-
//
13+
//
1414
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1515
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1616
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
@@ -19,6 +19,8 @@
1919
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2020
// THE SOFTWARE.
2121

22+
using System;
23+
using System.Linq;
2224
using UnitsNet.Units;
2325
using Xunit;
2426

@@ -34,5 +36,36 @@ public void GetHashCodeForDifferentQuantitiesWithSameValuesAreNotEqual()
3436

3537
Assert.NotEqual( length.GetHashCode(), area.GetHashCode() );
3638
}
39+
40+
[Fact]
41+
public void Length_QuantityInfo_ReturnsQuantityInfoAboutLength()
42+
{
43+
var length = new Length(1, LengthUnit.Centimeter);
44+
45+
QuantityInfo<LengthUnit> quantityInfo = length.QuantityInfo;
46+
47+
AssertQuantityInfoRepresentsLength(quantityInfo);
48+
}
49+
50+
[Fact]
51+
public void Length_Info_ReturnsQuantityInfoAboutLength()
52+
{
53+
QuantityInfo<LengthUnit> quantityInfo = Length.Info;
54+
55+
AssertQuantityInfoRepresentsLength(quantityInfo);
56+
}
57+
58+
private static void AssertQuantityInfoRepresentsLength(QuantityInfo<LengthUnit> quantityInfo)
59+
{
60+
Assert.Equal(Length.Zero, quantityInfo.Zero);
61+
Assert.Equal("Length", quantityInfo.Name);
62+
63+
var lengthUnits = EnumUtils.GetEnumValues<LengthUnit>().Except(new[] {LengthUnit.Undefined}).ToArray();
64+
Assert.Equal(lengthUnits, quantityInfo.Units);
65+
Assert.Equal(QuantityType.Length, quantityInfo.QuantityType);
66+
67+
var lengthUnitNames = lengthUnits.Select(x => x.ToString());
68+
Assert.Equal(lengthUnitNames, quantityInfo.UnitNames);
69+
}
3770
}
3871
}

0 commit comments

Comments
 (0)