require 'spec_helper' require 'mida' describe Mida::Vocabulary, 'when subclassed and given has statements with no blocks' do before do class Organization < Mida::Vocabulary itemtype %r{http://example\.com.*?organization$}i has_one 'name' has_many 'tel', 'url' end end it '#itemtype should return the correct regexp' do Organization.itemtype.should == %r{http://example\.com.*?organization$}i end it 'should specify name to appear once' do Organization.properties['name'][:num].should == :one end it 'should specify tel and url to appear many times' do Organization.properties['tel'][:num].should == :many Organization.properties['url'][:num].should == :many end end describe Mida::Vocabulary, 'when subclassed and given has statements with blocks' do before do class Rating < Mida::Vocabulary itemtype %r{http://example\.com.*?rating$}i has_one 'best', 'value' end class Comment < Mida::Vocabulary itemtype %r{http://example\.com.*?comment$}i has_one 'commentor', 'comment' end class Review < Mida::Vocabulary itemtype %r{http://example\.com.*?review$}i has_one 'itemreviewed' has_one 'rating' do extract Rating, Mida::DataType::Text end has_many 'comments' do extract Comment end end end it '#itemtype should return the correct regexp' do Review.itemtype.should == %r{http://example\.com.*?review$}i end it 'should specify itemreviewed to appear once' do Review.properties['itemreviewed'][:num].should == :one end it 'should specify that itemreviewed only have the type Mida::DataType::Text' do Review.properties['itemreviewed'][:types].should == [Mida::DataType::Text] end it 'should specify rating to appear once' do Review.properties['rating'][:num].should == :one end it 'should specify rating to only have the types: Rating, Mida::DataType::Text' do Review.properties['rating'][:types].should == [Rating, Mida::DataType::Text] end it 'should specify comments to appear many times' do Review.properties['comments'][:num].should == :many end it 'should specify that comments only have the type Comment' do Review.properties['comments'][:types].should == [Comment] end end describe Mida::Vocabulary, 'when subclassed and used with :any for properties and types' do before do class Person < Mida::Vocabulary itemtype %r{http://example.com/vocab/person} has_one 'name' has_many :any do extract :any end end end it '#itemtype should return the correct regexp' do Person.itemtype.should == %r{http://example.com/vocab/person} end it 'should specify that name only appears once' do Person.properties['name'][:num].should == :one end it 'should specify that any other property can appear many times' do Person.properties[:any][:num].should == :many end it 'should specify that any other property can have any type' do Person.properties[:any][:types].should == [:any] end end describe Mida::Vocabulary, 'when subclassed' do before do # Make sure the class is redefined afresh to make sure that # inherited() hook is called Mida::Vocabulary.unregister(Person) Object.send(:remove_const, :Person) class Person < Mida::Vocabulary itemtype %r{http://example.com/vocab/person} has_one 'name' has_many :any do extract :any end end end it 'should register the vocabulary subclass' do Mida::Vocabulary.vocabularies.should include(Person) end end describe Mida::Vocabulary, 'when subclassed and has no properties' do before do class Empty < Mida::Vocabulary itemtype %r{http://example.com/vocab/empty} end end it 'should register the vocabulary subclass' do Mida::Vocabulary.vocabularies.should include(Empty) end it '#properties should return an empty hash' do Mida::Vocabulary.properties.should == {} end end