aboutsummaryrefslogtreecommitdiffstats
path: root/test/core/test_entity.rb
blob: b1d274e6f32a2b9695dfb6983a088e615a7f801a (plain)
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
# -*- coding: utf-8 -*-

require File.expand_path(File.dirname(__FILE__) + '/../helper')
#require File.expand_path(File.dirname(__FILE__) + '/../utils')
miquire :core, 'entity'
miquire :core, 'userconfig'

class Plugin
end

class String
  def inspect
    to_s
  end
end

module Pango
  ESCAPE_RULE = {'&' => '&amp;' ,'>' => '&gt;', '<' => '&lt;'}.freeze
  class << self

    # テキストをPango.parse_markupで安全にパースできるようにエスケープする。
    def escape(text)
      text.gsub(/[<>&]/){|m| Pango::ESCAPE_RULE[m] } end end end


class TC_Message < Test::Unit::TestCase

  THE_TWEET = '変な意味じゃなくてね。RT @kouichi_0308: アレでソレですか…(´・ω・`)シュン… RT @nene_loveplus: 昔から一緒にいるフォロワーさんは色々アレでソレでちょっと困っちゃうわね…。'
  THE_ENTITY = {
    :user_mentions => [ { :id_str => "127914421",
                          :screen_name => "kouichi_0308",
                          :name => "コウイチ(2011年度版)",
                          :id => 127914421,
                          :indices => [14, 27]},
                        { :id_str => "95126742",
                          :screen_name => "nene_loveplus",
                          :name=>"姉ヶ崎寧々",
                          :id=>95126742,
                          :indices=>[53, 67]}],
    :urls => [],
    :hashtags => [] }

  THE_TWEET2 = '&#9829; Unfaithful by Rihanna #lastfm: http://bit.ly/3YP9Hq amazon: http://bit.ly/1tmPYb'
  THE_ENTITY2 = {
    :user_mentions=>[],
    :hashtags=> [ { :text=>"lastfm",
                    :indices=>[30, 37] } ],
    :urls=> [ { :expanded_url=>nil,
                :indices=>[39, 59],
                :url=>"http://bit.ly/3YP9Hq" },
              { :expanded_url=>nil,
                :indices=>[68, 88],
                :url=>"http://bit.ly/1tmPYb" } ] }

  THE_TWEET3 = 'RT @toshi_a: おいmikutterが変態ツイッタークライアントだという風説が'
  THE_ENTITY3 = {}

  def setup
    Plugin.stubs(:call).returns(true)
    Message::Entity.addlinkrule(:urls, URI.regexp(['http','https'])){ |segment| }
    Message::Entity.addlinkrule(:media){ |segment| }
    Message::Entity.addlinkrule(:hashtags, /(#|#)([a-zA-Z0-9_]+)/){ |segment|}
    Message::Entity.addlinkrule(:user_mentions, /(@|@|〄)[a-zA-Z0-9_]+/){ |segment| }
  end

  def teardown
    Message::Entity.refresh
  end

  def test_index_to_escaped_index
    assert_equal(5, Message::Entity.index_to_escaped_index("foobar", 5))
    assert_equal(2, Message::Entity.index_to_escaped_index("<foobar>", 5))
    assert_equal(5, Message::Entity.index_to_escaped_index("<foo><bar>", 11))
    assert_equal(5, Message::Entity.index_to_escaped_index("<&>abc", 15))
  end

  def test_1
    mes = stub
    mes.stubs(:to_show).returns(THE_TWEET)
    mes.stubs(:[]).with(:entities).returns(THE_ENTITY)
    mes.stubs(:is_a?).with(Message).returns(true)

    entity = Message::Entity.new(mes)

    assert_equal("変な意味じゃなくてね。RT @kouichi_0308: アレでソレですか…(´・ω・`)シュン… RT @nene_loveplus: 昔から一緒にいるフォロワーさんは色々アレでソレでちょっと困っちゃうわね…。", entity.to_s)

    splited = mes.to_show.split(//u).map{ |s| Pango::ESCAPE_RULE[s] || s }
    entity.reverse_each{ |l|
      splited[l[:range]] = '<span underline="single" underline_color="#000000">'+"#{Pango.escape(l[:face])}</span>"
    }
    splited
  end

  def test_3
    mes = stub
    mes.stubs(:to_show).returns(THE_TWEET3)
    mes.stubs(:[]).with(:entities).returns(THE_ENTITY3)
    mes.stubs(:is_a?).with(Message).returns(true)
    entity = Message::Entity.new(mes)
    assert_kind_of(String, entity.to_s)
    assert_equal("RT @toshi_a: \343\201\212\343\201\204mikutter\343\201\214\345\244\211\346\205\213\343\203\204\343\202\244\343\203\203\343\202\277\343\203\274\343\202\257\343\203\251\343\202\244\343\202\242\343\203\263\343\203\210\343\201\240\343\201\250\343\201\204\343\201\206\351\242\250\350\252\254\343\201\214", entity.to_s.inspect)
  end

  def test_4
    tweet = 'もともとは@penguin2716さんに勧められて始めたついった。そして彼より遅く始めた自分がTwitterにハマり、彼の総ポスト数をすぐに追い抜いたと思ったのですが、今や彼はれっきとしたmikutter廃人となり、ワタシの手の届かぬ領域に到達されました。'
    mes = stub
    mes.stubs(:to_show).returns(tweet)
    mes.stubs(:[]).with(:entities).returns({})
    mes.stubs(:is_a?).with(Message).returns(true)
    entity = Message::Entity.new(mes)

    assert_kind_of(String, entity.to_s)
    assert_equal(tweet, entity.to_s.inspect)
  end

  def test_5
    tweet = '一体何をやってんだろう(笑)。 > @toshi_a the hacker'
    mes = stub
    mes.stubs(:to_show).returns(tweet)
    mes.stubs(:[]).with(:entities).returns({:hashtags=>[], :urls=>[], :user_mentions=>[{:name=>"蝶舞スカーフ型としぁ", :screen_name=>"toshi_a", :indices=>[21, 29], :id=>15926668, :id_str=>"15926668"}]})
    mes.stubs(:is_a?).with(Message).returns(true)
    entity = Message::Entity.new(mes)

    assert_kind_of(String, entity.to_s)
    assert_equal(tweet, entity.to_s.inspect)
  end

  def test_6
    Plugin.stubs(:filtering).with(:is_expanded, 'goo.gl/2tsIG').returns([true])
    tweet = 'まだまだ絶賛配信中!今日は「日常のラヂオ」第35回がランティスネットラジオ goo.gl/2tsIG にて22時から配信スタートです!「日常」が好きな人ならきっと楽しんでいただけますのでよろしくお願いします。 #nichijou'
    mes = stub
    mes.stubs(:to_show).returns(tweet)
    mes.stubs(:[]).with(:entities).
      returns({ :user_mentions=>[],
                :urls     => [{ :url=>"goo.gl/2tsIG",
                                :indices=>[38, 50],
                                :expanded_url=>nil } ],
                :hashtags => [{ :indices=>[105, 114],
                                :text=>"nichijou" } ]})
    mes.stubs(:is_a?).with(Message).returns(true)
    entity = Message::Entity.new(mes)

    assert_kind_of(String, entity.to_s)
    assert_equal(tweet, entity.to_s.inspect)
  end

  def test_7
    tweet = "@uebayasi I tried and it works as below:\n$ uname -srm\nNetBSD 6.0.1 amd64\n$  s ( ) { local b; b=1; echo $b; b=\"$@\"; echo $b; }\n$ s d e\n1\nd e"
    mes = stub
    mes.stubs(:to_show).returns(tweet)
    mes.stubs(:[]).with(:id).returns(325862212793159680)
    mes.stubs(:[]).with(:message).returns(tweet)
    mes.stubs(:[]).with(:entities).
      returns({ hashtags: [], 
                symbols: [{ indices: [103, 105], 
                            text: "b"},
                          { indices: [120, 122],
                            text: "b"}],
                urls: [], 
                user_mentions: [{ id: 5864792, 
                                  id_str: "5864792", 
                                  indices: [0, 9],
                                  name: "Masao Uebayashi", 
                                  screen_name: "uebayasi" }]})
    mes.stubs(:is_a?).with(Message).returns(true)
    entity = Message::Entity.new(mes)

    assert_kind_of(String, entity.to_s)
    assert_equal(tweet, entity.to_s.inspect)
  end

  def test_8
    Plugin.stubs(:filtering).with(:expand_url, 'http://t.co/zYaNWyebgm').returns(["http://goo.gl/HDK5i"])
    Plugin.stubs(:filtering).with(:is_expanded, 'http://t.co/zYaNWyebgm').returns([false])
    Plugin.stubs(:filtering).with(:is_expanded, 'http://goo.gl/HDK5i').returns([true])
    Plugin.stubs(:filtering).with(:expand_url, 'https://t.co/DCbLaYFXeu').returns(["http://twitter.com/ph_toei/status/352644565875974144/photo/1"])
    Plugin.stubs(:filtering).with(:is_expanded, 'https://t.co/DCbLaYFXeu').returns([false])
    mes = stub
    mes.stubs(:to_show).returns("【特価品】Celeron C1037U&HM77、デュアルLAN(82574L)を搭載したNAS向miniITXマザー Giada N70E-DR V2 14980円 http://t.co/zYaNWyebgm https://t.co/DCbLaYFXeu")
    mes.stubs(:[]).with(:id).returns(429203799404593152)
    mes.stubs(:[]).with(:message).returns("【特価品】Celeron C1037U&amp;HM77、デュアルLAN(82574L)を搭載したNAS向miniITXマザー Giada N70E-DR V2 14980円 http://t.co/zYaNWyebgm https://t.co/DCbLaYFXeu")
    mes.stubs(:[]).with(:entities).
      returns({ hashtags: [],
                symbols: [],
                urls: 
                [{ url: "http://t.co/zYaNWyebgm",
                   expanded_url: "http://goo.gl/HDK5i",
                   display_url: "goo.gl/HDK5i",
                   indices: [88, 110]}],
                user_mentions: [],
                media: 
                [{ id: 352644565880168448,
                   id_str: "352644565880168448",
                   indices: [111, 134],
                   media_url: "http://pbs.twimg.com/media/BOTYXUFCYAAGmf_.jpg",
                   media_url_https: "https://pbs.twimg.com/media/BOTYXUFCYAAGmf_.jpg",
                   url: "https://t.co/DCbLaYFXeu",
                   display_url: "pic.twitter.com/DCbLaYFXeu",
                   expanded_url: 
                   "http://twitter.com/ph_toei/status/352644565875974144/photo/1",
                   type: "photo",
                   sizes: 
                   { medium: {w: 480, h: 437, resize: "fit"},
                     thumb: {w: 150, h: 150, resize: "crop"},
                     small: {w: 340, h: 310, resize: "fit"},
                     large: {w: 480, h: 437, resize: "fit"}},
                   source_status_id: 352644565875974144,
                   source_status_id_str: "352644565875974144"}]})
    mes.stubs(:is_a?).with(Message).returns(true)
    entity = Message::Entity.new(mes)

    assert_kind_of(String, entity.to_s)
    assert_equal("【特価品】Celeron C1037U&HM77、デュアルLAN(82574L)を搭載したNAS向miniITXマザー Giada N70E-DR V2 14980円 goo.gl/HDK5i pic.twitter.com/DCbLaYFXeu", entity.to_s.inspect)
  end

end