mida 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,10 +1,11 @@
1
- require_relative 'spec_helper'
2
- require_relative '../lib/mida'
1
+ require 'spec_helper'
2
+ require 'mida/itemprop'
3
3
 
4
4
 
5
5
  describe Mida::Itemprop, 'when parsing an element without an itemprop attribute' do
6
6
  before do
7
- @element = mock_element('span')
7
+ html = '<span>Lorry</span>'
8
+ @element = Nokogiri(html).children.first
8
9
  end
9
10
 
10
11
  it '#parse should return an empty Hash' do
@@ -14,7 +15,8 @@ end
14
15
 
15
16
  describe Mida::Itemprop, 'when parsing an element with one itemprop name' do
16
17
  before do
17
- @element = mock_element('span', {'itemprop' => 'reviewer'}, 'Lorry Woodman')
18
+ html = '<span itemprop="reviewer">Lorry Woodman</span>'
19
+ @element = Nokogiri(html).children.first
18
20
  end
19
21
 
20
22
  it '#parse should return a Hash with the correct name/value pair' do
@@ -26,8 +28,7 @@ describe Mida::Itemprop, "when parsing an element who's inner text contains\
26
28
  non microdata elements" do
27
29
  before do
28
30
  html = '<span itemprop="reviewer">Lorry <em>Woodman</em></span>'
29
- doc = Nokogiri(html)
30
- @itemprop = doc.search('./*').first
31
+ @itemprop = Nokogiri(html).children.first
31
32
  end
32
33
 
33
34
  it '#parse should return a Hash with the correct name/value pair' do
@@ -37,45 +38,35 @@ end
37
38
 
38
39
  describe Mida::Itemprop, 'when parsing an itemscope element that has a relative url' do
39
40
  before do
40
-
41
- # The first_name element
42
- fn = mock_element('span', {'itemprop' => 'first_name'}, 'Lorry')
43
-
44
- # The last_name element
45
- ln = mock_element('span', {'itemprop' => 'last_name'}, 'Woodman')
46
-
47
- # The url. This is important to check whether the page_url is passed onto Item
48
- url = mock_element('a', {'itemprop' => 'url', 'href' => 'home/LorryWoodman'})
49
-
50
- # The surrounding reviewer itemscope element
51
- @itemscope_el = mock_element('div', {'itemprop' => 'reviewer',
52
- 'itemtype' => 'person',
53
- 'itemscope' =>true}, nil, [fn,ln,url])
41
+ html = html_wrap <<-EOS
42
+ <div itemprop="reviewer" itemtype="person" itemscope>
43
+ <a itemprop="url" href="home/LorryWoodman">
44
+ </a>
45
+ </div>
46
+ EOS
47
+
48
+ itemprop = Nokogiri(html).search('//*[@itemscope]')
49
+ @properties = Mida::Itemprop.parse(itemprop, "http://example.com")
54
50
  end
55
51
 
56
- it '#parse should return a Hash with the correct name/value pair' do
57
- property = Mida::Itemprop.parse(@itemscope_el, "http://example.com")
58
- property.size.should == 1
59
- reviewer = property['reviewer']
60
- reviewer.type.should == 'person'
61
- reviewer.properties.should == {
62
- 'first_name' => ['Lorry'],
63
- 'last_name' => ['Woodman'],
64
- 'url' => ['http://example.com/home/LorryWoodman']
65
- }
52
+ it 'should create an absolute url' do
53
+ url = @properties['reviewer'].properties['url']
54
+ url.should == ['http://example.com/home/LorryWoodman']
66
55
  end
67
56
  end
68
57
 
69
58
  describe Mida::Itemprop, 'when parsing an element with multiple itemprop names' do
70
59
  before do
71
- @element = mock_element('span', {'itemprop' => 'reviewer friend person'}, 'the property text')
60
+ html = '<span itemprop="reviewer friend person">some text</span>'
61
+ itemprop = Nokogiri(html).children.first
62
+ @properties = Mida::Itemprop.parse(itemprop)
72
63
  end
73
64
 
