OSDN Git Service

開発版のバージョン表記を変更
[opentween/open-tween.git] / OpenTween.Tests / MyCommonTest.cs
index f120514..f84a724 100644 (file)
 
 using System;
 using System.Collections.Generic;
+using System.IO;
 using System.Linq;
-using System.Text;
-using NUnit.Framework;
-using NSubstitute;
-using OpenTween;
-using System.Runtime.InteropServices;
 using System.Reflection;
-using System.Windows.Forms;
+using System.Runtime.InteropServices;
 using System.Runtime.Serialization;
-using System.IO;
+using System.Text;
+using System.Windows.Forms;
+using Moq;
+using OpenTween;
+using OpenTween.Models;
+using Xunit;
+using Xunit.Extensions;
 
 namespace OpenTween
 {
-    [TestFixture]
     public class MyCommonTest
     {
-        [TestCase("http://ja.wikipedia.org/wiki/Wikipedia", Result = "http://ja.wikipedia.org/wiki/Wikipedia")]
-        [TestCase("http://ja.wikipedia.org/wiki/メインページ",
-            Result = "http://ja.wikipedia.org/wiki/%E3%83%A1%E3%82%A4%E3%83%B3%E3%83%9A%E3%83%BC%E3%82%B8")]
-        [TestCase("http://fr.wikipedia.org/wiki/Café", Result = "http://fr.wikipedia.org/wiki/Caf%E9")]
-        [TestCase("http://ja.wikipedia.org/wiki/勇気100%", Result = "http://ja.wikipedia.org/wiki/%E5%8B%87%E6%B0%97100%25")]
-        [TestCase("http://ja.wikipedia.org/wiki/Bio_100%", Result = "http://ja.wikipedia.org/wiki/Bio_100%25")]
-        public string urlEncodeMultibyteCharTest(string uri)
+        [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));
+        }
+
+        [Theory]
+        [InlineData("http://xn--wgv71a119e.idn.icann.org/", "http://日本語.idn.icann.org/")]
+        [InlineData("http://xn--r8jz45g.xn--zckzah/", "http://例え.テスト/")]
+        public void IDNDecodeTest(string uri, string expected)
         {
-            return MyCommon.urlEncodeMultibyteChar(uri);
+            Assert.Equal(expected, MyCommon.IDNDecode(uri));
         }
 
-        [TestCase("http://日本語.idn.icann.org/", Result = "http://xn--wgv71a119e.idn.icann.org/")]
-        [TestCase("http://例え.テスト/", Result = "http://xn--r8jz45g.xn--zckzah/")]
-        public string IDNDecodeTest(string uri)
+        [Theory]
+        [InlineData("http://xn--r8jz45g.xn--zckzah/", "http://例え.テスト/")]
+        [InlineData("http://ja.wikipedia.org/wiki/%3F", "http://ja.wikipedia.org/wiki/%3F")] // "?" に変換しない
+        [InlineData("http://ja.wikipedia.org/wiki/%E3%83%9E%E3%82%B8LOVE1000%25",
+            "http://ja.wikipedia.org/wiki/マジLOVE1000%25")] // "%" も変換しない
+        [InlineData("http://example..com/", "http://example..com/")] // 不正なURL
+        public void ConvertToReadableUrl(string url, string expected)
         {
-            return MyCommon.IDNDecode(uri);
+            Assert.Equal(expected, MyCommon.ConvertToReadableUrl(url));
         }
 
