OSDN Git Service

Assert.Equalの引数順の誤りを修正 (xUnit2000)
[opentween/open-tween.git] / OpenTween.Tests / TweetExtractorTest.cs
1 // OpenTween - Client of Twitter
2 // Copyright (c) 2015 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.Threading.Tasks;
27 using OpenTween.Api;
28 using Xunit;
29
30 namespace OpenTween
31 {
32     public class TweetExtractorTest
33     {
34         [Fact]
35         public void ExtractUrls_Test()
36         {
37             Assert.Equal(new[] { "http://example.com/" }, TweetExtractor.ExtractUrls("http://example.com/"));
38             Assert.Equal(new[] { "http://example.com/hogehoge" }, TweetExtractor.ExtractUrls("http://example.com/hogehoge"));
39             Assert.Equal(new[] { "http://example.com/" }, TweetExtractor.ExtractUrls("hogehoge http://example.com/"));
40
41             Assert.Equal(new[] { "https://example.com/" }, TweetExtractor.ExtractUrls("https://example.com/"));
42             Assert.Equal(new[] { "https://example.com/hogehoge" }, TweetExtractor.ExtractUrls("https://example.com/hogehoge"));
43             Assert.Equal(new[] { "https://example.com/" }, TweetExtractor.ExtractUrls("hogehoge https://example.com/"));
44
45             Assert.Equal(new[] { "example.com" }, TweetExtractor.ExtractUrls("example.com"));
46             Assert.Equal(new[] { "example.com/hogehoge" }, TweetExtractor.ExtractUrls("example.com/hogehoge"));
47             Assert.Equal(new[] { "example.com" }, TweetExtractor.ExtractUrls("hogehoge example.com"));
48
49             // スキーム (http://) を省略かつ末尾が ccTLD の場合は t.co に短縮されない
50             Assert.Empty(TweetExtractor.ExtractUrls("example.jp"));
51             // ただし、末尾にパスが続く場合は t.co に短縮される
52             Assert.Equal(new[] { "example.jp/hogehoge" }, TweetExtractor.ExtractUrls("example.jp/hogehoge"));
53         }
54
55         [Fact]
56         public void ExtractUrlEntities_Test()
57         {
58             var entity = TweetExtractor.ExtractUrlEntities("hogehoge http://example.com/").Single();
59
60             Assert.Equal(new[] { 9, 28 }, entity.Indices);
61             Assert.Equal("http://example.com/", entity.Url);
62             Assert.Equal("http://example.com/", entity.ExpandedUrl);
63             Assert.Equal("http://example.com/", entity.DisplayUrl);
64         }
65
66         [Fact]
67         public void ExtractUrlEntities_SurrogatePairTest()
68         {
69             var entity = TweetExtractor.ExtractUrlEntities("✨ http://example.com/ ✨").Single();
70
71             Assert.Equal(new[] { 2, 21 }, entity.Indices);
72             Assert.Equal("http://example.com/", entity.Url);
73             Assert.Equal("http://example.com/", entity.ExpandedUrl);
74             Assert.Equal("http://example.com/", entity.DisplayUrl);
75         }
76
77         [Fact]
78         public void ExtractUrlEntities_CompositeCharacterTest()
79         {
80             // 合成文字 é ( \u00e9 ) を含むツイート (1文字としてカウントする)
81             // 参照: https://dev.twitter.com/issues/251
82             var entity = TweetExtractor.ExtractUrlEntities("Caf\u00e9 http://example.com/").Single();
83
84             Assert.Equal(new[] { 5, 24 }, entity.Indices);
85             Assert.Equal("http://example.com/", entity.Url);
86             Assert.Equal("http://example.com/", entity.ExpandedUrl);
87             Assert.Equal("http://example.com/", entity.DisplayUrl);
88         }
89
90         [Fact]
91         public void ExtractUrlEntities_CombiningCharacterSequenceTest()
92         {
93             // 結合文字列 é ( e + \u0301 ) を含むツイート (2文字としてカウントする)
94             // 参照: https://dev.twitter.com/issues/251
95             var entity = TweetExtractor.ExtractUrlEntities("Cafe\u0301 http://example.com/").Single();
96
97             Assert.Equal(new[] { 6, 25 }, entity.Indices);
98             Assert.Equal("http://example.com/", entity.Url);
99             Assert.Equal("http://example.com/", entity.ExpandedUrl);
100             Assert.Equal("http://example.com/", entity.DisplayUrl);
101         }
102
103         [Fact]
104         public void ExtractMentionEntities_Test()
105         {
106             var entity = TweetExtractor.ExtractMentionEntities("hogehoge @twitterapi").Single();
107
108             // Indices は「@twitterapi」の範囲を指すが、ScreenName には「@」を含めない
109             Assert.Equal(new[] { 9, 20 }, entity.Indices);
110             Assert.Equal("twitterapi", entity.ScreenName);
111         }
112
113         [Fact]
114         public void ExtractMentionEntities_ListTest()
115         {
116             var entity = TweetExtractor.ExtractMentionEntities("hogehoge @twitter/developers").Single();
117
118             // Indices は「@twitter/developers」の範囲を指すが、ScreenName には「@」を含めない
119             Assert.Equal(new[] { 9, 28 }, entity.Indices);
120             Assert.Equal("twitter/developers", entity.ScreenName);
121         }
122
123         [Fact]
124         public void ExtractHashtagEntities_Test()
125         {
126             var entity = TweetExtractor.ExtractHashtagEntities("hogehoge #test").Single();
127
128             // Indices は「#test」の範囲を指すが、Text には「#」を含めない
129             Assert.Equal(new[] { 9, 14 }, entity.Indices);
130             Assert.Equal("test", entity.Text);
131         }
132     }
133 }