File: conformance_test.rb

package info (click to toggle)
ruby-twitter-text 1.14.7%2Bconformance-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 796 kB
  • sloc: ruby: 2,917; java: 1,571; makefile: 6
file content (211 lines) | stat: -rw-r--r-- 6,987 bytes parent folder | download | duplicates (4)
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
require 'multi_json'
require 'nokogiri'
require 'test/unit'
require 'yaml'

# Detect Ruby 1.8 and older to apply necessary encoding fixes
major, minor, patch = RUBY_VERSION.split('.')
OLD_RUBY = major.to_i == 1 && minor.to_i < 9

if OLD_RUBY
  $KCODE='u'
end

$:.unshift File.join(File.dirname(__FILE__), '..', 'lib')
require 'twitter-text'

class ConformanceTest < Test::Unit::TestCase
  include Twitter::Extractor
  include Twitter::Autolink
  include Twitter::HitHighlighter
  include Twitter::Validation

  private

  %w(description expected json hits).each do |key|
    define_method key.to_sym do
      @test_info[key]
    end
  end

  if OLD_RUBY
    def text
      @test_info['text'].gsub(/\\u([0-9a-f]{8})/i) do
        [$1.to_i(16)].pack('U*')
      end
    end
  else
    def text
      @test_info['text']
    end
  end

  def assert_equal_without_attribute_order(expected, actual, failure_message = nil)
    assert_block(build_message(failure_message, "<?> expected but was\n<?>", expected, actual)) do
      equal_nodes?(Nokogiri::HTML(expected).root, Nokogiri::HTML(actual).root)
    end
  end

  def equal_nodes?(expected, actual)
    return false unless expected.name == actual.name
    return false unless ordered_attributes(expected) == ordered_attributes(actual)
    return false if expected.text? && actual.text? && expected.content != actual.content

    expected.children.each_with_index do |child, index|
      return false unless equal_nodes?(child, actual.children[index])
    end

    true
  end

  def ordered_attributes(element)
    element.attribute_nodes.map{|attr| [attr.name, attr.value]}.sort
  end

  CONFORMANCE_DIR = ENV['CONFORMANCE_DIR'] || File.expand_path("../twitter-text-conformance", __FILE__)

  def self.def_conformance_test(file, test_type, &block)
    yaml = YAML.load_file(File.join(CONFORMANCE_DIR, file))
    raise  "No such test suite: #{test_type.to_s}" unless yaml["tests"][test_type.to_s]

    file_name = file.split('.').first

    yaml["tests"][test_type.to_s].each do |test_info|
      name = :"test_#{file_name}_#{test_type} #{test_info['description']}"
      define_method name do
        @test_info = test_info
        instance_eval(&block)
      end
    end
  end

  public

  # Extractor Conformance
  def_conformance_test("extract.yml", :replies) do
    assert_equal expected, extract_reply_screen_name(text), description
  end

  def_conformance_test("extract.yml", :mentions) do
    assert_equal expected, extract_mentioned_screen_names(text), description
  end

  def_conformance_test("extract.yml", :mentions_with_indices) do
    e = expected.map{|elem| elem.inject({}){|h, (k,v)| h[k.to_sym] = v; h} }
    assert_equal e, extract_mentioned_screen_names_with_indices(text), description
  end

  def_conformance_test("extract.yml", :mentions_or_lists_with_indices) do
    e = expected.map{|elem| elem.inject({}){|h, (k,v)| h[k.to_sym] = v; h} }
    assert_equal e, extract_mentions_or_lists_with_indices(text), description
  end

  def_conformance_test("extract.yml", :urls) do
    assert_equal expected, extract_urls(text), description
    expected.each do |expected_url|
      assert_equal true, valid_url?(expected_url, true, false), "expected url [#{expected_url}] not valid"
    end
  end

  def_conformance_test("tlds.yml", :generic) do
    assert_equal expected, extract_urls(text), description
  end

  def_conformance_test("tlds.yml", :country) do
    assert_equal expected, extract_urls(text), description
  end

  def_conformance_test("extract.yml", :urls_with_indices) do
    e = expected.map{|elem| elem.inject({}){|h, (k,v)| h[k.to_sym] = v; h} }
    assert_equal e, extract_urls_with_indices(text), description
  end

  def_conformance_test("extract.yml", :hashtags) do
    assert_equal expected, extract_hashtags(text), description
  end

  def_conformance_test("extract.yml", :hashtags_from_astral) do
    assert_equal expected, extract_hashtags(text), description
  end

  def_conformance_test("extract.yml", :hashtags_with_indices) do
    e = expected.map{|elem| elem.inject({}){|h, (k,v)| h[k.to_sym] = v; h} }
    assert_equal e, extract_hashtags_with_indices(text), description
  end

  def_conformance_test("extract.yml", :cashtags) do
    assert_equal expected, extract_cashtags(text), description
  end

  def_conformance_test("extract.yml", :cashtags_with_indices) do
    e = expected.map{|elem| elem.inject({}){|h, (k,v)| h[k.to_sym] = v; h} }
    assert_equal e, extract_cashtags_with_indices(text), description
  end

  # Autolink Conformance
  def_conformance_test("autolink.yml", :usernames) do
    assert_equal_without_attribute_order expected, auto_link_usernames_or_lists(text, :suppress_no_follow => true), description
  end

  def_conformance_test("autolink.yml", :lists) do
    assert_equal_without_attribute_order expected, auto_link_usernames_or_lists(text, :suppress_no_follow => true), description
  end

  def_conformance_test("autolink.yml", :urls) do
    assert_equal_without_attribute_order expected, auto_link_urls(text, :suppress_no_follow => true), description
  end

  def_conformance_test("autolink.yml", :hashtags) do
    assert_equal_without_attribute_order expected, auto_link_hashtags(text, :suppress_no_follow => true), description
  end

  def_conformance_test("autolink.yml", :cashtags) do
    assert_equal_without_attribute_order expected, auto_link_cashtags(text, :suppress_no_follow => true), description
  end

  def_conformance_test("autolink.yml", :all) do
    assert_equal_without_attribute_order expected, auto_link(text, :suppress_no_follow => true), description
  end

  def_conformance_test("autolink.yml", :json) do
    assert_equal_without_attribute_order expected, auto_link_with_json(text, MultiJson.load(json), :suppress_no_follow => true), description
  end

  # HitHighlighter Conformance
  def_conformance_test("hit_highlighting.yml", :plain_text) do
    assert_equal expected, hit_highlight(text, hits), description
  end

  def_conformance_test("hit_highlighting.yml", :with_links) do
    assert_equal expected, hit_highlight(text, hits), description
  end

  # Validation Conformance
  def_conformance_test("validate.yml", :tweets) do
    assert_equal expected, valid_tweet_text?(text), description
  end

  def_conformance_test("validate.yml", :usernames) do
    assert_equal expected, valid_username?(text), description
  end

  def_conformance_test("validate.yml", :lists) do
    assert_equal expected, valid_list?(text), description
  end

  def_conformance_test("validate.yml", :urls) do
    assert_equal expected, valid_url?(text), description
  end

  def_conformance_test("validate.yml", :urls_without_protocol) do
    assert_equal expected, valid_url?(text, true, false), description
  end

  def_conformance_test("validate.yml", :hashtags) do
    assert_equal expected, valid_hashtag?(text), description
  end

  def_conformance_test("validate.yml", :lengths) do
    assert_equal expected, tweet_length(text), description
  end
end