-        [TestCase(new int[] { 1, 2, 3, 4 }, 0, 3, Result = new int[] { 2, 3, 4, 1 })] // 左ローテイト?
-        [TestCase(new int[] { 1, 2, 3, 4 }, 3, 0, Result = new int[] { 4, 1, 2, 3 })] // 右ローテイト?
-        [TestCase(new int[] { 1, 2, 3, 4, 5 }, 1, 3, Result = new int[] { 1, 3, 4, 2, 5 })]
-        [TestCase(new int[] { 1, 2, 3, 4, 5 }, 3, 1, Result = new int[] { 1, 4, 2, 3, 5 })]
-        public int[] MoveArrayItemTest(int[] values, int idx_fr, int idx_to)
+        [Theory]
+        [InlineData(new int[] { 1, 2, 3, 4 }, 0, 3, new int[] { 2, 3, 4, 1 })] // 左ローテイト?
+        [InlineData(new int[] { 1, 2, 3, 4 }, 3, 0, new int[] { 4, 1, 2, 3 })] // 右ローテイト?
+        [InlineData(new int[] { 1, 2, 3, 4, 5 }, 1, 3, new int[] { 1, 3, 4, 2, 5 })]
+        [InlineData(new int[] { 1, 2, 3, 4, 5 }, 3, 1, new int[] { 1, 4, 2, 3, 5 })]
+        public void MoveArrayItemTest(int[] values, int idx_fr, int idx_to, int[] expected)
         {
             // MoveArrayItem は values を直接変更するため複製を用意する
             var copy = new int[values.Length];
             Array.Copy(values, copy, values.Length);
 
             MyCommon.MoveArrayItem(copy, idx_fr, idx_to);
-            return copy;
+            Assert.Equal(expected, copy);
         }
 
-        [Test]
+        [Fact]
         public void EncryptStringTest()
         {
             var str = "hogehoge";
 
             var crypto = MyCommon.EncryptString(str);
-            Assert.That(crypto, Is.Not.EqualTo(str));
+            Assert.NotEqual(str, crypto);
 
             var decrypt = MyCommon.DecryptString(crypto);
-            Assert.That(decrypt, Is.EqualTo(str));
+            Assert.Equal(str, decrypt);
         }
 
-        [TestCase(new byte[] { 0x01, 0x02 }, 3, Result = new byte[] { 0x01, 0x02, 0x00 })]
-        [TestCase(new byte[] { 0x01, 0x02 }, 2, Result = new byte[] { 0x01, 0x02 })]
-        [TestCase(new byte[] { 0x01, 0x02 }, 1, Result = new byte[] { 0x03 })]
-        public byte[] ResizeBytesArrayTest(byte[] bytes, int size)
+        [Theory]
+        [InlineData(new byte[] { 0x01, 0x02 }, 3, new byte[] { 0x01, 0x02, 0x00 })]
+        [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)
         {
-            return MyCommon.ResizeBytesArray(bytes, size);
+            Assert.Equal(expected, MyCommon.ResizeBytesArray(bytes, size));
         }
 
-        [TestCase("Resources/re.gif", Result = true)]
-        [TestCase("Resources/re1.gif", Result = false)]
-        [TestCase("Resources/re1.png", Result = false)]
-        public bool IsAnimatedGifTest(string filename)
+        [Theory]
+        [InlineData("Resources/re.gif", true)]
+        [InlineData("Resources/re1.gif", false)]
+        [InlineData("Resources/re1.png", false)]
+        public void IsAnimatedGifTest(string filename, bool expected)
         {
-            return MyCommon.IsAnimatedGif(filename);
+            Assert.Equal(expected, MyCommon.IsAnimatedGif(filename));
         }
 
-        public static object[] DateTimeParse_TestCase =
+        public static TheoryData<string, DateTimeUtc> DateTimeParse_TestCase = new TheoryData<string, DateTimeUtc>
         {
-            new object[] {
-                "Sun Nov 25 06:10:00 +00:00 2012",
-                new DateTime(2012, 11, 25, 6, 10, 0, DateTimeKind.Utc)
-            },
-            new object[] {
-                "Sun, 25 Nov 2012 06:10:00 +00:00",
-                new DateTime(2012, 11, 25, 6, 10, 0, DateTimeKind.Utc)
-            },
+            { "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) },
         };
-        [TestCaseSource("DateTimeParse_TestCase")]
-        public void DateTimeParseTest(string date, DateTime except)
-        {
-            Assert.That(MyCommon.DateTimeParse(date).ToUniversalTime(), Is.EqualTo(except));
-        }
+
+        [Theory]
+        [MemberData(nameof(DateTimeParse_TestCase))]
+        public void DateTimeParseTest(string date, DateTimeUtc excepted)
+            => Assert.Equal(excepted, MyCommon.DateTimeParse(date));
 
         [DataContract]
         public struct JsonData
@@ -120,115 +139,209 @@ namespace OpenTween
             [DataMember(Name = "id")] public string Id { get; set; }
             [DataMember(Name = "body")] public string Body { get; set; }
         }
