Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: crystal-lang/crystal
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 4d587a3f6f5a
Choose a base ref
...
head repository: crystal-lang/crystal
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: e602cd07a2e5
Choose a head ref
  • 2 commits
  • 2 files changed
  • 1 contributor

Commits on Oct 17, 2017

  1. Copy the full SHA
    fce8cba View commit details

Commits on Oct 26, 2017

  1. Merge pull request #5137 from asterite/bug/5063-xml-variables

    XML: missing forwarding of `variables` argument
    asterite authored Oct 26, 2017
    Copy the full SHA
    e602cd0 View commit details
Showing with 66 additions and 5 deletions.
  1. +61 −0 spec/std/xml/xpath_spec.cr
  2. +5 −5 src/xml/node.cr
61 changes: 61 additions & 0 deletions spec/std/xml/xpath_spec.cr
Original file line number Diff line number Diff line change
@@ -115,5 +115,66 @@ module XML
nodes.size.should eq(1)
nodes[0]["id"].should eq("2")
end

it "finds with variable binding (bool)" do
doc = XML.parse(%(\
<?xml version="1.0" encoding="UTF-8"?>
<feed>
<person id="1"/>
<person id="2"/>
</feed>
))
result = doc.xpath_bool("count(//feed/person[@id=$value]) = 1", variables: {"value" => 2})
result.should be_true
end

it "finds with variable binding (float)" do
doc = XML.parse(%(\
<?xml version="1.0" encoding="UTF-8"?>
<feed>
<person id="1"/>
<person id="2"/>
</feed>
))
result = doc.xpath_float("count(//feed/person[@id=$value])", variables: {"value" => 2})
result.should eq(1.0)
end

it "finds with variable binding (nodes)" do
doc = XML.parse(%(\
<?xml version="1.0" encoding="UTF-8"?>
<feed>
<person id="1"/>
<person id="2"/>
</feed>
))
nodes = doc.xpath_nodes("//feed/person[@id=$value]", variables: {"value" => 2})
nodes.size.should eq(1)
nodes[0]["id"].should eq("2")
end

it "finds with variable binding (node)" do
doc = XML.parse(%(\
<?xml version="1.0" encoding="UTF-8"?>
<feed>
<person id="1"/>
<person id="2"/>
</feed>
))
node = doc.xpath_node("//feed/person[@id=$value]", variables: {"value" => 2}).not_nil!
node["id"].should eq("2")
end

it "finds with variable binding (string)" do
doc = XML.parse(%(\
<?xml version="1.0" encoding="UTF-8"?>
<feed>
<person id="1"/>
<person id="2"/>
</feed>
))
result = doc.xpath_string("string(//feed/person[@id=$value]/@id)", variables: {"value" => 2})
result.should eq("2")
end
end
end
10 changes: 5 additions & 5 deletions src/xml/node.cr
Original file line number Diff line number Diff line change
@@ -490,7 +490,7 @@ struct XML::Node
# doc.xpath_bool("count(//person) > 0") # => true
# ```
def xpath_bool(path, namespaces = nil, variables = nil)
xpath(path, namespaces).as(Bool)
xpath(path, namespaces, variables).as(Bool)
end

# Searches this node for XPath *path* and restricts the return type to `Float64`.
@@ -499,7 +499,7 @@ struct XML::Node
# doc.xpath_float("count(//person)") # => 1.0
# ```
def xpath_float(path, namespaces = nil, variables = nil)
xpath(path, namespaces).as(Float64)
xpath(path, namespaces, variables).as(Float64)
end

# Searches this node for XPath *path* and restricts the return type to `NodeSet`.
@@ -510,7 +510,7 @@ struct XML::Node
# nodes.map(&.name) # => ["person"]
# ```
def xpath_nodes(path, namespaces = nil, variables = nil)
xpath(path, namespaces).as(NodeSet)
xpath(path, namespaces, variables).as(NodeSet)
end

# Searches this node for XPath *path* for nodes and returns the first one.
@@ -521,7 +521,7 @@ struct XML::Node
# doc.xpath_node("//invalid") # => nil
# ```
def xpath_node(path, namespaces = nil, variables = nil)
xpath_nodes(path, namespaces).first?
xpath_nodes(path, namespaces, variables).first?
end

# Searches this node for XPath *path* and restricts the return type to `String`.
@@ -530,7 +530,7 @@ struct XML::Node
# doc.xpath_string("string(/persons/person[1])")
# ```
def xpath_string(path, namespaces = nil, variables = nil)
xpath(path, namespaces).as(String)
xpath(path, namespaces, variables).as(String)
end

# :nodoc: