camdict 1.0.3 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,71 +1,67 @@
1
- require 'minitest/autorun'
2
- require 'camdict'
1
+ # frozen_string_literal: true
2
+ require_relative 'helper'
3
3
 
4
4
  module Camdict
5
5
  class ExplanationTest < Minitest::Test
6
-
7
6
  def test_get_level
8
7
  html = '<span class="def-info"><span class="epp-xref B1">B1</span>'
9
8
  html = Nokogiri::HTML(html)
10
9
  exp = Camdict::Explanation.new(html)
11
- assert_equal 'B1', (exp.send :get_level)
10
+ assert_equal 'B1', exp.level
12
11
  end
13
12
 
14
13
  def test_get_meaning
15
- html = '<span class="def">in <a class="query" href="http://cambridge' +
16
- '.org/british">agreement </a>with the true facts or with what is ' +
17
- 'generally accepted:'
14
+ html = '<span class="def">in <a class="query" href="http://cambridge' \
15
+ '.org/british">agreement </a>with the true facts or with what ' \
16
+ 'is generally accepted:'
18
17
  html = Nokogiri::HTML(html)
19
18
  exp = Camdict::Explanation.new(html)
20
- expected =
19
+ expected =
21
20
  'in agreement with the true facts or with what is generally accepted:'
22
- assert_equal expected, (exp.send :get_meaning)
21
+ assert_equal expected, exp.meaning
23
22
  end
24
23
 
25
- def test_gc
24
+ def test_code
26
25
  # rubber has region, usage, gc
27
26
  html = '<span class="def-info"><span class="gcs">U</span>'
28
27
  html = Nokogiri::HTML(html)
29
28
  exp = Camdict::Explanation.new(html)
30
- assert_equal 'U', exp.gc
29
+ assert_equal 'U', exp.code
31
30
  end
32
31
 
33
32
  def test_get_sentence
34
33
  html = '<span class="eg">a correct answer</span>'
35
34
  html = Nokogiri::HTML(html)
36
- exp = Camdict::Explanation::Sentence.new(html)
37
- assert_equal 'a correct answer', (exp.send :get_sentence)
35
+ exp = Camdict::Sentence.new(html)
36
+ assert_equal 'a correct answer', exp.sentence
38
37
  end
39
38
 
40
39
  def test_get_examples