-        public static object[] CreateDataFromJson_TestCase =
+        public static IEnumerable<object[]> CreateDataFromJson_TestCase
         {
-            new object[] {
-                @"{""id"":""1"", ""body"":""hogehoge""}",
-                new JsonData { Id = "1", Body = "hogehoge" },
-            },
-        };
-        [TestCaseSource("CreateDataFromJson_TestCase")]
-        public void CreateDataFromJsonTest<T>(string json, T expect)
+            get
+            {
+                yield return new object[] {
+                    @"{""id"":""1"", ""body"":""hogehoge""}",
+                    new JsonData { Id = "1", Body = "hogehoge" },
+                };
+            }
+        }
+
+        [Theory]
+        [MemberData(nameof(CreateDataFromJson_TestCase))]
+        public void CreateDataFromJsonTest<T>(string json, T expected)
         {
-            Assert.That(MyCommon.CreateDataFromJson<T>(json), Is.EqualTo(expect));
+            Assert.Equal(expected, MyCommon.CreateDataFromJson<T>(json));
         }
 
-        [TestCase("hoge123@example.com", Result = true)]
-        [TestCase("hogehoge", Result = false)]
-        [TestCase("foo.bar@example.com", Result = true)]
-        [TestCase("foo..bar@example.com", Result = false)]
-        [TestCase("foobar.@example.com", Result = false)]
-        [TestCase("foo+bar@example.com", Result = true)]
-        public bool IsValidEmailTest(string email)
+        [Theory]
+        [InlineData("hoge123@example.com", true)]
+        [InlineData("hogehoge", false)]
+        [InlineData("foo.bar@example.com", true)]
+        [InlineData("foo..bar@example.com", false)]
+        [InlineData("foobar.@example.com", false)]
+        [InlineData("foo+bar@example.com", true)]
+        public void IsValidEmailTest(string email, bool expected)
         {
-            return MyCommon.IsValidEmail(email);
+            Assert.Equal(expected, MyCommon.IsValidEmail(email));
         }
 
-        [TestCase(Keys.Shift, Keys.Shift, Result = true)]
-        [TestCase(Keys.Shift, Keys.Control, Result = false)]
-        [TestCase(Keys.Control | Keys.Alt, Keys.Control, Result = true)]
-        [TestCase(Keys.Control | Keys.Alt, Keys.Alt, Result = true)]
-        [TestCase(Keys.Control | Keys.Alt, Keys.Control, Keys.Alt, Result = true)]
-        [TestCase(Keys.Control | Keys.Alt, Keys.Shift, Result = false)]
-        public bool IsKeyDownTest(Keys modifierKeys, params Keys[] checkKeys)
+        [Theory]
+        [InlineData(Keys.Shift, new[] { Keys.Shift }, true)]
+        [InlineData(Keys.Shift, new[] { Keys.Control }, false)]
+        [InlineData(Keys.Control | Keys.Alt, new[] { Keys.Control }, true)]
+        [InlineData(Keys.Control | Keys.Alt, new[] { Keys.Alt }, true)]
+        [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)
         {
-            return MyCommon._IsKeyDown(modifierKeys, checkKeys);
+            Assert.Equal(expected, MyCommon._IsKeyDown(modifierKeys, checkKeys));
         }
 
-        [Test]
+        [Fact]
         public void GetAssemblyNameTest()
         {
-            var mockAssembly = Substitute.For<_Assembly>();
-            mockAssembly.GetName().Returns(new AssemblyName("OpenTween"));
-            MyCommon.EntryAssembly = mockAssembly;
+            var mockAssembly = new Mock<_Assembly>();
+            mockAssembly.Setup(m => m.GetName()).Returns(new AssemblyName("OpenTween"));
+            MyCommon.EntryAssembly = mockAssembly.Object;
 
-            Assert.That(MyCommon.GetAssemblyName(), Is.EqualTo("OpenTween"));
+            Assert.Equal("OpenTween", MyCommon.GetAssemblyName());
         }
 
