OSDN Git Service

C# 8.0 のnull許容参照型を有効化
[opentween/open-tween.git] / OpenTween.Tests / Thumbnail / Services / TinamiTest.cs
index 94a5e37..5e2e6c9 100644 (file)
@@ -1,37 +1,60 @@
-using System;
+// OpenTween - Client of Twitter
+// Copyright (c) 2012 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
+// All rights reserved.
+//
+// This file is part of OpenTween.
+//
+// This program is free software; you can redistribute it and/or modify it
+// under the terms of the GNU General Public License as published by the Free
+// Software Foundation; either version 3 of the License, or (at your option)
+// any later version.
+//
+// This program is distributed in the hope that it will be useful, but
+// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+// for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this program. If not, see <http://www.gnu.org/licenses/>, or write to
+// the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
+// Boston, MA 02110-1301, USA.
+
+using System;
 using System.Collections.Generic;
 using System.Linq;
+using System.Reflection;
+using System.Runtime.InteropServices;
 using System.Text;
-using NUnit.Framework;
+using System.Text.RegularExpressions;
+using System.Threading;
+using System.Threading.Tasks;
 using System.Xml.Linq;
+using Moq;
+using OpenTween.Models;
+using Xunit;
+using Xunit.Extensions;
 
 namespace OpenTween.Thumbnail.Services
 {
-    [TestFixture]
-    class TinamiTest
+    public class TinamiTest
     {
         class TestTinami : Tinami
         {
-            public string FakeXml { get; set; }
+            public string FakeXml { get; set; } = "";
 
-            public TestTinami(string pattern, string replacement)
-                : base(pattern, replacement)
+            public TestTinami()
+                : base(null)
             {
             }
 
-            protected override XDocument FetchContentInfoApi(string url)
-            {
-                Assert.That(url, Is.StringMatching(@"http://api\.tinami\.com/content/info\?cont_id=.+&api_key=.+"));
-
-                return XDocument.Parse(this.FakeXml);
-            }
+            protected override Task<XDocument> FetchContentInfoApiAsync(string contentId, CancellationToken token)
+                => Task.FromResult(XDocument.Parse(this.FakeXml));
         }
 
-        [Test]
-        public void ApiTest()
+        [Fact]
+        public async Task ApiTest()
         {
-            var service = new TestTinami(@"^http://www\.tinami\.com/view/(?<ContentId>\d+)$",
-                "http://api.tinami.com/content/info?cont_id=${ContentId}&api_key=" + ApplicationSettings.TINAMIApiKey);
+            var service = new TestTinami();
 
             service.FakeXml = @"<?xml version='1.0' encoding='utf-8' ?>
 <rsp stat='ok'>
@@ -48,27 +71,26 @@ namespace OpenTween.Thumbnail.Services
     </image>
   </content>
 </rsp>";
-            var thumbinfo = service.GetThumbnailInfo("http://www.tinami.com/view/12345", null);
+            var thumbinfo = await service.GetThumbnailInfoAsync("http://www.tinami.com/view/12345", new PostClass(), CancellationToken.None);
 
-            Assert.That(thumbinfo, Is.Not.Null);
-            Assert.That(thumbinfo.ImageUrl, Is.EqualTo("http://www.tinami.com/view/12345"));
-            Assert.That(thumbinfo.ThumbnailUrl, Is.EqualTo("http://img.tinami.com/hogehoge_150.gif"));
-            Assert.That(thumbinfo.TooltipText, Is.EqualTo("説明"));
+            Assert.NotNull(thumbinfo);
+            Assert.Equal("http://www.tinami.com/view/12345", thumbinfo!.MediaPageUrl);
+            Assert.Equal("http://img.tinami.com/hogehoge_150.gif", thumbinfo.ThumbnailImageUrl);
+            Assert.Equal("説明", thumbinfo.TooltipText);
         }
 
-        [Test]
-        public void ApiErrorTest()
+        [Fact]
+        public async Task ApiErrorTest()
         {
-            var service = new TestTinami(@"^http://www\.tinami\.com/view/(?<ContentId>\d+)$",
-                "http://api.tinami.com/content/info?cont_id=${ContentId}&api_key=" + ApplicationSettings.TINAMIApiKey);
+            var service = new TestTinami();
 
             service.FakeXml = @"<?xml version='1.0' encoding='utf-8'?>
 <rsp stat='user_only'>
   <err msg='この作品は登録ユーザー限定の作品です。'/>
 </rsp>";
-            var thumbinfo = service.GetThumbnailInfo("http://www.tinami.com/view/12345", null);
+            var thumbinfo = await service.GetThumbnailInfoAsync("http://www.tinami.com/view/12345", new PostClass(), CancellationToken.None);
 
-            Assert.That(thumbinfo, Is.Null);
+            Assert.Null(thumbinfo);
         }
     }
 }