OSDN Git Service

プロパティ等の並び順を修正, コメントアウトされた不要な記述を除去
[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 NSubstitute;
32 using OpenTween;
33 using Xunit;
34 using Xunit.Extensions;
35
36 namespace OpenTween
37 {
38     public class MyCommonTest
39     {
40         [Theory]
41         [InlineData("http://ja.wikipedia.org/wiki/Wikipedia", "http://ja.wikipedia.org/wiki/Wikipedia")]
42         [InlineData("http://ja.wikipedia.org/wiki/メインページ",
43             "http://ja.wikipedia.org/wiki/%E3%83%A1%E3%82%A4%E3%83%B3%E3%83%9A%E3%83%BC%E3%82%B8")]
44         [InlineData("http://fr.wikipedia.org/wiki/Café", "http://fr.wikipedia.org/wiki/Caf%E9")]
45         [InlineData("http://ja.wikipedia.org/wiki/勇気100%", "http://ja.wikipedia.org/wiki/%E5%8B%87%E6%B0%97100%25")]
46         [InlineData("http://ja.wikipedia.org/wiki/Bio_100%", "http://ja.wikipedia.org/wiki/Bio_100%25")]
47         public void urlEncodeMultibyteCharTest(string uri, string expected)
48         {
49             Assert.Equal(expected, MyCommon.urlEncodeMultibyteChar(uri));
50         }
51
52         [Theory]
53         [InlineData("http://日本語.idn.icann.org/", "http://xn--wgv71a119e.idn.icann.org/")]
54         [InlineData("http://例え.テスト/", "http://xn--r8jz45g.xn--zckzah/")]
55         public void IDNEncodeTest(string uri, string expected)
56         {
57             Assert.Equal(expected, MyCommon.IDNEncode(uri));
58         }
59
60         [Theory]
61         [InlineData("http://xn--wgv71a119e.idn.icann.org/", "http://日本語.idn.icann.org/")]
62         [InlineData("http://xn--r8jz45g.xn--zckzah/", "http://例え.テスト/")]
63         public void IDNDecodeTest(string uri, string expected)
64         {
65             Assert.Equal(expected, MyCommon.IDNDecode(uri));
66         }
67
68         [Theory]
69         [InlineData("http://xn--r8jz45g.xn--zckzah/", "http://例え.テスト/")]
70         [InlineData("http://ja.wikipedia.org/wiki/%3F", "http://ja.wikipedia.org/wiki/%3F")] // "?" に変換しない
71         [InlineData("http://ja.wikipedia.org/wiki/%E3%83%9E%E3%82%B8LOVE1000%25",
72             "http://ja.wikipedia.org/wiki/マジLOVE1000%25")] // "%" も変換しない
73         [InlineData("http://example..com/", "http://example..com/")] // 不正なURL
74         public void ConvertToReadableUrl(string url, string expected)
75         {
76             Assert.Equal(expected, MyCommon.ConvertToReadableUrl(url));
77         }
78
79         [Theory]
80         [InlineData(new int[] { 1, 2, 3, 4 }, 0, 3, new int[] { 2, 3, 4, 1 })] // 左ローテイト?
81         [InlineData(new int[] { 1, 2, 3, 4 }, 3, 0, new int[] { 4, 1, 2, 3 })] // 右ローテイト?
82         [InlineData(new int[] { 1, 2, 3, 4, 5 }, 1, 3, new int[] { 1, 3, 4, 2, 5 })]
83         [InlineData(new int[] { 1, 2, 3, 4, 5 }, 3, 1, new int[] { 1, 4, 2, 3, 5 })]
84         public void MoveArrayItemTest(int[] values, int idx_fr, int idx_to, int[] expected)
85         {
86             // MoveArrayItem は values を直接変更するため複製を用意する
87             var copy = new int[values.Length];
88             Array.Copy(values, copy, values.Length);
89
90             MyCommon.MoveArrayItem(copy, idx_fr, idx_to);
91             Assert.Equal(expected, copy);
92         }
93
94         [Fact]
95         public void EncryptStringTest()
96         {
97             var str = "hogehoge";
98
99             var crypto = MyCommon.EncryptString(str);
100             Assert.NotEqual(str, crypto);
101
102             var decrypt = MyCommon.DecryptString(crypto);
103             Assert.Equal(str, decrypt);
104         }
105
106         [Theory]
107         [InlineData(new byte[] { 0x01, 0x02 }, 3, new byte[] { 0x01, 0x02, 0x00 })]
108         [InlineData(new byte[] { 0x01, 0x02 }, 2, new byte[] { 0x01, 0x02 })]
109         [InlineData(new byte[] { 0x01, 0x02 }, 1, new byte[] { 0x03 })]
110         public void ResizeBytesArrayTest(byte[] bytes, int size, byte[] expected)
111         {
112             Assert.Equal(expected, MyCommon.ResizeBytesArray(bytes, size));
113         }
114
115         [Theory]
116         [InlineData("Resources/re.gif", true)]
117         [InlineData("Resources/re1.gif", false)]
118         [InlineData("Resources/re1.png", false)]
119         public void IsAnimatedGifTest(string filename, bool expected)
120         {
121             Assert.Equal(expected, MyCommon.IsAnimatedGif(filename));
122         }
123
124         public static IEnumerable<object[]> DateTimeParse_TestCase
125         {
126             get
127             {
128                 yield return new object[] {
129                     "Sun Nov 25 06:10:00 +00:00 2012",
130                     new DateTime(2012, 11, 25, 6, 10, 0, DateTimeKind.Utc),
131                 };
132                 yield return new object[] {
133                     "Sun, 25 Nov 2012 06:10:00 +00:00",
134                     new DateTime(2012, 11, 25, 6, 10, 0, DateTimeKind.Utc),
135                 };
136             }
137         }
138
139         [Theory]
140         [PropertyData("DateTimeParse_TestCase")]
141         public void DateTimeParseTest(string date, DateTime excepted)
142         {
143             Assert.Equal(excepted, MyCommon.DateTimeParse(date).ToUniversalTime());
144         }
145
146         [DataContract]
147         public struct JsonData
148         {
149             [DataMember(Name = "id")] public string Id { get; set; }
150             [DataMember(Name = "body")] public string Body { get; set; }
151         }
152         public static IEnumerable<object[]> CreateDataFromJson_TestCase
153         {
154             get
155             {
156                 yield return new object[] {
157                     @"{""id"":""1"", ""body"":""hogehoge""}",
158                     new JsonData { Id = "1", Body = "hogehoge" },
159                 };
160             }
161         }
162
163         [Theory]
164         [PropertyData("CreateDataFromJson_TestCase")]
165         public void CreateDataFromJsonTest<T>(string json, T expected)
166         {
167             Assert.Equal(expected, MyCommon.CreateDataFromJson<T>(json));
168         }
169
170         [Theory]
171         [InlineData("hoge123@example.com", true)]
172         [InlineData("hogehoge", false)]
173         [InlineData("foo.bar@example.com", true)]
174         [InlineData("foo..bar@example.com", false)]
175         [InlineData("foobar.@example.com", false)]
176         [InlineData("foo+bar@example.com", true)]
177         public void IsValidEmailTest(string email, bool expected)
178         {
179             Assert.Equal(expected, MyCommon.IsValidEmail(email));
180         }
181
182         [Theory]
183         [InlineData(Keys.Shift, new[] { Keys.Shift }, true)]
184         [InlineData(Keys.Shift, new[] { Keys.Control }, false)]
185         [InlineData(Keys.Control | Keys.Alt, new[] { Keys.Control }, true)]
186         [InlineData(Keys.Control | Keys.Alt, new[] { Keys.Alt }, true)]
187         [InlineData(Keys.Control | Keys.Alt, new[] { Keys.Control, Keys.Alt }, true)]
188         [InlineData(Keys.Control | Keys.Alt, new[] { Keys.Shift }, false)]
189         public void IsKeyDownTest(Keys modifierKeys, Keys[] checkKeys, bool expected)
190         {
191             Assert.Equal(expected, MyCommon._IsKeyDown(modifierKeys, checkKeys));
192         }
193
194         [Fact]
195         public void GetAssemblyNameTest()
196         {
197             var mockAssembly = Substitute.For<_Assembly>();
198             mockAssembly.GetName().Returns(new AssemblyName("OpenTween"));
199             MyCommon.EntryAssembly = mockAssembly;
200
201             Assert.Equal("OpenTween", MyCommon.GetAssemblyName());
202         }
203
204         [Theory]
205         [InlineData("", "")]
206         [InlineData("%AppName%", "OpenTween")]
207         [InlineData("%AppName% %AppName%", "OpenTween OpenTween")]
208         public void ReplaceAppNameTest(string str, string excepted)
209         {
210             Assert.Equal(excepted, MyCommon.ReplaceAppName(str, "OpenTween"));
211         }
212
213         [Theory]
214         [InlineData("1.0.0.0", "1.0.0")]
215         [InlineData("1.0.0.1", "1.0.1-beta1")]
216         [InlineData("1.0.0.9", "1.0.1-beta9")]
217         [InlineData("1.0.1.0", "1.0.1")]
218         [InlineData("1.0.9.1", "1.1.0-beta1")]
219         [InlineData("1.1.0.0", "1.1.0")]
220         [InlineData("1.9.9.1", "2.0.0-beta1")]
221         public void GetReadableVersionTest(string fileVersion, string expected)
222         {
223             Assert.Equal(expected, MyCommon.GetReadableVersion(fileVersion));
224         }
225
226         public static IEnumerable<object[]> GetStatusUrlTest1_TestCase
227         {
228             get
229             {
230                 yield return new object[] {
231                     new PostClass { StatusId = 249493863826350080L, ScreenName = "Favstar_LM", RetweetedId = null, RetweetedBy = null },
232                     "https://twitter.com/Favstar_LM/status/249493863826350080",
233                 };
234                 yield return new object[] {
235                     new PostClass { StatusId = 216033842434289664L, ScreenName = "haru067", RetweetedId = 200245741443235840L, RetweetedBy = "re4k"},
236                     "https://twitter.com/haru067/status/200245741443235840",
237                 };
238             }
239         }
240
241         [Theory]
242         [PropertyData("GetStatusUrlTest1_TestCase")]
243         public void GetStatusUrlTest1(PostClass post, string expected)
244         {
245             Assert.Equal(expected, MyCommon.GetStatusUrl(post));
246         }
247
248         [Theory]
249         [InlineData("Favstar_LM", 249493863826350080L, "https://twitter.com/Favstar_LM/status/249493863826350080")]
250         [InlineData("haru067", 200245741443235840L, "https://twitter.com/haru067/status/200245741443235840")]
251         public void GetStatusUrlTest2(string screenName, long statusId, string expected)
252         {
253             Assert.Equal(expected, MyCommon.GetStatusUrl(screenName, statusId));
254         }
255
256         [Fact]
257         public void GetErrorLogPathTest()
258         {
259             if (Environment.OSVersion.Platform == PlatformID.Win32NT)
260             {
261                 var mockAssembly = Substitute.For<_Assembly>();
262                 mockAssembly.Location.Returns(@"C:\hogehoge\OpenTween\OpenTween.exe");
263                 MyCommon.EntryAssembly = mockAssembly;
264
265                 Assert.Equal(@"C:\hogehoge\OpenTween\ErrorLogs", MyCommon.GetErrorLogPath());
266             }
267             else
268             {
269                 var mockAssembly = Substitute.For<_Assembly>();
270                 mockAssembly.Location.Returns(@"/hogehoge/OpenTween/OpenTween.exe");
271                 MyCommon.EntryAssembly = mockAssembly;
272
273                 Assert.Equal(@"/hogehoge/OpenTween/ErrorLogs", MyCommon.GetErrorLogPath());
274             }
275         }
276     }
277 }