Skip to content

Commit

Permalink
Merge pull request #337 from opentween/twitter-user-id
Browse files Browse the repository at this point in the history
TwitterUserIdクラスを追加
  • Loading branch information
upsilon authored May 17, 2024
2 parents f67a774 + b80e3aa commit e0730fe
Show file tree
Hide file tree
Showing 36 changed files with 519 additions and 221 deletions.
3 changes: 2 additions & 1 deletion OpenTween.Tests/Api/GraphQL/CreateTweetRequestTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
using System.Threading.Tasks;
using Moq;
using OpenTween.Connection;
using OpenTween.Models;
using Xunit;

namespace OpenTween.Api.GraphQL
Expand Down Expand Up @@ -78,7 +79,7 @@ public async Task Send_ReplyTest()
{
TweetText = "tetete",
InReplyToTweetId = new("12345"),
ExcludeReplyUserIds = new[] { "11111", "22222" },
ExcludeReplyUserIds = new TwitterUserId[] { new("11111"), new("22222") },
};
await request.Send(mock.Object);
mock.VerifyAll();
Expand Down
4 changes: 2 additions & 2 deletions OpenTween.Tests/Api/GraphQL/LikesRequestTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public async Task Send_Test()

var request = new LikesRequest
{
UserId = "12345",
UserId = new("12345"),
Count = 20,
};

Expand Down Expand Up @@ -83,7 +83,7 @@ public async Task Send_RequestCursor_Test()

var request = new LikesRequest
{
UserId = "12345",
UserId = new("12345"),
Count = 20,
Cursor = new("aaa"),
};
Expand Down
30 changes: 15 additions & 15 deletions OpenTween.Tests/Api/GraphQL/TimelineTweetTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ public void ToStatus_WithTwitterPostFactory_SimpleTweet_Test()
var timelineTweet = new TimelineTweet(rootElm);
var status = timelineTweet.ToTwitterStatus();
var postFactory = new TwitterPostFactory(this.CreateTabInfo(), new());
var post = postFactory.CreateFromStatus(status, selfUserId: 1L, new HashSet<long>(), firstLoad: false);
var post = postFactory.CreateFromStatus(status, selfUserId: new("1"), new HashSet<PersonId>(), firstLoad: false);

Assert.Equal("1613784711020826626", post.StatusId.Id);
Assert.Equal(40480664L, post.UserId);
Assert.Equal(new TwitterUserId("40480664"), post.UserId);
Assert.False(post.IsPromoted);
}

Expand All @@ -87,10 +87,10 @@ public void ToStatus_WithTwitterPostFactory_TweetWithMedia_Test()
var timelineTweet = new TimelineTweet(rootElm);
var status = timelineTweet.ToTwitterStatus();
var postFactory = new TwitterPostFactory(this.CreateTabInfo(), new());
var post = postFactory.CreateFromStatus(status, selfUserId: 1L, new HashSet<long>(), firstLoad: false);
var post = postFactory.CreateFromStatus(status, selfUserId: new("1"), new HashSet<PersonId>(), firstLoad: false);

Assert.Equal("1614587968567783424", post.StatusId.Id);
Assert.Equal(40480664L, post.UserId);
Assert.Equal(new TwitterUserId("40480664"), post.UserId);
Assert.Equal(2, post.Media.Count);
Assert.Equal("https://pbs.twimg.com/media/FmgrJiEaAAEU42G.png", post.Media[0].Url);
Assert.Equal("OpenTweenで @opentween のツイート一覧を表示しているスクショ", post.Media[0].AltText);
Expand All @@ -105,12 +105,12 @@ public void ToStatus_WithTwitterPostFactory_RetweetedTweet_Test()
var timelineTweet = new TimelineTweet(rootElm);
var status = timelineTweet.ToTwitterStatus();
var postFactory = new TwitterPostFactory(this.CreateTabInfo(), new());
var post = postFactory.CreateFromStatus(status, selfUserId: 1L, new HashSet<long>(), firstLoad: false);
var post = postFactory.CreateFromStatus(status, selfUserId: new("1"), new HashSet<PersonId>(), firstLoad: false);

