1 // OpenTween - Client of Twitter
2 // Copyright (c) 2012 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
3 // All rights reserved.
5 // This file is part of OpenTween.
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)
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
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.
23 using System.Collections.Generic;
26 using System.Reflection;
27 using System.Runtime.InteropServices;
28 using System.Runtime.Serialization;
30 using System.Windows.Forms;
33 using OpenTween.Models;
35 using Xunit.Extensions;
39 public class MyCommonTest
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)
50 Assert.Equal(expected, MyCommon.urlEncodeMultibyteChar(uri));
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)
58 Assert.Equal(expected, MyCommon.IDNEncode(uri));
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)
66 Assert.Equal(expected, MyCommon.IDNDecode(uri));
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)
77 Assert.Equal(expected, MyCommon.ConvertToReadableUrl(url));
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)
87 // MoveArrayItem は values を直接変更するため複製を用意する
88 var copy = new int[values.Length];
89 Array.Copy(values, copy, values.Length);
91 MyCommon.MoveArrayItem(copy, idx_fr, idx_to);
92 Assert.Equal(expected, copy);
96 public void EncryptStringTest()
100 var crypto = MyCommon.EncryptString(str);
101 Assert.NotEqual(str, crypto);
103 var decrypt = MyCommon.DecryptString(crypto);
104 Assert.Equal(str, decrypt);
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)
113 Assert.Equal(expected, MyCommon.ResizeBytesArray(bytes, size));
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)
122 Assert.Equal(expected, MyCommon.IsAnimatedGif(filename));
125 public static IEnumerable<object[]> DateTimeParse_TestCase
129 yield return new object[] {
130 "Sun Nov 25 06:10:00 +00:00 2012",
131 new DateTime(2012, 11, 25, 6, 10, 0, DateTimeKind.Utc),
133 yield return new object[] {
134 "Sun, 25 Nov 2012 06:10:00 +00:00",
135 new DateTime(2012, 11, 25, 6, 10, 0, DateTimeKind.Utc),
141 [MemberData("DateTimeParse_TestCase")]
142 public void DateTimeParseTest(string date, DateTime excepted)
144 Assert.Equal(excepted, MyCommon.DateTimeParse(date).ToUniversalTime());
148 public struct JsonData
150 [DataMember(Name = "id")] public string Id { get; set; }
151 [DataMember(Name = "body")] public string Body { get; set; }
153 public static IEnumerable<object[]> CreateDataFromJson_TestCase
157 yield return new object[] {
158 @"{""id"":""1"", ""body"":""hogehoge""}",
159 new JsonData { Id = "1", Body = "hogehoge" },
165 [MemberData("CreateDataFromJson_TestCase")]
166 public void CreateDataFromJsonTest<T>(string json, T expected)
168 Assert.Equal(expected, MyCommon.CreateDataFromJson<T>(json));
172 [InlineData("hoge123@example.com", true)]
173 [InlineData("hogehoge", false)]
174 [InlineData("foo.bar@example.com", true)]
175 [InlineData("foo..bar@example.com", false)]
176 [InlineData("foobar.@example.com", false)]
177 [InlineData("foo+bar@example.com", true)]
178 public void IsValidEmailTest(string email, bool expected)
180 Assert.Equal(expected, MyCommon.IsValidEmail(email));
184 [InlineData(Keys.Shift, new[] { Keys.Shift }, true)]
185 [InlineData(Keys.Shift, new[] { Keys.Control }, false)]
186 [InlineData(Keys.Control | Keys.Alt, new[] { Keys.Control }, true)]
187 [InlineData(Keys.Control | Keys.Alt, new[] { Keys.Alt }, true)]
188 [InlineData(Keys.Control | Keys.Alt, new[] { Keys.Control, Keys.Alt }, true)]
189 [InlineData(Keys.Control | Keys.Alt, new[] { Keys.Shift }, false)]
190 public void IsKeyDownTest(Keys modifierKeys, Keys[] checkKeys, bool expected)
192 Assert.Equal(expected, MyCommon._IsKeyDown(modifierKeys, checkKeys));
196 public void GetAssemblyNameTest()
198 var mockAssembly = new Mock<_Assembly>();
199 mockAssembly.Setup(m => m.GetName()).Returns(new AssemblyName("OpenTween"));
200 MyCommon.EntryAssembly = mockAssembly.Object;
202 Assert.Equal("OpenTween", MyCommon.GetAssemblyName());
207 [InlineData("%AppName%", "OpenTween")]
208 [InlineData("%AppName% %AppName%", "OpenTween OpenTween")]
209 public void ReplaceAppNameTest(string str, string excepted)
211 Assert.Equal(excepted, MyCommon.ReplaceAppName(str, "OpenTween"));
215 [InlineData("1.0.0.0", "1.0.0")]
216 [InlineData("1.0.0.1", "1.0.1-dev")]
217 [InlineData("1.0.0.12", "1.0.1-dev (Build 12)")]
218 [InlineData("1.0.1.0", "1.0.1")]
219 [InlineData("1.0.9.1", "1.1.0-dev")]
220 [InlineData("1.1.0.0", "1.1.0")]
221 [InlineData("1.9.9.1", "2.0.0-dev")]
222 public void GetReadableVersionTest(string fileVersion, string expected)
224 Assert.Equal(expected, MyCommon.GetReadableVersion(fileVersion));
227 public static IEnumerable<object[]> GetStatusUrlTest1_TestCase
231 yield return new object[] {
232 new PostClass { StatusId = 249493863826350080L, ScreenName = "Favstar_LM", RetweetedId = null, RetweetedBy = null },
233 "https://twitter.com/Favstar_LM/status/249493863826350080",
235 yield return new object[] {
236 new PostClass { StatusId = 216033842434289664L, ScreenName = "haru067", RetweetedId = 200245741443235840L, RetweetedBy = "re4k"},
237 "https://twitter.com/haru067/status/200245741443235840",
243 [MemberData("GetStatusUrlTest1_TestCase")]
244 public void GetStatusUrlTest1(PostClass post, string expected)
246 Assert.Equal(expected, MyCommon.GetStatusUrl(post));
250 [InlineData("Favstar_LM", 249493863826350080L, "https://twitter.com/Favstar_LM/status/249493863826350080")]
251 [InlineData("haru067", 200245741443235840L, "https://twitter.com/haru067/status/200245741443235840")]
252 public void GetStatusUrlTest2(string screenName, long statusId, string expected)
254 Assert.Equal(expected, MyCommon.GetStatusUrl(screenName, statusId));
258 public void GetErrorLogPathTest()
260 if (Environment.OSVersion.Platform == PlatformID.Win32NT)
262 var mockAssembly = new Mock<_Assembly>();
263 mockAssembly.Setup(m => m.Location).Returns(@"C:\hogehoge\OpenTween\OpenTween.exe");
264 MyCommon.EntryAssembly = mockAssembly.Object;
266 Assert.Equal(@"C:\hogehoge\OpenTween\ErrorLogs", MyCommon.GetErrorLogPath());
270 var mockAssembly = new Mock<_Assembly>();
271 mockAssembly.Setup(m => m.Location).Returns(@"/hogehoge/OpenTween/OpenTween.exe");
272 MyCommon.EntryAssembly = mockAssembly.Object;
274 Assert.Equal(@"/hogehoge/OpenTween/ErrorLogs", MyCommon.GetErrorLogPath());
279 public void CountUp_Test()
281 var actual = MyCommon.CountUp(from: 1, to: 5);
283 Assert.Equal(new[] { 1, 2, 3, 4, 5 }, actual);
287 public void CountUp_FromAndToAreEqualTest()
289 var actual = MyCommon.CountUp(from: 1, to: 1);
291 Assert.Equal(new[] { 1 }, actual);
295 public void CountUp_ToIsLessThanFromTest()
297 var actual = MyCommon.CountUp(from: 1, to: 0);
299 Assert.Empty(actual);
303 public void CountDown_Test()
305 var actual = MyCommon.CountDown(from: 5, to: 1);
307 Assert.Equal(new[] { 5, 4, 3, 2, 1 }, actual);
311 public void CountDown_FromAndToAreEqualTest()
313 var actual = MyCommon.CountDown(from: 5, to: 5);
315 Assert.Equal(new[] { 5 }, actual);
319 public void CountDown_ToIsGreaterThanFromTest()
321 var actual = MyCommon.CountDown(from: 5, to: 6);
323 Assert.Empty(actual);
327 public void CircularCountUp_Test()
329 var actual = MyCommon.CircularCountUp(length: 6, startIndex: 3);
331 Assert.Equal(new[] { 3, 4, 5, 0, 1, 2 }, actual);
335 public void CircularCountUp_StartFromZeroTest()
337 var actual = MyCommon.CircularCountUp(length: 6, startIndex: 0);
339 Assert.Equal(new[] { 0, 1, 2, 3, 4, 5 }, actual);
343 public void CircularCountDown_Test()
345 var actual = MyCommon.CircularCountDown(length: 6, startIndex: 3);
347 Assert.Equal(new[] { 3, 2, 1, 0, 5, 4 }, actual);
351 public void CircularCountDown_StartFromLastIndexTest()
353 var actual = MyCommon.CircularCountDown(length: 6, startIndex: 5);
355 Assert.Equal(new[] { 5, 4, 3, 2, 1, 0 }, actual);