OSDN Git Service

PostClass.Cloneメソッドを廃止
[opentween/open-tween.git] / OpenTween.Tests / PostClassTest.cs
1 // OpenTween - Client of Twitter
2 // Copyright (c) 2012 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
3 // All rights reserved.
4 //
5 // This file is part of OpenTween.
6 //
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)
10 // any later version.
11 //
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
15 // for more details.
16 //
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.
21
22 using System;
23 using System.Collections.Generic;
24 using System.Linq;
25 using System.Reflection;
26 using System.Text;
27 using Xunit;
28 using Xunit.Extensions;
29
30 namespace OpenTween
31 {
32     public class PostClassTest
33     {
34         class TestPostClass : PostClass
35         {
36             public TestPostClass(
37                 string Nickname = null,
38                 string textFromApi = null,
39                 string text = null,
40                 string ImageUrl = null,
41                 string screenName = null,
42                 DateTime createdAt = new DateTime(),
43                 long statusId = 0L,
44                 bool IsFav = false,
45                 bool IsRead = false,
46                 bool IsReply = false,
47                 bool IsExcludeReply = false,
48                 bool IsProtect = false,
49                 bool IsOwl = false,
50                 bool IsMark = false,
51                 string InReplyToUser = null,
52                 long? InReplyToStatusId = null,
53                 string Source = null,
54                 Uri SourceUri = null,
55                 List<string> ReplyToList = null,
56                 bool IsMe = false,
57                 bool IsDm = false,
58                 long userId = 0L,
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)
66             {
67             }
68
69             protected override PostClass RetweetSource
70             {
71                 get
72                 {
73                     var retweetedId = this.RetweetedId.Value;
74
75                     return PostClassTest.TestCases.ContainsKey(retweetedId) ?
76                         PostClassTest.TestCases[retweetedId] :
77                         null;
78                 }
79             }
80         }
81
82         private static Dictionary<long, PostClass> TestCases;
83
84         public PostClassTest()
85         {
86             PostClassTest.TestCases = new Dictionary<long, PostClass>
87             {
88                 [1L] = new TestPostClass(statusId: 1L),
89                 [2L] = new TestPostClass(statusId: 2L, IsFav: true),
90                 [3L] = new TestPostClass(statusId: 3L, IsFav: false, RetweetedId: 2L),
91             };
92         }
93
94         [Theory]
95         [InlineData(null,  null)]
96         [InlineData("", "")]
97         [InlineData("aaa\nbbb", "aaa bbb")]
98         public void TextSingleLineTest(string text, string expected)
99         {
100             var post = new TestPostClass(textFromApi: text);
101
102             Assert.Equal(expected, post.TextSingleLine);
103         }
104
105         [Theory]
106         [InlineData(1L, false)]
107         [InlineData(2L, true)]
108         [InlineData(3L, true)]
109         public void GetIsFavTest(long statusId, bool expected)
110         {
111             Assert.Equal(expected, PostClassTest.TestCases[statusId].IsFav);
112         }
113
114         [Theory]
115         [InlineData(2L, true)]
116         [InlineData(2L, false)]
117         [InlineData(3L, true)]
118         [InlineData(3L, false)]
119         public void SetIsFavTest(long statusId, bool isFav)
120         {
121             var post = PostClassTest.TestCases[statusId];
122
123             post.IsFav = isFav;
124             Assert.Equal(isFav, post.IsFav);
125
126             if (post.RetweetedId != null)
127                 Assert.Equal(isFav, PostClassTest.TestCases[post.RetweetedId.Value].IsFav);
128         }
129
130         [Theory]
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)
148         {
149             var post = new TestPostClass
150             {
151                 IsProtect = protect,
152                 IsMark = mark,
153                 InReplyToStatusId = reply ? (long?)100L : null,
154                 PostGeo = geo ? new PostClass.StatusGeo(-126.716667, -47.15) : (PostClass.StatusGeo?)null,
155             };
156
157             Assert.Equal(expected, post.StateIndex);
158         }
159
160         [Fact]
161         public void SourceHtml_Test()
162         {
163             var post = new TestPostClass
164             {
165                 Source = "Twitter Web Client",
166                 SourceUri = new Uri("http://twitter.com/"),
167             };
168
169             Assert.Equal("<a href=\"http://twitter.com/\" rel=\"nofollow\">Twitter Web Client</a>", post.SourceHtml);
170         }
171
172         [Fact]
173         public void SourceHtml_PlainTextTest()
174         {
175             var post = new TestPostClass
176             {
177                 Source = "web",
178                 SourceUri = null,
179             };
180
181             Assert.Equal("web", post.SourceHtml);
182         }
183
184         [Fact]
185         public void SourceHtml_EscapeTest()
186         {
187             var post = new TestPostClass
188             {
189                 Source = "<script>alert(1)</script>",
190                 SourceUri = new Uri("http://example.com/?aaa=123&bbb=456"),
191             };
192
193             Assert.Equal("<a href=\"http://example.com/?aaa=123&amp;bbb=456\" rel=\"nofollow\">&lt;script&gt;alert(1)&lt;/script&gt;</a>", post.SourceHtml);
194         }
195
196         [Fact]
197         public void SourceHtml_EscapePlainTextTest()
198         {
199             var post = new TestPostClass
200             {
201                 Source = "<script>alert(1)</script>",
202                 SourceUri = null,
203             };
204
205             Assert.Equal("&lt;script&gt;alert(1)&lt;/script&gt;", post.SourceHtml);
206         }
207
208         [Fact]
209         public void DeleteTest()
210         {
211             var post = new TestPostClass
212             {
213                 InReplyToStatusId = 10L,
214                 InReplyToUser = "hogehoge",
215                 InReplyToUserId = 100L,
216                 IsReply = true,
217                 ReplyToList = new List<string> {"hogehoge"},
218             };
219
220             post.IsDeleted = true;
221
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);
228         }
229
230         [Fact]
231         public void CanDeleteBy_SentDMTest()
232         {
233             var post = new TestPostClass
234             {
235                 IsDm = true,
236                 IsMe = true, // 自分が送信した DM
237                 UserId = 222L, // 送信先ユーザーID
238             };
239
240             Assert.True(post.CanDeleteBy(selfUserId: 111L));
241         }
242
243         [Fact]
244         public void CanDeleteBy_ReceivedDMTest()
245         {
246             var post = new TestPostClass
247             {
248                 IsDm = true,
249                 IsMe = false, // 自分が受け取った DM
250                 UserId = 222L, // 送信元ユーザーID
251             };
252
253             Assert.True(post.CanDeleteBy(selfUserId: 111L));
254         }
255
256         [Fact]
257         public void CanDeleteBy_MyTweetTest()
258         {
259             var post = new TestPostClass
260             {
261                 UserId = 111L, // 自分のツイート
262             };
263
264             Assert.True(post.CanDeleteBy(selfUserId: 111L));
265         }
266
267         [Fact]
268         public void CanDeleteBy_OthersTweetTest()
269         {
270             var post = new TestPostClass
271             {
272                 UserId = 222L, // 他人のツイート
273             };
274
275             Assert.False(post.CanDeleteBy(selfUserId: 111L));
276         }
277
278         [Fact]
279         public void CanDeleteBy_RetweetedByMeTest()
280         {
281             var post = new TestPostClass
282             {
283                 RetweetedByUserId = 111L, // 自分がリツイートした
284                 UserId = 222L, // 他人のツイート
285             };
286
287             Assert.True(post.CanDeleteBy(selfUserId: 111L));
288         }
289
290         [Fact]
291         public void CanDeleteBy_RetweetedByOthersTest()
292         {
293             var post = new TestPostClass
294             {
295                 RetweetedByUserId = 333L, // 他人がリツイートした
296                 UserId = 222L, // 他人のツイート
297             };
298
299             Assert.False(post.CanDeleteBy(selfUserId: 111L));
300         }
301
302         [Fact]
303         public void CanDeleteBy_MyTweetHaveBeenRetweetedByOthersTest()
304         {
305             var post = new TestPostClass
306             {
307                 RetweetedByUserId = 222L, // 他人がリツイートした
308                 UserId = 111L, // 自分のツイート
309             };
310
311             Assert.True(post.CanDeleteBy(selfUserId: 111L));
312         }
313     }
314 }