Assert.Equal("1617128268548964354", post.StatusId.Id);
Assert.Equal(40480664L, post.RetweetedByUserId);
Assert.Equal(new TwitterUserId("40480664"), post.RetweetedByUserId);
Assert.Equal("1617126084138659840", post.RetweetedId!.Id);
Assert.Equal(514241801L, post.UserId);
Assert.Equal(new TwitterUserId("514241801"), post.UserId);
}

[Fact]
Expand All @@ -120,10 +120,10 @@ public void ToStatus_WithTwitterPostFactory_TweetWithVisibility_Test()
var timelineTweet = new TimelineTweet(rootElm);
var status = timelineTweet.ToTwitterStatus();
var postFactory = new TwitterPostFactory(this.CreateTabInfo(), new());
var post = postFactory.CreateFromStatus(status, selfUserId: 1L, new HashSet<long>(), firstLoad: false);
var post = postFactory.CreateFromStatus(status, selfUserId: new("1"), new HashSet<PersonId>(), firstLoad: false);

Assert.Equal("1602775353088524288", post.StatusId.Id);
Assert.Equal(357750891L, post.UserId);
Assert.Equal(new TwitterUserId("357750891"), post.UserId);
}

[Fact]
Expand All @@ -133,10 +133,10 @@ public void ToStatus_WithTwitterPostFactory_SelfThread_Test()
var timelineTweet = new TimelineTweet(rootElm);
var status = timelineTweet.ToTwitterStatus();
var postFactory = new TwitterPostFactory(this.CreateTabInfo(), new());
var post = postFactory.CreateFromStatus(status, selfUserId: 1L, new HashSet<long>(), firstLoad: false);
var post = postFactory.CreateFromStatus(status, selfUserId: new("1"), new HashSet<PersonId>(), firstLoad: false);

Assert.Equal("1511751702684499968", post.StatusId.Id);
Assert.Equal(40480664L, post.UserId);
Assert.Equal(new TwitterUserId("40480664"), post.UserId);
}

[Fact]
Expand All @@ -146,7 +146,7 @@ public void ToStatus_WithTwitterPostFactory_QuotedTweet_Test()
var timelineTweet = new TimelineTweet(rootElm);
var status = timelineTweet.ToTwitterStatus();
var postFactory = new TwitterPostFactory(this.CreateTabInfo(), new());
var post = postFactory.CreateFromStatus(status, selfUserId: 1L, new HashSet<long>(), firstLoad: false);
var post = postFactory.CreateFromStatus(status, selfUserId: new("1"), new HashSet<PersonId>(), firstLoad: false);

Assert.Equal("1588614645866147840", post.StatusId.Id);
var quotedPostId = Assert.Single(post.QuoteStatusIds);
Expand All @@ -160,7 +160,7 @@ public void ToStatus_WithTwitterPostFactory_QuotedTweet_Tombstone_Test()
var timelineTweet = new TimelineTweet(rootElm);
var status = timelineTweet.ToTwitterStatus();
var postFactory = new TwitterPostFactory(this.CreateTabInfo(), new());
var post = postFactory.CreateFromStatus(status, selfUserId: 1L, new HashSet<long>(), firstLoad: false);
var post = postFactory.CreateFromStatus(status, selfUserId: new("1"), new HashSet<PersonId>(), firstLoad: false);

Assert.Equal("1614653321310253057", post.StatusId.Id);
var quotedPostId = Assert.Single(post.QuoteStatusIds);
Expand All @@ -174,10 +174,10 @@ public void ToStatus_WithTwitterPostFactory_PromotedTweet_Test()
var timelineTweet = new TimelineTweet(rootElm);
var status = timelineTweet.ToTwitterStatus();
var postFactory = new TwitterPostFactory(this.CreateTabInfo(), new());
var post = postFactory.CreateFromStatus(status, selfUserId: 1L, new HashSet<long>(), firstLoad: false);
var post = postFactory.CreateFromStatus(status, selfUserId: new("1"), new HashSet<PersonId>(), firstLoad: false);

Assert.Equal("1674737917363888129", post.StatusId.Id);
Assert.Equal(2941313791L, post.UserId);
Assert.Equal(new TwitterUserId("2941313791"), post.UserId);
Assert.True(post.IsPromoted);
Assert.Matches(new Regex(@"^\[Promoted\]\n"), post.TextFromApi);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
using System.Threading.Tasks;
using Moq;
using OpenTween.Connection;
using OpenTween.Models;
using Xunit;

namespace OpenTween.Api.GraphQL
Expand Down Expand Up @@ -49,7 +50,7 @@ public async Task Send_Test()
})
.ReturnsAsync(apiResponse);

var request = new UserTweetsAndRepliesRequest(userId: "40480664")
var request = new UserTweetsAndRepliesRequest(userId: new("40480664"))
{
Count = 20,
};
Expand Down Expand Up @@ -83,7 +84,7 @@ public async Task Send_RequestCursor_Test()
})
.ReturnsAsync(apiResponse);

var request = new UserTweetsAndRepliesRequest(userId: "40480664")
var request = new UserTweetsAndRepliesRequest(userId: new("40480664"))
{
Count = 20,
Cursor = new("aaa"),
Expand Down
9 changes: 5 additions & 4 deletions OpenTween.Tests/Api/TwitterApiTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
using Moq;
using OpenTween.Api.DataModel;
using OpenTween.Connection;
using OpenTween.Models;
using Xunit;

namespace OpenTween.Api
Expand Down Expand Up @@ -276,7 +277,7 @@ await twitterApi.StatusesUpdate(
replyToId: new("100"),
mediaIds: new[] { 10L, 20L },
autoPopulateReplyMetadata: true,
excludeReplyUserIds: new[] { 100L, 200L },
excludeReplyUserIds: new TwitterUserId[] { new("100"), new("200") },
attachmentUrl: "https://twitter.com/twitterapi/status/22634515958"
)
.IgnoreResponse();
Expand Down Expand Up @@ -304,7 +305,7 @@ public async Task StatusesUpdate_ExcludeReplyUserIdsEmptyTest()
using var twitterApi = new TwitterApi();
twitterApi.ApiConnection = mock.Object;

await twitterApi.StatusesUpdate("hogehoge", replyToId: null, mediaIds: null, excludeReplyUserIds: Array.Empty<long>())
await twitterApi.StatusesUpdate("hogehoge", replyToId: null, mediaIds: null, excludeReplyUserIds: Array.Empty<TwitterUserId>())
.IgnoreResponse();

mock.VerifyAll();
Expand Down Expand Up @@ -770,7 +771,7 @@ public async Task DirectMessagesEventsNew_Test()
using var twitterApi = new TwitterApi();
twitterApi.ApiConnection = mock.Object;

await twitterApi.DirectMessagesEventsNew(recipientId: 12345L, text: "hogehoge", mediaId: 67890L);
await twitterApi.DirectMessagesEventsNew(recipientId: new("12345"), text: "hogehoge", mediaId: 67890L);

mock.VerifyAll();
}
Expand Down Expand Up @@ -847,7 +848,7 @@ public async Task UsersLookup_Test()
using var twitterApi = new TwitterApi();
twitterApi.ApiConnection = mock.Object;

await twitterApi.UsersLookup(userIds: new[] { "11111", "22222" });
await twitterApi.UsersLookup(userIds: new TwitterUserId[] { new("11111"), new("22222") });

mock.VerifyAll();
}
Expand Down
157 changes: 157 additions & 0 deletions OpenTween.Tests/Models/PersonIdTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
// 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 Moq;
using Xunit;

