From fc0a637b9e8e1a55e1b9c1b5037eb738d89e278d Mon Sep 17 00:00:00 2001 From: Mark Zelesky Date: Wed, 13 Nov 2024 10:00:27 -0500 Subject: [PATCH] test the extra_space_fix method --- lib/marc_cleanup/variable_fields.rb | 8 ++-- spec/variable_fields/extra_spaces_spec.rb | 57 +++++++++++++++++++++++ 2 files changed, 61 insertions(+), 4 deletions(-) diff --git a/lib/marc_cleanup/variable_fields.rb b/lib/marc_cleanup/variable_fields.rb index c91f750..aed9e94 100644 --- a/lib/marc_cleanup/variable_fields.rb +++ b/lib/marc_cleanup/variable_fields.rb @@ -241,28 +241,28 @@ def extra_space_fix(record) curr_subfield += 1 next if subfield.value.nil? - record.fields[field_index].subfields[curr_subfield].value = extra_space_gsub(subfield.value) + record.fields[field_index].subfields[curr_subfield].value = extra_space_gsub(subfield.value.dup) end when '533' field.subfields.each do |subfield| curr_subfield += 1 next if subfield.code == '7' || subfield.value.nil? - record.fields[field_index].subfields[curr_subfield].value = extra_space_gsub(subfield.value) + record.fields[field_index].subfields[curr_subfield].value = extra_space_gsub(subfield.value.dup) end when /^7[6-8]./ field.subfields.each do |subfield| curr_subfield += 1 next if subfield.code =~ /[^a-v3-8]/ || subfield.value.nil? - record.fields[field_index].subfields[curr_subfield].value = extra_space_gsub(subfield.value) + record.fields[field_index].subfields[curr_subfield].value = extra_space_gsub(subfield.value.dup) end when /^8../ field.subfields.each do |subfield| curr_subfield += 1 next if %w[w 7].include?(subfield.code) || subfield.value.nil? - record.fields[field_index].subfields[curr_subfield].value = extra_space_gsub(subfield.value) + record.fields[field_index].subfields[curr_subfield].value = extra_space_gsub(subfield.value.dup) end end end diff --git a/spec/variable_fields/extra_spaces_spec.rb b/spec/variable_fields/extra_spaces_spec.rb index 3fe7c19..8cef495 100644 --- a/spec/variable_fields/extra_spaces_spec.rb +++ b/spec/variable_fields/extra_spaces_spec.rb @@ -58,3 +58,60 @@ end end end + +RSpec.describe 'extra_space_fix' do + let(:record) { MARC::Record.new_from_hash('fields' => fields, 'leader' => leader) } + let(:leader) { '01104naa a2200289 i 4500' } + + context '1xx field has extra spaces' do + let(:fields) do + [ + { '100' => { 'ind1' => '0', + 'ind2' => ' ', + 'subfields' => [{ 'a' => 'Candy wrapper.' }] } } + ] + end + it 'removes the extra spaces' do + expect(MarcCleanup.extra_space_fix(record)['100']['a']).to eq 'Candy wrapper.' + end + end + + context '533 field has extra spaces in a positionally-defined subfield' do + let(:fields) do + [ + { '533' => { 'ind1' => ' ', + 'ind2' => ' ', + 'subfields' => [ + { 'a' => "Positive\u3000photo" }, + { '7' => 's1989 nyu a'}] } } + ] + end + it 'does not modify the positionally-defined subfield' do + expect(MarcCleanup.extra_space_fix(record)['533']['7']).to eq 's1989 nyu a' + end + end + + context '775 field has extra spaces in main entry' do + let(:fields) do + [ + { '775' => { 'ind1' => '0', + 'ind2' => ' ', + 'subfields' => [ { 'a' => 'Modernist thought' }] } }] + end + it 'removes the extra spaces' do + expect(MarcCleanup.extra_space_fix(record)['775']['a']).to eq 'Modernist thought' + end + end + + context '830 field has no extra spaces in main entry' do + let(:fields) do + [ + { '830' => { 'ind1' => ' ', + 'ind2' => '4', + 'subfields' => [ { 'a' => 'The modern world.' }] } }] + end + it 'does not modify the record' do + expect(MarcCleanup.extra_space_fix(record)['830']['a']).to eq 'The modern world.' + end + end +end