OSDN Git Service

IApiConnection, IHttpRequest, ApiResponseで構成する新しいTwitterApiConnectionを実装
[opentween/open-tween.git] / OpenTween.Tests / MyCommonTest.cs
index 3457c12..1261d47 100644 (file)
@@ -39,33 +39,17 @@ namespace OpenTween
     public class MyCommonTest
     {
         [Theory]
-        [InlineData("http://ja.wikipedia.org/wiki/Wikipedia", "http://ja.wikipedia.org/wiki/Wikipedia")]
-        [InlineData("http://ja.wikipedia.org/wiki/メインページ",
-            "http://ja.wikipedia.org/wiki/%E3%83%A1%E3%82%A4%E3%83%B3%E3%83%9A%E3%83%BC%E3%82%B8")]
-        [InlineData("http://fr.wikipedia.org/wiki/Café", "http://fr.wikipedia.org/wiki/Caf%E9")]
-        [InlineData("http://ja.wikipedia.org/wiki/勇気100%", "http://ja.wikipedia.org/wiki/%E5%8B%87%E6%B0%97100%25")]
-        [InlineData("http://ja.wikipedia.org/wiki/Bio_100%", "http://ja.wikipedia.org/wiki/Bio_100%25")]
-        public void urlEncodeMultibyteCharTest(string uri, string expected)
-        {
-            Assert.Equal(expected, MyCommon.urlEncodeMultibyteChar(uri));
-        }
-
-        [Theory]
         [InlineData("http://日本語.idn.icann.org/", "http://xn--wgv71a119e.idn.icann.org/")]
         [InlineData("http://例え.テスト/", "http://xn--r8jz45g.xn--zckzah/")]
         public void IDNEncodeTest(string uri, string expected)
-        {
-            Assert.Equal(expected, MyCommon.IDNEncode(uri));
-        }
+            => Assert.Equal(expected, MyCommon.IDNEncode(uri));
 
         [Theory]
         [InlineData("http://xn--wgv71a119e.idn.icann.org/", "http://日本語.idn.icann.org/")]
         [InlineData("http://xn--r8jz45g.xn--zckzah/", "http://例え.テスト/")]
         [InlineData("http://xn--a/", "http://xn--a/")] // 不正なpunycode
         public void IDNDecodeTest(string uri, string expected)
-        {
-            Assert.Equal(expected, MyCommon.IDNDecode(uri));
-        }
+            => Assert.Equal(expected, MyCommon.IDNDecode(uri));
 
         [Theory]
         [InlineData("http://xn--r8jz45g.xn--zckzah/", "http://例え.テスト/")]
@@ -77,9 +61,7 @@ namespace OpenTween
         [InlineData("http://example.com/%E3%81%82%FF", "http://example.com/あ%FF")] // 不正なUTF-8シーケンス
         [InlineData("http://example.com/%E3%81%82%ED%A0%80", "http://example.com/あ%ED%A0%80")] // 不正なUTF-8シーケンス (high surrogate)
         public void ConvertToReadableUrl(string url, string expected)
-        {
-            Assert.Equal(expected, MyCommon.ConvertToReadableUrl(url));
-        }
+            => Assert.Equal(expected, MyCommon.ConvertToReadableUrl(url));
 
         [Theory]
         [InlineData(new int[] { 1, 2, 3, 4 }, 0, 3, new int[] { 2, 3, 4, 1 })] // 左ローテイト?
@@ -113,52 +95,55 @@ namespace OpenTween
         [InlineData(new byte[] { 0x01, 0x02 }, 2, new byte[] { 0x01, 0x02 })]
         [InlineData(new byte[] { 0x01, 0x02 }, 1, new byte[] { 0x03 })]
         public void ResizeBytesArrayTest(byte[] bytes, int size, byte[] expected)
-        {
-            Assert.Equal(expected, MyCommon.ResizeBytesArray(bytes, size));
-        }
+            => Assert.Equal(expected, MyCommon.ResizeBytesArray(bytes, size));
 
         [Theory]
         [InlineData("Resources/re.gif", true)]
         [InlineData("Resources/re1.gif", false)]
         [InlineData("Resources/re1.png", false)]
         public void IsAnimatedGifTest(string filename, bool expected)
-        {
-            Assert.Equal(expected, MyCommon.IsAnimatedGif(filename));
-        }
+            => Assert.Equal(expected, MyCommon.IsAnimatedGif(filename));
 
-        public static TheoryData<string, DateTimeUtc> DateTimeParse_TestCase = new TheoryData<string, DateTimeUtc>
+        public static readonly TheoryData<string, DateTimeUtc> DateTimeParseTestCase = new()
         {
             { "Sun Nov 25 06:10:00 +00:00 2012", new DateTimeUtc(2012, 11, 25, 6, 10, 0) },
             { "Sun, 25 Nov 2012 06:10:00 +00:00", new DateTimeUtc(2012, 11, 25, 6, 10, 0) },
         };
 
         [Theory]
-        [MemberData(nameof(DateTimeParse_TestCase))]
+        [MemberData(nameof(DateTimeParseTestCase))]
         public void DateTimeParseTest(string date, DateTimeUtc excepted)
             => Assert.Equal(excepted, MyCommon.DateTimeParse(date));
 
         [DataContract]
         public struct JsonData
         {
-            [DataMember(Name = "id")] public string Id { get; set; }
-            [DataMember(Name = "body")] public string Body { get; set; }
+            [DataMember(Name = "id")]
+            public string Id { get; set; }
+
+            [DataMember(Name = "body")]
+            public string Body { get; set; }
         }
-        public static IEnumerable<object[]> CreateDataFromJson_TestCase
+
+        public static readonly TheoryData<string, JsonData> CreateDataFromJsonTestCase = new()
         {
-            get
             {
-                yield return new object[] {
-                    @"{""id"":""1"", ""body"":""hogehoge""}",
-                    new JsonData { Id = "1", Body = "hogehoge" },
-                };
-            }
-        }
+                """{"id":"1", "body":"hogehoge"}""",
+                new JsonData { Id = "1", Body = "hogehoge" }
+            },
+        };
+
+        [Theory]
+        [MemberData(nameof(CreateDataFromJsonTestCase))]
+        public void CreateDataFromJson_StringTest<T>(string json, T expected)
+            => Assert.Equal(expected, MyCommon.CreateDataFromJson<T>(json));
 
         [Theory]
