describe "computations from /originInfo field" do let(:record) { Stanford::Mods::Record.new.from_str(modsxml) } context '#pub_year_display_str' do context 'when it has a dateIssued date' do let(:modsxml) do <<-EOF 1900 EOF end it 'returns the year from the dateIssued field' do expect(record.pub_year_display_str).to eq '1900' end end context 'when it has a dateCreated date' do let(:modsxml) do <<-EOF 1800 EOF end it 'returns the year from the dateCreated field' do expect(record.pub_year_display_str).to eq '1800' end end context 'when it has a dateCaptured date' do let(:modsxml) do <<-EOF 1700 EOF end it 'returns the year from the dateCaptured field' do expect(record.pub_year_display_str).to eq '1700' end end context 'when it has multiple types of date fields' do let(:modsxml) do <<-EOF 1900 1800 1700 EOF end it 'returns the year from the dateIssued field' do expect(record.pub_year_display_str).to eq '1900' end end context 'when it has a key date of the same type' do let(:modsxml) do <<-EOF 1900 1800 EOF end it 'returns the year from the keyDate field' do expect(record.pub_year_display_str).to eq '1900' end end context 'when it has a key date of a different type' do let(:modsxml) do <<-EOF 1900 1800 EOF end it 'returns the year from the preferred field type regardless of the keyDate' do expect(record.pub_year_display_str).to eq '1800' end end context 'when it has multiple dates' do let(:modsxml) do <<-EOF 1900 1800 EOF end it 'returns the earliest year' do expect(record.pub_year_display_str).to eq '1800' end end context 'when it has multiple originInfo elements' do let(:modsxml) do <<-EOF 1900 1800 EOF end it 'returns the earliest year across all the dates' do expect(record.pub_year_display_str).to eq '1800' end end context 'when it has a date range' do let(:modsxml) do <<-EOF 1800 1900 EOF end it 'returns the date range' do expect(record.pub_year_display_str).to eq '1800 - 1900' end end context 'when it has an open-ended date range' do let(:modsxml) do <<-EOF uuuu 1900 EOF end it 'returns the date range' do expect(record.pub_year_display_str).to eq ' - 1900' end end context 'when it has an encoded date' do let(:modsxml) do <<-EOF 1800 1900 EOF end it 'returns the encoded date' do expect(record.pub_year_display_str).to eq '1900' end end context 'when it has a date range and an earlier single date' do let(:modsxml) do <<-EOF 1799 1800 1900 EOF end it 'returns the earliest date' do expect(record.pub_year_display_str).to eq '1799' end end context 'when it has a date range and a later single date' do let(:modsxml) do <<-EOF 1850 1800 1900 EOF end it 'returns the date range' do expect(record.pub_year_display_str).to eq '1800 - 1900' end end context 'with BCE dates' do let(:modsxml) do <<-EOF -0249 -0149 EOF end it 'returns the earliest date' do expect(record.pub_year_display_str).to eq '250 BCE' end end context 'with a BCE date range' do let(:modsxml) do <<-EOF -0249 -0149 EOF end it 'returns the date range' do expect(record.pub_year_display_str).to eq '250 BCE - 150 BCE' end end context 'with a qualified date' do let(:modsxml) do <<-EOF 249 EOF end it 'returns the date without any qualifiers' do expect(record.pub_year_display_str).to eq '249 CE' end context 'with ignore_approximate: true' do it 'returns nothing' do expect(record.pub_year_display_str(ignore_approximate: true)).to eq nil end end end context 'with non-year data in the field' do let(:modsxml) do <<-EOF 12th May 1800 EOF end it 'returns only the year part of the date' do expect(record.pub_year_display_str).to eq '1800' end end context 'with a placeholder dates' do let(:modsxml) do <<-EOF 9999 EOF end it 'ignores the date' do expect(record.pub_year_display_str).to eq nil end end end context '#pub_year_sort_str' do context 'when it has a dateIssued date' do let(:modsxml) do <<-EOF 1900 EOF end it 'returns the year from the field' do expect(record.pub_year_sort_str).to eq '1900' end end context 'when it has a date range' do let(:modsxml) do <<-EOF 1800 1900 EOF end it 'returns just the earliest date in the range' do expect(record.pub_year_sort_str).to eq '1800' end end context 'with BCE dates' do let(:modsxml) do <<-EOF -0249 -0149 EOF end it 'returns the earliest date with the funky lexical sort encoding' do expect(record.pub_year_sort_str).to eq '-751' end end context 'with a BCE date range' do let(:modsxml) do <<-EOF -0249 -0149 EOF end it 'returns the earliest date of the range with the funky lexical sort encoding' do expect(record.pub_year_sort_str).to eq '-751' end end context 'with a qualified date' do let(:modsxml) do <<-EOF 249 EOF end it 'returns the date without any qualifiers' do expect(record.pub_year_sort_str).to eq '0249' end context 'with ignore_approximate: true' do it 'returns nothing' do expect(record.pub_year_sort_str(ignore_approximate: true)).to eq nil end end end context 'with a placeholder dates' do let(:modsxml) do <<-EOF 9999 EOF end it 'ignores the date' do expect(record.pub_year_sort_str).to eq nil end end context 'when it has an open-ended date range' do let(:modsxml) do <<-EOF uuuu 1900 EOF end it 'returns the known part of the date range' do expect(record.pub_year_sort_str).to eq '1900' end end end context '#pub_year_int' do context 'when it has a dateIssued date' do let(:modsxml) do <<-EOF 1900 EOF end it 'returns the year from the dateIssued field' do expect(record.pub_year_int).to eq 1900 end end context 'when it has multiple dates' do let(:modsxml) do <<-EOF 1900 1800 EOF end it 'returns the earliest year' do expect(record.pub_year_int).to eq 1800 end end context 'when it has a date range' do let(:modsxml) do <<-EOF 1800 1900 EOF end it 'returns the earliest year from the date range' do expect(record.pub_year_int).to eq 1800 end end context 'when it has an open-ended date range' do let(:modsxml) do <<-EOF uuuu 1900 EOF end it 'returns the known part of the date range' do expect(record.pub_year_int).to eq 1900 end end context 'with BCE dates' do let(:modsxml) do <<-EOF -0249 -0149 EOF end it 'returns the earliest date' do expect(record.pub_year_int).to eq(-249) end end context 'with a BCE date range' do let(:modsxml) do <<-EOF -0249 -0149 EOF end it 'returns the year of the earliest date in the range' do expect(record.pub_year_int).to eq(-249) end end context 'with a qualified date' do let(:modsxml) do <<-EOF 249 EOF end it 'returns the date without any qualifiers' do expect(record.pub_year_int).to eq 249 end context 'with ignore_approximate: true' do it 'returns nothing' do expect(record.pub_year_int(ignore_approximate: true)).to eq nil end end end context 'with a placeholder dates' do let(:modsxml) do <<-EOF 9999 EOF end it 'ignores the date' do expect(record.pub_year_int).to eq nil end end end end