Skip to content

Commit

Permalink
Merge pull request #37 from antoinereyt/master
Browse files Browse the repository at this point in the history
Make float sigil works with integer an zero
  • Loading branch information
awetzel committed Oct 12, 2016
2 parents 0b61477 + 9f7225e commit 5abcce8
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
language: elixir
elixir:
- 1.0.2
- 1.2.0
sudo: false
script: mix test
5 changes: 4 additions & 1 deletion lib/sweet_xml.ex
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,9 @@ defmodule SweetXml do
defp to_cast(value, false), do: value
defp to_cast(value, :string), do: to_string(value)
defp to_cast(value, :integer), do: String.to_integer(to_string(value))
defp to_cast(value, :float), do: String.to_float(to_string(value))
defp to_cast(value, :float) do
{float,_} = Float.parse(to_string(value))
float
end

end
17 changes: 17 additions & 0 deletions test/files/float.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.05" encoding="UTF-8"?>
<order>
<products>
<product id="float">
<name>Product float</name>
<price>1.4</price>
</product>
<product id="integer">
<name>Product integer</name>
<price>3</price>
</product>
<product id="zero">
<name>Product with 0</name>
<price>0</price>
</product>
</products>
</order>
9 changes: 8 additions & 1 deletion test/sweet_xml_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ defmodule SweetXmlTest do
simple_stream = File.stream!("./test/files/simple_stream.xml")
readme = File.read!("test/files/readme.xml")
namespaces = File.read!("test/files/namespaces.xml")
float_sigil = File.read!("test/files/float.xml")
{:ok, [simple: simple,
complex: complex,
readme: readme,
complex_stream: complex_stream,
simple_stream: simple_stream,
namespaces: namespaces]}
namespaces: namespaces, float_sigil: float_sigil]}
end

test "parse", %{simple: doc} do
Expand Down Expand Up @@ -468,4 +469,10 @@ defmodule SweetXmlTest do

assert result == "courier"
end

test "float sigil with integer and zero", %{float_sigil: doc} do
assert doc |> xpath(~x"//product[@id=\"float\"]/price/text()"f) == 1.4
assert doc |> xpath(~x"//product[@id=\"integer\"]/price/text()"f) == 3.0
assert doc |> xpath(~x"//product[@id=\"zero\"]/price/text()"f) == 0.0
end
end

0 comments on commit 5abcce8

Please sign in to comment.