-        [TestCase("", "")]
-        [TestCase("%AppName%", "OpenTween")]
-        [TestCase("%AppName% %AppName%", "OpenTween OpenTween")]
-        public void ReplaceAppNameTest(string str, string except)
+        [Theory]
+        [InlineData("", "")]
+        [InlineData("%AppName%", "OpenTween")]
+        [InlineData("%AppName% %AppName%", "OpenTween OpenTween")]
+        public void ReplaceAppNameTest(string str, string excepted)
         {
-            Assert.That(MyCommon.ReplaceAppName(str, "OpenTween"), Is.EqualTo(except));
+            Assert.Equal(excepted, MyCommon.ReplaceAppName(str, "OpenTween"));
         }
 
-        [TestCase("1.0.0.0", "1.0.0")]
-        [TestCase("1.0.0.1", "1.0.1-beta1")]
-        [TestCase("1.0.0.9", "1.0.1-beta9")]
-        [TestCase("1.0.1.0", "1.0.1")]
-        [TestCase("1.0.9.1", "1.1.0-beta1")]
-        [TestCase("1.1.0.0", "1.1.0")]
-        [TestCase("1.9.9.1", "2.0.0-beta1")]
-        public void GetReadableVersionTest(string fileVersion, string expect)
+        [Theory]
+        [InlineData("1.0.0.0", "1.0.0")]
+        [InlineData("1.0.0.1", "1.0.1-dev")]
+        [InlineData("1.0.0.12", "1.0.1-dev+build.12")]
+        [InlineData("1.0.1.0", "1.0.1")]
+        [InlineData("1.0.9.1", "1.0.10-dev")]
+        [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.That(OpenTween.MyCommon.GetReadableVersion(fileVersion), Is.EqualTo(expect));
+            Assert.Equal(expected, MyCommon.GetReadableVersion(fileVersion));
         }
 
-        public static object[] GetStatusUrlTest1_TestCase =
+        public static IEnumerable<object[]> GetStatusUrlTest1_TestCase
         {
-            new object[] {
-                new PostClass { StatusId = 249493863826350080L, ScreenName = "Favstar_LM", RetweetedId = 0L, RetweetedBy = null },
-                "https://twitter.com/Favstar_LM/status/249493863826350080",
-            },
-            new object[] {
-                new PostClass { StatusId = 216033842434289664L, ScreenName = "haru067", RetweetedId = 200245741443235840L, RetweetedBy = "re4k"},
-                "https://twitter.com/haru067/status/200245741443235840",
-            },
-        };
-        [TestCaseSource("GetStatusUrlTest1_TestCase")]
-        public void GetStatusUrlTest1(PostClass post, string except)
+            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",
+                };
+            }
+        }
+
+        [Theory]
+        [MemberData(nameof(GetStatusUrlTest1_TestCase))]
+        public void GetStatusUrlTest1(PostClass post, string expected)
+        {
+            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));
+        }
+
+        [Fact]
+        public void GetErrorLogPathTest()
         {
-            Assert.That(MyCommon.GetStatusUrl(post), Is.EqualTo(except));
+            if (Environment.OSVersion.Platform == PlatformID.Win32NT)
+            {
+                var mockAssembly = new Mock<_Assembly>();
+                mockAssembly.Setup(m => m.Location).Returns(@"C:\hogehoge\OpenTween\OpenTween.exe");
+                MyCommon.EntryAssembly = mockAssembly.Object;
+
+                Assert.Equal(@"C:\hogehoge\OpenTween\ErrorLogs", MyCommon.GetErrorLogPath());
+            }
+            else
+            {
+                var mockAssembly = new Mock<_Assembly>();
+                mockAssembly.Setup(m => m.Location).Returns(@"/hogehoge/OpenTween/OpenTween.exe");
+                MyCommon.EntryAssembly = mockAssembly.Object;
+
+                Assert.Equal(@"/hogehoge/OpenTween/ErrorLogs", MyCommon.GetErrorLogPath());
+            }
+        }
+
+        [Fact]
+        public void CountUp_Test()
+        {
+            var actual = MyCommon.CountUp(from: 1, to: 5);
+
+            Assert.Equal(new[] { 1, 2, 3, 4, 5 }, actual);
+        }
+
+        [Fact]
+        public void CountUp_FromAndToAreEqualTest()
+        {
+            var actual = MyCommon.CountUp(from: 1, to: 1);
+
+            Assert.Equal(new[] { 1 }, actual);
+        }
+
+        [Fact]
+        public void CountUp_ToIsLessThanFromTest()
+        {
+            var actual = MyCommon.CountUp(from: 1, to: 0);
+
+            Assert.Empty(actual);
         }
 
