Skip to content

Commit

Permalink
Add tests for REXML::Text.check (#165)
Browse files Browse the repository at this point in the history
This patch will add missing REXML::Text.check tests.

This is the tests for the part that is checked using a regular
expression:

https://github.com/ruby/rexml/blob/b2ec329dc1dc7635b224a6d61687c24b1e1db6fd/lib/rexml/text.rb#L155-L172
  • Loading branch information
Watson1978 authored Jul 11, 2024
1 parent 5e140ed commit 6d6400c
Showing 1 changed file with 92 additions and 0 deletions.
92 changes: 92 additions & 0 deletions test/test_text_check.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# frozen_string_literal: false

module REXMLTests
class TextCheckTester < Test::Unit::TestCase

def check(string)
REXML::Text.check(string, REXML::Text::NEEDS_A_SECOND_CHECK, nil)
end

def assert_check(string)
assert_nothing_raised { check(string) }
end

def assert_check_failed(string, illegal_part)
message = "Illegal character #{illegal_part.inspect} in raw string #{string.inspect}"
assert_raise(RuntimeError.new(message)) do
check(string)
end
end

class TestValid < self
def test_entity_name_start_char_colon
assert_check('&:;')
end

def test_entity_name_start_char_under_score
assert_check('&_;')
end

def test_entity_name_mix
assert_check('&A.b-0123;')
end

def test_character_reference_decimal
assert_check('&#0162;')
end

def test_character_reference_hex
assert_check('&#x10FFFF;')
end

def test_entity_name_non_ascii
# U+3042 HIRAGANA LETTER A
# U+3044 HIRAGANA LETTER I
assert_check("&\u3042\u3044;")
end

def test_normal_string
assert_check("foo")
end
end

class TestInvalid < self
def test_lt
assert_check_failed('<;', '<')
end

def test_lt_mix
assert_check_failed('ab<cd', '<')
end

def test_entity_reference_missing_colon
assert_check_failed('&amp', '&')
end

def test_character_reference_decimal_invalid_value
# U+0008 BACKSPACE
assert_check_failed('&#8;', '&#8;')
end

def test_character_reference_format_hex_0x
# U+0041 LATIN CAPITAL LETTER A
assert_check_failed('&#0x41;', '&#0x41;')
end

def test_character_reference_format_hex_00x
# U+0041 LATIN CAPITAL LETTER A
assert_check_failed('&#00x41;', '&#00x41;')
end

def test_character_reference_hex_surrogate_block
# U+0D800 SURROGATE PAIR
assert_check_failed('&#xD800;', '&#xD800;')
end

def test_entity_name_non_ascii_symbol
# U+00BF INVERTED QUESTION MARK
assert_check_failed('&\u00BF;', '&')
end
end
end
end

0 comments on commit 6d6400c

Please sign in to comment.