OSDN Git Service

不要な ListView.Update の呼び出しを削除
[opentween/open-tween.git] / OpenTween.Tests / MyCommonTest.cs
1 // OpenTween - Client of Twitter
2 // Copyright (c) 2012 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.Collections.Generic;
24 using System.IO;
25 using System.Linq;
26 using System.Reflection;
27 using System.Runtime.InteropServices;
28 using System.Runtime.Serialization;
29 using System.Text;
30 using System.Windows.Forms;
31 using Moq;
32 using OpenTween;
33 using OpenTween.Models;
34 using Xunit;
35 using Xunit.Extensions;
36
37 namespace OpenTween
38 {
39     public class MyCommonTest
40     {
41         [Theory]
42         [InlineData("http://日本語.idn.icann.org/", "http://xn--wgv71a119e.idn.icann.org/")]
43         [InlineData("http://例え.テスト/", "http://xn--r8jz45g.xn--zckzah/")]
44         public void IDNEncodeTest(string uri, string expected)
45             => Assert.Equal(expected, MyCommon.IDNEncode(uri));
46
47         [Theory]
48         [InlineData("http://xn--wgv71a119e.idn.icann.org/", "http://日本語.idn.icann.org/")]
49         [InlineData("http://xn--r8jz45g.xn--zckzah/", "http://例え.テスト/")]
50         [InlineData("http://xn--a/", "http://xn--a/")] // 不正なpunycode
51         public void IDNDecodeTest(string uri, string expected)
52             => Assert.Equal(expected, MyCommon.IDNDecode(uri));
53
54         [Theory]
55         [InlineData("http://xn--r8jz45g.xn--zckzah/", "http://例え.テスト/")]
56         [InlineData("http://ja.wikipedia.org/wiki/%3F", "http://ja.wikipedia.org/wiki/%3F")] // "?" に変換しない
57         [InlineData("http://ja.wikipedia.org/wiki/%E3%83%9E%E3%82%B8LOVE1000%25",
58             "http://ja.wikipedia.org/wiki/マジLOVE1000%25")] // "%" も変換しない
59         [InlineData("http://xn--a/%E3%81%82", "http://xn--a/あ")] // 不正なpunycode
60         [InlineData("http://example..com/", "http://example..com/")] // 不正なURL
61         [InlineData("http://example.com/%E3%81%82%FF", "http://example.com/あ%FF")] // 不正なUTF-8シーケンス
62         [InlineData("http://example.com/%E3%81%82%ED%A0%80", "http://example.com/あ%ED%A0%80")] // 不正なUTF-8シーケンス (high surrogate)
63         public void ConvertToReadableUrl(string url, string expected)
64             => Assert.Equal(expected, MyCommon.ConvertToReadableUrl(url));
65
66         [Theory]
67         [InlineData(new int[] { 1, 2, 3, 4 }, 0, 3, new int[] { 2, 3, 4, 1 })] // 左ローテイト?
68         [InlineData(new int[] { 1, 2, 3, 4 }, 3, 0, new int[] { 4, 1, 2, 3 })] // 右ローテイト?
69         [InlineData(new int[] { 1, 2, 3, 4, 5 }, 1, 3, new int[] { 1, 3, 4, 2, 5 })]
70         [InlineData(new int[] { 1, 2, 3, 4, 5 }, 3, 1, new int[] { 1, 4, 2, 3, 5 })]
71         public void MoveArrayItemTest(int[] values, int idx_fr, int idx_to, int[] expected)
72         {
73             // MoveArrayItem は values を直接変更するため複製を用意する
74             var copy = new int[values.Length];
75             Array.Copy(values, copy, values.Length);
76
77             MyCommon.MoveArrayItem(copy, idx_fr, idx_to);
78             Assert.Equal(expected, copy);
79         }
80
81         [Fact]
82         public void EncryptStringTest()
83         {
84             var str = "hogehoge";
85
86             var crypto = MyCommon.EncryptString(str);
87             Assert.NotEqual(str, crypto);
88
89             var decrypt = MyCommon.DecryptString(crypto);
90             Assert.Equal(str, decrypt);
91         }
92
93         [Theory]
94         [InlineData(new byte[] { 0x01, 0x02 }, 3, new byte[] { 0x01, 0x02, 0x00 })]
95         [InlineData(new byte[] { 0x01, 0x02 }, 2, new byte[] { 0x01, 0x02 })]
96         [InlineData(new byte[] { 0x01, 0x02 }, 1, new byte[] { 0x03 })]
97         public void ResizeBytesArrayTest(byte[] bytes, int size, byte[] expected)
98             => Assert.Equal(expected, MyCommon.ResizeBytesArray(bytes, size));
99
100         [Theory]
101         [InlineData("Resources/re.gif", true)]
102         [InlineData("Resources/re1.gif", false)]
103         [InlineData("Resources/re1.png", false)]
104         public void IsAnimatedGifTest(string filename, bool expected)
105             => Assert.Equal(expected, MyCommon.IsAnimatedGif(filename));
106
107         public static readonly TheoryData<string, DateTimeUtc> DateTimeParse_TestCase = new TheoryData<string, DateTimeUtc>
108         {
109             { "Sun Nov 25 06:10:00 +00:00 2012", new DateTimeUtc(2012, 11, 25, 6, 10, 0) },
110             { "Sun, 25 Nov 2012 06:10:00 +00:00", new DateTimeUtc(2012, 11, 25, 6, 10, 0) },
111         };
112
113         [Theory]
114         [MemberData(nameof(DateTimeParse_TestCase))]
115         public void DateTimeParseTest(string date, DateTimeUtc excepted)
116             => Assert.Equal(excepted, MyCommon.DateTimeParse(date));
117
118         [DataContract]
119         public struct JsonData
120         {
121             [DataMember(Name = "id")] public string Id { get; set; }
122             [DataMember(Name = "body")] public string Body { get; set; }
123         }
124         public static readonly TheoryData<string, JsonData> CreateDataFromJson_TestCase = new TheoryData<string, JsonData>
125         {
126             {
127                 @"{""id"":""1"", ""body"":""hogehoge""}",
128                 new JsonData { Id = "1", Body = "hogehoge" }
129             },
130         };
131
132         [Theory]
133         [MemberData(nameof(CreateDataFromJson_TestCase))]
134         public void CreateDataFromJsonTest<T>(string json, T expected)
135             => Assert.Equal(expected, MyCommon.CreateDataFromJson<T>(json));
136
137         [Theory]
138         [InlineData("hoge123@example.com", true)]
139         [InlineData("hogehoge", false)]
140         [InlineData("foo.bar@example.com", true)]
141         [InlineData("foo..bar@example.com", false)]
142         [InlineData("foobar.@example.com", false)]
143         [InlineData("foo+bar@example.com", true)]
144         public void IsValidEmailTest(string email, bool expected)
145             => Assert.Equal(expected, MyCommon.IsValidEmail(email));
146
147         [Theory]
148         [InlineData(Keys.Shift, new[] { Keys.Shift }, true)]
149         [InlineData(Keys.Shift, new[] { Keys.Control }, false)]
150         [InlineData(Keys.Control | Keys.Alt, new[] { Keys.Control }, true)]
151         [InlineData(Keys.Control | Keys.Alt, new[] { Keys.Alt }, true)]
152         [InlineData(Keys.Control | Keys.Alt, new[] { Keys.Control, Keys.Alt }, true)]
153         [InlineData(Keys.Control | Keys.Alt, new[] { Keys.Shift }, false)]
154         public void IsKeyDownTest(Keys modifierKeys, Keys[] checkKeys, bool expected)
155             => Assert.Equal(expected, MyCommon._IsKeyDown(modifierKeys, checkKeys));
156
157         [Fact]
158         public void GetAssemblyNameTest()
159         {
160             var mockAssembly = new Mock<_Assembly>();
161             mockAssembly.Setup(m => m.GetName()).Returns(new AssemblyName("OpenTween"));
162             MyCommon.EntryAssembly = mockAssembly.Object;
163
164             Assert.Equal("OpenTween", MyCommon.GetAssemblyName());
165         }
166
167         [Theory]
168         [InlineData("", "")]
169         [InlineData("%AppName%", "OpenTween")]
170         [InlineData("%AppName% %AppName%", "OpenTween OpenTween")]
171         public void ReplaceAppNameTest(string str, string excepted)
172             => Assert.Equal(excepted, MyCommon.ReplaceAppName(str, "OpenTween"));
173
174         [Theory]
175         [InlineData("1.0.0.0", "1.0.0")]
176         [InlineData("1.0.0.1", "1.0.1-dev")]
177         [InlineData("1.0.0.12", "1.0.1-dev+build.12")]
178         [InlineData("1.0.1.0", "1.0.1")]
179         [InlineData("1.0.9.1", "1.0.10-dev")]
180         [InlineData("1.1.0.0", "1.1.0")]
181         [InlineData("1.9.9.1", "1.9.10-dev")]
182         public void GetReadableVersionTest(string fileVersion, string expected)
183             => Assert.Equal(expected, MyCommon.GetReadableVersion(fileVersion));
184
185         public static readonly TheoryData<PostClass, string> GetStatusUrlTest1_TestCase = new TheoryData<PostClass, string>
186         {
187             {
188                 new PostClass { StatusId = 249493863826350080L, ScreenName = "Favstar_LM", RetweetedId = null, RetweetedBy = null },
189                 "https://twitter.com/Favstar_LM/status/249493863826350080"
190             },
191             {
192                 new PostClass { StatusId = 216033842434289664L, ScreenName = "haru067", RetweetedId = 200245741443235840L, RetweetedBy = "re4k"},
193                 "https://twitter.com/haru067/status/200245741443235840"
194             },
195         };
196
197         [Theory]
198         [MemberData(nameof(GetStatusUrlTest1_TestCase))]
199         public void GetStatusUrlTest1(PostClass post, string expected)
200             => Assert.Equal(expected, MyCommon.GetStatusUrl(post));
201
202         [Theory]
203         [InlineData("Favstar_LM", 249493863826350080L, "https://twitter.com/Favstar_LM/status/249493863826350080")]
204         [InlineData("haru067", 200245741443235840L, "https://twitter.com/haru067/status/200245741443235840")]
205         public void GetStatusUrlTest2(string screenName, long statusId, string expected)
206             => Assert.Equal(expected, MyCommon.GetStatusUrl(screenName, statusId));
207
208         [Fact]
209         public void GetErrorLogPathTest()
210         {
211             if (Environment.OSVersion.Platform == PlatformID.Win32NT)
212             {
213                 var mockAssembly = new Mock<_Assembly>();
214                 mockAssembly.Setup(m => m.Location).Returns(@"C:\hogehoge\OpenTween\OpenTween.exe");
215                 MyCommon.EntryAssembly = mockAssembly.Object;
216
217                 Assert.Equal(@"C:\hogehoge\OpenTween\ErrorLogs", MyCommon.GetErrorLogPath());
218             }
219             else
220             {
221                 var mockAssembly = new Mock<_Assembly>();
222                 mockAssembly.Setup(m => m.Location).Returns(@"/hogehoge/OpenTween/OpenTween.exe");
223                 MyCommon.EntryAssembly = mockAssembly.Object;
224
225                 Assert.Equal(@"/hogehoge/OpenTween/ErrorLogs", MyCommon.GetErrorLogPath());
226             }
227         }
228
229         [Fact]
230         public void CountUp_Test()
231         {
232             var actual = MyCommon.CountUp(from: 1, to: 5);
233
234             Assert.Equal(new[] { 1, 2, 3, 4, 5 }, actual);
235         }
236
237         [Fact]
238         public void CountUp_FromAndToAreEqualTest()
239         {
240             var actual = MyCommon.CountUp(from: 1, to: 1);
241
242             Assert.Equal(new[] { 1 }, actual);
243         }
244
245         [Fact]
246         public void CountUp_ToIsLessThanFromTest()
247         {
248             var actual = MyCommon.CountUp(from: 1, to: 0);
249
250             Assert.Empty(actual);
251         }
252
253         [Fact]
254         public void CountDown_Test()
255         {
256             var actual = MyCommon.CountDown(from: 5, to: 1);
257
258             Assert.Equal(new[] { 5, 4, 3, 2, 1 }, actual);
259         }
260
261         [Fact]
262         public void CountDown_FromAndToAreEqualTest()
263         {
264             var actual = MyCommon.CountDown(from: 5, to: 5);
265
266             Assert.Equal(new[] { 5 }, actual);
267         }
268
269         [Fact]
270         public void CountDown_ToIsGreaterThanFromTest()
271         {
272             var actual = MyCommon.CountDown(from: 5, to: 6);
273
274             Assert.Empty(actual);
275         }
276
277         [Fact]
278         public void CircularCountUp_Test()
279         {
280             var actual = MyCommon.CircularCountUp(length: 6, startIndex: 3);
281
282             Assert.Equal(new[] { 3, 4, 5, 0, 1, 2 }, actual);
283         }
284
285         [Fact]
286         public void CircularCountUp_StartFromZeroTest()
287         {
288             var actual = MyCommon.CircularCountUp(length: 6, startIndex: 0);
289
290             Assert.Equal(new[] { 0, 1, 2, 3, 4, 5 }, actual);
291         }
292
293         [Fact]
294         public void CircularCountDown_Test()
295         {
296             var actual = MyCommon.CircularCountDown(length: 6, startIndex: 3);
297
298             Assert.Equal(new[] { 3, 2, 1, 0, 5, 4 }, actual);
299         }
300
301         [Fact]
302         public void CircularCountDown_StartFromLastIndexTest()
303         {
304             var actual = MyCommon.CircularCountDown(length: 6, startIndex: 5);
305
306             Assert.Equal(new[] { 5, 4, 3, 2, 1, 0 }, actual);
307         }
308     }
309 }