Skip to content

Commit 7a80b83

Browse files
committed
tests: Add tests on ToString() and conversions
1 parent 1f83b5c commit 7a80b83

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

UnitsNet.Tests/QuantityTests.Ctor.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,24 @@ public void DefaultCtorOfRepresentativeTypes_SetsValueToZeroAndUnitToBaseUnit()
4343
Assert.Equal(LevelUnit.Decibel, new Level().Unit);
4444
}
4545

46+
/// <summary>
47+
/// This test is a bit misplaced, but was added because when working on #389 unit+value there were two
48+
/// ways to implement this; either assume BaseUnit of unit is not specified or throw if quantity did not have unit explicitly set.
49+
/// Struct types do not allow custom default ctor implementations, so that exception would then be thrown when trying to convert.
50+
/// </summary>
51+
[Fact]
52+
public void DefaultCtorOfRepresentativeTypes_DoesNotThrowWhenConvertingToOtherUnits()
53+
{
54+
// double types
55+
Assert.Equal(0, new Mass().Hectograms);
56+
57+
// decimal types
58+
Assert.Equal(0, new Information().Kibibits);
59+
60+
// logarithmic types
61+
Assert.Equal(0, new Level().Nepers);
62+
}
63+
4664
[Fact]
4765
public void CtorWithOnlyValueOfRepresentativeTypes_SetsValueToGivenValueAndUnitToBaseUnit()
4866
{

UnitsNet.Tests/QuantityTests.ToString.cs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,55 @@ public partial class QuantityTests
2929
{
3030
public class ToString
3131
{
32+
[Fact]
33+
public void CreatedByDefaultCtor_ReturnsValueInBaseUnit()
34+
{
35+
// double types
36+
Assert.Equal("0 kg", new Mass().ToString());
37+
38+
// decimal types
39+
Assert.Equal("0 b", new Information().ToString());
40+
41+
// logarithmic types
42+
Assert.Equal("0 dB", new Level().ToString());
43+
}
44+
45+
[Fact]
46+
public void CreatedByCtorWithValue_ReturnsValueInBaseUnit()
47+
{
48+
#pragma warning disable 618
49+
// double types
50+
Assert.Equal("5 kg", new Mass(5L).ToString());
51+
Assert.Equal("5 kg", new Mass(5d).ToString());
52+
Assert.Equal("5 kg", new Mass(5m).ToString());
53+
54+
// decimal types
55+
Assert.Equal("5 b", new Information(5L).ToString());
56+
Assert.Equal("5 b", new Information(5d).ToString());
57+
Assert.Equal("5 b", new Information(5m).ToString());
58+
59+
// logarithmic types
60+
Assert.Equal("5 dB", new Level(5L).ToString());
61+
Assert.Equal("5 dB", new Level(5d).ToString());
62+
Assert.Equal("5 dB", new Level(5m).ToString());
63+
#pragma warning restore 618
64+
}
65+
66+
[Fact]
67+
public void CreatedByCtorWithValueAndUnit_ReturnsValueAndUnit()
68+
{
69+
#pragma warning disable 618
70+
// double types
71+
Assert.Equal("50 hg", new Mass(5).ToString(MassUnit.Hectogram));
72+
73+
// decimal types
74+
Assert.Equal("1 B", new Information(8).ToString(InformationUnit.Byte));
75+
76+
// logarithmic types
77+
Assert.Equal("0.58 Np", new Level(5).ToString(LevelUnit.Neper));
78+
#pragma warning restore 618
79+
}
80+
3281
[Fact]
3382
public void ReturnsTheOriginalValueAndUnit()
3483
{

0 commit comments

Comments
 (0)