19
19
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20
20
// THE SOFTWARE.
21
21
22
+ using NUnit . Framework ;
23
+ using System ;
24
+
22
25
namespace UnitsNet . Tests . CustomCode
23
26
{
24
27
public class DurationTests : DurationTestsBase
@@ -42,5 +45,71 @@ public class DurationTests : DurationTestsBase
42
45
protected override double WeeksInOneSecond => 1.6534e-6 ;
43
46
44
47
protected override double YearsInOneSecond => 3.1689e-8 ;
48
+
49
+ [ Test ]
50
+ public static void ToTimeSpanShouldThrowExceptionOnValuesLargerThanTimeSpanMax ( )
51
+ {
52
+ Duration duration = Duration . FromSeconds ( TimeSpan . MaxValue . TotalSeconds + 1 ) ;
53
+ Assert . Throws < ArgumentOutOfRangeException > ( ( ) => duration . ToTimeSpan ( ) ) ;
54
+ }
55
+
56
+ [ Test ]
57
+ public static void ToTimeSpanShouldThrowExceptionOnValuesSmallerThanTimeSpanMin ( )
58
+ {
59
+ Duration duration = Duration . FromSeconds ( TimeSpan . MinValue . TotalSeconds - 1 ) ;
60
+ Assert . Throws < ArgumentOutOfRangeException > ( ( ) => duration . ToTimeSpan ( ) ) ;
61
+ }
62
+
63
+ [ Test ]
64
+ public static void ToTimeSpanShouldNotThrowExceptionOnValuesSlightlyLargerThanTimeSpanMin ( )
65
+ {
66
+ Duration duration = Duration . FromSeconds ( TimeSpan . MinValue . TotalSeconds + 1 ) ;
67
+ TimeSpan timeSpan = duration . ToTimeSpan ( ) ;
68
+ Assert . AreEqual ( duration . Seconds , timeSpan . TotalSeconds , 1e-3 ) ;
69
+ }
70
+
71
+ [ Test ]
72
+ public static void ToTimeSpanShouldNotThrowExceptionOnValuesSlightlySmallerThanTimeSpanMax ( )
73
+ {
74
+ Duration duration = Duration . FromSeconds ( TimeSpan . MaxValue . TotalSeconds - 1 ) ;
75
+ TimeSpan timeSpan = duration . ToTimeSpan ( ) ;
76
+ Assert . AreEqual ( duration . Seconds , timeSpan . TotalSeconds , 1e-3 ) ;
77
+ }
78
+
79
+ [ Test ]
80
+ public static void ExplicitCastToTimeSpanShouldReturnSameValue ( )
81
+ {
82
+ Duration duration = Duration . FromSeconds ( 60 ) ;
83
+ TimeSpan timeSpan = ( TimeSpan ) duration ;
84
+ Assert . AreEqual ( duration . Seconds , timeSpan . TotalSeconds , 1e-10 ) ;
85
+ }
86
+
87
+ [ Test ]
88
+ public static void ExplicitCastToDurationShouldReturnSameValue ( )
89
+ {
90
+ TimeSpan timeSpan = TimeSpan . FromSeconds ( 60 ) ;
91
+ Duration duration = ( Duration ) timeSpan ;
92
+ Assert . AreEqual ( timeSpan . TotalSeconds , duration . Seconds , 1e-10 ) ;
93
+ }
94
+
95
+ [ Test ]
96
+ public static void DateTimePlusDurationReturnsDateTime ( )
97
+ {
98
+ DateTime dateTime = new DateTime ( 2016 , 1 , 1 ) ;
99
+ Duration oneDay = Duration . FromDays ( 1 ) ;
100
+ DateTime result = dateTime + oneDay ;
101
+ DateTime expected = new DateTime ( 2016 , 1 , 2 ) ;
102
+ Assert . AreEqual ( expected , result ) ;
103
+ }
104
+
105
+ [ Test ]
106
+ public static void DateTimeMinusDurationReturnsDateTime ( )
107
+ {
108
+ DateTime dateTime = new DateTime ( 2016 , 1 , 2 ) ;
109
+ Duration oneDay = Duration . FromDays ( 1 ) ;
110
+ DateTime result = dateTime - oneDay ;
111
+ DateTime expected = new DateTime ( 2016 , 1 , 1 ) ;
112
+ Assert . AreEqual ( expected , result ) ;
113
+ }
45
114
}
46
115
}
0 commit comments