OSDN Git Service

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