-        [TestCase("Favstar_LM", 249493863826350080L, "https://twitter.com/Favstar_LM/status/249493863826350080")]
-        [TestCase("haru067", 200245741443235840L, "https://twitter.com/haru067/status/200245741443235840")]
-        public void GetStatusUrlTest2(string screenName, long statusId, string except)
+        [Fact]
+        public void CountDown_Test()
         {
-            Assert.That(MyCommon.GetStatusUrl(screenName, statusId), Is.EqualTo(except));
+            var actual = MyCommon.CountDown(from: 5, to: 1);
+
+            Assert.Equal(new[] { 5, 4, 3, 2, 1 }, actual);
+        }
+
+        [Fact]
+        public void CountDown_FromAndToAreEqualTest()
+        {
+            var actual = MyCommon.CountDown(from: 5, to: 5);
+
+            Assert.Equal(new[] { 5 }, actual);
+        }
+
+        [Fact]
+        public void CountDown_ToIsGreaterThanFromTest()
+        {
+            var actual = MyCommon.CountDown(from: 5, to: 6);
+
+            Assert.Empty(actual);
+        }
+
+        [Fact]
+        public void CircularCountUp_Test()
+        {
+            var actual = MyCommon.CircularCountUp(length: 6, startIndex: 3);
+
+            Assert.Equal(new[] { 3, 4, 5, 0, 1, 2 }, actual);
+        }
+
+        [Fact]
+        public void CircularCountUp_StartFromZeroTest()
+        {
+            var actual = MyCommon.CircularCountUp(length: 6, startIndex: 0);
+
+            Assert.Equal(new[] { 0, 1, 2, 3, 4, 5 }, actual);
         }
 
-        [Test]
-        [Platform("Win")]
-        public void GetErrorLogPathTestWindows()
+        [Fact]
+        public void CircularCountDown_Test()
         {
-            var mockAssembly = Substitute.For<_Assembly>();
-            mockAssembly.Location.Returns(@"C:\hogehoge\OpenTween\OpenTween.exe");
-            MyCommon.EntryAssembly = mockAssembly;
+            var actual = MyCommon.CircularCountDown(length: 6, startIndex: 3);
 
-            Assert.That(MyCommon.GetErrorLogPath(), Is.SamePath(@"C:\hogehoge\OpenTween\ErrorLogs\"));
+            Assert.Equal(new[] { 3, 2, 1, 0, 5, 4 }, actual);
         }
 
-        [Test]
-        [Platform(Exclude = "Win")]
-        public void GetErrorLogPathTestOther()
+        [Fact]
+        public void CircularCountDown_StartFromLastIndexTest()
         {
-            var mockAssembly = Substitute.For<_Assembly>();
-            mockAssembly.Location.Returns(@"/hogehoge/OpenTween/OpenTween.exe");
-            MyCommon.EntryAssembly = mockAssembly;
+            var actual = MyCommon.CircularCountDown(length: 6, startIndex: 5);
 
-            Assert.That(MyCommon.GetErrorLogPath(), Is.SamePath(@"/hogehoge/OpenTween/ErrorLogs/"));
+            Assert.Equal(new[] { 5, 4, 3, 2, 1, 0 }, actual);
         }
     }
 }