1 // OpenTween - Client of Twitter
2 // Copyright (c) 2015 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;
26 using System.Threading.Tasks;
32 public class TweetExtractorTest
35 public void ExtractUrls_Test()
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/"));
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/"));
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"));
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"));
56 public void ExtractUrlEntities_Test()
58 var entity = TweetExtractor.ExtractUrlEntities("hogehoge http://example.com/").Single();
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);
67 public void ExtractUrlEntities_SurrogatePairTest()
69 var entity = TweetExtractor.ExtractUrlEntities("✨ http://example.com/ ✨").Single();
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);
78 public void ExtractUrlEntities_CompositeCharacterTest()
80 // 合成文字 é ( \u00e9 ) を含むツイート (1文字としてカウントする)
81 // 参照: https://dev.twitter.com/issues/251
82 var entity = TweetExtractor.ExtractUrlEntities("Caf\u00e9 http://example.com/").Single();
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);
91 public void ExtractUrlEntities_CombiningCharacterSequenceTest()
93 // 結合文字列 é ( e + \u0301 ) を含むツイート (2文字としてカウントする)
94 // 参照: https://dev.twitter.com/issues/251
95 var entity = TweetExtractor.ExtractUrlEntities("Cafe\u0301 http://example.com/").Single();
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);