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://日本語.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));
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));
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));
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)
73 // MoveArrayItem は values を直接変更するため複製を用意する
74 var copy = new int[values.Length];
75 Array.Copy(values, copy, values.Length);
77 MyCommon.MoveArrayItem(copy, idx_fr, idx_to);
78 Assert.Equal(expected, copy);
82 public void EncryptStringTest()
86 var crypto = MyCommon.EncryptString(str);
87 Assert.NotEqual(str, crypto);
89 var decrypt = MyCommon.DecryptString(crypto);
90 Assert.Equal(str, decrypt);
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));
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));
107 public static readonly TheoryData<string, DateTimeUtc> DateTimeParseTestCase = new()
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) },
114 [MemberData(nameof(DateTimeParseTestCase))]
115 public void DateTimeParseTest(string date, DateTimeUtc excepted)
116 => Assert.Equal(excepted, MyCommon.DateTimeParse(date));
119 public struct JsonData
121 [DataMember(Name = "id")]
122 public string Id { get; set; }
124 [DataMember(Name = "body")]
125 public string Body { get; set; }
128 public static readonly TheoryData<string, JsonData> CreateDataFromJsonTestCase = new()
131 @"{""id"":""1"", ""body"":""hogehoge""}",
132 new JsonData { Id = "1", Body = "hogehoge" }
137 [MemberData(nameof(CreateDataFromJsonTestCase))]
138 public void CreateDataFromJsonTest<T>(string json, T expected)
139 => Assert.Equal(expected, MyCommon.CreateDataFromJson<T>(json));
142 [InlineData("hoge123@example.com", true)]
143 [InlineData("hogehoge", false)]
144 [InlineData("foo.bar@example.com", true)]
145 [InlineData("foo..bar@example.com", false)]
146 [InlineData("foobar.@example.com", false)]
147 [InlineData("foo+bar@example.com", true)]
148 public void IsValidEmailTest(string email, bool expected)
149 => Assert.Equal(expected, MyCommon.IsValidEmail(email));
152 [InlineData(Keys.Shift, new[] { Keys.Shift }, true)]
153 [InlineData(Keys.Shift, new[] { Keys.Control }, false)]
154 [InlineData(Keys.Control | Keys.Alt, new[] { Keys.Control }, true)]
155 [InlineData(Keys.Control | Keys.Alt, new[] { Keys.Alt }, true)]
156 [InlineData(Keys.Control | Keys.Alt, new[] { Keys.Control, Keys.Alt }, true)]
157 [InlineData(Keys.Control | Keys.Alt, new[] { Keys.Shift }, false)]
158 public void IsKeyDownTest(Keys modifierKeys, Keys[] checkKeys, bool expected)
159 => Assert.Equal(expected, MyCommon.IsKeyDownInternal(modifierKeys, checkKeys));
162 public void GetAssemblyNameTest()
164 var mockAssembly = new Mock<_Assembly>();
165 mockAssembly.Setup(m => m.GetName()).Returns(new AssemblyName("OpenTween"));
166 MyCommon.EntryAssembly = mockAssembly.Object;
168 Assert.Equal("OpenTween", MyCommon.GetAssemblyName());
173 [InlineData("%AppName%", "OpenTween")]
174 [InlineData("%AppName% %AppName%", "OpenTween OpenTween")]
175 public void ReplaceAppNameTest(string str, string excepted)
176 => Assert.Equal(excepted, MyCommon.ReplaceAppName(str, "OpenTween"));
179 [InlineData("1.0.0.0", "1.0.0")]
180 [InlineData("1.0.0.1", "1.0.1-dev")]
181 [InlineData("1.0.0.12", "1.0.1-dev+build.12")]
182 [InlineData("1.0.1.0", "1.0.1")]
183 [InlineData("1.0.9.1", "1.0.10-dev")]
184 [InlineData("1.1.0.0", "1.1.0")]
185 [InlineData("1.9.9.1", "1.9.10-dev")]
186 public void GetReadableVersionTest(string fileVersion, string expected)
187 => Assert.Equal(expected, MyCommon.GetReadableVersion(fileVersion));
189 public static readonly TheoryData<PostClass, string> GetStatusUrlTest1TestCase = new()
192 new PostClass { StatusId = 249493863826350080L, ScreenName = "Favstar_LM", RetweetedId = null, RetweetedBy = null },
193 "https://twitter.com/Favstar_LM/status/249493863826350080"
196 new PostClass { StatusId = 216033842434289664L, ScreenName = "haru067", RetweetedId = 200245741443235840L, RetweetedBy = "re4k" },
197 "https://twitter.com/haru067/status/200245741443235840"
202 [MemberData(nameof(GetStatusUrlTest1TestCase))]
203 public void GetStatusUrlTest1(PostClass post, string expected)
204 => Assert.Equal(expected, MyCommon.GetStatusUrl(post));
207 [InlineData("Favstar_LM", 249493863826350080L, "https://twitter.com/Favstar_LM/status/249493863826350080")]
208 [InlineData("haru067", 200245741443235840L, "https://twitter.com/haru067/status/200245741443235840")]
209 public void GetStatusUrlTest2(string screenName, long statusId, string expected)
210 => Assert.Equal(expected, MyCommon.GetStatusUrl(screenName, statusId));
213 public void GetErrorLogPathTest()
215 if (Environment.OSVersion.Platform == PlatformID.Win32NT)
217 var mockAssembly = new Mock<_Assembly>();
218 mockAssembly.Setup(m => m.Location).Returns(@"C:\hogehoge\OpenTween\OpenTween.exe");
219 MyCommon.EntryAssembly = mockAssembly.Object;
221 Assert.Equal(@"C:\hogehoge\OpenTween\ErrorLogs", MyCommon.GetErrorLogPath());
225 var mockAssembly = new Mock<_Assembly>();
226 mockAssembly.Setup(m => m.Location).Returns(@"/hogehoge/OpenTween/OpenTween.exe");
227 MyCommon.EntryAssembly = mockAssembly.Object;
229 Assert.Equal(@"/hogehoge/OpenTween/ErrorLogs", MyCommon.GetErrorLogPath());
234 public void CountUp_Test()
236 var actual = MyCommon.CountUp(from: 1, to: 5);
238 Assert.Equal(new[] { 1, 2, 3, 4, 5 }, actual);
242 public void CountUp_FromAndToAreEqualTest()
244 var actual = MyCommon.CountUp(from: 1, to: 1);
246 Assert.Equal(new[] { 1 }, actual);
250 public void CountUp_ToIsLessThanFromTest()
252 var actual = MyCommon.CountUp(from: 1, to: 0);
254 Assert.Empty(actual);
258 public void CountDown_Test()
260 var actual = MyCommon.CountDown(from: 5, to: 1);
262 Assert.Equal(new[] { 5, 4, 3, 2, 1 }, actual);
266 public void CountDown_FromAndToAreEqualTest()
268 var actual = MyCommon.CountDown(from: 5, to: 5);
270 Assert.Equal(new[] { 5 }, actual);
274 public void CountDown_ToIsGreaterThanFromTest()
276 var actual = MyCommon.CountDown(from: 5, to: 6);
278 Assert.Empty(actual);
282 public void CircularCountUp_Test()
284 var actual = MyCommon.CircularCountUp(length: 6, startIndex: 3);
286 Assert.Equal(new[] { 3, 4, 5, 0, 1, 2 }, actual);
290 public void CircularCountUp_StartFromZeroTest()
292 var actual = MyCommon.CircularCountUp(length: 6, startIndex: 0);
294 Assert.Equal(new[] { 0, 1, 2, 3, 4, 5 }, actual);
298 public void CircularCountDown_Test()
300 var actual = MyCommon.CircularCountDown(length: 6, startIndex: 3);
302 Assert.Equal(new[] { 3, 2, 1, 0, 5, 4 }, actual);
306 public void CircularCountDown_StartFromLastIndexTest()
308 var actual = MyCommon.CircularCountDown(length: 6, startIndex: 5);
310 Assert.Equal(new[] { 5, 4, 3, 2, 1, 0 }, actual);
314 public void CreateBrowserProcessStartInfo_BrowserPathTest()
316 var startInfo = MyCommon.CreateBrowserProcessStartInfo("C:\\browser.exe", "https://example.com/");
317 Assert.Equal("C:\\browser.exe", startInfo.FileName);
318 Assert.Equal("\"https://example.com/\"", startInfo.Arguments);
319 Assert.False(startInfo.UseShellExecute);
323 public void CreateBrowserProcessStartInfo_BrowserPathWithSpacesTest()
325 var startInfo = MyCommon.CreateBrowserProcessStartInfo("C:\\Program Files\\browser.exe", "https://example.com/");
326 Assert.Equal("C:\\Program Files\\browser.exe", startInfo.FileName);
327 Assert.Equal("\"https://example.com/\"", startInfo.Arguments);
328 Assert.False(startInfo.UseShellExecute);
332 public void CreateBrowserProcessStartInfo_QuotedBrowserPathTest()
334 var startInfo = MyCommon.CreateBrowserProcessStartInfo("\"C:\\Program Files\\browser.exe\"", "https://example.com/");
335 Assert.Equal("C:\\Program Files\\browser.exe", startInfo.FileName);
336 Assert.Equal("\"https://example.com/\"", startInfo.Arguments);
337 Assert.False(startInfo.UseShellExecute);
341 public void CreateBrowserProcessStartInfo_QuotedBrowserPathWithArgsTest()
343 var startInfo = MyCommon.CreateBrowserProcessStartInfo("\"C:\\Program Files\\browser.exe\" /hoge", "https://example.com/");
344 Assert.Equal("C:\\Program Files\\browser.exe", startInfo.FileName);
345 Assert.Equal("/hoge \"https://example.com/\"", startInfo.Arguments);
346 Assert.False(startInfo.UseShellExecute);
349 public static readonly TheoryData<int[], (int, int)[]> ToRangeChunkTestCase = new()
361 new[] { (1, 1), (3, 3) }
366 [MemberData(nameof(ToRangeChunkTestCase))]
367 public void ToRangeChunk_Test(int[] values, (int Start, int End)[] expected)
369 Assert.Equal(expected, MyCommon.ToRangeChunk(values));