diff --git a/lib/oga/xml/parser.y b/lib/oga/xml/parser.y index 0798d71..ed555b5 100644 --- a/lib/oga/xml/parser.y +++ b/lib/oga/xml/parser.y @@ -206,7 +206,7 @@ end end end - raise Racc::ParseError, <<-EOF + raise Racc::ParseError, <<-EOF.strip Unexpected #{name} with value #{value.inspect} on line #{@line}: #{lines} diff --git a/spec/oga/xml/parser/error_spec.rb b/spec/oga/xml/parser/error_spec.rb new file mode 100644 index 0000000..dbbd07b --- /dev/null +++ b/spec/oga/xml/parser/error_spec.rb @@ -0,0 +1,33 @@ +require 'spec_helper' + +describe Oga::XML::Parser do + context 'raising syntax errors' do + before do + @invalid_xml = <<-EOF.strip + + Alice + 25 + Dutch + + EOF + end + + example 'raise a Racc::ParseError' do + expect { parse(@invalid_xml) }.to raise_error(Racc::ParseError) + end + + example 'display a more meaningful error message' do + # Racc basically reports errors at the last moment instead of where they + # *actually* occur. + partial = <<-EOF.strip + 1. + 2. Alice + 3. 25 + 4. Dutch +=> 5. + EOF + + parse_error(@invalid_xml).should =~ /#{partial}/ + end + end +end diff --git a/spec/support/parsing.rb b/spec/support/parsing.rb index c189acd..9e347bd 100644 --- a/spec/support/parsing.rb +++ b/spec/support/parsing.rb @@ -41,5 +41,17 @@ module Oga def parse_html(input, options = {}) return Oga::HTML::Parser.new(options).parse(input) end + + ## + # Parses the given invalid XML and returns the error message. + # + # @param [String] xml + # @return [String] + # + def parse_error(xml) + parse(xml) + rescue Racc::ParseError => error + return error.message + end end # ParsingHelpers end # Oga