41
- sent1 = %q(It's not correct to describe them as 'student')
42
- sent2 = %q("Your name is Angela Black?""That is correct.")
40
+ sent1 = %(It's not correct to describe them as 'student')
41
+ sent2 = %("Your name is Angela Black?""That is correct.")
43
42
  html = "<span class='examp'><span class='eg'>#{sent1}</span></span>"
44
- html += "<span class='examp'><span class='eg'>#{sent2}</span>" +
45
- '<span class="usage">formal</span>'
43
+ html += "<span class='examp'><span class='eg'>#{sent2}</span>" \
44
+ '<span class="usage">formal</span>'
46
45
  html = Nokogiri::HTML(html)
47
- exp = Camdict::Explanation.new(html)
48
- expected = exp.send :get_examples
49
- e1, e2 = expected.flatten
46
+ e1, e2 = Camdict::Explanation.new(html).examples
50
47
  assert_equal sent1, e1.sentence
51
48
  assert_equal sent2, e2.sentence
52
- assert_equal "formal", e2.usage
49
+ assert_equal 'formal', e2.usage
53
50
  end
54
51
 
55
52
  def test_get_synonym
56
53
  html = '<span class="entry-xref" type="Synonym"><span class="x-h">right'
57
54
  html = Nokogiri::HTML(html)
58
55
  exp = Camdict::Explanation.new(html)
59
- assert_equal "right", (exp.send :get_synonym)
56
+ assert_equal 'right', exp.synonym
60
57
  end
61
58
 
62
59
  def test_get_opposite
63
- html =
60
+ html =
64
61
  '<span class="entry-xref" type="Opposite"><span class="x-h">incorrect'
65
62
  html = Nokogiri::HTML(html)
66
63
  exp = Camdict::Explanation.new(html)
67
- assert_equal "incorrect", (exp.send :get_opposite)
64
+ assert_equal 'incorrect', exp.opposite
68
65
  end
69
-
70
66
  end
71
67
  end
@@ -1,28 +1,42 @@
1
+ # frozen_string_literal: true
1
2
  require 'minitest/autorun'
2
3
  require 'camdict'
3
4
 
4
5
  module Camdict
5
6
  class HTTPClientTest < Minitest::Test
6
-
7
7
  def test_get_html
8
8
  require 'webrick'
9
9
 
10
- server = WEBrick::HTTPServer.new(:Port=>0, :BindAddress=>"127.0.0.1",
11
- :Logger=>WEBrick::Log.new(nil,WEBrick::BasicLog::FATAL))
12
- Thread.new {
13
- res = Proc.new { |r, q|
14
- q.body ="hello"
15
- }
10
+ server = WEBrick::HTTPServer.new(
11
+ Port: 0, BindAddress: '127.0.0.1',
12
+ Logger: WEBrick::Log.new(nil, WEBrick::BasicLog::FATAL)
13
+ )
14
+ start_hello_thread(server)
15
+ http_hello(server)
16
+ end
17
+
18
+ def test_self_get_html
19
+ assert Camdict::HTTP::Client.respond_to? :get_html
20
+ assert Camdict::HTTP::Client.new.respond_to? :get_html
21
+ end
22
+
23
+ def start_hello_thread(server)
24
+ Thread.new do
25
+ res = proc do |_r, q|
26
+ q.body = 'hello'
27
+ end
16
28
  server.mount_proc '/hi', nil, &res
17
29
  server.start
18
- }
19
- Thread.new {
30
+ end
31
+ end
32
+
33
+ def http_hello(server)
34
+ Thread.new do
20
35
  url = "http://127.0.0.1:#{server.config[:Port]}/hi"
21
36
  page = Camdict::HTTP::Client.get_html(url)
22
- server.stop
23
- assert_equal "hello", page.text
24
- }.join
37
+ server.stop
38
+ assert_equal 'hello', page.text
39
+ end.join
25
40
  end
26
-
27
41
  end
28
42
  end
@@ -0,0 +1,95 @@
1
+ # frozen_string_literal: true
2
+ require_relative 'helper'
3
+
4
+ module Camdict
5
+ class StringExtTest < Minitest::Test
6
+ using Camdict::StringExt
7
+
8
+ def test_slash_in_middle
9
+ str = 'blow a kiss to/at sb'
10
+ expected = ['blow a kiss to sb', 'blow a kiss at sb']
11
+ assert_equal expected, str.flatten
12
+ end
13
+
14
+ def test_slash_at_first
15
+ str = 'blow/blew a kiss'
16
+ expected = ['blow a kiss', 'blew a kiss']
17
+ assert_equal expected, str.flatten
18
+ end
19
+
20
+ def test_slash_at_last
21
+ str = 'knock around/about'
22
+ expected = ['knock around', 'knock about']
23
+ assert_equal expected, str.flatten
24
+ end
25
+
26
+ def test_two_middle_slashes
27
+ str = 'not give/budge/move an inch'
28
+ expected = ['not give an inch', 'not budge an inch', 'not move an inch']
29
+ assert_equal expected, str.flatten
30
+ end
31
+
32
+ def test_single_quote
33
+ str = "fall into the/sb's trap"
34
+ expected = ['fall into the trap', 'fall into sb\'s trap']
35
+ assert_equal expected, str.flatten
36
+ end
37
+
38
+ def test_question_mark
39
+ str = 'what is sb/sth?'
40
+ expected = ['what is sb?', 'what is sth?']
41
+ assert_equal expected, str.flatten
42
+ end
43
+
44
+ def test_exlamation_mark
45
+ str = 'look lively/sharp!'
46
+ expected = ['look lively!', 'look sharp!']
47
+ assert_equal expected, str.flatten
48
+ end
49
+
50
+ def test_semicolon
51
+ str = "the like of sb/sth; sb's/sth's like"
52
+ expected = ['the like of sb', 'the like of sth',
53
+ "sb's like", "sth's like"]
54
+ assert_equal expected, str.flatten
55
+ end
56
+
57
+ def test_middle_parentheses
58
+ str = 'go (like/down) a bomb'
59
+ expected = ['go a bomb', 'go like a bomb', 'go down a bomb']
60
+ assert_equal expected, str.flatten
61
+ end
62
+
63
+ def test_ending_parentheses
64
+ str = 'the other side/end (of sth)'
65
+ expected = ['the other side', 'the other end', 'the other side of sth',
66
+ 'the other end of sth']
67
+ assert_equal expected, str.flatten
68
+ end
69
+
70
+ def test_slash_means_non_alternative
71
+ skip 'special cases for flatten'
72
+ # strs = ['20/20 vision', 'public enemy number one/no. 1']
73
+ # todo: still have uncovered special cases for flatten
74
+ # "20/20 vision".flatten => "20/20 vision" no change expected
75
+ # public enemy number one/no. 1 =>
76
+ # public enemy number one
77
+ # public enemy no. 1
78
+ # need more examples to support complex 'or' separators
79
+ # sound like/as if/as though
80
+ # look on/upon sb/sth as sth
81
+ # look at/see sth through rose-coloured/tinted glasses
82
+ # give /quote sth/sb chapter and verse
83
+ end
84
+
85
+ def test_ellipsis
86
+ str = 'the more...the more/less'
87
+ expected = ['the more...the more', 'the more...the less']
88
+ assert_equal expected, str.flatten
89
+ end
90
+
91
+ def test_has?
92
+ assert 'blow your nose'.has?('nose')
93
+ end
94
+ end
95
+ end
metadata CHANGED
@@ -1,15 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: camdict
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pan Gaoyong
8
- - "潘高勇"
8
+ - 潘高勇
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-11-17 00:00:00.000000000 Z
12
+ date: 2017-05-01 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: nokogiri
@@ -25,6 +25,20 @@ dependencies:
25
25
  - - ">="
26
26
  - !ruby/object:Gem::Version
27
27
  version: 1.6.2
28
+ - !ruby/object:Gem::Dependency
29
+ name: minitest
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: 5.8.3
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: 5.8.3
28
42
  description: Get definitions, pronunciation and example sentences of a word or phrase
29
43
  from the online Cambridge dictionaries.
30
44
  email: pan.gaoyong@gmail.com
@@ -35,21 +49,35 @@ files:
35
49
  - README.md
36
50
  - Rakefile
37
51
  - lib/camdict.rb
52
+ - lib/camdict/array_ext.rb
38
53
  - lib/camdict/client.rb
39
54
  - lib/camdict/common.rb
40
55
  - lib/camdict/definition.rb
56
+ - lib/camdict/entry.rb
57
+ - lib/camdict/exception.rb
41
58
  - lib/camdict/explanation.rb
42
59
  - lib/camdict/http_client.rb
60
+ - lib/camdict/ipa.rb
61
+ - lib/camdict/pronunciation.rb
62
+ - lib/camdict/sentence.rb
63
+ - lib/camdict/string_ext.rb
43
64
  - lib/camdict/word.rb
44
65
  - license
66
+ - test/debug.rb
67
+ - test/helper.rb
45
68
  - test/itest_client.rb
46
69
  - test/itest_definition.rb
70
+ - test/itest_entry.rb
47
71
  - test/itest_explanation.rb
72
+ - test/itest_ipa.rb
73
+ - test/itest_pronunciation.rb
74
+ - test/itest_word.rb
75
+ - test/test_array_ext.rb
48
76
  - test/test_client.rb
49
77
  - test/test_common.rb
50
- - test/test_definition.rb
51
78
  - test/test_explanation.rb
52
79
  - test/test_http_client.rb
80
+ - test/test_string_ext.rb
53
81
  homepage: https://github.com/pan/camdict
54
82
  licenses:
55
83
  - MIT
@@ -62,7 +90,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
62
90
  requirements:
63
91
  - - ">="
64
92
  - !ruby/object:Gem::Version
65
- version: 1.9.3
93
+ version: 2.3.0
66
94
  required_rubygems_version: !ruby/object:Gem::Requirement
67
95
  requirements:
68
96
  - - ">="
@@ -70,16 +98,23 @@ required_rubygems_version: !ruby/object:Gem::Requirement
70
98
  version: '0'
71
99
  requirements: []
72
100
  rubyforge_project:
73
- rubygems_version: 2.2.2
101
+ rubygems_version: 2.5.1
74
102
  signing_key:
75
103
  specification_version: 4
76
104
  summary: online Cambridge dictionary client
77
105
  test_files:
78
106
  - test/itest_explanation.rb
107
+ - test/itest_pronunciation.rb
108
+ - test/test_string_ext.rb
79
109
  - test/test_common.rb
80
110
  - test/itest_client.rb
81
111
  - test/itest_definition.rb
82
- - test/test_definition.rb
112
+ - test/helper.rb
113
+ - test/itest_entry.rb
83
114
  - test/test_http_client.rb
115
+ - test/itest_word.rb
84
116
  - test/test_client.rb
117
+ - test/debug.rb
118
+ - test/itest_ipa.rb
119
+ - test/test_array_ext.rb
85
120
  - test/test_explanation.rb
@@ -1,345 +0,0 @@
1
- require 'minitest/autorun'
2
- require 'camdict'
3
-
4
- module Camdict
5
- # this word has two derived words
6
- PLAGIARIZE = '<h2 class="di-title cdo-section-title-hw">plagiarize</h2>' +
7
- '<span class="runon"><span class=runon-title" title="Derived word">' +
8
- '<span class="w">plagiarism</span></span>' +
9
- '<span class="runon-info"><span class="posgram"><span class="pos">noun' +
10
- '</span></span></span></span>' +
11
- '<span class="runon"><span class=runon-title" title="Derived word">' +
12
- '<span class="w">plagiarist</span></span>'+
13
- '<span class="runon-info"><span class="posgram"><span class="pos">noun' +
14
- '</span></span></span></span>'
15
-
16
- class DefinitionTest < Minitest::Test
17
-
18
- def test_pos
19
- html = '<h2 class="di-title cdo-section-title-hw">favourite</h2>' +
20
- '<span class="di-info"><span class="posgram">' +
21
- '<span class="pos" title="A word that ...">noun</span></span>' +
22
- '<span class="spellvar"><span class="v" title="Variant form">' +
23
- 'favorite</span></span></span>'
24
- w = Camdict::Definition.new("favourite", :favourite=>html)
25
- assert_equal "noun", w.send(:pos)
26
- w = Camdict::Definition.new("favorite", :favourite=>html)
27
- assert_equal "noun", w.send(:pos)
28
- html = '<h2 class="di-title cdo-section-title-hw">look at sth</h2>' +
29
- '<span class="di-info"><span class="anc-info-head"><span class="pos"' +
30
- ' title="Verb with an adverb...">phrasal verb</span><span class='+
31
- '"posgram">' +
32
- '<span class="pos" title="Verb with an adverb...">verb</span></span>'
33
- w = Camdict::Definition.new("look at sth","look-at-sth_1" => html)
34
- assert_equal "phrasal verb", w.send(:pos)
35
- w = Camdict::Definition.new("plagiarist",:plagiarize => PLAGIARIZE)
36
- assert_equal "noun", w.send(:pos)
37
- htmli = '<h2 class="di-title cdo-section-title-hw">pass water</h2>' +
38
- '<div class="di-body"><div class="idiom-block">' +
39
- '<span class="idiom-body">'
40
- w = Camdict::Definition.new("pass water","pass-water" => htmli)
41
- assert_equal "idiom", w.send(:pos)
42
- end
43
-
44
- def test_title_word
45
- html = '<h2 class="di-title cdo-section-title-hw">aluminium</h2>'
46
- w = Camdict::Definition.new("aluminium",:aluminium=>html)
47
- assert_equal "aluminium", w.send(:title_word)
48
- end
49
-
50
- def test_derived_words
51
- w = Camdict::Definition.new("plagiarize",:plagiarize => PLAGIARIZE)
52
- r = w.send :derived_words
53
- assert_equal %w(plagiarism plagiarist), r
54
- w = Camdict::Definition.new("mind", :mind=>"<h1>mind</h1>")
55
- assert ! (w.send :derived_words)
56
- end
57
-
58
- def test_where?
59
- w = Camdict::Definition.new("plagiarize",:plagiarize => PLAGIARIZE)
60
- assert_equal "title", w.send(:where?)
61
- w = Camdict::Definition.new("plagiarism",:plagiarize => PLAGIARIZE)
62
- assert_equal "derived", w.send(:where?)
63
- html = '<h2 class="di-title cdo-section-title-hw">knock around/about'
64
- w = Camdict::Definition.new("knock about","knock-around-about"=>html)
65
- assert_equal "title", w.send(:where?)
66
- w = Camdict::Definition.new("knock around","knock-around-about"=>html)
67
- assert_equal "title", w.send(:where?)
68
- end
69
-
70
- def test_get_head_variant
71
- html = '<h2 class="di-title cdo-section-title-hw">aluminium</h2>' +
72
- '<span class="di-info"><span class="var"><span class="v" ' +
73
- 'title="Variant form">aluminum</span></span>'
74
- w = Camdict::Definition.new("aluminum",:aluminium=>html)
75
- assert_equal "aluminum", w.send(:get_head_variant).first
76
- end
77
-
78
- def test_spell_variant
79
- html = '<span class="spellvar">'+
80
- '<h2 class="di-title cdo-section-title-hw">aluminium</h2>' +
81
- '<span class="v" title="Variant form">aluminum</span></span>'
82
- w = Camdict::Definition.new("aluminum",:aluminium=>html)
83
- assert_equal "aluminum", w.send(:spell_variant)
84
- end
85
-
86
- def test_get_phrase
87
- phrase = %q(correct me if I'm wrong but...)
88
- html = '<h2 class="di-title cdo-section-title-hw">aluminium</h2>' +
89
- '<span class="phrase-block"><span class="phrase">' + phrase
90
- w = Camdict::Definition.new("correct me",:correct=>html)
91
- assert_equal [phrase], w.send(:get_phrase)
92
- end
93
-
94
- def test_idiom_explanation
95
- word = 'have it out with sb'
96
- meaning = 'to talk to someone about something they have done that makes' +
97
- ' you angry, in order to try to solve the problem:'
98
- html = "<h2 class='di-title cdo-section-title-hw'>#{word}</h2>" +
99
- '<span class="idiom-block"><span class="idiom-body">'+
100
- "<span class='def-block'><span class='def'>#{meaning}"
101
- def1 = Camdict::Definition.new(word, "have-it-out-with-sb"=>html)
102
- assert def1.is_idiom
103
- assert_equal meaning, def1.explanations.first.meaning
104
- def2 = Camdict::Definition.new("have it out", "have-it-out-with-sb"=>html)
105
- assert_equal meaning, def2.explanations.first.meaning
106
- end
107
-
108
- def test_parse_ipa
109
- imagin = %w(26a 2c8 6d e6 64 292 2e 26a 2e 6e).map { |c|
110
- c.to_i 16}.pack 'U*'
111
- a = %w(259).map { |c| c.to_i 16}.pack 'U'
112
- ry = %w(72 2e 69).map {|c| c.to_i 16}.pack 'U*'
113
- html = '<h2 class="di-title cdo-section-title-hw">imaginary</h2>' +
114
- "<span class='di-info'><span class='ipa'>#{imagin}<span class='sp'>" +
115
- "#{a}</span>#{ry}</span>"
116
- w = Camdict::Definition.new("imaginary",:imaginary=>html)
117
- node = Nokogiri::HTML(html).css(".ipa")
118
- ukipa = w.send :parse_ipa, node
119
- actual = {baseipa: ukipa[:baseipa], sindex: ukipa[:sindex]}
120
- expected = {baseipa: imagin+a+ry, sindex:[10,1]}
121
- assert_equal expected, actual
122
- end
123
-
124
- def test_join_ipa
125
- html = '<h2 class="di-title cdo-section-title-hw">understand</h2>'
126
- w = Camdict::Definition.new("understand",:understand=>html)
127
- # head-tail hyphen
128
- understand = {
129
- :uk_utf8 => %w(2cc 28c 6e 2e 64 259 2c8 73 74 e6 6e 64),
130
- :us_utf8 => %w(2d 64 25a 2d),
131
- :expected => %w(2cc 28c 6e 2e 64 25a 2c8 73 74 e6 6e 64),
132
- :us_inx => nil,
133
- :uk_inx => nil,
134
- :spiexp => nil
135
- }
136
- imaginary = {
137
- :uk_utf8 => %w(26a 2c8 6d e6 64 292 2e 26a 2e 6e 259 72 2e 69),
138
- :us_utf8 => %w(2d 259 2e 6e 65 72 2d),
139
- :expected => %w(26a 2c8 6d e6 64 292 2e 259 2e 6e 65 72 2e 69),
140
- :us_inx => nil,
141
- :uk_inx => [10,1],
142
- :spiexp => nil
143
- }
144
- plagiarism = {
145
- :uk_utf8 => %w(2c8 70 6c 65 26a 2e 64 292 259 72 2e 61 26a 7a),
146
- :us_utf8 => %w(2d 64 292 259 72 2e 26a 2e 7a 259 6d),
147
- :expected => %w(2c8 70 6c 65 26a 2e 64 292 259 72 2e 26a 2e 7a 259 6d),
148
- :us_inx => [3,1,9,1],
149
- :uk_inx => [8,1],
150
- :spiexp => [8,1,14,1]
151
- }
152
- # left hyphen
153
- plagiarize = {
154
- :uk_utf8 => %w(2c8 70 6c 65 26a 2e 64 292 259 72 2e 61 26a 7a),
155
- :us_utf8 => %w(2d 64 292 259 2e 72 61 26a 7a),
156
- :expected => %w(2c8 70 6c 65 26a 2e 64 292 259 2e 72 61 26a 7a),
157
- :us_inx => nil,
158
- :uk_inx => [8,1],
159
- :spiexp => nil
160
- }
161
- painting = {
162
- :uk_utf8 => %w(2c8 70 65 26a 6e 2e 74 26a 14b),
163
- :us_utf8 => %w(2d 74 32c 26a 14b),
164
- :expected => %w(2c8 70 65 26a 6e 2e 74 32c 26a 14b),
165
- :us_inx => nil,
166
- :uk_inx => nil,
167
- :spiexp => nil
168
- }
169
- dictionary = {
170
- :uk_utf8 => %w(2c8 64 26a 6b 2e 283 259 6e 2e 259 72 2e 69),
171
- :us_utf8 => %w(2d 65 72 2e 69),
172
- :expected => %w(2c8 64 26a 6b 2e 283 259 6e 2e 65 72 2e 69),
173
- :us_inx => nil,
174
- :uk_inx => [6,1, 9,1],
175
- :spiexp => [6,1]
176
- }
177
- harmfulness = {
178
- :uk_utf8 => %w(2c8 68 251 2d0 6d 2e 66 259 6c),
179
- :us_utf8 => %w(2d 6e 259 73),
180
- :expected => %w(2c8 68 251 2d0 6d 2e 66 259 6c 6e 259 73),
181
- :us_inx => nil,
182
- :uk_inx => [7,1],
183
- :spiexp => [7,1]
184
- }
185
- # right hyphen
186
- toxic = {
187
- :uk_utf8 => %w(2c8 74 252 6b 2e 73 26a 6b),
188
- :us_utf8 => %w(2c8 74 251 2d0 6b 2d),
189
- :expected => %w(2c8 74 251 2d0 6b 73 26a 6b),
190
- :us_inx => nil,
191
- :uk_inx => nil,
192
- :spiexp => nil
193
- }
194
- data = [understand, imaginary, plagiarize, plagiarism, painting,
195
- harmfulness, toxic]
196
- data.each_with_index { |word, i|
197
- full = word[:uk_utf8].map {|c| c.to_i 16}.pack 'U*'
198
- short = word[:us_utf8].map {|c| c.to_i 16}.pack 'U*'
199
- full_sp = { baseipa: full, sindex: word[:uk_inx]}
200
- short_sp = { baseipa: short, sindex: word[:us_inx]}
201
- us = w.send :join_ipa, full_sp, short_sp
202
- a = us[:baseipa].unpack 'U*'
203
- spind = us[:sindex]
204
- actual = a.map { |n| n.to_s 16 }
205
- assert_equal word[:expected], actual
206
- assert_equal word[:spiexp], spind
207
- }
208
- end
209
-
210
- def test_mix_spi
211
- html = '<h2 class="di-title cdo-section-title-hw">understand</h2>'
212
- w = Camdict::Definition.new("understand",:understand=>html)
213
- # an IPA is 12 letters long, 012345678901 -345-,
214
- lsp = [2,1, 5,1, 9,2]
215
- lrange = 0..3
216
- csp = [ 3, 2 ]
217
- cn = 3
218
- rrang = 8..12
219
- expected = [2,1, 6,2, 9,2]
220
- actual = w.send :mix_spi,lsp, lrange, csp, cn, lsp, rrang
221
- assert_equal expected, actual
222
- end
223
-
224
- def test_get_pronunciation
225
- title = '<h2 class="di-title cdo-section-title-hw">understand</h2>'
226
- ogglink = 'http://cam.org/british/ukunder112.ogg'
227
- mp3link = 'http://cam.org/british/ukunder112.mp3'
228
- html = title + %q(<span class='di-info'><a class='pron-uk' ) +
229
- "data-src-ogg='#{ogglink}' data-src-mp3='#{mp3link}' href='#'>"
230
- w = Camdict::Definition.new("understand",:understand=>html)
231
- pron = w.send :get_pronunciation
232
- assert_equal mp3link, pron.uk.mp3
233
- assert_equal ogglink, pron.uk.ogg
234
- assert_nil pron.us.mp3
235
- assert_equal mp3link, w.pronunciation.uk.mp3
236
- end
237
-
238
- def test_get_region
239
- belaughing = {
240
- :word => 'be laughing',
241
- :expected => 'UK',
242
- :piece => ''
243
- }
244
- favorite = {
245
- :word => 'favorite',
246
- :expected => 'US',
247
- :piece => "<span class='spellvar'><span class='region'>US</span>" +
248
- "<span class='v' title='Variant form'>favorite"
249
- }
250
- aluminum = {
251
- :word => 'aluminum',
252
- :expected => 'US',
253
- :piece => "<span class='var'><span class='region'>US</span>" +
254
- "<span class='v' title='Variant form'>aluminum"
255
- }
256
- data = [belaughing, favorite, aluminum]
257
- data.each { |d|
258
- title = "<h2 class='di-title cdo-section-title-hw'>#{d[:word]}</h2>"
259
- html = title + %q(<span class='di-info'><a class='lab'><span class=) +
260
- "'region'>UK</span><span class='usage'>informal</span>"
261
- w = Camdict::Definition.new(d[:word], d[:word]=>html+d[:piece])
262
- region = w.send :get_region
263
- assert_equal d[:expected], region
264
- }
265
- end
266
-
267
- def test_gc
268
- plagiarize = {
269
- :word => 'plagiarize',
270
- :expected => 'I or T',
271
- :piece => ''
272
- }
273
- data = [plagiarize]
274
- data.each { |d|
275
- title = "<h2 class='di-title cdo-section-title-hw'>#{d[:word]}</h2>"
276
- html = title + %q(<span class='di-info'><span class='gcs'>) +
277
- "<span class='gc'>I</span> or <span class='gc'>T</span>"
278
- w = Camdict::Definition.new(d[:word], d[:word]=>html+d[:piece])
279
- actual = w.send :get_gc
280
- assert_equal d[:expected], actual
281
- }
282
- end
283
-
284
- def test_get_plural
285
- mouse = {
286
- :word => 'mouse',
287
- :expected => 'mice',
288
- :piece => "<span class='inf'>mice</span>"
289
- }
290
- fish = {
291
- :word => 'fish',
292
- :expected => %w(fish fishes),
293
- :piece => '<span class="inf">fish</span> or <span class="inf">fishes'
294
- }
295
- data = [mouse, fish]
296
- data.each { |d|
297
- title = "<h2 class='di-title cdo-section-title-hw'>#{d[:word]}</h2>"
298
- html = title + %q(<span class='di-info'><span class='inf-group' ) +
299
- "type='plural'>"
300
- w = Camdict::Definition.new(d[:word], d[:word]=>html+d[:piece])
301
- w.instance_eval { @part_of_speech = 'noun'}
302
- actual = w.send :get_plural
303
- assert_equal d[:expected], actual
304
- }
305
- end
306
-
307
- def test_guided_word
308
- d = { word: 'rubber', expected: 'SUBSTANCE'}
309
- title = "<h2 class='di-title cdo-section-title-hw'>#{d[:word]}</h2>"
310
- html = title + %q(<span class='di-info'><strong class='gw'>(SUBSTANCE))
311
- w = Camdict::Definition.new(d[:word], d[:word]=>html)
312
- actual = w.send :get_guided_word
313
- assert_equal d[:expected], actual
314
- end
315
-
316
- def test_get_irregular
317
- blow = {
318
- :word => 'blow',
319
- :expected => ['blew','blown', nil],
320
- :piece => "><span class='inf'>blew</span></span>,<span class='inf-" +
321
- "group'><span class='inf'>blown</span></span>"
322
- }
323
- bet = {
324
- :word => 'bet',
325
- :expected => %w(bet bet betting),
326
- :piece => 'type="pres_part"><span class="inf">betting</span>' +
327
- '</span><span class="inf-group" type="past"><span class="inf">bet'
328
- }
329
- data = [blow, bet]
330
- data.each { |d|
331
- title = "<h2 class='di-title cdo-section-title-hw'>#{d[:word]}</h2>"
332
- html = title + %q(<span class='di-info'><span class='inf-group' )
333
- w = Camdict::Definition.new(d[:word], d[:word]=>html+d[:piece])
334
- expected = w.instance_eval {
335
- @part_of_speech = 'verb'
336
- sp, pp, pr = d[:expected]
337
- Camdict::Definition::Irregular.new(sp, pp, pr)
338
- }
339
- actual = w.send :get_irregular
340
- assert_equal expected, actual
341
- }
342
- end
343
-
344
- end
345
- end