Skip to content

Commit e2033b3

Browse files
committed
tests: Add tests on ctors
1 parent ec0221d commit e2033b3

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
using System.Diagnostics.CodeAnalysis;
2+
using JetBrains.Annotations;
3+
using UnitsNet.Units;
4+
using Xunit;
5+
6+
namespace UnitsNet.Tests
7+
{
8+
[UsedImplicitly]
9+
public partial class QuantityTests
10+
{
11+
/// <summary>
12+
/// Tests constructors of quantity types.
13+
/// </summary>
14+
public class Ctor
15+
{
16+
[Fact]
17+
[SuppressMessage("ReSharper", "ObjectCreationAsStatement", Justification = "Only testing the ctor itself, not the resulting value.")]
18+
public void DefaultCtorOfRepresentativeTypes_DoesNotThrow()
19+
{
20+
// double types
21+
new Length();
22+
23+
// decimal types
24+
new Information();
25+
26+
// logarithmic types
27+
new Level();
28+
}
29+
30+
[Fact]
31+
public void DefaultCtorOfRepresentativeTypes_SetsValueToZeroAndUnitToBaseUnit()
32+
{
33+
// double types
34+
Assert.Equal(0, new Mass().Value);
35+
Assert.Equal(MassUnit.Kilogram, new Mass().Unit);
36+
37+
// decimal types
38+
Assert.Equal(0, new Information().Value);
39+
Assert.Equal(InformationUnit.Bit, new Information().Unit);
40+
41+
// logarithmic types
42+
Assert.Equal(0, new Level().Value);
43+
Assert.Equal(LevelUnit.Decibel, new Level().Unit);
44+
}
45+
46+
[Fact]
47+
public void CtorWithOnlyValueOfRepresentativeTypes_SetsValueToGivenValueAndUnitToBaseUnit()
48+
{
49+
#pragma warning disable 618
50+
// double types
51+
Assert.Equal(5, new Mass(5L).Value);
52+
Assert.Equal(5, new Mass(5d).Value);
53+
Assert.Equal(5, new Mass(5m).Value);
54+
Assert.Equal(MassUnit.Kilogram, new Mass(5L).Unit);
55+
Assert.Equal(MassUnit.Kilogram, new Mass(5d).Unit);
56+
Assert.Equal(MassUnit.Kilogram, new Mass(5m).Unit);
57+
58+
// decimal types
59+
Assert.Equal(5, new Information(5L).Value);
60+
Assert.Equal(5, new Information(5d).Value);
61+
Assert.Equal(5, new Information(5m).Value);
62+
Assert.Equal(InformationUnit.Bit, new Information(5L).Unit);
63+
Assert.Equal(InformationUnit.Bit, new Information(5d).Unit);
64+
Assert.Equal(InformationUnit.Bit, new Information(5m).Unit);
65+
66+
// logarithmic types
67+
Assert.Equal(5, new Level(5L).Value);
68+
Assert.Equal(5, new Level(5d).Value);
69+
Assert.Equal(5, new Level(5m).Value);
70+
Assert.Equal(LevelUnit.Decibel, new Level(5L).Unit);
71+
Assert.Equal(LevelUnit.Decibel, new Level(5d).Unit);
72+
Assert.Equal(LevelUnit.Decibel, new Level(5m).Unit);
73+
#pragma warning restore 618
74+
}
75+
76+
[Fact]
77+
public void CtorWithValueAndUnitOfRepresentativeTypes_SetsValueAndUnit()
78+
{
79+
// double types
80+
var mass = new Mass(5L, MassUnit.Centigram);
81+
Assert.Equal(5, mass.Value);
82+
Assert.Equal(MassUnit.Centigram, mass.Unit);
83+
84+
// decimal types
85+
var information = new Information(5, InformationUnit.Kibibit);
86+
Assert.Equal(5, information.Value);
87+
Assert.Equal(InformationUnit.Kibibit, information.Unit);
88+
89+
// logarithmic types
90+
var level = new Level(5, LevelUnit.Neper);
91+
Assert.Equal(5, level.Value);
92+
Assert.Equal(LevelUnit.Neper, level.Unit);
93+
}
94+
}
95+
}
96+
}

UnitsNet.Tests/UnitsNet.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
</ItemGroup>
1414

1515
<ItemGroup>
16+
<PackageReference Include="JetBrains.Annotations" Version="11.1.0" />
1617
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0" />
1718
<PackageReference Include="xunit" Version="2.3.0-beta4-build3742" />
1819
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.0-beta4-build3742" />

0 commit comments

Comments
 (0)