74
- it '#parse should return a Hash with the name/value pairs' do
75
- Mida::Itemprop.parse(@element).should == {
76
- 'reviewer' => 'the property text',
77
- 'friend' => 'the property text',
78
- 'person' => 'the property text'
65
+ it 'it should return a Hash with the each of the itemprop names as keys' do
66
+ @properties.should == {
67
+ 'reviewer' => 'some text',
68
+ 'friend' => 'some text',
69
+ 'person' => 'some text'
79
70
  }
80
71
  end
81
72
  end
@@ -97,7 +88,8 @@ describe Mida::Itemprop, 'when parsing an element with non text content url valu
97
88
  it 'should return nothing for relative urls' do
98
89
  url = 'register/index.html'
99
90
  URL_ELEMENTS.each do |tag, attr|
100
- element = mock_element(tag, {'itemprop' => 'url', attr => url})
91
+ html = html_wrap %Q{<#{tag} itemprop="url" #{attr}="#{url}">The url</#{tag}>}
92
+ element = Nokogiri(html).search('//*[@itemprop]').first
101
93
  Mida::Itemprop.parse(element).should == {'url' => ''}
102
94
  end
103
95
  end
@@ -111,7 +103,8 @@ describe Mida::Itemprop, 'when parsing an element with non text content url valu
111
103
 
112
104
  urls.each do |url|
113
105
  URL_ELEMENTS.each do |tag, attr|
114
- element = mock_element(tag, {'itemprop' => 'url', attr => url})
106
+ html = html_wrap %Q{<#{tag} itemprop="url" #{attr}="#{url}">The url</#{tag}>}
107
+ element = Nokogiri(html).search('//*[@itemprop]').first
115
108
  Mida::Itemprop.parse(element).should == {'url' => url}
116
109
  end
117
110
  end
@@ -126,7 +119,8 @@ describe Mida::Itemprop, 'when parsing an element with non text content url valu
126
119
  it 'should return the absolute url for relative urls' do
127
120
  url = 'register/index.html'
128
121
  URL_ELEMENTS.each do |tag, attr|
129
- element = mock_element(tag, {'itemprop' => 'url', attr => url})
122
+ html = html_wrap %Q{<#{tag} itemprop="url" #{attr}="#{url}">The url</#{tag}>}
123
+ element = Nokogiri(html).search('//*[@itemprop]').first
130
124
  Mida::Itemprop.parse(element, @page_url).should ==
131
125
  {'url' => 'http://example.com/test/register/index.html'}
132
126
  end
@@ -141,7 +135,8 @@ describe Mida::Itemprop, 'when parsing an element with non text content url valu
141
135
 
142
136
  urls.each do |url|
143
137
  URL_ELEMENTS.each do |tag, attr|
144
- element = mock_element(tag, {'itemprop' => 'url', attr => url})
138
+ html = html_wrap %Q{<#{tag} itemprop="url" #{attr}="#{url}">The url</#{tag}>}
139
+ element = Nokogiri(html).search('//*[@itemprop]').first
145
140
  Mida::Itemprop.parse(element, @page_url).should == {'url' => url}
146
141
  end
147
142
  end
@@ -152,14 +147,14 @@ end
152
147
 
153
148
  describe Mida::Itemprop, 'when parsing an element with non text content non url values' do
154
149
  it 'should get values from a meta content attribute' do
155
- element = mock_element('meta', {'itemprop' => 'reviewer',
156
- 'content' => 'Lorry Woodman'})
150
+ html = html_wrap %q{<meta itemprop="reviewer" content="Lorry Woodman"/>}
151
+ element = Nokogiri(html).search('//*[@itemprop]').first
157
152
  Mida::Itemprop.parse(element).should == {'reviewer' => 'Lorry Woodman'}
158
153
  end
159
154
 
160
155
  it 'should get time from an time datatime attribute' do
161
- element = mock_element('time', {'itemprop' => 'dtreviewed',
162
- 'datetime' => '2011-04-04'})
163
- Mida::Itemprop.parse(element).should == {'dtreviewed' => '2011-04-04'}
156
+ html = html_wrap %q{<time itemprop="dtreviewed" datetime="2011-05-04"/>}
157
+ element = Nokogiri(html).search('//*[@itemprop]').first
158
+ Mida::Itemprop.parse(element).should == {'dtreviewed' => '2011-05-04'}
164
159
  end
165
160
  end
@@ -0,0 +1,287 @@
1
+ require 'spec_helper'
2
+ require 'mida'
3
+
4
+ describe Mida::Itemscope, 'when initialized with an itemscope_node containing just itemprops' do
5
+ before do
6
+ html = <<-EOS
7
+ <div itemprop="reviewer" itemscope>
8
+ <span itemprop="first_name">Lorry </span>
9
+ <span itemprop="last_name">Woodman</span>
10
+ </div>
11
+ EOS
12
+ doc = Nokogiri(html).children.first
13
+ @itemscope = Mida::Itemscope.new(doc)
14
+ end
15
+
16
+ it '#properties should return the correct name/value pairs' do
17
+ @itemscope.properties.should == {
18
+ 'first_name' => ['Lorry'],
19
+ 'last_name' => ['Woodman']
20
+ }
21
+ end
22
+ end
23
+
24
+
25
+ describe Mida::Itemscope, 'when initialized with an itemscope_node not containing an itemtype' do
26
+ before do
27
+ html = <<-EOS
28
+ <div itemprop="reviewer" itemscope>
29
+ <span itemprop="first_name">Lorry </span>
30
+ <span itemprop="last_name">Woodman</span>
31
+ </div>
32
+ EOS
33
+ doc = Nokogiri(html).children.first
34
+ @itemscope = Mida::Itemscope.new(doc)
35
+ end
36
+
37
+ it '#type should return nil' do
38
+ @itemscope.type.should == nil
39
+ end
40
+ end
41
+
42
+ describe Mida::Itemscope, 'when initialized with an itemscope_node containing an itemtype' do
43
+ before do
44
+ html = <<-EOS
45
+ <div itemtype="person" itemprop="reviewer" itemscope>
46
+ <span itemprop="first_name">Lorry </span>
47
+ <span itemprop="last_name">Woodman</span>
48
+ </div>
49
+ EOS
50
+ doc = Nokogiri(html).children.first
51
+ @itemscope = Mida::Itemscope.new(doc)
52
+ end
53
+
54
+ it '#type should return nil' do
55
+ @itemscope.type.should == "person"
56
+ end
57
+ end
58
+
59
+ describe Mida::Itemscope, 'when initialized with an itemscope containing an itemprop with a relative url' do
60
+ before do
61
+ html = <<-EOS
62
+ <div itemscope>
63
+ <a itemprop="url" href="home/lorry">Lorry</a>
64
+ </div>
65
+ EOS
66
+ @doc = Nokogiri(html).children.first
67
+ end
68
+
69
+ context 'and the page url is not passed' do
70
+ before do
71
+ @itemscope = Mida::Itemscope.new(@doc)
72
+ end
73
+
74
+ it 'should return the url as an absolute url' do
75
+ @itemscope.properties['url'].should == ['']
76
+ end
77
+ end
78
+
79
+ context 'and the page url is passed' do
80
+ before do
81
+ @itemscope = Mida::Itemscope.new(@doc, "http://example.com")
82
+ end
83
+
84
+ it 'should return the url as an absolute url' do
85
+ @itemscope.properties['url'].should == ['http://example.com/home/lorry']
86
+ end
87
+ end
88
+
89
+ end
90
+
91
+ describe Mida::Itemscope, 'when initialized with an itemscope containing itemprops surrounded by a non microdata element' do
92
+ before do
93
+ html = <<-EOS
94
+ <div itemprop="reviewer" itemtype="person" itemscope>
95
+ <span itemprop="first_name">Lorry</span>
96
+ <span><span itemprop="last_name">Woodman</span></span>
97
+ </div>
98
+ EOS
99
+ doc = Nokogiri(html).children.first
100
+ @itemscope = Mida::Itemscope.new(doc)
101
+ end
102
+
103
+ it '#properties should return the correct name/value pairs' do
104
+ @itemscope.properties.should == {
105
+ 'first_name' => ['Lorry'],
106
+ 'last_name' => ['Woodman']
107
+ }
108
+ end
109
+
110
+ end
111
+
112
+ describe Mida::Itemscope, "when initialized with an itemscope containing itemprops
113
+ who's inner text is surrounded by non-microdata elements" do
114
+ before do
115
+ html = <<-EOS
116
+ <div itemscope>
117
+ <span itemprop="reviewer">Lorry <em>Woodman</em></span>
118
+ </div>'
119
+ EOS
120
+ doc = Nokogiri(html).children.first
121
+ @itemscope = Mida::Itemscope.new(doc)
122
+ end
123
+
124
+ it '#properties should return the correct name/value pairs' do
125
+ @itemscope.properties.should == {
126
+ 'reviewer' => ['Lorry Woodman']
127
+ }
128
+ end
129
+
130
+ end
131
+
132
+ describe Mida::Itemscope, "when initialized with an itemscope containing an itemprop
133
+ nested within another itemprop" do
134
+ before do
135
+ html = <<-EOS
136
+ <div itemscope>
137
+ <span itemprop="description">The animal is a <span itemprop="colour">green</span> parrot.</span>
138
+ </div>
139
+ EOS
140
+ doc = Nokogiri(html).children.first
141
+ @itemscope = Mida::Itemscope.new(doc)
142
+ end
143
+
144
+ it '#properties should return both properties' do
145
+ @itemscope.properties.should == {
146
+ 'description' => ['The animal is a green parrot.'],
147
+ 'colour' => ['green']
148
+ }
149
+ end
150
+
151
+ end
152
+
153
+ describe Mida::Itemscope, 'when initialized with an itemscope containing itemprops with the same name' do
154
+ before do
155
+ strawberry_html = <<-EOS
156
+ <div itemprop="flavour" itemtype="icecream-type" itemscope>
157
+ <span itemprop="fruit">Strawberry</span>
158
+ <span itemprop="style">Homemade</span>
159
+ </div>
160
+ EOS
161
+ html = html_wrap <<-EOS
162
+ <div itemtype="icecreams" itemscope>
163
+ <span itemprop="flavour">Lemon Sorbet</span>
164
+ <span itemprop="flavour">Apricot Sorbet</span>
165
+ #{strawberry_html}
166
+ </div>
167
+ EOS
168
+ doc = Nokogiri(html).search('//*[@itemscope]').first
169
+ strawberry_doc = Nokogiri(html_wrap(strawberry_html))
170
+ strawberry_doc = strawberry_doc.search('//*[@itemscope]').first
171
+ @itemscope = Mida::Itemscope.new(doc)
172
+ @strawberry_itemscope = Mida::Itemscope.new(strawberry_doc)
173
+ end
174
+
175
+ it '#properties should return the correct name/value pairs' do
176
+ @itemscope.properties.should == {
177
+ 'flavour' => [
178
+ 'Lemon Sorbet',
179
+ 'Apricot Sorbet',
180
+ @strawberry_itemscope
181
+ ]
182
+ }
183
+ end
184
+
185
+ end
186
+
187
+ describe Mida::Itemscope, 'when initialized with an itemscope containing itemrefs' do
188
+
189
+ before do
190
+ empty_band_html = <<-EOS
191
+ <div id="b" itemprop="band" itemscope itemref="c">
192
+ </div>
193
+ <div id="c">
194
+ <span itemprop="band_name">Jazz Band</span>
195
+ <span itemprop="band_size">12</span>
196
+ </div>
197
+ EOS
198
+ html = html_wrap <<-EOS
199
+ <div itemscope itemref="a b">
200
+ <span itemprop="age">30</span>
201
+ </div>
202
+ <div>
203
+ <p id="a"><span itemprop="name">Amanda</span></p>
204
+ </div>
205
+ #{empty_band_html}
206
+ EOS
207
+
208
+ doc = Nokogiri(html).search('//*[@itemscope]').first
209
+ empty_band_doc = Nokogiri(html_wrap(empty_band_html))
210
+ empty_band_doc = empty_band_doc.search('//*[@itemscope]').first
211
+ @itemscope = Mida::Itemscope.new(doc)
212
+ @empty_band_itemscope = Mida::Itemscope.new(empty_band_doc)
213
+ end
214
+
215
+ it '#properties should return the correct name/value pairs' do
216
+ @itemscope.properties.should == {
217
+ 'age' => ['30'],
218
+ 'name' => ['Amanda'],
219
+ 'band' => [@empty_band_itemscope]
220
+ }
221
+ end
222
+
223
+ end
224
+
225
+ describe Mida::Itemscope, 'when initialized with an itemscope containing an itemid' do
226
+
227
+ before do
228
+ html = html_wrap <<-EOS
229
+ <div itemtype="book" itemscope itemid="urn:isbn:978-1-849510-50-9">
230
+ <span itemprop="title">Hacking Vim 7.2</span>
231
+ <span itemprop="author">Kim Schulz</span>
232
+ </div>
233
+ EOS
234
+
235
+ doc = Nokogiri(html).search('//*[@itemscope]').first
236
+ @itemscope = Mida::Itemscope.new(doc)
237
+ end
238
+
239
+ it '#id should return the correct id' do
240
+ @itemscope.id.should == 'urn:isbn:978-1-849510-50-9'
241
+ end
242
+
243
+ end
244
+
245
+ describe Mida::Itemscope, 'when initialized with an itemscope containing itemscopes as properties nested two deep' do
246
+ before do
247
+ reviewer_html = <<-EOS
248
+ <div itemprop="reviewer" itemtype="person" itemscope>
249
+ <span itemprop="first_name">Lorry</span>
250
+ <span itemprop="last_name">Woodman</span>
251
+ <div itemprop="represents" itemtype="organization" itemscope>
252
+ <span itemprop="name">Acme</span>
253
+ </div>
254
+ </div>
255
+ EOS
256
+ html = html_wrap <<-EOS
257
+ <div itemtype="review" itemscope>
258
+ <span itemprop="item_name">Acme Anvil</span>
259
+ <span itemprop="rating">5</span>
260
+ #{reviewer_html}
261
+ </div>
262
+ EOS
263
+
264
+ doc = Nokogiri(html).search('//*[@itemscope]').first
265
+ reviewer_doc = Nokogiri(html_wrap(reviewer_html))
266
+ reviewer_doc = reviewer_doc.search('//*[@itemscope]').first
267
+ @itemscope = Mida::Itemscope.new(doc)
268
+ @reviewer_itemscope = Mida::Itemscope.new(reviewer_doc)
269
+ end
270
+
271
+ it '#type should return the correct type' do
272
+ @itemscope.type.should == 'review'
273
+ end
274
+
275
+ it '#id should return the correct id' do
276
+ @itemscope.id.should == nil
277
+ end
278
+
279
+ it '#properties should return the correct name/value pairs' do
280
+ @itemscope.properties.should == {
281
+ 'item_name' => ['Acme Anvil'],
282
+ 'rating' => ['5'],
283
+ 'reviewer' => [@reviewer_itemscope]
284
+ }
285
+ end
286
+
287
+ end
@@ -0,0 +1,56 @@
1
+ require 'spec_helper'
2
+ require 'mida/propertydesc'
3
+
4
+ describe Mida::PropertyDesc, 'when initialized without a block' do
5
+ before do
6
+ @propertyDesc = Mida::PropertyDesc.new(3)
7
+ end
8
+
9
+ it '#to_h[:num] should equal num passed' do
10
+ @propertyDesc.to_h[:num].should == 3
11
+ end
12
+
13
+ it '#to_h[:types] should equal DataType::Text' do
14
+ @propertyDesc.to_h[:types].should == [Mida::DataType::Text]
15
+ end
16
+ end
17
+
18
+ describe Mida::PropertyDesc, 'when initialized with a block with no specified types' do
19
+ before do
20
+ @propertyDesc = Mida::PropertyDesc.new(3) {}
21
+ end
22
+
23
+ it '#to_h[:types] should equal DataType::Text' do
24
+ @propertyDesc.to_h[:types].should == [Mida::DataType::Text]
25
+ end
26
+ end
27
+
28
+ describe Mida::PropertyDesc, 'when initialized with a block that specifies types with extract()' do
29
+ before do
30
+ @propertyDesc = Mida::PropertyDesc.new(3) { extract String, Array}
31
+ end
32
+
33
+ it '#to_h[:types] should equal [String, Array]' do
34
+ @propertyDesc.to_h[:types].should == [String, Array]
35
+ end
36
+ end
37
+
38
+ describe Mida::PropertyDesc, 'when initialized with a block that specifies types with types()' do
39
+ before do
40
+ @propertyDesc = nil
41
+ @error = last_stderr do
42
+ @propertyDesc = Mida::PropertyDesc.new(3) { types String, Array}
43
+ end
44
+ end
45
+
46
+ it 'should warn on initialization if types() is used in the block' do
47
+ @error.should eq("[DEPRECATION] Mida::PropertyDesc#types is deprecated. "+
48
+ "Please use Mida::PropertyDesc#extract instead."
49
+ )
50
+ end
51
+
52
+ it '#to_h[:types] should equal [String, Array]' do
53
+ @propertyDesc.to_h[:types].should == [String, Array]
54
+ end
55
+
56
+ end