mida 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,393 @@
1
+ require_relative 'spec_helper'
2
+ require_relative '../lib/mida'
3
+
4
+ describe Mida::Item, 'when initialized with an itemscope containing just itemprops' do
5
+ before do
6
+ # The first_name element
7
+ @fn = mock_element('span', {'itemprop' => 'first_name'}, 'Lorry')
8
+
9
+ # The last_name element
10
+ @ln = mock_element('span', {'itemprop' => 'last_name'}, 'Woodman')
11
+ end
12
+
13
+ context 'when there is no itemtype' do
14
+ before do
15
+ # The surrounding reviewer itemscope element
16
+ itemscope_el = mock_element('div', {'itemprop' => 'reviewer', 'itemscope' => true}, nil, [@fn,@ln])
17
+ @item = Mida::Item.new(itemscope_el)
18
+ end
19
+
20
+ it '#type should return the correct type' do
21
+ @item.type.should == nil
22
+ end
23
+
24
+ it '#id should return the correct id' do
25
+ @item.id.should == nil
26
+ end
27
+
28
+ it '#properties should return the correct name/value pairs' do
29
+ @item.properties.should == {
30
+ 'first_name' => ['Lorry'],
31
+ 'last_name' => ['Woodman']
32
+ }
33
+ end
34
+
35
+ it '#to_h should return the correct type and properties' do
36
+ @item.to_h.should == {
37
+ type: nil, id: nil, properties: {
38
+ 'first_name' => ['Lorry'],
39
+ 'last_name' => ['Woodman']
40
+ }
41
+ }
42
+ end
43
+ end
44
+
45
+ context 'when there is an itemtype' do
46
+ before do
47
+ # The surrounding reviewer itemscope element
48
+ itemscope_el = mock_element('div', {'itemprop' => 'reviewer', 'itemtype' => 'person', 'itemscope' => true}, nil, [@fn,@ln])
49
+ @item = Mida::Item.new(itemscope_el)
50
+ end
51
+
52
+ it '#type should return the correct type' do
53
+ @item.type.should == 'person'
54
+ end
55
+
56
+ it '#id should return the correct id' do
57
+ @item.id.should == nil
58
+ end
59
+
60
+ it '#properties should return the correct name/value pairs' do
61
+ @item.properties.should == {
62
+ 'first_name' => ['Lorry'],
63
+ 'last_name' => ['Woodman']
64
+ }
65
+ end
66
+
67
+ it '#to_h should return the correct type and properties' do
68
+ @item.to_h.should == {
69
+ type: 'person',
70
+ id: nil,
71
+ properties: {
72
+ 'first_name' => ['Lorry'],
73
+ 'last_name' => ['Woodman']
74
+ }
75
+ }
76
+ end
77
+ end
78
+ end
79
+
80
+ describe Mida::Item, 'when initialized with an itemscope containing an itemprop with a relative url' do
81
+ before do
82
+ @url = mock_element('a', {'itemprop' => 'url', 'href' => 'home/lorry'})
83
+ itemscope_el = mock_element('div', {'itemscope' => true}, nil, [@url])
84
+ @item = Mida::Item.new(itemscope_el, "http://example.com")
85
+ end
86
+
87
+ it 'should return the url as an absolute url' do
88
+ @item.properties['url'].should == ['http://example.com/home/lorry']
89
+ end
90
+ end
91
+
92
+ describe Mida::Item, 'when initialized with an itemscope containing itemprops surrounded by a non microdata element' do
93
+ before do
94
+ # The first_name element
95
+ fn = mock_element('span', {'itemprop' => 'first_name'}, 'Lorry')
96
+
97
+ # The last_name element
98
+ ln = mock_element('span', {'itemprop' => 'last_name'}, 'Woodman')
99
+
100
+ # A non microdata element surrounding last_name
101
+ surround = mock_element('span', {}, nil, [ln])
102
+
103
+ # The surrounding reviewer itemscope element
104
+ itemscope_el = mock_element('div', {'itemprop' => 'reviewer',
105
+ 'itemtype' => 'person',
106
+ 'itemscope' => true},
107
+ nil, [fn,surround])
108
+ @item = Mida::Item.new(itemscope_el)
109
+ end
110
+
111
+ it '#type should return the correct type' do
112
+ @item.type.should == 'person'
113
+ end
114
+
115
+ it '#id should return the correct id' do
116
+ @item.id.should == nil
117
+ end
118
+
119
+ it '#properties should return the correct name/value pairs' do
120
+ @item.properties.should == {
121
+ 'first_name' => ['Lorry'],
122
+ 'last_name' => ['Woodman']
123
+ }
124
+ end
125
+
126
+ it '#to_h should return the correct type and properties' do
127
+ @item.to_h.should == {
128
+ type: 'person',
129
+ id: nil,
130
+ properties: {
131
+ 'first_name' => ['Lorry'],
132
+ 'last_name' => ['Woodman']
133
+ }
134
+ }
135
+ end
136
+
137
+ end
138
+
139
+ describe Mida::Item, 'when initialized with an itemscope containing itemprops with the same name' do
140
+ before do
141
+ # Lemon Sorbet flavour
142
+ ls_flavour = mock_element('span', {'itemprop' => 'flavour'}, 'Lemon Sorbet')
143
+
144
+ # Apricot Sorbet flavour
145
+ as_flavour = mock_element('span', {'itemprop' => 'flavour'}, 'Apricot Sorbet')
146
+
147
+ # Strawberry icecream
148
+ fruit = mock_element('span', {'itemprop' => 'fruit'}, 'Strawberry')
149
+
150
+ # Homemade icecream
151
+ style = mock_element('span', {'itemprop' => 'style'}, 'Homemade')
152
+
153
+ # The surrounding icecreame-type itemscope
154
+ @sb_flavour = mock_element('div', {'itemprop' => 'flavour',
155
+ 'itemtype' => 'icecream-type',
156
+ 'itemscope' => true},
157
+ nil, [fruit,style])
158
+
159
+ # The surrounding icecreams itemscope element
160
+ icecreams = mock_element('div', {'itemtype' => 'icecreams',
161
+ 'itemscope' => true},
162
+ nil, [ls_flavour, as_flavour, @sb_flavour])
163
+ @item = Mida::Item.new(icecreams)
164
+ end
165
+
166
+ it '#type should return the correct type' do
167
+ @item.type.should == 'icecreams'
168
+ end
169
+
170
+ it '#id should return the correct id' do
171
+ @item.id.should == nil
172
+ end
173
+
174
+ it '#properties should return the correct name/value pairs' do
175
+ @item.properties.should == {
176
+ 'flavour' => [
177
+ 'Lemon Sorbet',
178
+ 'Apricot Sorbet',
179
+ Mida::Item.new(@sb_flavour)
180
+ ]
181
+ }
182
+ end
183
+
184
+ it '#to_h should return the correct type and properties' do
185
+ @item.to_h.should == {
186
+ type: 'icecreams',
187
+ id: nil,
188
+ properties: {
189
+ 'flavour' => [
190
+ 'Lemon Sorbet',
191
+ 'Apricot Sorbet',
192
+ { type: 'icecream-type',
193
+ id: nil,
194
+ properties: {
195
+ 'fruit' => ['Strawberry'],
196
+ 'style' => ['Homemade']
197
+ }
198
+ }
199
+ ]
200
+ }
201
+ }
202
+ end
203
+
204
+ end
205
+
206
+ describe Mida::Item, 'when initialized with an itemscope containing itemrefs' do
207
+
208
+ before do
209
+
210
+ name = mock_element('span', {'itemprop' => 'name'}, 'Amanda')
211
+ name_p = mock_element('p', {'id' => 'a'}, nil, [name])
212
+
213
+ band_name = mock_element('span', {'itemprop' => 'band_name'}, 'Jazz Band')
214
+ band_size = mock_element('span', {'itemprop' => 'band_size'}, '12')
215
+ band_div = mock_element('div', {'id' => 'c'}, nil, [band_name, band_size])
216
+ @empty_band_div = mock_element('div', {'id' => 'b',
217
+ 'itemprop' => 'band',
218
+ 'itemref' => 'c',
219
+ 'itemscope' => true},
220
+ nil, [],
221
+ {'c' => band_div})
222
+
223
+
224
+ age = mock_element('span', {'itemprop' => 'age'}, '30')
225
+ age_div = mock_element('div', {'itemref' => 'a b',
226
+ 'itemscope' => true},
227
+ nil, [age],
228
+ {'a' => name_p, 'b' => @empty_band_div})
229
+
230
+ @item = Mida::Item.new(age_div)
231
+ end
232
+
233
+ it '#type should return the correct type' do
234
+ @item.type.should == nil
235
+ end
236
+
237
+ it '#id should return the correct id' do
238
+ @item.id.should == nil
239
+ end
240
+
241
+ it '#properties should return the correct name/value pairs' do
242
+ @item.properties.should == {
243
+ 'age' => ['30'],
244
+ 'name' => ['Amanda'],
245
+ 'band' => [Mida::Item.new(@empty_band_div)]
246
+ }
247
+ end
248
+
249
+ it '#to_h should return the correct type and properties' do
250
+ @item.to_h.should == {
251
+ type: nil,
252
+ id: nil,
253
+ properties: {
254
+ 'age' => ['30'],
255
+ 'name' => ['Amanda'],
256
+ 'band' => [{
257
+ type: nil,
258
+ id: nil,
259
+ properties: {
260
+ 'band_name' => ['Jazz Band'],
261
+ 'band_size' => ['12']
262
+ }
263
+ }]
264
+ }
265
+ }
266
+ end
267
+
268
+ end
269
+
270
+ describe Mida::Item, 'when initialized with an itemscope containing an itemid' do
271
+
272
+ before do
273
+
274
+ title = mock_element('span', {'itemprop' => 'title'}, 'Hacking Vim 7.2')
275
+ author = mock_element('span', {'itemprop' => 'author'}, 'Kim Schulz')
276
+ book = mock_element('div', {
277
+ 'itemtype' => 'book',
278
+ 'itemid' => 'urn:isbn:978-1-849510-50-9',
279
+ 'itemscope' => true},
280
+ nil, [title,author])
281
+
282
+ @item = Mida::Item.new(book)
283
+ end
284
+
285
+ it '#type should return the correct type' do
286
+ @item.type.should == 'book'
287
+ end
288
+
289
+ it '#id should return the correct id' do
290
+ @item.id.should == 'urn:isbn:978-1-849510-50-9'
291
+ end
292
+
293
+ it '#properties should return the correct name/value pairs' do
294
+ @item.properties.should == {
295
+ 'title' => ['Hacking Vim 7.2'],
296
+ 'author' => ['Kim Schulz']
297
+ }
298
+ end
299
+
300
+ it '#to_h should return the correct type and properties' do
301
+ @item.to_h.should == {
302
+ type: 'book',
303
+ id: 'urn:isbn:978-1-849510-50-9',
304
+ properties: {
305
+ 'title' => ['Hacking Vim 7.2'],
306
+ 'author' => ['Kim Schulz']
307
+ }
308
+ }
309
+ end
310
+
311
+ end
312
+
313
+ describe Mida::Item, 'when initialized with an itemscope containing itemscopes as properties nested two deep' do
314
+ before do
315
+
316
+ # The name of the item reviewed
317
+ @item_name = mock_element('span', {'itemprop' => 'item_name'}, 'Acme Anvil')
318
+
319
+ # The rating of the item
320
+ @rating = mock_element('span', {'itemprop' => 'rating'}, '5')
321
+
322
+ # The first_name
323
+ @fn = mock_element('span', {'itemprop' => 'first_name'}, 'Lorry')
324
+
325
+ # The last_name
326
+ @ln = mock_element('span', {'itemprop' => 'last_name'}, 'Woodman')
327
+
328
+ # The organization name
329
+ @org_name = mock_element('span', {'itemprop' => 'name'}, 'Acme')
330
+
331
+ # The surrounding organization itemscope
332
+ @org_el = mock_element('div', {'itemprop' => 'represents',
333
+ 'itemtype' => 'organization',
334
+ 'itemscope' => true}, nil, [@org_name])
335
+
336
+ # The surrounding reviewer itemscope
337
+ @reviewer_el = mock_element('div', {'itemprop' => 'reviewer',
338
+ 'itemtype' => 'person',
339
+ 'itemscope' => true},
340
+ nil, [@fn,@ln, @org_el])
341
+
342
+ # The surrounding reviewer itemscope
343
+ @review_el = mock_element('div', {'itemtype' => 'review', 'itemscope' => true}, nil, [@item_name, @rating, @reviewer_el])
344
+
345
+ @item = Mida::Item.new(@review_el)
346
+
347
+ end
348
+
349
+ before do
350
+ end
351
+
352
+ it '#type should return the correct type' do
353
+ @item.type.should == 'review'
354
+ end
355
+
356
+ it '#id should return the correct id' do
357
+ @item.id.should == nil
358
+ end
359
+
360
+ it '#properties should return the correct name/value pairs' do
361
+ @item.properties.should == {
362
+ 'item_name' => ['Acme Anvil'],
363
+ 'rating' => ['5'],
364
+ 'reviewer' => [Mida::Item.new(@reviewer_el)]
365
+ }
366
+ end
367
+
368
+ it '#to_h should return the correct type and properties' do
369
+ @item.to_h.should == {
370
+ type: 'review',
371
+ id: nil,
372
+ properties: {
373
+ 'item_name' => ['Acme Anvil'],
374
+ 'rating' => ['5'],
375
+ 'reviewer' => [{
376
+ type: 'person',
377
+ id: nil,
378
+ properties: {
379
+ 'first_name' => ['Lorry'],
380
+ 'last_name' => ['Woodman'],
381
+ 'represents' => [{
382
+ type: 'organization',
383
+ id: nil,
384
+ properties: {
385
+ 'name' => ['Acme']
386
+ }
387
+ }]
388
+ }
389
+ }]
390
+ }
391
+ }
392
+ end
393
+ end
@@ -0,0 +1,152 @@
1
+ require_relative 'spec_helper'
2
+ require_relative '../lib/mida'
3
+
4
+
5
+ describe Mida::Property, 'when parsing an element without an itemprop attribute' do
6
+ before do
7
+ @element = mock_element('span')
8
+ end
9
+
10
+ it '#parse should return an empty Hash' do
11
+ Mida::Property.parse(@element).should == {}
12
+ end
13
+ end
14
+
15
+ describe Mida::Property, 'when parsing an element with one itemprop name' do
16
+ before do
17
+ @element = mock_element('span', {'itemprop' => 'reviewer'}, 'Lorry Woodman')
18
+ end
19
+
20
+ it '#parse should return a Hash with the correct name/value pair' do
21
+ Mida::Property.parse(@element).should == {'reviewer' => 'Lorry Woodman'}
22
+ end
23
+ end
24
+
25
+ describe Mida::Property, 'when parsing an itemscope element that has a relative url' do
26
+ before do
27
+
28
+ # The first_name element
29
+ fn = mock_element('span', {'itemprop' => 'first_name'}, 'Lorry')
30
+
31
+ # The last_name element
32
+ ln = mock_element('span', {'itemprop' => 'last_name'}, 'Woodman')
33
+
34
+ # The url. This is important to check whether the page_url is passed onto Item
35
+ url = mock_element('a', {'itemprop' => 'url', 'href' => 'home/LorryWoodman'})
36
+
37
+ # The surrounding reviewer itemscope element
38
+ @itemscope_el = mock_element('div', {'itemprop' => 'reviewer',
39
+ 'itemtype' => 'person',
40
+ 'itemscope' =>true}, nil, [fn,ln,url])
41
+ end
42
+
43
+ it '#parse should return a Hash with the correct name/value pair' do
44
+ property = Mida::Property.parse(@itemscope_el, "http://example.com")
45
+ property.size.should == 1
46
+ reviewer = property['reviewer']
47
+ reviewer.type.should == 'person'
48
+ reviewer.properties.should == {
49
+ 'first_name' => ['Lorry'],
50
+ 'last_name' => ['Woodman'],
51
+ 'url' => ['http://example.com/home/LorryWoodman']
52
+ }
53
+ end
54
+ end
55
+
56
+ describe Mida::Property, 'when parsing an element with multiple itemprop names' do
57
+ before do
58
+ @element = mock_element('span', {'itemprop' => 'reviewer friend person'}, 'the property text')
59
+ end
60
+
61
+ it '#parse should return a Hash with the name/value pairs' do
62
+ Mida::Property.parse(@element).should == {
63
+ 'reviewer' => 'the property text',
64
+ 'friend' => 'the property text',
65
+ 'person' => 'the property text'
66
+ }
67
+ end
68
+ end
69
+
70
+ describe Mida::Property, 'when parsing an element with non text content url values' do
71
+ before :all do
72
+ URL_ELEMENTS = {
73
+ 'a' => 'href', 'area' => 'href',
74
+ 'audio' => 'src', 'embed' => 'src',
75
+ 'iframe' => 'src', 'img' => 'src',
76
+ 'link' => 'href', 'source' => 'src',
77
+ 'object' => 'data', 'track' => 'src',
78
+ 'video' => 'src'
79
+ }
80
+ end
81
+
82
+ context 'when not given a page_url' do
83
+
84
+ it 'should return nothing for relative urls' do
85
+ url = 'register/index.html'
86
+ URL_ELEMENTS.each do |tag, attr|
87
+ element = mock_element(tag, {'itemprop' => 'url', attr => url})
88
+ Mida::Property.parse(element).should == {'url' => ''}
89
+ end
90
+ end
91
+
92
+ it 'should return the url for absolute urls' do
93
+ urls = [
94
+ 'http://example.com',
95
+ 'http://example.com/register',
96
+ 'http://example.com/register/index.html'
97
+ ]
98
+
99
+ urls.each do |url|
100
+ URL_ELEMENTS.each do |tag, attr|
101
+ element = mock_element(tag, {'itemprop' => 'url', attr => url})
102
+ Mida::Property.parse(element).should == {'url' => url}
103
+ end
104
+ end
105
+ end
106
+ end
107
+
108
+ context 'when given a page_url' do
109
+ before do
110
+ @page_url = 'http://example.com/test/index.html'
111
+ end
112
+
113
+ it 'should return the absolute url for relative urls' do
114
+ url = 'register/index.html'
115
+ URL_ELEMENTS.each do |tag, attr|
116
+ element = mock_element(tag, {'itemprop' => 'url', attr => url})
117
+ Mida::Property.parse(element, @page_url).should ==
118
+ {'url' => 'http://example.com/test/register/index.html'}
119
+ end
120
+ end
121
+
122
+ it 'should return the url unchanged for absolute urls' do
123
+ urls = [
124
+ 'http://example.com',
125
+ 'http://example.com/register',
126
+ 'http://example.com/register/index.html'
127
+ ]
128
+
129
+ urls.each do |url|
130
+ URL_ELEMENTS.each do |tag, attr|
131
+ element = mock_element(tag, {'itemprop' => 'url', attr => url})
132
+ Mida::Property.parse(element, @page_url).should == {'url' => url}
133
+ end
134
+ end
135
+ end
136
+
137
+ end
138
+ end
139
+
140
+ describe Mida::Property, 'when parsing an element with non text content non url values' do
141
+ it 'should get values from a meta content attribute' do
142
+ element = mock_element('meta', {'itemprop' => 'reviewer',
143
+ 'content' => 'Lorry Woodman'})
144
+ Mida::Property.parse(element).should == {'reviewer' => 'Lorry Woodman'}
145
+ end
146
+
147
+ it 'should get time from an time datatime attribute' do
148
+ element = mock_element('time', {'itemprop' => 'dtreviewed',
149
+ 'datetime' => '2011-04-04'})
150
+ Mida::Property.parse(element).should == {'dtreviewed' => '2011-04-04'}
151
+ end
152
+ end