-        [MemberData(nameof(CreateDataFromJson_TestCase))]
-        public void CreateDataFromJsonTest<T>(string json, T expected)
+        [MemberData(nameof(CreateDataFromJsonTestCase))]
+        public void CreateDataFromJson_BytesTest<T>(string json, T expected)
         {
-            Assert.Equal(expected, MyCommon.CreateDataFromJson<T>(json));
+            var jsonBytes = Encoding.UTF8.GetBytes(json);
+            Assert.Equal(expected, MyCommon.CreateDataFromJson<T>(jsonBytes));
         }
 
         [Theory]
@@ -169,9 +154,7 @@ namespace OpenTween
         [InlineData("foobar.@example.com", false)]
         [InlineData("foo+bar@example.com", true)]
         public void IsValidEmailTest(string email, bool expected)
-        {
-            Assert.Equal(expected, MyCommon.IsValidEmail(email));
-        }
+            => Assert.Equal(expected, MyCommon.IsValidEmail(email));
 
         [Theory]
         [InlineData(Keys.Shift, new[] { Keys.Shift }, true)]
@@ -181,9 +164,7 @@ namespace OpenTween
         [InlineData(Keys.Control | Keys.Alt, new[] { Keys.Control, Keys.Alt }, true)]
         [InlineData(Keys.Control | Keys.Alt, new[] { Keys.Shift }, false)]
         public void IsKeyDownTest(Keys modifierKeys, Keys[] checkKeys, bool expected)
-        {
-            Assert.Equal(expected, MyCommon._IsKeyDown(modifierKeys, checkKeys));
-        }
+            => Assert.Equal(expected, MyCommon.IsKeyDownInternal(modifierKeys, checkKeys));
 
         [Fact]
         public void GetAssemblyNameTest()
@@ -200,9 +181,7 @@ namespace OpenTween
         [InlineData("%AppName%", "OpenTween")]
         [InlineData("%AppName% %AppName%", "OpenTween OpenTween")]
         public void ReplaceAppNameTest(string str, string excepted)
-        {
-            Assert.Equal(excepted, MyCommon.ReplaceAppName(str, "OpenTween"));
-        }
+            => Assert.Equal(excepted, MyCommon.ReplaceAppName(str, "OpenTween"));
 
         [Theory]
         [InlineData("1.0.0.0", "1.0.0")]
@@ -213,39 +192,30 @@ namespace OpenTween
         [InlineData("1.1.0.0", "1.1.0")]
         [InlineData("1.9.9.1", "1.9.10-dev")]
         public void GetReadableVersionTest(string fileVersion, string expected)
-        {
-            Assert.Equal(expected, MyCommon.GetReadableVersion(fileVersion));
-        }
+            => Assert.Equal(expected, MyCommon.GetReadableVersion(fileVersion));
 
-        public static IEnumerable<object[]> GetStatusUrlTest1_TestCase
+        public static readonly TheoryData<PostClass, string> GetStatusUrlTest1TestCase = new()
         {
-            get
             {
-                yield return new object[] {
-                    new PostClass { StatusId = 249493863826350080L, ScreenName = "Favstar_LM", RetweetedId = null, RetweetedBy = null },
-                    "https://twitter.com/Favstar_LM/status/249493863826350080",
-                };
-                yield return new object[] {
-                    new PostClass { StatusId = 216033842434289664L, ScreenName = "haru067", RetweetedId = 200245741443235840L, RetweetedBy = "re4k"},
-                    "https://twitter.com/haru067/status/200245741443235840",
-                };
-            }
-        }
+                new PostClass { StatusId = new TwitterStatusId("249493863826350080"), ScreenName = "Favstar_LM", RetweetedId = null, RetweetedBy = null },
+                "https://twitter.com/Favstar_LM/status/249493863826350080"
+            },
+            {
+                new PostClass { StatusId = new TwitterStatusId("216033842434289664"), ScreenName = "haru067", RetweetedId = new TwitterStatusId("200245741443235840"), RetweetedBy = "re4k" },
+                "https://twitter.com/haru067/status/200245741443235840"
+            },
+        };
 
         [Theory]
-        [MemberData(nameof(GetStatusUrlTest1_TestCase))]
+        [MemberData(nameof(GetStatusUrlTest1TestCase))]
         public void GetStatusUrlTest1(PostClass post, string expected)
-        {
-            Assert.Equal(expected, MyCommon.GetStatusUrl(post));
-        }
+            => Assert.Equal(expected, MyCommon.GetStatusUrl(post));
 
         [Theory]
         [InlineData("Favstar_LM", 249493863826350080L, "https://twitter.com/Favstar_LM/status/249493863826350080")]
         [InlineData("haru067", 200245741443235840L, "https://twitter.com/haru067/status/200245741443235840")]
         public void GetStatusUrlTest2(string screenName, long statusId, string expected)
-        {
-            Assert.Equal(expected, MyCommon.GetStatusUrl(screenName, statusId));
-        }
+            => Assert.Equal(expected, MyCommon.GetStatusUrl(screenName, new TwitterStatusId(statusId)));
 
         [Fact]
         public void GetErrorLogPathTest()
@@ -347,5 +317,64 @@ namespace OpenTween
 
             Assert.Equal(new[] { 5, 4, 3, 2, 1, 0 }, actual);
         }
+
+        [Fact]
+        public void CreateBrowserProcessStartInfo_BrowserPathTest()
+        {
+            var startInfo = MyCommon.CreateBrowserProcessStartInfo("C:\\browser.exe", "https://example.com/");
+            Assert.Equal("C:\\browser.exe", startInfo.FileName);
+            Assert.Equal("\"https://example.com/\"", startInfo.Arguments);
+            Assert.False(startInfo.UseShellExecute);
+        }
+
+        [Fact]
+        public void CreateBrowserProcessStartInfo_BrowserPathWithSpacesTest()
+        {
+            var startInfo = MyCommon.CreateBrowserProcessStartInfo("C:\\Program Files\\browser.exe", "https://example.com/");
+            Assert.Equal("C:\\Program Files\\browser.exe", startInfo.FileName);
+            Assert.Equal("\"https://example.com/\"", startInfo.Arguments);
+            Assert.False(startInfo.UseShellExecute);
+        }
+
+        [Fact]
+        public void CreateBrowserProcessStartInfo_QuotedBrowserPathTest()
+        {
+            var startInfo = MyCommon.CreateBrowserProcessStartInfo("\"C:\\Program Files\\browser.exe\"", "https://example.com/");
+            Assert.Equal("C:\\Program Files\\browser.exe", startInfo.FileName);
+            Assert.Equal("\"https://example.com/\"", startInfo.Arguments);
+            Assert.False(startInfo.UseShellExecute);
+        }
+
+        [Fact]
+        public void CreateBrowserProcessStartInfo_QuotedBrowserPathWithArgsTest()
+        {
+            var startInfo = MyCommon.CreateBrowserProcessStartInfo("\"C:\\Program Files\\browser.exe\" /hoge", "https://example.com/");
+            Assert.Equal("C:\\Program Files\\browser.exe", startInfo.FileName);
+            Assert.Equal("/hoge \"https://example.com/\"", startInfo.Arguments);
+            Assert.False(startInfo.UseShellExecute);
+        }
+
+        public static readonly TheoryData<int[], (int Start, int End)[]> ToRangeChunkTestCase = new()
+        {
+            {
+                new[] { 1 },
+                new[] { (1, 1) }
+            },
+            {
+                new[] { 1, 2 },
+                new[] { (1, 2) }
+            },
+            {
+                new[] { 1, 3 },
+                new[] { (1, 1), (3, 3) }
+            },
+        };
+
+        [Theory]
+        [MemberData(nameof(ToRangeChunkTestCase))]
+        public void ToRangeChunk_Test(int[] values, (int Start, int End)[] expected)
+        {
+            Assert.Equal(expected, MyCommon.ToRangeChunk(values));
+        }
     }
 }