spec/happymapper_spec.rb in nokogiri-happymapper-0.5.5 vs spec/happymapper_spec.rb in nokogiri-happymapper-0.5.6

- old
+ new

@@ -118,11 +118,11 @@ has_one :feature_bullets, FeatureBullet has_one :address, Address end class Rate - include HappyMapper + include HappyMapper end module FamilySearch class AlternateIds include HappyMapper @@ -311,11 +311,11 @@ content :name, String end class State - include HappyMapper + include HappyMapper end class Address include HappyMapper @@ -451,103 +451,103 @@ end end class PublishOptions include HappyMapper - + tag 'publishOptions' - + element :author, String, :tag => 'author' element :draft, Boolean, :tag => 'draft' element :scheduled_day, String, :tag => 'scheduledDay' element :scheduled_time, String, :tag => 'scheduledTime' element :published_day, String, :tag => 'publishDisplayDay' element :published_time, String, :tag => 'publishDisplayTime' element :created_day, String, :tag => 'publishDisplayDay' element :created_time, String, :tag => 'publishDisplayTime' - + end class Article include HappyMapper - + tag 'Article' namespace 'article' - + attr_writer :xml_value - + element :title, String element :text, String has_many :photos, 'Photo', :tag => 'Photo', :namespace => 'photo', :xpath => '/article:Article' has_many :galleries, 'Gallery', :tag => 'Gallery', :namespace => 'gallery' - + element :publish_options, PublishOptions, :tag => 'publishOptions', :namespace => 'article' - + end class PartiallyBadArticle include HappyMapper - + attr_writer :xml_value - + tag 'Article' namespace 'article' - + element :title, String element :text, String has_many :photos, 'Photo', :tag => 'Photo', :namespace => 'photo', :xpath => '/article:Article' has_many :videos, 'Video', :tag => 'Video', :namespace => 'video' - + element :publish_options, PublishOptions, :tag => 'publishOptions', :namespace => 'article' - + end class Photo include HappyMapper tag 'Photo' namespace 'photo' - + attr_writer :xml_value - + element :title, String element :publish_options, PublishOptions, :tag => 'publishOptions', :namespace => 'photo' - + end class Gallery include HappyMapper - + tag 'Gallery' namespace 'gallery' attr_writer :xml_value element :title, String - + end class Video include HappyMapper - + tag 'Video' namespace 'video' attr_writer :xml_value - + element :title, String element :publish_options, PublishOptions, :tag => 'publishOptions', :namespace => 'video' - + end class OptionalAttribute include HappyMapper tag 'address' - + attribute :street, String -end +end class DefaultNamespaceCombi include HappyMapper @@ -671,27 +671,27 @@ Post.elements.size.should == 0 Status.elements.size.should == 10 end end - describe "#content" do - it "should take String as default argument for type" do + describe "#content" do + it "should take String as default argument for type" do State.content :name address = Address.parse(fixture_file('address.xml')) address.state.name.should == "Lower Saxony" address.state.name.class == String end - - it "should work when specific type is provided" do + + it "should work when specific type is provided" do Rate.content :value, Float Product.has_one :rate, Rate - product = Product.parse(fixture_file('product_default_namespace.xml'), :single => true) + product = Product.parse(fixture_file('product_default_namespace.xml'), :single => true) product.rate.value.should == 120.25 product.rate.class == Float - end + end end - + it "should parse xml attributes into ruby objects" do posts = Post.parse(fixture_file('posts.xml')) posts.size.should == 20 first = posts.first first.href.should == 'http://roxml.rubyforge.org/' @@ -944,30 +944,30 @@ it "should parse lastfm namespaces" do l = Location.parse(fixture_file('lastfm.xml')) l.first.latitude.should == "51.53469" end - + describe "Parse optional attributes" do - + it "should parse an empty String as empty" do a = OptionalAttribute.parse(fixture_file('optional_attributes.xml')) a[0].street.should == "" end - + it "should parse a String with value" do a = OptionalAttribute.parse(fixture_file('optional_attributes.xml')) a[1].street.should == "Milchstrasse" end - + it "should parse a String with value" do a = OptionalAttribute.parse(fixture_file('optional_attributes.xml')) a[2].street.should be_nil end - + end - + describe "Default namespace combi" do before(:each) do file_contents = fixture_file('default_namespace_combi.xml') @book = DefaultNamespaceCombi.parse(file_contents, :single => true) end @@ -1003,20 +1003,20 @@ it "should parse ambigous items" do items = AmbigousItems::Item.parse(fixture_file('ambigous_items.xml'), :xpath => '/ambigous/my-items') items.map(&:name).should == %w(first second third).map{|s| "My #{s} item" } end - - + + context Article do it "should parse the publish options for Article and Photo" do @article.title.should_not be_nil @article.text.should_not be_nil @article.photos.should_not be_nil @article.photos.first.title.should_not be_nil end - + it "should parse the publish options for Article" do @article.publish_options.should_not be_nil end it "should parse the publish options for Photo" do @@ -1024,29 +1024,29 @@ end it "should only find only items at the parent level" do @article.photos.length.should == 1 end - + before(:all) do @article = Article.parse(fixture_file('subclass_namespace.xml')) end - + end - + context "Namespace is missing because an optional element that uses it is not present" do it "should parse successfully" do @article = PartiallyBadArticle.parse(fixture_file('subclass_namespace.xml')) @article.should_not be_nil @article.title.should_not be_nil @article.text.should_not be_nil @article.photos.should_not be_nil @article.photos.first.title.should_not be_nil end end - - + + describe "with limit option" do it "should return results with limited size: 6" do sizes = [] posts = Post.parse(fixture_file('posts.xml'), :in_groups_of => 6) do |a| sizes << a.size @@ -1060,7 +1060,46 @@ sizes << a.size end sizes.should == [10, 10] end end - + + context "when letting user set Nokogiri::XML::ParseOptions" do + let(:default) { + Class.new do + include HappyMapper + element :item, String + end + } + let(:custom) { + Class.new do + include HappyMapper + element :item, String + with_nokogiri_config do |config| + config.default_xml + end + end + } + + it 'initializes @nokogiri_config_callback to nil' do + default.nokogiri_config_callback.should be_nil + end + + it 'defaults to Nokogiri::XML::ParseOptions::STRICT' do + expect { default.parse(fixture_file('set_config_options.xml')) }.to raise_error(Nokogiri::XML::SyntaxError) + end + + it 'accepts .on_config callback' do + custom.nokogiri_config_callback.should_not be_nil + end + + it 'parses according to @nokogiri_config_callback' do + expect { custom.parse(fixture_file('set_config_options.xml')) }.to_not raise_error(Nokogiri::XML::SyntaxError) + end + + it 'can clear @nokogiri_config_callback' do + custom.with_nokogiri_config {} + expect { custom.parse(fixture_file('set_config_options.xml')) }.to raise_error(Nokogiri::XML::SyntaxError) + end + end + end