Specs for XML parser errors.

This commit is contained in:
Yorick Peterse 2014-04-07 21:31:36 +02:00
parent 915d3ee505
commit cb74c7edf9
3 changed files with 46 additions and 1 deletions

View File

@ -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}

View File

@ -0,0 +1,33 @@
require 'spec_helper'
describe Oga::XML::Parser do
context 'raising syntax errors' do
before do
@invalid_xml = <<-EOF.strip
<person>
<name>Alice</name>
<age>25
<nationality>Dutch</nationality>
</person>
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. <person>
2. <name>Alice</name>
3. <age>25
4. <nationality>Dutch</nationality>
=> 5. </person>
EOF
parse_error(@invalid_xml).should =~ /#{partial}/
end
end
end

View File

@ -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