1 // OpenTween - Client of Twitter
2 // Copyright (c) 2012 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
3 // All rights reserved.
5 // This file is part of OpenTween.
7 // This program is free software; you can redistribute it and/or modify it
8 // under the terms of the GNU General Public License as published by the Free
9 // Software Foundation; either version 3 of the License, or (at your option)
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 // You should have received a copy of the GNU General Public License along
18 // with this program. If not, see <http://www.gnu.org/licenses/>, or write to
19 // the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
20 // Boston, MA 02110-1301, USA.
23 using System.Collections.Generic;
25 using System.Reflection;
28 using Xunit.Extensions;
32 public class PostClassTest
34 class TestPostClass : PostClass
37 string Nickname = null,
38 string textFromApi = null,
40 string ImageUrl = null,
41 string screenName = null,
42 DateTime createdAt = new DateTime(),
47 bool IsExcludeReply = false,
48 bool IsProtect = false,
51 string InReplyToUser = null,
52 long? InReplyToStatusId = null,
55 List<string> ReplyToList = null,
59 bool FilterHit = false,
60 string RetweetedBy = null,
61 long? RetweetedId = null,
62 StatusGeo? Geo = null) :
63 base(Nickname, textFromApi, text, ImageUrl, screenName, createdAt, statusId, IsFav, IsRead,
64 IsReply, IsExcludeReply, IsProtect, IsOwl, IsMark, InReplyToUser, InReplyToStatusId, Source,
65 SourceUri, ReplyToList, IsMe, IsDm, userId, FilterHit, RetweetedBy, RetweetedId, Geo)
69 protected override PostClass RetweetSource
73 var retweetedId = this.RetweetedId.Value;
75 return PostClassTest.TestCases.ContainsKey(retweetedId) ?
76 PostClassTest.TestCases[retweetedId] :
82 private static Dictionary<long, PostClass> TestCases;
84 public PostClassTest()
86 PostClassTest.TestCases = new Dictionary<long, PostClass>
88 [1L] = new TestPostClass(statusId: 1L),
89 [2L] = new TestPostClass(statusId: 2L, IsFav: true),
90 [3L] = new TestPostClass(statusId: 3L, IsFav: false, RetweetedId: 2L),
95 [InlineData(null, null)]
97 [InlineData("aaa\nbbb", "aaa bbb")]
98 public void TextSingleLineTest(string text, string expected)
100 var post = new TestPostClass(textFromApi: text);
102 Assert.Equal(expected, post.TextSingleLine);
106 [InlineData(1L, false)]
107 [InlineData(2L, true)]
108 [InlineData(3L, true)]
109 public void GetIsFavTest(long statusId, bool expected)
111 Assert.Equal(expected, PostClassTest.TestCases[statusId].IsFav);
115 [InlineData(2L, true)]
116 [InlineData(2L, false)]
117 [InlineData(3L, true)]
118 [InlineData(3L, false)]
119 public void SetIsFavTest(long statusId, bool isFav)
121 var post = PostClassTest.TestCases[statusId];
124 Assert.Equal(isFav, post.IsFav);
126 if (post.RetweetedId != null)
127 Assert.Equal(isFav, PostClassTest.TestCases[post.RetweetedId.Value].IsFav);
131 [InlineData(false, false, false, false, -0x01)]
132 [InlineData( true, false, false, false, 0x00)]
133 [InlineData(false, true, false, false, 0x01)]
134 [InlineData( true, true, false, false, 0x02)]
135 [InlineData(false, false, true, false, 0x03)]
136 [InlineData( true, false, true, false, 0x04)]
137 [InlineData(false, true, true, false, 0x05)]
138 [InlineData( true, true, true, false, 0x06)]
139 [InlineData(false, false, false, true, 0x07)]
140 [InlineData( true, false, false, true, 0x08)]
141 [InlineData(false, true, false, true, 0x09)]
142 [InlineData( true, true, false, true, 0x0A)]
143 [InlineData(false, false, true, true, 0x0B)]
144 [InlineData( true, false, true, true, 0x0C)]
145 [InlineData(false, true, true, true, 0x0D)]
146 [InlineData( true, true, true, true, 0x0E)]
147 public void StateIndexTest(bool protect, bool mark, bool reply, bool geo, int expected)
149 var post = new TestPostClass
153 InReplyToStatusId = reply ? (long?)100L : null,
154 PostGeo = geo ? new PostClass.StatusGeo(-126.716667, -47.15) : (PostClass.StatusGeo?)null,
157 Assert.Equal(expected, post.StateIndex);
161 public void SourceHtml_Test()
163 var post = new TestPostClass
165 Source = "Twitter Web Client",
166 SourceUri = new Uri("http://twitter.com/"),
169 Assert.Equal("<a href=\"http://twitter.com/\" rel=\"nofollow\">Twitter Web Client</a>", post.SourceHtml);
173 public void SourceHtml_PlainTextTest()
175 var post = new TestPostClass
181 Assert.Equal("web", post.SourceHtml);
185 public void SourceHtml_EscapeTest()
187 var post = new TestPostClass
189 Source = "<script>alert(1)</script>",
190 SourceUri = new Uri("http://example.com/?aaa=123&bbb=456"),
193 Assert.Equal("<a href=\"http://example.com/?aaa=123&bbb=456\" rel=\"nofollow\"><script>alert(1)</script></a>", post.SourceHtml);
197 public void SourceHtml_EscapePlainTextTest()
199 var post = new TestPostClass
201 Source = "<script>alert(1)</script>",
205 Assert.Equal("<script>alert(1)</script>", post.SourceHtml);
209 public void DeleteTest()
211 var post = new TestPostClass
213 InReplyToStatusId = 10L,
214 InReplyToUser = "hogehoge",
215 InReplyToUserId = 100L,
217 ReplyToList = new List<string> {"hogehoge"},
220 post.IsDeleted = true;
222 Assert.Null(post.InReplyToStatusId);
223 Assert.Equal("", post.InReplyToUser);
224 Assert.Null(post.InReplyToUserId);
225 Assert.False(post.IsReply);
226 Assert.Empty(post.ReplyToList);
227 Assert.Equal(-1, post.StateIndex);
231 public void CanDeleteBy_SentDMTest()
233 var post = new TestPostClass
236 IsMe = true, // 自分が送信した DM
237 UserId = 222L, // 送信先ユーザーID
240 Assert.True(post.CanDeleteBy(selfUserId: 111L));
244 public void CanDeleteBy_ReceivedDMTest()
246 var post = new TestPostClass
249 IsMe = false, // 自分が受け取った DM
250 UserId = 222L, // 送信元ユーザーID
253 Assert.True(post.CanDeleteBy(selfUserId: 111L));
257 public void CanDeleteBy_MyTweetTest()
259 var post = new TestPostClass
261 UserId = 111L, // 自分のツイート
264 Assert.True(post.CanDeleteBy(selfUserId: 111L));
268 public void CanDeleteBy_OthersTweetTest()
270 var post = new TestPostClass
272 UserId = 222L, // 他人のツイート
275 Assert.False(post.CanDeleteBy(selfUserId: 111L));
279 public void CanDeleteBy_RetweetedByMeTest()
281 var post = new TestPostClass
283 RetweetedByUserId = 111L, // 自分がリツイートした
284 UserId = 222L, // 他人のツイート
287 Assert.True(post.CanDeleteBy(selfUserId: 111L));
291 public void CanDeleteBy_RetweetedByOthersTest()
293 var post = new TestPostClass
295 RetweetedByUserId = 333L, // 他人がリツイートした
296 UserId = 222L, // 他人のツイート
299 Assert.False(post.CanDeleteBy(selfUserId: 111L));
303 public void CanDeleteBy_MyTweetHaveBeenRetweetedByOthersTest()
305 var post = new TestPostClass
307 RetweetedByUserId = 222L, // 他人がリツイートした
308 UserId = 111L, // 自分のツイート
311 Assert.True(post.CanDeleteBy(selfUserId: 111L));