namespace OpenTween.Models
{
public class PersonIdTest
{
private PersonId CreatePersonId(string type, string id)
{
var mock = new Mock<PersonId>() { CallBase = true };
mock.Setup(x => x.IdType).Returns(type);
mock.Setup(x => x.Id).Returns(id);
return mock.Object;
}

[Fact]
public void CompareTo_Test()
{
var a = this.CreatePersonId("mastodon", "200");
var b = this.CreatePersonId("twitter", "100");
Assert.True(a.CompareTo(b) < 0);
Assert.True(b.CompareTo(a) > 0);
Assert.Equal(0, a.CompareTo(a));
}

[Fact]
public void CompareTo_SameIdTypeTest()
{
var a = this.CreatePersonId("twitter", "100");
var b = this.CreatePersonId("twitter", "200");
Assert.True(a.CompareTo(b) < 0);
Assert.True(b.CompareTo(a) > 0);
Assert.Equal(0, a.CompareTo(a));
}

[Fact]
public void CompareTo_IdLengthTest()
{
var a = this.CreatePersonId("twitter", "200");
var b = this.CreatePersonId("twitter", "1000");
Assert.True(a.CompareTo(b) < 0);
Assert.True(b.CompareTo(a) > 0);
}

[Fact]
public void OperatorGreaterThan_Test()
{
var a = this.CreatePersonId("twitter", "100");
var b = this.CreatePersonId("twitter", "200");
#pragma warning disable CS1718
Assert.False(a < a);
Assert.True(a < b);
Assert.False(b < a);
Assert.False(b < b);
Assert.True(a <= a);
Assert.True(a <= b);
Assert.False(b <= a);
Assert.True(b <= b);
#pragma warning restore CS1718
}

[Fact]
public void OperatorLessThan_Test()
{
var a = this.CreatePersonId("twitter", "100");
var b = this.CreatePersonId("twitter", "200");
#pragma warning disable CS1718
Assert.False(a > a);
Assert.False(a > b);
Assert.True(b > a);
Assert.False(b > b);
Assert.True(a >= a);
Assert.False(a >= b);
Assert.True(b >= a);
Assert.True(b >= b);
#pragma warning restore CS1718
}

[Fact]
public void Equals_Test()
{
var a = this.CreatePersonId("twitter", "100");
var b = this.CreatePersonId("twitter", "100");
Assert.True(a.Equals(b));
Assert.True(b.Equals(a));
Assert.True(a == b);
Assert.True(b == a);
}

[Fact]
public void Equals_NotSameIdTypeTest()
{
var a = this.CreatePersonId("mastodon", "100");
var b = this.CreatePersonId("twitter", "100");
Assert.False(a.Equals(b));
Assert.False(b.Equals(a));
Assert.True(a != b);
Assert.True(b != a);
}

[Fact]
public void Equals_NotSameIdTest()
{
var a = this.CreatePersonId("twitter", "100");
var b = this.CreatePersonId("twitter", "200");
Assert.False(a.Equals(b));
Assert.False(b.Equals(a));
Assert.True(a != b);
Assert.True(b != a);
}

[Fact]
public void GetHashCode_SameIdTest()
{
var a = this.CreatePersonId("twitter", "100");
var b = this.CreatePersonId("twitter", "100");
Assert.Equal(a.GetHashCode(), b.GetHashCode());
}

[Fact]
public void GetHashCode_NotSameIdTypeTest()
{
var a = this.CreatePersonId("mastodon", "100");
var b = this.CreatePersonId("twitter", "100");
Assert.NotEqual(a.GetHashCode(), b.GetHashCode());
}

[Fact]
public void GetHashCode_NotSameIdTest()
{
var a = this.CreatePersonId("twitter", "100");
var b = this.CreatePersonId("twitter", "200");
Assert.NotEqual(a.GetHashCode(), b.GetHashCode());
}
}
}
Loading

0 comments on commit e0730fe

Please sign in to comment.