mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
rexml: Fix XPath string() implementation
* lib/rexml/functions.rb( REXML::Functions.string): * Support context node. * Fix implementation for document node to remove out of root nodes. * Support processing instruction node. * Improve implementation for integer to omit decimals. * test/rexml/test_jaxen.rb: Enable processing instruction test. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63221 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
e044924ee5
commit
6793c0a227
2 changed files with 29 additions and 31 deletions
|
@ -136,21 +136,36 @@ module REXML
|
||||||
# An object of a type other than the four basic types is converted to a
|
# An object of a type other than the four basic types is converted to a
|
||||||
# string in a way that is dependent on that type.
|
# string in a way that is dependent on that type.
|
||||||
def Functions::string( object=nil )
|
def Functions::string( object=nil )
|
||||||
#object = @context unless object
|
object = @@context[:node] if object.nil?
|
||||||
if object.instance_of? Array
|
if object.respond_to?(:node_type)
|
||||||
string( object[0] )
|
case object.node_type
|
||||||
elsif defined? object.node_type
|
when :attribute
|
||||||
if object.node_type == :attribute
|
|
||||||
object.value
|
object.value
|
||||||
elsif object.node_type == :element || object.node_type == :document
|
when :element
|
||||||
string_value(object)
|
string_value(object)
|
||||||
|
when :document
|
||||||
|
string_value(object.root)
|
||||||
|
when :processing_instruction
|
||||||
|
object.content
|
||||||
else
|
else
|
||||||
object.to_s
|
object.to_s
|
||||||
end
|
end
|
||||||
elsif object.nil?
|
|
||||||
return ""
|
|
||||||
else
|
else
|
||||||
object.to_s
|
case object
|
||||||
|
when Array
|
||||||
|
string(object[0])
|
||||||
|
when Numeric
|
||||||
|
integer = object.to_i
|
||||||
|
if object == integer
|
||||||
|
"%d" % integer
|
||||||
|
else
|
||||||
|
object.to_s
|
||||||
|
end
|
||||||
|
when nil
|
||||||
|
""
|
||||||
|
else
|
||||||
|
object.to_s
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -28,7 +28,7 @@ module REXMLTests
|
||||||
def _test_namespaces ; process_test_case("namespaces") ; end
|
def _test_namespaces ; process_test_case("namespaces") ; end
|
||||||
def _test_nitf ; process_test_case("nitf") ; end
|
def _test_nitf ; process_test_case("nitf") ; end
|
||||||
def _test_numbers ; process_test_case("numbers") ; end
|
def _test_numbers ; process_test_case("numbers") ; end
|
||||||
def _test_pi ; process_test_case("pi") ; end
|
def test_pi ; process_test_case("pi") ; end
|
||||||
def _test_pi2 ; process_test_case("pi2") ; end
|
def _test_pi2 ; process_test_case("pi2") ; end
|
||||||
def _test_simple ; process_test_case("simple") ; end
|
def _test_simple ; process_test_case("simple") ; end
|
||||||
def _test_testNamespaces ; process_test_case("testNamespaces") ; end
|
def _test_testNamespaces ; process_test_case("testNamespaces") ; end
|
||||||
|
@ -79,29 +79,12 @@ module REXMLTests
|
||||||
def process_value_of(context, variables, namespaces, value_of)
|
def process_value_of(context, variables, namespaces, value_of)
|
||||||
expected = value_of.text
|
expected = value_of.text
|
||||||
xpath = value_of.attributes["select"]
|
xpath = value_of.attributes["select"]
|
||||||
matched = XPath.first(context, xpath, namespaces, variables)
|
matched = XPath.match(context, xpath, namespaces, variables)
|
||||||
|
|
||||||
message = user_message(context, xpath, matched)
|
message = user_message(context, xpath, matched)
|
||||||
|
assert_equal(expected || "",
|
||||||
if expected.nil?
|
REXML::Functions.string(matched),
|
||||||
assert_nil(matched, message)
|
message)
|
||||||
else
|
|
||||||
case matched
|
|
||||||
when Element
|
|
||||||
assert_equal(expected, matched.text, message)
|
|
||||||
when Attribute, Text, Comment, TrueClass, FalseClass
|
|
||||||
assert_equal(expected, matched.to_s, message)
|
|
||||||
when Instruction
|
|
||||||
assert_equal(expected, matched.content, message)
|
|
||||||
when Integer, Float
|
|
||||||
assert_equal(expected.to_f, matched, message)
|
|
||||||
when String
|
|
||||||
assert_equal(expected, matched, message)
|
|
||||||
else
|
|
||||||
flunk("#{message}\n" +
|
|
||||||
"Unexpected match value: <#{matched.inspect}>")
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# processes a tests/document/context/test node ( where @exception is false or doesn't exist )
|
# processes a tests/document/context/test node ( where @exception is false or doesn't exist )
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue