1require 'rexml_test_utils' 2require 'rexml/document' 3begin 4 require 'zlib' 5rescue LoadError 6end 7 8class OrderTester < Test::Unit::TestCase 9 include REXMLTestUtils 10 11 TESTDOC = <<END 12<a> 13 <b/> 14 <x id='1'/> 15 <c/> 16 <d> 17 <x id='2'/> 18 </d> 19 <x id='3'/> 20</a> 21END 22 23 def setup 24 @doc = REXML::Document.new(TESTDOC) 25 @items = REXML::XPath.match(@doc,'//x') 26 end 27 def test_first_element 28 assert_equal '1', @items[0].attributes['id'] 29 end 30 def test_second_element 31 assert_equal '2', @items[1].attributes['id'] 32 end 33 def test_third_element 34 assert_equal '3', @items[2].attributes['id'] 35 end 36 def test_order 37 d = REXML::Document.new( "<a><x id='1'/><x id='2'/><x id='3'/> 38 <x id='4'/><x id='5'/></a>" ) 39 items = REXML::XPath.match( d, '//x' ) 40 assert_equal( %w{1 2 3 4 5}, items.collect{|e| e.attributes['id']} ) 41 d = REXML::Document.new( "<a> 42 <x><z><y id='1'/><y id='2'/></z><y id='3'/></x> 43 <x><y id='4'/></x></a>" ) 44 items = REXML::XPath.match( d, '//y' ) 45 assert_equal( %w{1 2 3 4}, items.collect{|e| e.attributes['id']} ) 46 end 47 # Provided by Tom Talbott 48 def test_more_ordering 49 doc = REXML::Document.new(Zlib::GzipReader.open(fixture_path('LostineRiver.kml.gz'), encoding: 'utf-8')) 50 actual = [ 51 "Head south from Phinney Ave N", 52 "Turn left at N 36th St", 53 "Turn right at Fremont Ave N", 54 "Continue on 4th Ave N", 55 "Turn left at Westlake Ave N", 56 "Bear right at 9th Ave N", 57 "Turn left at Mercer St", 58 "Take the I-5 ramp", 59 "Take the I-5 S ramp", 60 "Take the I-90 E exit #164 to Bellevue/Spokane/4th Ave S.", 61 "Take the I-90 E ramp to Bellevue/Spokane", 62 "Take exit #137 to Wanapum Dam/Richland", 63 "Bear right at WA-26", 64 "Bear right and head toward WA-243", 65 "Continue on WA-243", 66 "Bear right at WA-24", 67 "Continue on WA-240", 68 "Turn right at WA-240 E", 69 "Take the I-182 W ramp to Yakima (I-82)/Pendleton", 70 "Take the I-82 E ramp to Umatilla/Pendleton", 71 "Take the I-84 E ramp to Pendleton", 72 "Take the OR-82 exit #261 to La Grande/Elgin", 73 "Turn right at Island Ave", 74 "Continue on W 1st St", 75 "Turn left at N McAlister Rd", 76 "Bear right at OR-82", 77 "Continue on Wallowa Lake Hwy", 78 "Continue on OR-82", 79 "Continue on Ruckman Ave", 80 "Continue on OR-82", 81 "Continue on S 8th Ave", 82 "Turn right at Albany St", 83 "Continue on OR-82", 84 "Continue on Wallowa Lake Hwy", 85 "Continue on N Madison St", 86 "Bear left at W 1st St", 87 "Continue on Wallowa Lake Hwy", 88 "Continue on Water St", 89 "Bear right at Lostine River Rd", 90 "Bear right and head toward Lostine River Rd", 91 "Turn right at Lostine River Rd", 92 "Continue on NF-8210", 93 "Turn right and head toward NF-8210", 94 "Turn right at NF-8210", 95 "", 96 "Route" 97 ] 98 count = 0 99 REXML::XPath.each( doc, "//Placemark") { |element| 100 n = element.elements["name"].text.squeeze(" ") 101 assert_equal( actual[count], n ) unless n =~ /Arrive at/ 102 count += 1 103 } 104 end if defined?(Zlib::GzipReader) 105end 106