OSDN Git Service

TwitterV1Clientでのcursorに対するテストコードを追加
authorKimura Youichi <kim.upsilon@bucyou.net>
Tue, 14 May 2024 19:24:05 +0000 (04:24 +0900)
committerKimura Youichi <kim.upsilon@bucyou.net>
Tue, 14 May 2024 19:50:31 +0000 (04:50 +0900)
OpenTween.Tests/SocialProtocol/Twitter/TwitterV1ClientTest.cs [new file with mode: 0644]
OpenTween/SocialProtocol/Twitter/TwitterV1Client.cs

diff --git a/OpenTween.Tests/SocialProtocol/Twitter/TwitterV1ClientTest.cs b/OpenTween.Tests/SocialProtocol/Twitter/TwitterV1ClientTest.cs
new file mode 100644 (file)
index 0000000..ac91fdc
--- /dev/null
@@ -0,0 +1,92 @@
+// OpenTween - Client of Twitter
+// Copyright (c) 2024 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 OpenTween.Api.DataModel;
+using OpenTween.Models;
+using Xunit;
+
+namespace OpenTween.SocialProtocol.Twitter
+{
+    public class TwitterV1ClientTest
+    {
+        [Fact]
+        public void GetCursorParams_NoneTest()
+        {
+            var cursor = (IQueryCursor?)null;
+            var (sinceId, maxId) = TwitterV1Client.GetCursorParams(cursor);
+            Assert.Null(sinceId);
+            Assert.Null(maxId);
+        }
+
+        [Fact]
+        public void GetCursorParams_TopTest()
+        {
+            var cursor = new QueryCursor<TwitterStatusId>(CursorType.Top, new("11111"));
+            var (sinceId, maxId) = TwitterV1Client.GetCursorParams(cursor);
+            Assert.Equal(new("11111"), sinceId);
+            Assert.Null(maxId);
+        }
+
+        [Fact]
+        public void GetCursorParams_BottomTest()
+        {
+            var cursor = new QueryCursor<TwitterStatusId>(CursorType.Bottom, new("11111"));
+            var (sinceId, maxId) = TwitterV1Client.GetCursorParams(cursor);
+            Assert.Null(sinceId);
+            Assert.Equal(new("11111"), maxId);
+        }
+
+        [Fact]
+        public void GetCursorFromResponse_EmptyTest()
+        {
+            var statuses = Array.Empty<TwitterStatus>();
+            var (cursorTop, cursorBottom) = TwitterV1Client.GetCursorFromResponse(statuses);
+            Assert.Null(cursorTop);
+            Assert.Null(cursorBottom);
+        }
+
+        [Fact]
+        public void GetCursorFromResponse_MinMaxTest()
+        {
+            var statuses = new[]
+            {
+                new TwitterStatus
+                {
+                    IdStr = "11111",
+                },
+                new TwitterStatus
+                {
+                    IdStr = "22222",
+                },
+            };
+            var (cursorTop, cursorBottom) = TwitterV1Client.GetCursorFromResponse(statuses);
+
+            var statusIdCursorTop = Assert.IsType<QueryCursor<TwitterStatusId>>(cursorTop);
+            Assert.Equal(CursorType.Top, statusIdCursorTop.Type);
+            Assert.Equal(new("22222"), statusIdCursorTop.Value);
+
+            var statusIdCursorBottom = Assert.IsType<QueryCursor<TwitterStatusId>>(cursorBottom);
+            Assert.Equal(CursorType.Bottom, statusIdCursorBottom.Type);
+            Assert.Equal(new("11111"), statusIdCursorBottom.Value);
+        }
+    }
+}
index d0e7db9..7585839 100644 (file)
@@ -62,28 +62,12 @@ namespace OpenTween.SocialProtocol.Twitter
         {
             this.account.Legacy.CheckAccountState();
 
-            TwitterStatusId? maxId = null, sinceId = null;
-
-            if (cursor is QueryCursor<TwitterStatusId> statusIdCursor)
-            {
-                if (statusIdCursor.Type == CursorType.Top)
-                    sinceId = statusIdCursor.Value;
-                else if (statusIdCursor.Type == CursorType.Bottom)
-                    maxId = statusIdCursor.Value;
-            }
+            var (sinceId, maxId) = GetCursorParams(cursor);
 
             var statuses = await this.account.Legacy.Api.StatusesHomeTimeline(count, maxId, sinceId)
                 .ConfigureAwait(false);
 
-            IQueryCursor? cursorTop = null, cursorBottom = null;
-
-            if (statuses.Length > 0)
-            {
-                var (min, max) = statuses.Select(x => new TwitterStatusId(x.IdStr)).MinMax();
-                cursorTop = new QueryCursor<TwitterStatusId>(CursorType.Top, max);
-                cursorBottom = new QueryCursor<TwitterStatusId>(CursorType.Bottom, min);
-            }
-
+            var (cursorTop, cursorBottom) = GetCursorFromResponse(statuses);
             var posts = this.account.Legacy.CreatePostsFromJson(statuses, firstLoad);
             posts = this.account.Legacy.FilterNoRetweetUserPosts(posts);
 
@@ -94,30 +78,14 @@ namespace OpenTween.SocialProtocol.Twitter
         {
             this.account.Legacy.CheckAccountState();
 
-            TwitterStatusId? maxId = null, sinceId = null;
-
-            if (cursor is QueryCursor<TwitterStatusId> statusIdCursor)
-            {
-                if (statusIdCursor.Type == CursorType.Top)
-                    sinceId = statusIdCursor.Value;
-                else if (statusIdCursor.Type == CursorType.Bottom)
-                    maxId = statusIdCursor.Value;
-            }
+            var (sinceId, maxId) = GetCursorParams(cursor);
 
             var searchResult = await this.account.Legacy.Api.SearchTweets(query, lang, count, maxId, sinceId)
                 .ConfigureAwait(false);
 
             var statuses = searchResult.Statuses;
 
-            IQueryCursor? cursorTop = null, cursorBottom = null;
-
-            if (statuses.Length > 0)
-            {
-                var (min, max) = statuses.Select(x => new TwitterStatusId(x.IdStr)).MinMax();
-                cursorTop = new QueryCursor<TwitterStatusId>(CursorType.Top, max);
-                cursorBottom = new QueryCursor<TwitterStatusId>(CursorType.Bottom, min);
-            }
-
+            var (cursorTop, cursorBottom) = GetCursorFromResponse(statuses);
             var posts = this.account.Legacy.CreatePostsFromJson(statuses, firstLoad);
             posts = this.account.Legacy.FilterNoRetweetUserPosts(posts);
 
@@ -202,5 +170,34 @@ namespace OpenTween.SocialProtocol.Twitter
 
         private PostClass CreatePostFromJson(TwitterStatus status)
             => this.account.Legacy.CreatePostsFromStatusData(status, firstLoad: false, favTweet: false);
+
+        public static (TwitterStatusId? SinceId, TwitterStatusId? MaxId) GetCursorParams(IQueryCursor? cursor)
+        {
+            TwitterStatusId? sinceId = null, maxId = null;
+
+            if (cursor is QueryCursor<TwitterStatusId> statusIdCursor)
+            {
+                if (statusIdCursor.Type == CursorType.Top)
+                    sinceId = statusIdCursor.Value;
+                else if (statusIdCursor.Type == CursorType.Bottom)
+                    maxId = statusIdCursor.Value;
+            }
+
+            return (sinceId, maxId);
+        }
+
+        public static (IQueryCursor? CursorTop, IQueryCursor? CursorBottom) GetCursorFromResponse(TwitterStatus[] statuses)
+        {
+            IQueryCursor? cursorTop = null, cursorBottom = null;
+
+            if (statuses.Length > 0)
+            {
+                var (min, max) = statuses.Select(x => new TwitterStatusId(x.IdStr)).MinMax();
+                cursorTop = new QueryCursor<TwitterStatusId>(CursorType.Top, max);
+                cursorBottom = new QueryCursor<TwitterStatusId>(CursorType.Bottom, min);
+            }
+
+            return (cursorTop, cursorBottom);
+        }
     }
 }