1 // OpenTween - Client of Twitter
2 // Copyright (c) 2014 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 OpenTween.Api.DataModel;
28 using Xunit.Extensions;
32 public class TweetFormatterTest
35 public void FormatUrlEntity_Test()
37 var text = "http://t.co/6IwepKCM0P";
42 Indices = new[] { 0, 22 },
43 DisplayUrl = "example.com",
44 ExpandedUrl = "http://example.com/",
45 Url = "http://t.co/6IwepKCM0P",
49 var expected = "<a href=\"http://t.co/6IwepKCM0P\" title=\"http://example.com/\">example.com</a>";
50 Assert.Equal(expected, TweetFormatter.AutoLinkHtml(text, entities));
54 public void FormatUrlEntity_TwitterComTest()
56 var text = "https://t.co/0Ko1I27m0a";
61 Indices = new[] { 0, 23 },
62 DisplayUrl = "twitter.com/twitterapi",
63 ExpandedUrl = "https://twitter.com/twitterapi",
64 Url = "https://t.co/0Ko1I27m0a",
68 // twitter.com 宛のリンクは t.co を経由せずにリンクする
69 var expected = "<a href=\"https://twitter.com/twitterapi\" title=\"https://twitter.com/twitterapi\">twitter.com/twitterapi</a>";
70 Assert.Equal(expected, TweetFormatter.AutoLinkHtml(text, entities));
74 public void FormatHashtagEntity_Test()
76 var text = "#OpenTween";
79 new TwitterEntityHashtag
81 Indices = new[] { 0, 10 },
86 var expected = "<a class=\"hashtag\" href=\"https://twitter.com/search?q=%23OpenTween\">#OpenTween</a>";
87 Assert.Equal(expected, TweetFormatter.AutoLinkHtml(text, entities));
91 public void FormatMentionEntity_Test()
93 var text = "@TwitterAPI";
96 new TwitterEntityMention
98 Indices = new[] { 0, 11 },
100 Name = "Twitter API",
101 ScreenName = "twitterapi",
105 var expected = "<a class=\"mention\" href=\"https://twitter.com/twitterapi\">@TwitterAPI</a>";
106 Assert.Equal(expected, TweetFormatter.AutoLinkHtml(text, entities));
110 public void FormatMediaEntity_Test()
112 var text = "http://t.co/h5dCr4ftN4";
115 new TwitterEntityMedia
117 Indices = new[] { 0, 22 },
118 Sizes = new TwitterMediaSizes
120 Large = new TwitterMediaSizes.Size { Resize = "fit", Height = 329, Width = 1024 },
121 Medium = new TwitterMediaSizes.Size { Resize = "fit", Height = 204, Width = 600 },
122 Small = new TwitterMediaSizes.Size { Resize = "fit", Height = 116, Width = 340 },
123 Thumb = new TwitterMediaSizes.Size { Resize = "crop", Height = 150, Width = 150 },
126 Id = 426404550379986940L,
127 MediaUrlHttps = "https://pbs.twimg.com/media/BerkrewCYAAV4Kf.png",
128 Url = "http://t.co/h5dCr4ftN4",
129 DisplayUrl = "pic.twitter.com/h5dCr4ftN4",
130 ExpandedUrl = "http://twitter.com/kim_upsilon/status/426404550371598337/photo/1",
134 var expected = "<a href=\"http://t.co/h5dCr4ftN4\" title=\"http://twitter.com/kim_upsilon/status/426404550371598337/photo/1\">pic.twitter.com/h5dCr4ftN4</a>";
135 Assert.Equal(expected, TweetFormatter.AutoLinkHtml(text, entities));
139 public void FormatMediaEntity_AltTextTest()
141 var text = "http://t.co/h5dCr4ftN4";
144 new TwitterEntityMedia
146 Indices = new[] { 0, 22 },
147 Sizes = new TwitterMediaSizes
149 Large = new TwitterMediaSizes.Size { Resize = "fit", Height = 329, Width = 1024 },
150 Medium = new TwitterMediaSizes.Size { Resize = "fit", Height = 204, Width = 600 },
151 Small = new TwitterMediaSizes.Size { Resize = "fit", Height = 116, Width = 340 },
152 Thumb = new TwitterMediaSizes.Size { Resize = "crop", Height = 150, Width = 150 },
155 Id = 426404550379986940L,
156 MediaUrlHttps = "https://pbs.twimg.com/media/BerkrewCYAAV4Kf.png",
157 Url = "http://t.co/h5dCr4ftN4",
158 DisplayUrl = "pic.twitter.com/h5dCr4ftN4",
159 ExpandedUrl = "http://twitter.com/kim_upsilon/status/426404550371598337/photo/1",
160 AltText = "ATOKの推測変換候補のスクリーンショット",
164 var expected = "<a href=\"http://t.co/h5dCr4ftN4\" title=\"ATOKの推測変換候補のスクリーンショット\">pic.twitter.com/h5dCr4ftN4</a>";
165 Assert.Equal(expected, TweetFormatter.AutoLinkHtml(text, entities));
170 public void FormatEmojiEntity_Test()
175 new TwitterEntityEmoji
177 Indices = new[] { 0, 1 },
179 Url = "https://twemoji.maxcdn.com/2/72x72/1f363.png",
183 var expected = "<img class=\"emoji\" src=\"https://twemoji.maxcdn.com/2/72x72/1f363.png\" alt=\"🍣\" />";
184 Assert.Equal(expected, TweetFormatter.AutoLinkHtml(text, entities));
187 public void AutoLinkHtml_EntityNullTest()
189 var text = "てすとてすとー";
190 TwitterEntities entities = null;
192 var expected = "てすとてすとー";
193 Assert.Equal(expected, TweetFormatter.AutoLinkHtml(text, entities));
197 public void AutoLinkHtml_EntityNullTest2()
199 var text = "てすとてすとー";
200 TwitterEntities entities = new TwitterEntities
208 var expected = "てすとてすとー";
209 Assert.Equal(expected, TweetFormatter.AutoLinkHtml(text, entities));
213 public void AutoLinkHtml_EntityNullTest3()
215 var text = "てすとてすとー";
216 IEnumerable<TwitterEntity> entities = null;
218 var expected = "てすとてすとー";
219 Assert.Equal(expected, TweetFormatter.AutoLinkHtml(text, entities));
223 public void AutoLinkHtml_EntityNullTest4()
225 var text = "てすとてすとー";
226 IEnumerable<TwitterEntity> entities = new TwitterEntity[] { null };
228 var expected = "てすとてすとー";
229 Assert.Equal(expected, TweetFormatter.AutoLinkHtml(text, entities));
233 public void AutoLinkHtml_EscapeTest()
235 // Twitter APIの中途半端なエスケープの対象とならない「"」や「'」に対するエスケープ処理を施す
236 var text = "\"\'@twitterapi\'\"";
239 new TwitterEntityMention
241 Indices = new[] { 2, 13 },
243 Name = "Twitter API",
244 ScreenName = "twitterapi",
248 var expected = ""'<a class=\"mention\" href=\"https://twitter.com/twitterapi\">@twitterapi</a>'"";
249 Assert.Equal(expected, TweetFormatter.AutoLinkHtml(text, entities));
253 public void AutoLinkHtml_EscapeTest2()
255 // 「<」や「>」についてはエスケープされた状態でAPIからテキストが返されるため、二重エスケープとならないように考慮する
256 var text = "<b> @twitterapi </b>";
259 new TwitterEntityMention
261 Indices = new[] { 10, 21 },
263 Name = "Twitter API",
264 ScreenName = "twitterapi",
268 var expected = "<b> <a class=\"mention\" href=\"https://twitter.com/twitterapi\">@twitterapi</a> </b>";
269 Assert.Equal(expected, TweetFormatter.AutoLinkHtml(text, entities));
273 public void AutoLinkHtml_EscapeTest3()
275 // 万が一「<」や「>」がエスケープされていない状態のテキストを受け取っても適切にエスケープが施されるようにする
276 var text = "<b> @twitterapi </b>";
279 new TwitterEntityMention
281 Indices = new[] { 4, 15 },
283 Name = "Twitter API",
284 ScreenName = "twitterapi",
288 var expected = "<b> <a class=\"mention\" href=\"https://twitter.com/twitterapi\">@twitterapi</a> </b>";
289 Assert.Equal(expected, TweetFormatter.AutoLinkHtml(text, entities));
293 public void AutoLinkHtml_EscapeUrlTest()
295 // 日本語ハッシュタグのリンク先URLを適切にエスケープする
296 var text = "#ぜんぶ雪のせいだ";
299 new TwitterEntityHashtag
301 Indices = new[] { 0, 9 },
306 var expected = "<a class=\"hashtag\" href=\"https://twitter.com/search?q=%23%E3%81%9C%E3%82%93%E3%81%B6%E9%9B%AA%E3%81%AE%E3%81%9B%E3%81%84%E3%81%A0\">#ぜんぶ雪のせいだ</a>";
307 Assert.Equal(expected, TweetFormatter.AutoLinkHtml(text, entities));
311 public void AutoLinkHtml_SurrogatePairTest()
313 // UTF-16 で 4 バイトで表される文字を含むツイート
314 // 参照: https://sourceforge.jp/ticket/browse.php?group_id=6526&tid=33079
315 var text = "🐬🐬 @irucame 🐬🐬";
318 new TwitterEntityMention
320 Indices = new[] { 3, 11 },
322 ScreenName = "irucame",
326 var expected = "🐬🐬 <a class=\"mention\" href=\"https://twitter.com/irucame\">@irucame</a> 🐬🐬";
327 Assert.Equal(expected, TweetFormatter.AutoLinkHtml(text, entities));
331 public void AutoLinkHtml_SurrogatePairTest2()
333 // 現時点では存在しないものの、ハッシュタグなどエンティティ内にサロゲートペアが含まれる場合も考慮する
334 var text = "🐬🐬 #🐬🐬 🐬🐬 #🐬🐬 🐬🐬";
337 new TwitterEntityHashtag
339 Indices = new[] { 3, 6 },
342 new TwitterEntityHashtag
344 Indices = new[] { 10, 13 },
349 var expected = "🐬🐬 <a class=\"hashtag\" href=\"https://twitter.com/search?q=%23%F0%9F%90%AC%F0%9F%90%AC\">#🐬🐬</a> " +
350 "🐬🐬 <a class=\"hashtag\" href=\"https://twitter.com/search?q=%23%F0%9F%90%AC%F0%9F%90%AC\">#🐬🐬</a> 🐬🐬";
351 Assert.Equal(expected, TweetFormatter.AutoLinkHtml(text, entities));
355 public void AutoLinkHtml_CompositeCharacterTest()
357 // 合成文字 é ( \u00e9 ) を含むツイート
358 // 参照: https://dev.twitter.com/issues/251
359 var text = "Caf\u00e9 #test";
362 new TwitterEntityHashtag
364 Indices = new[] { 5, 10 },
369 var expected = "Caf\u00e9 <a class=\"hashtag\" href=\"https://twitter.com/search?q=%23test\">#test</a>";
370 Assert.Equal(expected, TweetFormatter.AutoLinkHtml(text, entities));
374 public void AutoLinkHtml_CombiningCharacterSequenceTest()
376 // 結合文字列 é ( e + \u0301 ) を含むツイート
377 // 参照: https://dev.twitter.com/issues/251
378 var text = "Cafe\u0301 #test";
381 new TwitterEntityHashtag
383 Indices = new[] { 6, 11 },
388 var expected = "Cafe\u0301 <a class=\"hashtag\" href=\"https://twitter.com/search?q=%23test\">#test</a>";
389 Assert.Equal(expected, TweetFormatter.AutoLinkHtml(text, entities));
393 public void AutoLinkHtml_BreakLineTest()
395 var text = "てすと\nてすと\nてすと";
396 TwitterEntities entities = null;
398 var expected = "てすと<br>てすと<br>てすと";
399 Assert.Equal(expected, TweetFormatter.AutoLinkHtml(text, entities));
403 public void AutoLinkHtml_WhitespaceTest()
405 // 連続する半角スペースを表示するため 1 文字おきに に変換する
406 var text = "a a a a a";
408 var expected = "a a a a a";
409 Assert.Equal(expected, TweetFormatter.AutoLinkHtml(text, entities: null));
413 public void AutoLinkHtml_OverlappedEntitiesTest()
415 // extended_entities で追加される、区間が重複したエンティティを考慮
416 // 参照: https://dev.twitter.com/docs/api/multiple-media-extended-entities
418 var text = "\"I hope you'll keep...building bonds of friendship that will enrich your lives & enrich our world\" \u2014FLOTUS in China, http://t.co/fxmuQN9JL9";
421 new TwitterEntityMedia
423 DisplayUrl = "pic.twitter.com/fxmuQN9JL9",
424 ExpandedUrl = "http://twitter.com/FLOTUS/status/449660889793581056/photo/1",
425 Indices = new[] { 121, 143 },
426 MediaUrlHttps = "https://pbs.twimg.com/media/Bj2EH6yIQAEYvxu.jpg",
427 Url = "http://t.co/fxmuQN9JL9",
429 new TwitterEntityMedia
431 DisplayUrl = "pic.twitter.com/fxmuQN9JL9",
432 ExpandedUrl = "http://twitter.com/FLOTUS/status/449660889793581056/photo/1",
433 Indices = new[] { 121, 143 },
434 MediaUrlHttps = "https://pbs.twimg.com/media/Bj2EHxAIIAE8dtg.jpg",
435 Url = "http://t.co/fxmuQN9JL9",
437 new TwitterEntityMedia
439 DisplayUrl = "pic.twitter.com/fxmuQN9JL9",
440 ExpandedUrl = "http://twitter.com/FLOTUS/status/449660889793581056/photo/1",
441 Indices = new[] { 121, 143 },
442 MediaUrlHttps = "https://pbs.twimg.com/media/Bj2EH3pIYAE4LQn.jpg",
443 Url = "http://t.co/fxmuQN9JL9",
445 new TwitterEntityMedia
447 DisplayUrl = "pic.twitter.com/fxmuQN9JL9",
448 ExpandedUrl = "http://twitter.com/FLOTUS/status/449660889793581056/photo/1",
449 Indices = new[] { 121, 143 },
450 MediaUrlHttps = "https://pbs.twimg.com/media/Bj2EL3DIEAAzGAX.jpg",
451 Url = "http://t.co/fxmuQN9JL9",
455 var expected = ""I hope you'll keep...building bonds of friendship that will enrich your lives & enrich our world" \u2014FLOTUS in China, " +
456 "<a href=\"http://t.co/fxmuQN9JL9\" title=\"http://twitter.com/FLOTUS/status/449660889793581056/photo/1\">pic.twitter.com/fxmuQN9JL9</a>";
457 Assert.Equal(expected, TweetFormatter.AutoLinkHtml(text, entities));