@@ -719,9 +719,9 @@ def collision(nodes)
719
719
node = doc . at_xpath ( "//ns:child" , { "ns" => "http://nokogiri.org/ns1" } )
720
720
assert_equal ( "ns1" , node . text )
721
721
722
- assert_raises ( XPath ::SyntaxError ) {
722
+ assert_raises ( XPath ::SyntaxError ) do
723
723
doc . at_xpath ( "//ns:child" )
724
- }
724
+ end
725
725
726
726
node = doc . at_xpath ( "//child" )
727
727
assert_nil ( node )
@@ -743,9 +743,9 @@ def collision(nodes)
743
743
doc . xpath ( "//xmlns:child[nokogiri:thing(.)]" , @handler )
744
744
assert_equal ( 1 , @handler . things . length )
745
745
746
- assert_raises ( XPath ::SyntaxError ) {
746
+ assert_raises ( XPath ::SyntaxError ) do
747
747
doc . xpath ( "//xmlns:child[nokogiri:thing(.)]" )
748
- }
748
+ end
749
749
750
750
doc . xpath ( "//xmlns:child[nokogiri:thing(.)]" , @handler )
751
751
assert_equal ( 2 , @handler . things . length )
@@ -763,34 +763,36 @@ def collision(nodes)
763
763
nodes = @xml . xpath ( "//address[@domestic=$value]" , nil , value : "Yes" )
764
764
assert_equal ( 4 , nodes . length )
765
765
766
- assert_raises ( XPath ::SyntaxError ) {
766
+ assert_raises ( XPath ::SyntaxError ) do
767
767
@xml . xpath ( "//address[@domestic=$value]" )
768
- }
768
+ end
769
769
770
770
nodes = @xml . xpath ( "//address[@domestic=$value]" , nil , value : "Qwerty" )
771
771
assert_empty ( nodes )
772
772
773
- assert_raises ( XPath ::SyntaxError ) {
773
+ assert_raises ( XPath ::SyntaxError ) do
774
774
@xml . xpath ( "//address[@domestic=$value]" )
775
- }
775
+ end
776
776
777
777
nodes = @xml . xpath ( "//address[@domestic=$value]" , nil , value : "Yes" )
778
778
assert_equal ( 4 , nodes . length )
779
779
end
780
780
end
781
781
782
782
describe "compiled" do
783
- let ( :doc ) {
784
- Nokogiri :: XML :: Document . parse ( <<~XML )
783
+ let ( :xml ) {
784
+ <<~XML
785
785
<root xmlns="http://nokogiri.org/default" xmlns:ns1="http://nokogiri.org/ns1">
786
786
<child>default</child>
787
787
<ns1:child>ns1</ns1:child>
788
788
</root>
789
789
XML
790
790
}
791
791
792
+ let ( :doc ) { Nokogiri ::XML ::Document . parse ( xml ) }
793
+
792
794
describe "XPath expressions" do
793
- it "works" do
795
+ it "works in the trivial case " do
794
796
expr = Nokogiri ::XML ::XPath . expression ( "//xmlns:child" )
795
797
796
798
result = doc . xpath ( expr )
@@ -800,13 +802,55 @@ def collision(nodes)
800
802
end
801
803
end
802
804
803
- it "can be evaluated in different documents"
805
+ it "works as expected with namespace bindings" do
806
+ expr = Nokogiri ::XML ::XPath . expression ( "//ns:child" )
804
807
805
- it "work with function handlers"
808
+ node = doc . at_xpath ( expr , { "ns" => "http://nokogiri.org/ns1" } )
809
+ assert_equal ( "ns1" , node . text )
806
810
807
- it "work with variable bindings"
811
+ assert_raises ( XPath ::SyntaxError ) do
812
+ doc . at_xpath ( "//ns:child" )
813
+ end
814
+ end
808
815
809
- it "work with namespace bindings"
816
+ it "works as expected with a function handler" do
817
+ expr = Nokogiri ::XML ::XPath . expression ( "//xmlns:child[nokogiri:thing(.)]" )
818
+
819
+ doc . xpath ( expr , @handler )
820
+ assert_equal ( 1 , @handler . things . length )
821
+
822
+ assert_raises ( XPath ::SyntaxError ) do
823
+ doc . xpath ( "//xmlns:child[nokogiri:thing(.)]" )
824
+ end
825
+ end
826
+
827
+ it "works as expected with bound variables" do
828
+ expr = Nokogiri ::XML ::XPath . expression ( "//address[@domestic=$value]" )
829
+
830
+ nodes = @xml . xpath ( "//address[@domestic=$value]" , nil , value : "Yes" )
831
+ assert_equal ( 4 , nodes . length )
832
+
833
+ assert_raises ( XPath ::SyntaxError ) do
834
+ @xml . xpath ( expr )
835
+ end
836
+ end
837
+
838
+ it "can be evaluated in different documents" do
839
+ doc1 = Nokogiri ::XML ::Document . parse ( xml )
840
+ doc2 = Nokogiri ::XML ::Document . parse ( xml )
841
+
842
+ expr = Nokogiri ::XML ::XPath . expression ( "//xmlns:child" )
843
+
844
+ result1 = doc1 . xpath ( expr )
845
+ result2 = doc2 . xpath ( expr )
846
+
847
+ assert_pattern do
848
+ result1 => [ { name : "child" , namespace : { href : "http://nokogiri.org/default" } } ]
849
+ end
850
+ assert_pattern do
851
+ result2 => [ { name : "child" , namespace : { href : "http://nokogiri.org/default" } } ]
852
+ end
853
+ end
810
854
end
811
855
812
856
describe "CSS selectors" do
0 commit comments