spec/reader_spec.rb in milkfarm-onix-0.8.11 vs spec/reader_spec.rb in milkfarm-onix-0.8.12

- old
+ new

@@ -10,10 +10,11 @@ @file2 = File.join(@data_path, "two_products.xml") @long_file = File.join(@data_path, "Bookwise_July_2008.xml") @entity_file = File.join(@data_path, "entities.xml") @utf_16_file = File.join(@data_path, "utf_16.xml") @iso_8859_1_file = File.join(@data_path, "iso_8859_1.xml") + @no_encoding_decl_file = File.join(@data_path, "aau.xml") end it "should initialize with a filename" do reader = ONIX::Reader.new(@file1) reader.instance_variable_get("@reader").should be_a_kind_of(Nokogiri::XML::Reader) @@ -88,23 +89,40 @@ counter.should eql(346) end it "should transparently convert a iso-8859-1 file to utf-8" do reader = ONIX::Reader.new(@iso_8859_1_file) - product = nil - reader.each do |p| - product = p - end + reader.each do |product| + if RUBY_VERSION >= "1.9" + utf8 = Encoding.find("utf-8") + product.contributors[0].person_name_inverted.encoding.should eql(utf8) + end - # ROXML appears to munge the string encodings - if RUBY_VERSION >= "1.9" - utf8 = Encoding.find("utf-8") - product.contributors[0].person_name_inverted.encoding.should eql(utf8) + product.contributors[0].person_name_inverted.should eql("Küng, Hans") end + end - product.contributors[0].person_name_inverted.should eql("Küng, Hans") + # This isn't ideal behaviour, but i'm somewhat hamstrung by nokogiri API. It'd + # be nice to have the option to replace unrecognised bytes with a valid char. + it "should raise an exception when an iso-8859-1 file isn't declared as such" do + reader = ONIX::Reader.new(@no_encoding_decl_file) + lambda { + reader.each do |product| + end + }.should raise_error(Nokogiri::XML::SyntaxError) + end + it "should transparently convert an iso-8859-1 file to utf-8 when there's no declaration but the user manually specifies iso-8859-1" do + reader = ONIX::Reader.new(@no_encoding_decl_file, :encoding => "iso-8859-1") + reader.each do |product| + if RUBY_VERSION >= "1.9" + utf8 = Encoding.find("utf-8") + product.contributors[0].person_name_inverted.encoding.should eql(utf8) + end + + product.contributors[0].person_name_inverted.should eql("Melo,Patr¡cia") + end end it "should transparently convert a utf-16 file to utf-8" do reader = ONIX::Reader.new(@utf_16_file) product = nil @@ -117,8 +135,21 @@ utf8 = Encoding.find("utf-8") product.contributors[0].person_name_inverted.encoding.should eql(utf8) end product.contributors[0].person_name_inverted.should eql("Küng, Hans") + end + it "should support returning an APAProduct using deprecated API" do + reader = ONIX::Reader.new(@file1, ONIX::APAProduct) + reader.each do |product| + product.should be_a_kind_of(ONIX::APAProduct) + end + end + + it "should support returning an APAProduct using new API" do + reader = ONIX::Reader.new(@file1, :product_class => ONIX::APAProduct) + reader.each do |product| + product.should be_a_kind_of(ONIX::APAProduct) + end end end