OSDN Git Service

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