@@ -382,32 +382,39 @@ def test_preceding
382382 start = XPath . first ( d , "/a/b[@id='1']" )
383383 assert_equal 'b' , start . name
384384 c = XPath . first ( start , "preceding::c" )
385- assert_equal '2 ' , c . attributes [ 'id' ]
385+ assert_equal '0 ' , c . attributes [ 'id' ]
386386
387- c1 , c0 = XPath . match ( d , "/a/b/c[@id='2']/preceding::node()" )
388- assert_equal '1' , c1 . attributes [ 'id' ]
387+ b0 , b2 , c0 , c1 = XPath . match ( d , "/a/b/c[@id='2']/preceding::node()" )
388+ assert_equal 'b' , b0 . name
389+ assert_equal 'b' , b2 . name
390+ assert_equal 'c' , c0 . name
391+ assert_equal 'c' , c1 . name
392+
393+ assert_equal '0' , b0 . attributes [ 'id' ]
394+ assert_equal '2' , b2 . attributes [ 'id' ]
389395 assert_equal '0' , c0 . attributes [ 'id' ]
396+ assert_equal '1' , c1 . attributes [ 'id' ]
390397
391- c2 , c1 , c0 , b , b2 , b0 = XPath . match ( start , "preceding::node()" )
398+ b0 , b2 , b , c0 , c1 , c2 = XPath . match ( start , "preceding::node()" )
392399
393- assert_equal 'c' , c2 . name
394- assert_equal 'c' , c1 . name
395400 assert_equal 'c' , c0 . name
396- assert_equal 'b ' , b . name
397- assert_equal 'b ' , b2 . name
401+ assert_equal 'c ' , c1 . name
402+ assert_equal 'c ' , c2 . name
398403 assert_equal 'b' , b0 . name
404+ assert_equal 'b' , b2 . name
405+ assert_equal 'b' , b . name
399406
400- assert_equal '2' , c2 . attributes [ 'id' ]
401- assert_equal '1' , c1 . attributes [ 'id' ]
402407 assert_equal '0' , c0 . attributes [ 'id' ]
403- assert b . attributes . empty?
404- assert_equal '2' , b2 . attributes [ 'id' ]
408+ assert_equal '1' , c1 . attributes [ 'id' ]
409+ assert_equal '2' , c2 . attributes [ 'id' ]
405410 assert_equal '0' , b0 . attributes [ 'id' ]
411+ assert_equal '2' , b2 . attributes [ 'id' ]
412+ assert b . attributes . empty?
406413
407414 d = REXML ::Document . new ( "<a><b/><c/><d/></a>" )
408415 matches = REXML ::XPath . match ( d , "/a/d/preceding::node()" )
409- assert_equal ( "c " , matches [ 0 ] . name )
410- assert_equal ( "b " , matches [ 1 ] . name )
416+ assert_equal ( "b " , matches [ 0 ] . name )
417+ assert_equal ( "c " , matches [ 1 ] . name )
411418
412419 s = "<a><b><c id='1'/></b><b><b><c id='2'/><c id='3'/></b><c id='4'/></b><c id='NOMATCH'><c id='5'/></c></a>"
413420 d = REXML ::Document . new ( s )
@@ -425,7 +432,7 @@ def test_preceding_multiple
425432 XML
426433 doc = REXML ::Document . new ( source )
427434 matches = REXML ::XPath . match ( doc , "a/d/preceding::*" )
428- assert_equal ( [ "d " , "c" , "b " ] , matches . map ( &:name ) )
435+ assert_equal ( [ "b " , "c" , "d " ] , matches . map ( &:name ) )
429436 end
430437
431438 def test_following_multiple
@@ -498,7 +505,7 @@ def test_preceding_sibling_across_multiple_nodes
498505 XML
499506 doc = REXML ::Document . new ( source )
500507 matches = REXML ::XPath . match ( doc , "a/b/x/preceding-sibling::*" )
501- assert_equal ( [ "e " , "d" , "c " ] , matches . map ( &:name ) )
508+ assert_equal ( [ "c " , "d" , "e " ] , matches . map ( &:name ) )
502509 end
503510
504511 def test_preceding_sibling_within_single_node
@@ -511,7 +518,7 @@ def test_preceding_sibling_within_single_node
511518 XML
512519 doc = REXML ::Document . new ( source )
513520 matches = REXML ::XPath . match ( doc , "a/b/x/preceding-sibling::*" )
514- assert_equal ( [ "e " , "x " , "d " , "c " ] , matches . map ( &:name ) )
521+ assert_equal ( [ "c " , "d " , "x " , "e " ] , matches . map ( &:name ) )
515522 end
516523
517524 def test_following
@@ -883,6 +890,30 @@ def test_ordering
883890 r . collect { |element | element . attribute ( "id" ) . value } )
884891 end
885892
893+ def test_order_consistency
894+ doc = REXML ::Document . new ( '<a><b><c><d id="1"><d id="2"/></d></c></b></a>' )
895+ assert_equal ( 'a' , REXML ::XPath . first ( doc , '//d/ancestor::*' ) . name )
896+ assert_equal ( 'a' , REXML ::XPath . first ( doc , '//d[@id="2"]/ancestor::*' ) . name )
897+ assert_equal ( %w[ a b c d ] , REXML ::XPath . match ( doc , '//d/ancestor::*' ) . map ( &:name ) )
898+ assert_equal ( %w[ a b c d ] , REXML ::XPath . match ( doc , '//d[@id="2"]/ancestor::*' ) . map ( &:name ) )
899+
900+ assert_equal ( 'a' , REXML ::XPath . first ( doc , '//d/ancestor-or-self::*' ) . name )
901+ assert_equal ( 'a' , REXML ::XPath . first ( doc , '//d[@id="2"]/ancestor-or-self::*' ) . name )
902+ assert_equal ( %w[ a b c d d ] , REXML ::XPath . match ( doc , '//d/ancestor-or-self::*' ) . map ( &:name ) )
903+ assert_equal ( %w[ a b c d d ] , REXML ::XPath . match ( doc , '//d[@id="2"]/ancestor-or-self::*' ) . map ( &:name ) )
904+
905+ doc = REXML ::Document . new ( '<a><b/><c/><d id="1"/><d id="2"/></a>' )
906+ assert_equal ( 'b' , REXML ::XPath . first ( doc , '//d/preceding::*' ) . name )
907+ assert_equal ( 'b' , REXML ::XPath . first ( doc , '//d[@id="2"]/preceding::*' ) . name )
908+ assert_equal ( %w[ b c d ] , REXML ::XPath . match ( doc , '//d/preceding::*' ) . map ( &:name ) )
909+ assert_equal ( %w[ b c d ] , REXML ::XPath . match ( doc , '//d[@id="2"]/preceding::*' ) . map ( &:name ) )
910+
911+ assert_equal ( 'b' , REXML ::XPath . first ( doc , '//d/preceding-sibling::*' ) . name )
912+ assert_equal ( 'b' , REXML ::XPath . first ( doc , '//d[@id="2"]/preceding-sibling::*' ) . name )
913+ assert_equal ( %w[ b c d ] , REXML ::XPath . match ( doc , '//d/preceding-sibling::*' ) . map ( &:name ) )
914+ assert_equal ( %w[ b c d ] , REXML ::XPath . match ( doc , '//d[@id="2"]/preceding-sibling::*' ) . map ( &:name ) )
915+ end
916+
886917 def test_descendant_or_self_ordering
887918 source = "<a>
888919 <b>
0 commit comments