Skip to content

Commit

Permalink
Fix method scope in test in order to invoke the tests properly and fi…
Browse files Browse the repository at this point in the history
…x exception message (#182)

This PR includes following two fixes.

1. The `test_empty` and `test_linear_performance_gt` were defined as
private method. Seems that test-unit runner does not invoke private
methods even if the methods have `test_` prefix.
2. When parse malformed entity declaration, the exception might have the
message about `NoMethodError`. The proper exception message will be
contained by this fix.
  • Loading branch information
Watson1978 committed Jul 19, 2024
1 parent 7e75de2 commit 2c39c91
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
6 changes: 5 additions & 1 deletion lib/rexml/parsers/baseparser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,11 @@ def pull_event
raise REXML::ParseException.new( "Bad ELEMENT declaration!", @source ) if md.nil?
return [ :elementdecl, "<!ELEMENT" + md[1] ]
elsif @source.match("ENTITY", true)
match = [:entitydecl, *@source.match(Private::ENTITYDECL_PATTERN, true, term: Private::ENTITY_TERM).captures.compact]
match_data = @source.match(Private::ENTITYDECL_PATTERN, true, term: Private::ENTITY_TERM)
unless match_data
raise REXML::ParseException.new("Malformed entity declaration", @source)
end
match = [:entitydecl, *match_data.captures.compact]
ref = false
if match[1] == '%'
ref = true
Expand Down
17 changes: 11 additions & 6 deletions test/parse/test_entity_declaration.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
# frozen_string_literal: false
require 'test/unit'
require 'rexml/document'
require "test/unit"
require "core_assertions"

require "rexml/document"

module REXMLTests
class TestParseEntityDeclaration < Test::Unit::TestCase
include Test::Unit::CoreAssertions

private
def xml(internal_subset)
<<-XML
Expand All @@ -18,25 +22,26 @@ def parse(internal_subset)
REXML::Document.new(xml(internal_subset)).doctype
end

public
def test_empty
exception = assert_raise(REXML::ParseException) do
parse(<<-INTERNAL_SUBSET)
<!ENTITY>
INTERNAL_SUBSET
end
assert_equal(<<-DETAIL.chomp, exception.to_s)
Malformed notation declaration: name is missing
Malformed entity declaration
Line: 5
Position: 72
Position: 70
Last 80 unconsumed characters:
<!ENTITY> ]> <r/>
> ]> <r/>
DETAIL
end

def test_linear_performance_gt
seq = [10000, 50000, 100000, 150000, 200000]
assert_linear_performance(seq, rehearsal: 10) do |n|
REXML::Document.new('<!DOCTYPE rubynet [<!ENTITY rbconfig.ruby_version "' + '>' * n + '">')
REXML::Document.new('<!DOCTYPE rubynet [<!ENTITY rbconfig.ruby_version "' + '>' * n + '">]>')
end
end
end
Expand Down

0 comments on commit 2c39c91

Please sign in to comment.