OSDN Git Service

Nullable<T> 型を積極的に使うように変更
[opentween/open-tween.git] / OpenTween.Tests / TwitterTest.cs
1 // OpenTween - Client of Twitter
2 // Copyright (c) 2013 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.Text;
26 using System.Text.RegularExpressions;
27 using NUnit.Framework;
28
29 namespace OpenTween
30 {
31     [TestFixture]
32     class TwitterTest
33     {
34         [TestCase("https://twitter.com/twitterapi/status/22634515958",
35             Result = new[] { "22634515958" })]
36         [TestCase("<a target=\"_self\" href=\"https://t.co/aaaaaaaa\" title=\"https://twitter.com/twitterapi/status/22634515958\">twitter.com/twitterapi/stat…</a>",
37             Result = new[] { "22634515958" })]
38         [TestCase("<a target=\"_self\" href=\"https://t.co/bU3oR95KIy\" title=\"https://twitter.com/haru067/status/224782458816692224\">https://t.co/bU3oR95KIy</a>" +
39             "<a target=\"_self\" href=\"https://t.co/bbbbbbbb\" title=\"https://twitter.com/karno/status/311081657790771200\">https://t.co/bbbbbbbb</a>",
40             Result = new[] { "224782458816692224", "311081657790771200" })]
41         [TestCase("https://mobile.twitter.com/muji_net/status/21984934471",
42             Result = new[] { "21984934471" })]
43         [TestCase("https://twitter.com/imgazyobuzi/status/293333871171354624/photo/1",
44             Result = new[] { "293333871171354624" })]
45         public string[] StatusUrlRegexTest(string url)
46         {
47             return Twitter.StatusUrlRegex.Matches(url).Cast<Match>()
48                 .Select(x => x.Groups["StatusId"].Value).ToArray();
49         }
50
51         [TestCase("http://favstar.fm/users/twitterapi/status/22634515958",
52             Result = new[] { "22634515958" })]
53         [TestCase("http://ja.favstar.fm/users/twitterapi/status/22634515958",
54             Result = new[] { "22634515958" })]
55         [TestCase("http://favstar.fm/t/22634515958",
56             Result = new[] { "22634515958" })]
57         [TestCase("http://aclog.koba789.com/i/312485321239564288",
58             Result = new[] { "312485321239564288" })]
59         [TestCase("http://frtrt.net/solo_status.php?status=263483634307198977",
60             Result = new[] { "263483634307198977" })]
61         public string[] ThirdPartyStatusUrlRegexTest(string url)
62         {
63             return Twitter.ThirdPartyStatusUrlRegex.Matches(url).Cast<Match>()
64                 .Select(x => x.Groups["StatusId"].Value).ToArray();
65         }
66
67         [Test]
68         public void FindTopOfReplyChainTest()
69         {
70             var posts = new Dictionary<long, PostClass>
71             {
72                 {950L, new PostClass { StatusId = 950L, InReplyToStatusId = null }}, // このツイートが末端
73                 {987L, new PostClass { StatusId = 987L, InReplyToStatusId = 950L }},
74                 {999L, new PostClass { StatusId = 999L, InReplyToStatusId = 987L }},
75                 {1000L, new PostClass { StatusId = 1000L, InReplyToStatusId = 999L }},
76             };
77             Assert.That(Twitter.FindTopOfReplyChain(posts, 1000L).StatusId, Is.EqualTo(950L));
78             Assert.That(Twitter.FindTopOfReplyChain(posts, 950L).StatusId, Is.EqualTo(950L));
79             Assert.That(() => Twitter.FindTopOfReplyChain(posts, 500L), Throws.ArgumentException);
80
81             posts = new Dictionary<long, PostClass>
82             {
83                 // 1200L は posts の中に存在しない
84                 {1210L, new PostClass { StatusId = 1210L, InReplyToStatusId = 1200L }},
85                 {1220L, new PostClass { StatusId = 1220L, InReplyToStatusId = 1210L }},
86                 {1230L, new PostClass { StatusId = 1230L, InReplyToStatusId = 1220L }},
87             };
88             Assert.That(Twitter.FindTopOfReplyChain(posts, 1230L).StatusId, Is.EqualTo(1210L));
89             Assert.That(Twitter.FindTopOfReplyChain(posts, 1210L).StatusId, Is.EqualTo(1210L));
90         }
91     }
92 }