-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathPostClassTest.cs
More file actions
463 lines (391 loc) · 14.8 KB
/
Copy pathPostClassTest.cs
File metadata and controls
463 lines (391 loc) · 14.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
// 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.Text;
using System.Threading.Tasks;
using Xunit;
using Xunit.Extensions;
namespace OpenTween.Models
{
public class PostClassTest
{
private class PostClassGroup
{
private readonly Dictionary<long, PostClass> testCases;
public PostClassGroup(params TestPostClass[] postClasses)
{
this.testCases = new Dictionary<long, PostClass>();
foreach (var p in postClasses)
{
p.Group = this;
this.testCases.Add(p.StatusId, p);
}
}
public PostClass this[long id] => this.testCases[id];
}
private class TestPostClass : PostClass
{
public PostClassGroup? Group;
protected override PostClass RetweetSource
{
get
{
var retweetedId = this.RetweetedId;
var group = this.Group;
if (group == null)
throw new InvalidOperationException("TestPostClass needs group");
return group[retweetedId];
}
}
}
private readonly PostClassGroup postGroup;
public PostClassTest()
{
this.postGroup = new PostClassGroup(
new TestPostClass { StatusId = 1L },
new TestPostClass { StatusId = 2L, IsFav = true },
new TestPostClass { StatusId = 3L, IsFav = false, RetweetedId = 2L });
}
[Fact]
public void CloneTest()
{
var post = new PostClass();
var clonePost = post.Clone();
TestUtils.CheckDeepCloning(post, clonePost);
}
[Theory]
[InlineData("", "")]
[InlineData("aaa\nbbb", "aaa bbb")]
public void TextSingleLineTest(string text, string expected)
{
var post = new PostClass { TextFromApi = text };
Assert.Equal(expected, post.TextSingleLine);
}
[Theory]
[InlineData(1L, false)]
[InlineData(2L, true)]
[InlineData(3L, true)]
public void GetIsFavTest(long statusId, bool expected)
=> Assert.Equal(expected, this.postGroup[statusId].IsFav);
[Theory]
[InlineData(2L, true)]
[InlineData(2L, false)]
[InlineData(3L, true)]
[InlineData(3L, false)]
public void SetIsFavTest(long statusId, bool isFav)
{
var post = this.postGroup[statusId];
post.IsFav = isFav;
Assert.Equal(isFav, post.IsFav);
if (post.IsRetweet)
Assert.Equal(isFav, this.postGroup[post.RetweetedId].IsFav);
}
#pragma warning disable SA1008 // Opening parenthesis should be spaced correctly
[Theory]
[InlineData(false, false, false, false, -0x01)]
[InlineData( true, false, false, false, 0x00)]
[InlineData(false, true, false, false, 0x01)]
[InlineData( true, true, false, false, 0x02)]
[InlineData(false, false, true, false, 0x03)]
[InlineData( true, false, true, false, 0x04)]
[InlineData(false, true, true, false, 0x05)]
[InlineData( true, true, true, false, 0x06)]
[InlineData(false, false, false, true, 0x07)]
[InlineData( true, false, false, true, 0x08)]
[InlineData(false, true, false, true, 0x09)]
[InlineData( true, true, false, true, 0x0A)]
[InlineData(false, false, true, true, 0x0B)]
[InlineData( true, false, true, true, 0x0C)]
[InlineData(false, true, true, true, 0x0D)]
[InlineData( true, true, true, true, 0x0E)]
#pragma warning restore SA1008
public void StateIndexTest(bool protect, bool mark, bool reply, bool geo, int expected)
{
var post = new TestPostClass
{
IsProtect = protect,
IsMark = mark,
PostGeo = geo ? new PostClass.StatusGeo(-126.716667, -47.15) : (PostClass.StatusGeo?)null,
};
if (reply)
post.InReplyToStatusId = 100L;
Assert.Equal(expected, post.StateIndex);
}
[Fact]
public void SourceHtml_Test()
{
var post = new TestPostClass
{
Source = "Twitter Web Client",
SourceUri = new Uri("http://twitter.com/"),
};
Assert.Equal("<a href=\"http://twitter.com/\" rel=\"nofollow\">Twitter Web Client</a>", post.SourceHtml);
}
[Fact]
public void SourceHtml_PlainTextTest()
{
var post = new TestPostClass
{
Source = "web",
SourceUri = null,
};
Assert.Equal("web", post.SourceHtml);
}
[Fact]
public void SourceHtml_EscapeTest()
{
var post = new TestPostClass
{
Source = "<script>alert(1)</script>",
SourceUri = new Uri("http://example.com/?aaa=123&bbb=456"),
};
Assert.Equal("<a href=\"http://example.com/?aaa=123&bbb=456\" rel=\"nofollow\"><script>alert(1)</script></a>", post.SourceHtml);
}
[Fact]
public void SourceHtml_EscapePlainTextTest()
{
var post = new TestPostClass
{
Source = "<script>alert(1)</script>",
SourceUri = null,
};
Assert.Equal("<script>alert(1)</script>", post.SourceHtml);
}
[Fact]
public void DeleteTest()
{
var post = new TestPostClass
{
InReplyToStatusId = 10L,
InReplyToUser = "hogehoge",
InReplyToUserId = 100L,
IsReply = true,
ReplyToList = { (100L, "hogehoge") },
};
post.IsDeleted = true;
Assert.False(post.HasInReplyTo);
Assert.False(post.IsReply);
Assert.Empty(post.ReplyToList);
Assert.Equal(-1, post.StateIndex);
}
[Fact]
public void CanDeleteBy_SentDMTest()
{
var post = new TestPostClass
{
IsDm = true,
IsMe = true, // 自分が送信した DM
UserId = 222L, // 送信先ユーザーID
};
Assert.True(post.CanDeleteBy(selfUserId: 111L));
}
[Fact]
public void CanDeleteBy_ReceivedDMTest()
{
var post = new TestPostClass
{
IsDm = true,
IsMe = false, // 自分が受け取った DM
UserId = 222L, // 送信元ユーザーID
};
Assert.True(post.CanDeleteBy(selfUserId: 111L));
}
[Fact]
public void CanDeleteBy_MyTweetTest()
{
var post = new TestPostClass
{
UserId = 111L, // 自分のツイート
};
Assert.True(post.CanDeleteBy(selfUserId: 111L));
}
[Fact]
public void CanDeleteBy_OthersTweetTest()
{
var post = new TestPostClass
{
UserId = 222L, // 他人のツイート
};
Assert.False(post.CanDeleteBy(selfUserId: 111L));
}
[Fact]
public void CanDeleteBy_RetweetedByMeTest()
{
var post = new TestPostClass
{
RetweetedId = 100L,
RetweetedByUserId = 111L, // 自分がリツイートした
UserId = 222L, // 他人のツイート
};
Assert.True(post.CanDeleteBy(selfUserId: 111L));
}
[Fact]
public void CanDeleteBy_RetweetedByOthersTest()
{
var post = new TestPostClass
{
RetweetedId = 100L,
RetweetedByUserId = 333L, // 他人がリツイートした
UserId = 222L, // 他人のツイート
};
Assert.False(post.CanDeleteBy(selfUserId: 111L));
}
[Fact]
public void CanDeleteBy_MyTweetHaveBeenRetweetedByOthersTest()
{
var post = new TestPostClass
{
RetweetedId = 100L,
RetweetedByUserId = 222L, // 他人がリツイートした
UserId = 111L, // 自分のツイート
};
Assert.True(post.CanDeleteBy(selfUserId: 111L));
}
[Fact]
public void CanRetweetBy_DMTest()
{
var post = new TestPostClass
{
IsDm = true,
IsMe = false, // 自分が受け取った DM
UserId = 222L, // 送信元ユーザーID
};
Assert.False(post.CanRetweetBy(selfUserId: 111L));
}
[Fact]
public void CanRetweetBy_MyTweetTest()
{
var post = new TestPostClass
{
UserId = 111L, // 自分のツイート
};
Assert.True(post.CanRetweetBy(selfUserId: 111L));
}
[Fact]
public void CanRetweetBy_ProtectedMyTweetTest()
{
var post = new TestPostClass
{
UserId = 111L, // 自分のツイート
IsProtect = true,
};
Assert.True(post.CanRetweetBy(selfUserId: 111L));
}
[Fact]
public void CanRetweetBy_OthersTweet_NotProtectedTest()
{
var post = new TestPostClass
{
UserId = 222L, // 他人のツイート
IsProtect = false,
};
Assert.True(post.CanRetweetBy(selfUserId: 111L));
}
[Fact]
public void CanRetweetBy_OthersTweet_ProtectedTest()
{
var post = new TestPostClass
{
UserId = 222L, // 他人のツイート
IsProtect = true,
};
Assert.False(post.CanRetweetBy(selfUserId: 111L));
}
[Fact]
public void ConvertToOriginalPost_Test()
{
var retweetPost = new PostClass
{
StatusId = 100L,
ScreenName = "@aaa",
UserId = 1L,
RetweetedId = 50L,
RetweetedBy = "@bbb",
RetweetedByUserId = 2L,
RetweetedCount = 0,
};
var originalPost = retweetPost.ConvertToOriginalPost();
Assert.Equal(50L, originalPost.StatusId);
Assert.Equal("@aaa", originalPost.ScreenName);
Assert.Equal(1L, originalPost.UserId);
Assert.False(originalPost.IsRetweet);
Assert.Equal(1, originalPost.RetweetedCount);
}
[Fact]
public void ConvertToOriginalPost_ErrorTest()
{
// 公式 RT でないツイート
var post = new PostClass { StatusId = 100L };
Assert.Throws<InvalidOperationException>(() => post.ConvertToOriginalPost());
}
private class FakeExpandedUrlInfo : PostClass.ExpandedUrlInfo
{
public TaskCompletionSource<string> FakeResult = new();
public FakeExpandedUrlInfo(string url, string expandedUrl, bool deepExpand)
: base(url, expandedUrl, deepExpand)
{
}
protected override async Task DeepExpandAsync()
=> this.expandedUrl = await this.FakeResult.Task;
}
[Fact]
public async Task ExpandedUrls_BasicScenario()
{
PostClass.ExpandedUrlInfo.AutoExpand = true;
var post = new PostClass
{
Text = "<a href=\"http://t.co/aaaaaaa\" title=\"http://t.co/aaaaaaa\">bit.ly/abcde</a>",
ExpandedUrls = new[]
{
new FakeExpandedUrlInfo(
// 展開前の t.co ドメインの URL
url: "http://t.co/aaaaaaa",
// Entity の expanded_url に含まれる URL
expandedUrl: "http://bit.ly/abcde",
// expandedUrl をさらに ShortUrl クラスで再帰的に展開する
deepExpand: true
),
},
};
var urlInfo = (FakeExpandedUrlInfo)post.ExpandedUrls.Single();
// ExpandedUrlInfo による展開が完了していない状態
// → この段階では Entity に含まれる expanded_url の URL が使用される
Assert.False(urlInfo.ExpandedCompleted);
Assert.Equal("http://bit.ly/abcde", urlInfo.ExpandedUrl);
Assert.Equal("http://bit.ly/abcde", post.GetExpandedUrl("http://t.co/aaaaaaa"));
Assert.Equal(new[] { "http://bit.ly/abcde" }, post.GetExpandedUrls());
Assert.Equal("<a href=\"http://t.co/aaaaaaa\" title=\"http://bit.ly/abcde\">bit.ly/abcde</a>", post.Text);
// bit.ly 展開後の URL は「http://example.com/abcde」
urlInfo.FakeResult.SetResult("http://example.com/abcde");
await urlInfo.ExpandTask;
// ExpandedUrlInfo による展開が完了した後の状態
// → 再帰的な展開後の URL が使用される
Assert.True(urlInfo.ExpandedCompleted);
Assert.Equal("http://example.com/abcde", urlInfo.ExpandedUrl);
Assert.Equal("http://example.com/abcde", post.GetExpandedUrl("http://t.co/aaaaaaa"));
Assert.Equal(new[] { "http://example.com/abcde" }, post.GetExpandedUrls());
Assert.Equal("<a href=\"http://t.co/aaaaaaa\" title=\"http://example.com/abcde\">bit.ly/abcde</a>", post.Text);
}
}
}