From 6afedc4156cf69aea1e289c3f9df2152cb89b3a1 Mon Sep 17 00:00:00 2001 From: Marcos Wright-Kuhns Date: Sun, 3 Apr 2016 06:42:02 -0700 Subject: [PATCH] REXML backend no longer transforms errors ...because Formatter.decode method takes care of ensuring all errors are OEmbed::ParseError instances. --- lib/oembed/formatter/xml/backends/rexml.rb | 7 -- spec/formatter/xml/rexml_backend_spec.rb | 71 ++++++++++++++------ spec/formatter/xml/xmlsimple_backend_spec.rb | 2 +- 3 files changed, 51 insertions(+), 29 deletions(-) diff --git a/lib/oembed/formatter/xml/backends/rexml.rb b/lib/oembed/formatter/xml/backends/rexml.rb index 3a15ef0..9ea2cb5 100644 --- a/lib/oembed/formatter/xml/backends/rexml.rb +++ b/lib/oembed/formatter/xml/backends/rexml.rb @@ -17,13 +17,6 @@ def decode(xml) obj[el.name] = el.text end obj - rescue - case $! - when parse_error - raise $! - else - raise parse_error, 'Couldn\'t parse the given document.' - end end def decode_fail_msg diff --git a/spec/formatter/xml/rexml_backend_spec.rb b/spec/formatter/xml/rexml_backend_spec.rb index 217b1b1..7bbf4cc 100644 --- a/spec/formatter/xml/rexml_backend_spec.rb +++ b/spec/formatter/xml/rexml_backend_spec.rb @@ -8,7 +8,7 @@ OEmbed::Formatter::XML.backend = 'REXML' }.to_not raise_error - expect(!!defined?(REXML)).to eq(true) + expect(defined?(REXML)).to be_truthy end it 'should support XML' do @@ -18,7 +18,8 @@ end it 'should be using the XmlSimple backend' do - expect(OEmbed::Formatter::XML.backend).to eq(OEmbed::Formatter::XML::Backends::REXML) + expect(OEmbed::Formatter::XML.backend) + .to eq(OEmbed::Formatter::XML::Backends::REXML) end it 'should decode an XML String' do @@ -26,30 +27,58 @@ # We need to compare keys & values separately because we don't expect all # non-string values to be recognized correctly. expect(decoded.keys).to eq(valid_response(:object).keys) - expect(decoded.values.map(&:to_s)).to eq(valid_response(:object).values.map(&:to_s)) + expect(decoded.values.map(&:to_s)) + .to eq(valid_response(:object).values.map(&:to_s)) end - it 'should raise an OEmbed::ParseError when decoding an invalid XML String' do - expect { - decode = OEmbed::Formatter.decode(:xml, invalid_response('unclosed_container', :xml)) - }.to raise_error(OEmbed::ParseError) - expect { - decode = OEmbed::Formatter.decode(:xml, invalid_response('unclosed_tag', :xml)) - }.to raise_error(OEmbed::ParseError) - expect { - decode = OEmbed::Formatter.decode(:xml, invalid_response('invalid_syntax', :xml)) - }.to raise_error(OEmbed::ParseError) + RSpec.shared_examples 'a backend' do + around(:example) do |example| + RSpec::Expectations + .configuration.warn_about_potential_false_positives = false + + example.run + + RSpec::Expectations + .configuration.warn_about_potential_false_positives = true + end + + it 'should not catch that error when decoding' do + expect { + OEmbed::Formatter::XML::Backends::REXML.decode(xml_input) + }.to raise_error + end end - it 'should raise an OEmbed::ParseError when decoding fails with an unexpected error' do - error_to_raise = ArgumentError - expect(OEmbed::Formatter::XML.backend.parse_error).to_not be_kind_of(error_to_raise) + context 'given an unclosed xml continer' do + it_behaves_like 'a backend' do + let(:xml_input) { invalid_response('unclosed_container', :xml) } + end + end - expect(::REXML::Document).to receive(:new) - .and_raise(error_to_raise.new('unknown error')) + context 'given an unclosed xml tag' do + it_behaves_like 'a backend' do + let(:xml_input) { invalid_response('unclosed_tag', :xml) } + end + end - expect { - decode = OEmbed::Formatter.decode(:xml, valid_response(:xml)) - }.to raise_error(OEmbed::ParseError) + context 'given invalid xml syntax' do + it_behaves_like 'a backend' do + let(:xml_input) { invalid_response('invalid_syntax', :xml) } + end + end + + context 'given an unexpected error when parsing xml' do + it 'should not catch that error when decoding' do + error_to_raise = Interrupt + expect(OEmbed::Formatter::XML::Backends::REXML.parse_error) + .to_not be_kind_of(error_to_raise) + + expect(::REXML::Document).to receive(:new) + .and_raise(error_to_raise.new('unknown error')) + + expect { + OEmbed::Formatter::XML::Backends::REXML.decode(valid_response(:xml)) + }.to raise_error(error_to_raise) + end end end diff --git a/spec/formatter/xml/xmlsimple_backend_spec.rb b/spec/formatter/xml/xmlsimple_backend_spec.rb index 27560a6..1475beb 100644 --- a/spec/formatter/xml/xmlsimple_backend_spec.rb +++ b/spec/formatter/xml/xmlsimple_backend_spec.rb @@ -73,7 +73,7 @@ context 'given an unexpected error when parsing xml' do it 'should not catch that error when decoding' do - error_to_raise = RuntimeError + error_to_raise = Interrupt expect(OEmbed::Formatter::XML.backend.parse_error) .to_not be_kind_of(error_to_raise)