OSDN Git Service

OAuthUtilityでのタイムスタンプの出力にDateTimeUtcを使用する
[opentween/open-tween.git] / OpenTween.Tests / DateTimeUtcTest.cs
1 // OpenTween - Client of Twitter
2 // Copyright (c) 2018 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
3 // All rights reserved.
4 //
5 // This file is part of OpenTween.
6 //
7 // This program is free software; you can redistribute it and/or modify it
8 // under the terms of the GNU General Public License as published by the Free
9 // Software Foundation; either version 3 of the License, or (at your option)
10 // any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 // for more details.
16 //
17 // You should have received a copy of the GNU General Public License along
18 // with this program. If not, see <http://www.gnu.org/licenses/>, or write to
19 // the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
20 // Boston, MA 02110-1301, USA.
21
22 using System;
23 using Xunit;
24
25 namespace OpenTween
26 {
27     public class DateTimeUtcTest
28     {
29         [Fact]
30         public void Constructor_DateTest()
31         {
32             var utc = new DateTimeUtc(2018, 5, 6);
33
34             Assert.Equal(new DateTime(2018, 5, 6, 0, 0, 0, 0, DateTimeKind.Utc),
35                 utc.ToDateTimeUnsafe());
36         }
37
38         [Fact]
39         public void Constructor_DateAndTimeTest()
40         {
41             var utc = new DateTimeUtc(2018, 5, 6, 11, 22, 33);
42
43             Assert.Equal(new DateTime(2018, 5, 6, 11, 22, 33, 0, DateTimeKind.Utc),
44                 utc.ToDateTimeUnsafe());
45         }
46
47         [Fact]
48         public void Constructor_DateAndTimeMillisecondsTest()
49         {
50             var utc = new DateTimeUtc(2018, 5, 6, 11, 22, 33, 456);
51
52             Assert.Equal(new DateTime(2018, 5, 6, 11, 22, 33, 456, DateTimeKind.Utc),
53                 utc.ToDateTimeUnsafe());
54         }
55
56         [Fact]
57         public void Constructor_DateTimeOffsetTest()
58         {
59             var datetimeOffset = new DateTimeOffset(2018, 5, 6, 11, 22, 33, 456, TimeSpan.FromHours(9));
60             var utc = new DateTimeUtc(datetimeOffset);
61
62             Assert.Equal(new DateTime(2018, 5, 6, 2, 22, 33, 456, DateTimeKind.Utc),
63                 utc.ToDateTimeUnsafe());
64         }
65
66         [Fact]
67         public void Constructor_DateTimeTest()
68         {
69             var datetime = new DateTime(2018, 5, 6, 11, 22, 33, DateTimeKind.Utc);
70             var utc = new DateTimeUtc(datetime);
71
72             Assert.Equal(datetime, utc.ToDateTimeUnsafe());
73         }
74
75         [Fact]
76         public void Constructor_LocalDateTimeTest()
77         {
78             Assert.Throws<ArgumentException>(
79                 () => new DateTimeUtc(new DateTime(2018, 5, 6, 12, 0, 0, DateTimeKind.Local)));
80
81             Assert.Throws<ArgumentException>(
82                 () => new DateTimeUtc(new DateTime(2018, 5, 6, 12, 0, 0, DateTimeKind.Unspecified)));
83         }
84
85         [Fact]
86         public void ToUnixTime_Test()
87         {
88             var utc = new DateTimeUtc(2009, 2, 13, 23, 31, 30, 0);
89
90             Assert.Equal(1234567890, utc.ToUnixTime());
91         }
92
93         [Fact]
94         public void ToDateTimeOffset()
95         {
96             var utc = new DateTimeUtc(2018, 5, 6, 11, 22, 33, 111);
97
98             Assert.Equal(new DateTimeOffset(2018, 5, 6, 11, 22, 33, 111, TimeSpan.Zero),
99                 utc.ToDateTimeOffset());
100         }
101
102         [Fact]
103         public void CompareTo_Test()
104         {
105             var utc1 = new DateTimeUtc(2018, 5, 6, 11, 22, 33, 111);
106             var utc2 = new DateTimeUtc(2018, 5, 6, 11, 22, 33, 111);
107             var utc3 = new DateTimeUtc(2018, 5, 6, 11, 22, 33, 222);
108
109             Assert.Equal(0, utc1.CompareTo(utc2));
110             Assert.True(utc1.CompareTo(utc3) < 0);
111             Assert.True(utc3.CompareTo(utc1) > 0);
112         }
113
114         [Fact]
115         public void Equals_Test()
116         {
117             var utc1 = new DateTimeUtc(2018, 5, 6, 11, 22, 33, 111);
118             var utc2 = new DateTimeUtc(2018, 5, 6, 11, 22, 33, 111);
119             var utc3 = new DateTimeUtc(2018, 5, 6, 11, 22, 33, 222);
120
121             Assert.True(utc1.Equals(utc2));
122             Assert.True(utc1.Equals((object)utc2));
123
124             Assert.False(utc1.Equals(utc3));
125             Assert.False(utc1.Equals((object)utc3));
126         }
127
128         [Fact]
129         public void GetHashCode_Test()
130         {
131             var utc1 = new DateTimeUtc(2018, 5, 6, 11, 22, 33, 111);
132             var utc2 = new DateTimeUtc(2018, 5, 6, 11, 22, 33, 111);
133             var utc3 = new DateTimeUtc(2018, 5, 6, 11, 22, 33, 222);
134
135             Assert.Equal(utc1.GetHashCode(), utc2.GetHashCode());
136             Assert.NotEqual(utc1.GetHashCode(), utc3.GetHashCode());
137         }
138
139         [Fact]
140         public void OperatorPlus_Test()
141         {
142             var utc = new DateTimeUtc(2018, 5, 6, 11, 22, 33, 111);
143             var diff = TimeSpan.FromDays(1);
144
145             Assert.Equal(new DateTime(2018, 5, 7, 11, 22, 33, 111, DateTimeKind.Utc),
146                 (utc + diff).ToDateTimeUnsafe());
147         }
148
149         [Fact]
150         public void OperatorMinus_TimeSpanTest()
151         {
152             var utc = new DateTimeUtc(2018, 5, 6, 11, 22, 33, 111);
153             var diff = TimeSpan.FromDays(1);
154
155             Assert.Equal(new DateTime(2018, 5, 5, 11, 22, 33, 111, DateTimeKind.Utc),
156                 (utc - diff).ToDateTimeUnsafe());
157         }
158
159         [Fact]
160         public void OperatorMinus_DateTimeTest()
161         {
162             var utc1 = new DateTimeUtc(2018, 5, 6, 11, 22, 33, 111);
163             var utc2 = new DateTimeUtc(2018, 5, 7, 11, 22, 33, 111);
164
165             Assert.Equal(TimeSpan.Zero, utc1 - utc1);
166             Assert.Equal(TimeSpan.FromDays(-1), utc1 - utc2);
167             Assert.Equal(TimeSpan.FromDays(1), utc2 - utc1);
168         }
169
170         [Fact]
171         public void OperatorEqual_Test()
172         {
173             var utc1 = new DateTimeUtc(2018, 5, 6, 11, 22, 33, 111);
174             var utc2 = new DateTimeUtc(2018, 5, 6, 11, 22, 33, 222);
175
176 #pragma warning disable CS1718
177             Assert.True(utc1 == utc1);
178             Assert.False(utc1 == utc2);
179
180             Assert.False(utc1 != utc1);
181             Assert.True(utc1 != utc2);
182 #pragma warning restore CS1718
183         }
184
185         [Fact]
186         public void OperatorCompare_Test()
187         {
188             var utc1 = new DateTimeUtc(2018, 5, 6, 11, 22, 33, 111);
189             var utc2 = new DateTimeUtc(2018, 5, 6, 11, 22, 33, 222);
190
191 #pragma warning disable CS1718
192             Assert.False(utc1 < utc1);
193             Assert.True(utc1 < utc2);
194             Assert.False(utc2 < utc1);
195
196             Assert.True(utc1 <= utc1);
197             Assert.True(utc1 <= utc2);
198             Assert.False(utc2 <= utc1);
199
200             Assert.False(utc1 > utc1);
201             Assert.False(utc1 > utc2);
202             Assert.True(utc2 > utc1);
203
204             Assert.True(utc1 >= utc1);
205             Assert.False(utc1 >= utc2);
206             Assert.True(utc2 >= utc1);
207 #pragma warning restore CS1718
208         }
209
210         [Fact]
211         public void MinValue_Test()
212             => Assert.Equal(DateTime.MinValue.Ticks, DateTimeUtc.MinValue.ToDateTimeUnsafe().Ticks);
213
214         [Fact]
215         public void MaxValue_Test()
216             => Assert.Equal(DateTime.MaxValue.Ticks, DateTimeUtc.MaxValue.ToDateTimeUnsafe().Ticks);
217
218         [Fact]
219         public void FromUnixTime_Test()
220         {
221             var utc = DateTimeUtc.FromUnixTime(1234567890);
222
223             Assert.Equal(new DateTime(2009, 2, 13, 23, 31, 30, 0, DateTimeKind.Utc),
224                 utc.ToDateTimeUnsafe());
225         }
226     }
227 }