require 'spec_helper' describe "MARC4J4R_reader" do it "can read binary" do reader = MARC4J4R::Reader.new("#{DIR}/one.dat") rec = reader.first rec['245']['a'].should.equal "The Texas ranger" end it "can read XML" do reader = MARC4J4R::Reader.new("#{DIR}/one.xml", :marcxml) rec = reader.first rec['245']['a'].should.equal "The Texas ranger" end it "enumerates through all binary records" do reader = MARC4J4R::Reader.new("#{DIR}/batch.dat") recs = reader.collect recs.size.should.equal 10 end it "enumerates through all XML records" do reader = MARC4J4R::Reader.new("#{DIR}/batch.xml", :marcxml) recs = reader.collect recs.size.should.equal 10 end it "Bails if passed an illegal type" do lambda{reader = MARC4J4R::Reader.new("#{DIR}/batch.xml", :marc)}.should.raise(ArgumentError).message.should.match(/^Reader type/) end it "Bails if passed an illegal encoding" do lambda{reader = MARC4J4R::Reader.new("#{DIR}/batch.xml", :strictmarc, :utf)}.should.raise(ArgumentError).message.should.match(/^Encoding/) end end describe "Reader encodings, strict reader" do it "Works with UTF-8 without explicit encoding" do reader = MARC4J4R::Reader.new("#{DIR}/chinese_utf8.dat") r = reader.first second_title = r.find_by_tag('245')[1] second_title['a'].should.equal "湘鄂贛苏区史稿 /" end it "Works with UTF-8, explicit encoding" do reader = MARC4J4R::Reader.new("#{DIR}/chinese_utf8.dat", :strictmarc, :utf8) r = reader.first second_title = r.find_by_tag('245')[1] second_title['a'].should.equal "湘鄂贛苏区史稿 /" end it "Fails with UTF-8, wrong explicit encoding" do reader = MARC4J4R::Reader.new("#{DIR}/chinese_utf8.dat", :strictmarc, :marc8) r = reader.first second_title = r.find_by_tag('245')[1] second_title['a'].should.not.equal "湘鄂贛苏区史稿 /" end it "Fails with UTF-8, wrong explicit encoding" do reader = MARC4J4R::Reader.new("#{DIR}/chinese_utf8.dat", :strictmarc, :iso) r = reader.first second_title = r.find_by_tag('245')[1] second_title['a'].should.not.equal "湘鄂贛苏区史稿 /" end end describe "Reader encodings, permissive reader" do it "Works with UTF-8 without explicit encoding" do reader = MARC4J4R::Reader.new("#{DIR}/chinese_utf8.dat", :permissivemarc) r = reader.first second_title = r.find_by_tag('245')[1] second_title['a'].should.equal "湘鄂贛苏区史稿 /" end it "Works with UTF-8, explicit encoding" do reader = MARC4J4R::Reader.new("#{DIR}/chinese_utf8.dat", :permissivemarc, :utf8) r = reader.first second_title = r.find_by_tag('245')[1] second_title['a'].should.equal "湘鄂贛苏区史稿 /" end it "Fails with UTF-8, wrong explicit encoding" do reader = MARC4J4R::Reader.new("#{DIR}/chinese_utf8.dat", :permissivemarc, :iso) r = reader.first second_title = r.find_by_tag('245')[1] second_title['a'].should.equal "湘鄂贛苏区史稿 /" # equal, because it's permissive and tries to do the right thing end end describe "Reader round-tripping" do before do @one = MARC4J4R::Reader.new("#{DIR}/one.dat").first end it "should round-trip binary" do rt = MARC4J4R::Reader.new(StringIO.new(@one.to_marc)).first rt.should.equal @one end it "should round-trip xml" do rt = MARC4J4R::Reader.new(StringIO.new(@one.to_xml), :marcxml).first rt.should.equal @one end it "should show xml and binary forms equal after parsing" do rt = MARC4J4R::Reader.new("#{DIR}/one.xml", :marcxml).first rt.should.equal @one end end