ruby/test/rexml/test_xpath_pred.rb
yugui 911692e568 merges r29282,r29283,r29284,r29285,r29286,r29287,r29288 and r29302 from trunk into ruby_1_9_2.
--
* test/rexml/: import REXML tests from
  http://www.germane-software.com/repos/rexml/trunk/test/.
  Many tests are failed temporary. I'll fix them quickly. Sorry.
--
* test/rexml/test_listener.rb: remove needless codes.
--
* test/rexml/: fix fixture data path. All REXML tests are worked.
--
* test/rexml/: untabify.
--
* test/rexml/test_core.rb: enable.
--
* test/rexml/test_sax.rb: don't use thread and sleep to avoid slow test.
--
* lib/rexml/xpath_parser.rb, test/rexml/test_xpath.rb:
  add missing method availability check. [ruby-core:32447]
  Reported by Wiebe Cazemier. Thanks!!!
--
Specify external encoding.

When external encoding is not specified, it uses default
external
encoding. But it depends the environment. So specify as
UTF-8 for
environments whose locale is not UTF-8.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_9_2@29915 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2010-11-24 09:37:27 +00:00

80 lines
2.1 KiB
Ruby

require "test/unit/testcase"
require "rexml/document"
require "rexml/xpath"
require "rexml/parsers/xpathparser"
class XPathPredicateTester < Test::Unit::TestCase
include REXML
SRC=<<-EOL
<article>
<section role="subdivision" id="1">
<para>free flowing text.</para>
</section>
<section role="division">
<section role="subdivision" id="2">
<para>free flowing text.</para>
</section>
<section role="division">
<para>free flowing text.</para>
</section>
</section>
</article>
EOL
def setup
@doc = REXML::Document.new( SRC )
@parser = REXML::Parsers::XPathParser.new
end
def test_predicates_parent
path = '//section[../self::section[@role="division"]]'
m = do_path( path )
assert_equal( 2, m.size )
assert_equal( "2", m[0].attributes["id"] )
assert_nil( m[1].attributes["id"] )
end
def test_predicates_single
path = '//section[@role="subdivision" and not(../self::section[@role="division"])]'
m = do_path( path )
assert_equal( 1, m.size )
assert_equal( "1", m[0].attributes["id"] )
end
def test_predicates_multi
path = '//section[@role="subdivision"][not(../self::section[@role="division"])]'
m = do_path( path )
assert_equal( 1, m.size )
assert_equal( "1", m[0].attributes["id"] )
end
def do_path( path )
m = REXML::XPath.match( @doc, path )
#puts path, @parser.parse( path ).inspect
return m
end
def test_get_no_siblings_terminal_nodes
source = <<-XML
<a>
<b number='1' str='abc'>TEXT1</b>
<c number='1'/>
<c number='2' str='def'>
<b number='3'/>
<d number='1' str='abc'>TEXT2</d>
<b number='2'><!--COMMENT--></b>
</c>
</a>
XML
doc = REXML::Document.new(source)
predicate = "count(child::node()|" +
"following-sibling::node()|" +
"preceding-sibling::node())=0"
m = REXML::XPath.match(doc, "/descendant-or-self::node()[#{predicate}]")
assert_equal( [REXML::Text.new("TEXT1"),
REXML::Text.new("TEXT2"),
REXML::Comment.new("COMMENT")],
m )
end
end