spec/item_spec.rb in mida-0.1.3 vs spec/item_spec.rb in mida-0.2.0

- old
+ new

@@ -15,10 +15,14 @@ # The surrounding reviewer itemscope element itemscope_el = mock_element('div', {'itemprop' => 'reviewer', 'itemscope' => true}, nil, [@fn,@ln]) @item = Mida::Item.new(itemscope_el) end + it '#vocabulary should return the correct vocabulary' do + @item.vocabulary.should == Mida::Vocabulary::Generic + end + it '#type should return the correct type' do @item.type.should == nil end it '#id should return the correct id' do @@ -32,11 +36,11 @@ } end it '#to_h should return the correct type and properties' do @item.to_h.should == { - type: nil, id: nil, properties: { + vocabulary: Mida::Vocabulary::Generic, type: nil, id: nil, properties: { 'first_name' => ['Lorry'], 'last_name' => ['Woodman'] } } end @@ -47,10 +51,14 @@ # The surrounding reviewer itemscope element itemscope_el = mock_element('div', {'itemprop' => 'reviewer', 'itemtype' => 'person', 'itemscope' => true}, nil, [@fn,@ln]) @item = Mida::Item.new(itemscope_el) end + it '#vocabulary should return the correct vocabulary' do + @item.vocabulary.should == Mida::Vocabulary::Generic + end + it '#type should return the correct type' do @item.type.should == 'person' end it '#id should return the correct id' do @@ -64,10 +72,11 @@ } end it '#to_h should return the correct type and properties' do @item.to_h.should == { + vocabulary: Mida::Vocabulary::Generic, type: 'person', id: nil, properties: { 'first_name' => ['Lorry'], 'last_name' => ['Woodman'] @@ -110,10 +119,14 @@ it '#type should return the correct type' do @item.type.should == 'person' end + it '#vocabulary should return the correct vocabulary' do + @item.vocabulary.should == Mida::Vocabulary::Generic + end + it '#id should return the correct id' do @item.id.should == nil end it '#properties should return the correct name/value pairs' do @@ -123,10 +136,11 @@ } end it '#to_h should return the correct type and properties' do @item.to_h.should == { + vocabulary: Mida::Vocabulary::Generic, type: 'person', id: nil, properties: { 'first_name' => ['Lorry'], 'last_name' => ['Woodman'] @@ -143,10 +157,14 @@ doc = Nokogiri(html) itemscope = doc.search('./*').first @item = Mida::Item.new(itemscope) end + it '#vocabulary should return the correct vocabulary' do + @item.vocabulary.should == Mida::Vocabulary::Generic + end + it '#type should return the correct type' do @item.type.should == nil end it '#id should return the correct id' do @@ -159,10 +177,11 @@ } end it '#to_h should return the correct type and properties' do @item.to_h.should == { + vocabulary: Mida::Vocabulary::Generic, type: nil, id: nil, properties: { 'reviewer' => ['Lorry Woodman'] } @@ -182,10 +201,14 @@ doc = Nokogiri(html) itemscope = doc.search('./*').first @item = Mida::Item.new(itemscope) end + it '#vocabulary should return the correct vocabulary' do + @item.vocabulary.should == Mida::Vocabulary::Generic + end + it '#type should return the correct type' do @item.type.should == nil end it '#id should return the correct id' do @@ -199,10 +222,11 @@ } end it '#to_h should return the correct type and properties' do @item.to_h.should == { + vocabulary: Mida::Vocabulary::Generic, type: nil, id: nil, properties: { 'description' => ['The animal is a green parrot.'], 'colour' => ['green'] @@ -237,10 +261,14 @@ 'itemscope' => true}, nil, [ls_flavour, as_flavour, @sb_flavour]) @item = Mida::Item.new(icecreams) end + it '#vocabulary should return the correct vocabulary' do + @item.vocabulary.should == Mida::Vocabulary::Generic + end + it '#type should return the correct type' do @item.type.should == 'icecreams' end it '#id should return the correct id' do @@ -257,17 +285,19 @@ } end it '#to_h should return the correct type and properties' do @item.to_h.should == { + vocabulary: Mida::Vocabulary::Generic, type: 'icecreams', id: nil, properties: { 'flavour' => [ 'Lemon Sorbet', 'Apricot Sorbet', - { type: 'icecream-type', + { vocabulary: Mida::Vocabulary::Generic, + type: 'icecream-type', id: nil, properties: { 'fruit' => ['Strawberry'], 'style' => ['Homemade'] } @@ -304,10 +334,14 @@ {'a' => name_p, 'b' => @empty_band_div}) @item = Mida::Item.new(age_div) end + it '#vocabulary should return the correct vocabulary' do + @item.vocabulary.should == Mida::Vocabulary::Generic + end + it '#type should return the correct type' do @item.type.should == nil end it '#id should return the correct id' do @@ -322,16 +356,18 @@ } end it '#to_h should return the correct type and properties' do @item.to_h.should == { + vocabulary: Mida::Vocabulary::Generic, type: nil, id: nil, properties: { 'age' => ['30'], 'name' => ['Amanda'], 'band' => [{ + vocabulary: Mida::Vocabulary::Generic, type: nil, id: nil, properties: { 'band_name' => ['Jazz Band'], 'band_size' => ['12'] @@ -356,10 +392,14 @@ nil, [title,author]) @item = Mida::Item.new(book) end + it '#vocabulary should return the correct vocabulary' do + @item.vocabulary.should == Mida::Vocabulary::Generic + end + it '#type should return the correct type' do @item.type.should == 'book' end it '#id should return the correct id' do @@ -373,10 +413,11 @@ } end it '#to_h should return the correct type and properties' do @item.to_h.should == { + vocabulary: Mida::Vocabulary::Generic, type: 'book', id: 'urn:isbn:978-1-849510-50-9', properties: { 'title' => ['Hacking Vim 7.2'], 'author' => ['Kim Schulz'] @@ -423,10 +464,14 @@ end before do end + it '#vocabulary should return the correct vocabulary' do + @item.vocabulary.should == Mida::Vocabulary::Generic + end + it '#type should return the correct type' do @item.type.should == 'review' end it '#id should return the correct id' do @@ -441,29 +486,99 @@ } end it '#to_h should return the correct type and properties' do @item.to_h.should == { + vocabulary: Mida::Vocabulary::Generic, type: 'review', id: nil, properties: { 'item_name' => ['Acme Anvil'], 'rating' => ['5'], 'reviewer' => [{ + vocabulary: Mida::Vocabulary::Generic, type: 'person', id: nil, properties: { 'first_name' => ['Lorry'], 'last_name' => ['Woodman'], 'represents' => [{ + vocabulary: Mida::Vocabulary::Generic, type: 'organization', id: nil, properties: { 'name' => ['Acme'] } }] } }] } } + end +end + +describe Mida::Item, 'when initialized with an itemscope that matches a non-generic registered vocabulary' do + before do + + class Colour < Mida::VocabularyDesc + itemtype %r{http://example.com/vocab/colour} + has_one 'red', 'green', 'blue' + end + Mida::Vocabulary.register(Colour) + + class Person < Mida::VocabularyDesc + itemtype %r{http://example.com/vocab/person} + has_one 'name' + has_one 'url' + has_many 'limbs' + has_many 'favourite-colours' do + types Colour + end + end + Mida::Vocabulary.register(Person) + + red = mock_element('span', {'itemprop' => 'red'}, '0xFF') + green = mock_element('span', {'itemprop' => 'green'}, '0x00') + blue = mock_element('span', {'itemprop' => 'blue'}, '0xFF') + purple = mock_element('div', {'itemscope' => true, + 'itemtype' => 'http://example.com/vocab/colour', + 'itemprop' => 'favourite-colours'}, + nil, [red, green, blue]) + orange = mock_element('span', {'itemprop' => 'favourite-colours'}, 'Orange') + + name1 = mock_element('span', {'itemprop' => 'name'}, 'Lawrence Woodman') + name2 = mock_element('span', {'itemprop' => 'name'}, 'Lorry Woodman') + url = mock_element('a', {'itemprop' => 'url', 'href' => 'http://example.com/myhomepage'}) + arm = mock_element('span', {'itemprop' => 'limbs'}, 'Arm') + leg = mock_element('span', {'itemprop' => 'limbs'}, 'Leg') + robert_wilson = mock_element('span', {'itemprop' => 'favourite-author'}, 'Robert Wilson') + itemscope_el = mock_element('div', {'itemscope' => true, + 'itemtype' =>'http://example.com/vocab/person' + }, nil, [name1, name2, url, arm, leg, purple, orange, robert_wilson]) + @item = Mida::Item.new(itemscope_el, "http://example.com") + end + + it '#vocabulary should return the correct vocabulary' do + @item.vocabulary.should == Person + end + + it 'should reject properties that have multiple values if has_one specified' do + @item.properties.should_not have_key('name') + end + + it 'should accept properties that have a single value if has_one specified' do + @item.properties['url'].should == ['http://example.com/myhomepage'] + end + + it 'should accept properties that have a many values if has_many specified' do + @item.properties['limbs'].should == ['Arm', 'Leg'] + end + + it 'should register properties using the specified types' do + @item.properties['favourite-colours'].size.should == 1 + @item.properties['favourite-colours'].first.vocabulary.should == Colour + end + + it 'should reject properties that are not specified' do + @item.properties.should_not have_key('favourite-author') end end