OSDN Git Service

PostClass.CreatedAtの型を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 System.Globalization;
24 using Xunit;
25
26 namespace OpenTween
27 {
28     public class DateTimeUtcTest
29     {
30         [Fact]
31         public void Constructor_DateTest()
32         {
33             var utc = new DateTimeUtc(2018, 5, 6);
34
35             Assert.Equal(new DateTime(2018, 5, 6, 0, 0, 0, 0, DateTimeKind.Utc),
36                 utc.ToDateTimeUnsafe());
37         }
38
39         [Fact]
40         public void Constructor_DateAndTimeTest()
41         {
42             var utc = new DateTimeUtc(2018, 5, 6, 11, 22, 33);
43
44             Assert.Equal(new DateTime(2018, 5, 6, 11, 22, 33, 0, DateTimeKind.Utc),
45                 utc.ToDateTimeUnsafe());
46         }
47
48         [Fact]
49         public void Constructor_DateAndTimeMillisecondsTest()
50         {
51             var utc = new DateTimeUtc(2018, 5, 6, 11, 22, 33, 456);
52
53             Assert.Equal(new DateTime(2018, 5, 6, 11, 22, 33, 456, DateTimeKind.Utc),
54                 utc.ToDateTimeUnsafe());
55         }
56
57         [Fact]
58         public void Constructor_DateTimeOffsetTest()
59         {
60             var datetimeOffset = new DateTimeOffset(2018, 5, 6, 11, 22, 33, 456, TimeSpan.FromHours(9));
61             var utc = new DateTimeUtc(datetimeOffset);
62
63             Assert.Equal(new DateTime(2018, 5, 6, 2, 22, 33, 456, DateTimeKind.Utc),
64                 utc.ToDateTimeUnsafe());
65         }
66
67         [Fact]
68         public void Constructor_DateTimeTest()
69         {
70             var datetime = new DateTime(2018, 5, 6, 11, 22, 33, DateTimeKind.Utc);
71             var utc = new DateTimeUtc(datetime);
72
73             Assert.Equal(datetime, utc.ToDateTimeUnsafe());
74         }
75
76         [Fact]
77         public void Constructor_LocalDateTimeTest()
78         {
79             Assert.Throws<ArgumentException>(
80                 () => new DateTimeUtc(new DateTime(2018, 5, 6, 12, 0, 0, DateTimeKind.Local)));
81
82             Assert.Throws<ArgumentException>(
83                 () => new DateTimeUtc(new DateTime(2018, 5, 6, 12, 0, 0, DateTimeKind.Unspecified)));
84         }
85
86         [Fact]
87         public void ToUnixTime_Test()
88         {
89             var utc = new DateTimeUtc(2009, 2, 13, 23, 31, 30, 0);
90
91             Assert.Equal(1234567890, utc.ToUnixTime());
92         }
93
94         [Fact]
95         public void ToDateTimeOffset()
96         {
97             var utc = new DateTimeUtc(2018, 5, 6, 11, 22, 33, 111);
98
99             Assert.Equal(new DateTimeOffset(2018, 5, 6, 11, 22, 33, 111, TimeSpan.Zero),
100                 utc.ToDateTimeOffset());
101         }
102
103         [Fact]
104         public void ToLocalTime()
105         {
106             var utc = new DateTimeUtc(2018, 5, 6, 11, 22, 33, 111);
107             var expected = new DateTimeOffset(2018, 5, 6, 11, 22, 33, 111, TimeSpan.Zero).ToLocalTime();
108
109             Assert.Equal(expected, utc.ToLocalTime());
110         }
111
112         [Fact]
113         public void CompareTo_Test()
114         {
115             var utc1 = new DateTimeUtc(2018, 5, 6, 11, 22, 33, 111);
116             var utc2 = new DateTimeUtc(2018, 5, 6, 11, 22, 33, 111);
117             var utc3 = new DateTimeUtc(2018, 5, 6, 11, 22, 33, 222);
118
119             Assert.Equal(0, utc1.CompareTo(utc2));
120             Assert.True(utc1.CompareTo(utc3) < 0);
121             Assert.True(utc3.CompareTo(utc1) > 0);
122         }
123
124         [Fact]
125         public void Equals_Test()
126         {
127             var utc1 = new DateTimeUtc(2018, 5, 6, 11, 22, 33, 111);
128             var utc2 = new DateTimeUtc(2018, 5, 6, 11, 22, 33, 111);
129             var utc3 = new DateTimeUtc(2018, 5, 6, 11, 22, 33, 222);
130
131             Assert.True(utc1.Equals(utc2));
132             Assert.True(utc1.Equals((object)utc2));
133
134             Assert.False(utc1.Equals(utc3));
135             Assert.False(utc1.Equals((object)utc3));
136         }
137
138         [Fact]
139         public void GetHashCode_Test()
140         {
141             var utc1 = new DateTimeUtc(2018, 5, 6, 11, 22, 33, 111);
142             var utc2 = new DateTimeUtc(2018, 5, 6, 11, 22, 33, 111);
143             var utc3 = new DateTimeUtc(2018, 5, 6, 11, 22, 33, 222);
144
145             Assert.Equal(utc1.GetHashCode(), utc2.GetHashCode());
146             Assert.NotEqual(utc1.GetHashCode(), utc3.GetHashCode());
147         }
148
149         [Fact]
150         public void ToString_Test()
151         {
152             var datetime = new DateTime(2018, 5, 6, 11, 22, 33, 111, DateTimeKind.Utc);
153             var utc = new DateTimeUtc(datetime);
154
155             Assert.Equal(datetime.ToString(), utc.ToString());
156         }
157
158         [Fact]
159         public void ToString_FormatTest()
160         {
161             var utc = new DateTimeUtc(2018, 5, 6, 11, 22, 33, 111);
162
163             Assert.Equal("2018-05-06 11:22:33.111 +00:00", utc.ToString("yyyy-MM-dd HH:mm:ss.fff zzz"));
164         }
165
166         [Fact]
167         public void ToLocalTimeString_Test()
168         {
169             var datetime = new DateTime(2018, 5, 6, 11, 22, 33, 111, DateTimeKind.Local);
170             var utc = new DateTimeUtc(datetime.ToUniversalTime());
171
172             Assert.Equal(datetime.ToString(), utc.ToLocalTimeString());
173         }
174
175         [Fact]
176         public void ToLocalTimeString_FormatTest()
177         {
178             var localDatetime = new DateTime(2018, 5, 6, 11, 22, 33, 111, DateTimeKind.Local);
179             var utc = new DateTimeUtc(localDatetime.ToUniversalTime());
180
181             Assert.Equal(localDatetime.ToString("O"), utc.ToLocalTimeString("O"));
182         }
183
184         [Fact]
185         public void OperatorPlus_Test()
186         {
187             var utc = new DateTimeUtc(2018, 5, 6, 11, 22, 33, 111);
188             var diff = TimeSpan.FromDays(1);
189
190             Assert.Equal(new DateTime(2018, 5, 7, 11, 22, 33, 111, DateTimeKind.Utc),
191                 (utc + diff).ToDateTimeUnsafe());
192         }
193
194         [Fact]
195         public void OperatorMinus_TimeSpanTest()
196         {
197             var utc = new DateTimeUtc(2018, 5, 6, 11, 22, 33, 111);
198             var diff = TimeSpan.FromDays(1);
199
200             Assert.Equal(new DateTime(2018, 5, 5, 11, 22, 33, 111, DateTimeKind.Utc),
201                 (utc - diff).ToDateTimeUnsafe());
202         }
203
204         [Fact]
205         public void OperatorMinus_DateTimeTest()
206         {
207             var utc1 = new DateTimeUtc(2018, 5, 6, 11, 22, 33, 111);
208             var utc2 = new DateTimeUtc(2018, 5, 7, 11, 22, 33, 111);
209
210             Assert.Equal(TimeSpan.Zero, utc1 - utc1);
211             Assert.Equal(TimeSpan.FromDays(-1), utc1 - utc2);
212             Assert.Equal(TimeSpan.FromDays(1), utc2 - utc1);
213         }
214
215         [Fact]
216         public void OperatorEqual_Test()
217         {
218             var utc1 = new DateTimeUtc(2018, 5, 6, 11, 22, 33, 111);
219             var utc2 = new DateTimeUtc(2018, 5, 6, 11, 22, 33, 222);
220
221 #pragma warning disable CS1718
222             Assert.True(utc1 == utc1);
223             Assert.False(utc1 == utc2);
224
225             Assert.False(utc1 != utc1);
226             Assert.True(utc1 != utc2);
227 #pragma warning restore CS1718
228         }
229
230         [Fact]
231         public void OperatorCompare_Test()
232         {
233             var utc1 = new DateTimeUtc(2018, 5, 6, 11, 22, 33, 111);
234             var utc2 = new DateTimeUtc(2018, 5, 6, 11, 22, 33, 222);
235
236 #pragma warning disable CS1718
237             Assert.False(utc1 < utc1);
238             Assert.True(utc1 < utc2);
239             Assert.False(utc2 < utc1);
240
241             Assert.True(utc1 <= utc1);
242             Assert.True(utc1 <= utc2);
243             Assert.False(utc2 <= utc1);
244
245             Assert.False(utc1 > utc1);
246             Assert.False(utc1 > utc2);
247             Assert.True(utc2 > utc1);
248
249             Assert.True(utc1 >= utc1);
250             Assert.False(utc1 >= utc2);
251             Assert.True(utc2 >= utc1);
252 #pragma warning restore CS1718
253         }
254
255         [Fact]
256         public void MinValue_Test()
257             => Assert.Equal(DateTime.MinValue.Ticks, DateTimeUtc.MinValue.ToDateTimeUnsafe().Ticks);
258
259         [Fact]
260         public void MaxValue_Test()
261             => Assert.Equal(DateTime.MaxValue.Ticks, DateTimeUtc.MaxValue.ToDateTimeUnsafe().Ticks);
262
263         [Fact]
264         public void FromUnixTime_Test()
265         {
266             var utc = DateTimeUtc.FromUnixTime(1234567890);
267
268             Assert.Equal(new DateTime(2009, 2, 13, 23, 31, 30, 0, DateTimeKind.Utc),
269                 utc.ToDateTimeUnsafe());
270         }
271
272         public static TheoryData<string, DateTimeUtc> Parse_Test_Fixtures = new TheoryData<string, DateTimeUtc>
273         {
274             { "2018-05-06T11:22:33.111", new DateTimeUtc(2018, 5, 6, 11, 22, 33, 111) },
275             { "2018-05-06T11:22:33.111+00:00", new DateTimeUtc(2018, 5, 6, 11, 22, 33, 111) },
276             { "2018-05-06T11:22:33.111+09:00", new DateTimeUtc(2018, 5, 6, 2, 22, 33, 111) },
277         };
278
279         [Theory]
280         [MemberData(nameof(Parse_Test_Fixtures))]
281         public void Parse_Test(string input, DateTimeUtc expected)
282             => Assert.Equal(expected, DateTimeUtc.Parse(input, DateTimeFormatInfo.InvariantInfo));
283
284         [Fact]
285         public void Parse_ErrorTest()
286             => Assert.Throws<FormatException>(() => DateTimeUtc.Parse("### INVALID ###", DateTimeFormatInfo.InvariantInfo));
287
288         public static TheoryData<string, bool, DateTimeUtc> TryParse_Test_Fixtures = new TheoryData<string, bool, DateTimeUtc>
289         {
290             { "2018-05-06T11:22:33.111", true, new DateTimeUtc(2018, 5, 6, 11, 22, 33, 111) },
291             { "2018-05-06T11:22:33.111+00:00", true, new DateTimeUtc(2018, 5, 6, 11, 22, 33, 111) },
292             { "2018-05-06T11:22:33.111+09:00", true, new DateTimeUtc(2018, 5, 6, 2, 22, 33, 111) },
293             { "### INVALID ###", false, DateTimeUtc.MinValue },
294         };
295
296         [Theory]
297         [MemberData(nameof(TryParse_Test_Fixtures))]
298         public void TryParse_Test(string input, bool expectedParsed, DateTimeUtc expectedResult)
299         {
300             var parsed = DateTimeUtc.TryParse(input, DateTimeFormatInfo.InvariantInfo, out var result);
301
302             Assert.Equal(expectedParsed, parsed);
303             Assert.Equal(expectedResult, result);
304         }
305     }
306 }